BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file fairqueue_test.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <string.h>
31 #include <stdio.h>
32 #include <stddef.h>
33  
34 #include <misc/debug.h>
35 #include <system/BReactor.h>
36 #include <base/BLog.h>
37 #include <system/BTime.h>
38 #include <flow/PacketPassFairQueue.h>
39 #include <examples/FastPacketSource.h>
40 #include <examples/TimerPacketSink.h>
41  
42 #define OUTPUT_INTERVAL 0
43 #define REMOVE_INTERVAL 1
44 #define NUM_INPUTS 3
45  
46 BReactor reactor;
47 TimerPacketSink sink;
48 PacketPassFairQueue fq;
49 PacketPassFairQueueFlow flows[NUM_INPUTS];
50 FastPacketSource sources[NUM_INPUTS];
51 char *data[] = {"0 data", "1 datadatadata", "2 datadatadatadatadata"};
52 BTimer timer;
53 int current_cancel;
54  
55 static void init_input (int i)
56 {
57 PacketPassFairQueueFlow_Init(&flows[i], &fq);
58 FastPacketSource_Init(&sources[i], PacketPassFairQueueFlow_GetInput(&flows[i]), (uint8_t *)data[i], strlen(data[i]), BReactor_PendingGroup(&reactor));
59 }
60  
61 static void free_input (int i)
62 {
63 FastPacketSource_Free(&sources[i]);
64 PacketPassFairQueueFlow_Free(&flows[i]);
65 }
66  
67 static void reset_input (void)
68 {
69 PacketPassFairQueueFlow_AssertFree(&flows[current_cancel]);
70  
71 printf("removing %d\n", current_cancel);
72  
73 // remove flow
74 free_input(current_cancel);
75  
76 // init flow
77 init_input(current_cancel);
78  
79 // increment cancel
80 current_cancel = (current_cancel + 1) % NUM_INPUTS;
81  
82 // reset timer
83 BReactor_SetTimer(&reactor, &timer);
84 }
85  
86 static void flow_handler_busy (void *user)
87 {
88 PacketPassFairQueueFlow_AssertFree(&flows[current_cancel]);
89  
90 reset_input();
91 }
92  
93 static void timer_handler (void *user)
94 {
95 // if flow is busy, request cancel and wait for it
96 if (PacketPassFairQueueFlow_IsBusy(&flows[current_cancel])) {
97 printf("cancelling %d\n", current_cancel);
98 PacketPassFairQueueFlow_RequestCancel(&flows[current_cancel]);
99 PacketPassFairQueueFlow_SetBusyHandler(&flows[current_cancel], flow_handler_busy, NULL);
100 return;
101 }
102  
103 reset_input();
104 }
105  
106 int main ()
107 {
108 // initialize logging
109 BLog_InitStdout();
110  
111 // init time
112 BTime_Init();
113  
114 // initialize reactor
115 if (!BReactor_Init(&reactor)) {
116 DEBUG("BReactor_Init failed");
117 return 1;
118 }
119  
120 // initialize sink
121 TimerPacketSink_Init(&sink, &reactor, 500, OUTPUT_INTERVAL);
122  
123 // initialize queue
124 if (!PacketPassFairQueue_Init(&fq, TimerPacketSink_GetInput(&sink), BReactor_PendingGroup(&reactor), 1, 1)) {
125 DEBUG("PacketPassFairQueue_Init failed");
126 return 1;
127 }
128  
129 // initialize inputs
130 for (int i = 0; i < NUM_INPUTS; i++) {
131 init_input(i);
132 }
133  
134 // init cancel timer
135 BTimer_Init(&timer, REMOVE_INTERVAL, timer_handler, NULL);
136 BReactor_SetTimer(&reactor, &timer);
137  
138 // init cancel counter
139 current_cancel = 0;
140  
141 // run reactor
142 int ret = BReactor_Exec(&reactor);
143 BReactor_Free(&reactor);
144 return ret;
145 }