fixes and removed slave2 from controller link test
[oweals/gnunet.git] / src / testbed / testbed_api_operations.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file testbed/testbed_api_operations.c
23  * @brief functions to manage operation queues
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "testbed_api_operations.h"
28
29
30 /**
31  * An entry in the operation queue
32  */
33 struct QueueEntry
34 {
35   /**
36    * The next DLL pointer
37    */
38   struct QueueEntry *next;
39
40   /**
41    * The prev DLL pointer
42    */
43   struct QueueEntry *prev;
44
45   /**
46    * The operation this entry holds
47    */
48   struct GNUNET_TESTBED_Operation *op;
49 };
50
51
52 /**
53  * Queue of operations where we can only support a certain
54  * number of concurrent operations of a particular type.
55  */
56 struct OperationQueue
57 {
58  /**
59    * The head of the operation queue
60    */
61   struct QueueEntry *head;
62
63   /**
64    * The tail of the operation queue
65    */
66   struct QueueEntry *tail;
67
68   /**
69    * Number of operations that can be concurrently
70    * active in this queue.
71    */
72   unsigned int active;  
73 };
74
75
76 /**
77  * Operation state
78  */
79 enum OperationState
80   {
81     /**
82      * The operation is currently waiting for resources
83      */
84     OP_STATE_WAITING,
85
86     /**
87      * The operation has started
88      */
89     OP_STATE_STARTED,
90   };
91
92
93 /**
94  * Opaque handle to an abstract operation to be executed by the testing framework.
95  */
96 struct GNUNET_TESTBED_Operation
97 {
98   /**
99    * Function to call when we have the resources to begin the operation.
100    */
101   OperationStart start;
102
103   /**
104    * Function to call to clean up after the operation (which may or may
105    * not have been started yet).
106    */
107   OperationRelease release;
108                                  
109   /**
110    * Closure for callbacks.
111    */
112   void *cb_cls;
113
114   /**
115    * Array of operation queues this Operation belongs to.
116    */
117   struct OperationQueue **queues;
118
119   /**
120    * The id of the task which calls OperationStart for this operation
121    */
122   GNUNET_SCHEDULER_TaskIdentifier start_task_id;
123
124   /**
125    * Number of queues in the operation queues array
126    */
127   unsigned int nqueues;
128
129   /**
130    * The state of the operation
131    */
132   enum OperationState state;  
133   
134 };
135
136
137 /**
138  * Task for calling OperationStart on operation
139  *
140  * @param cls the Operation
141  * @param tc the TaskContext from scheduler
142  */
143 static void
144 call_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
145 {  
146   struct GNUNET_TESTBED_Operation *op = cls;
147   
148   op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
149   op->state = OP_STATE_STARTED;
150   if (NULL != op->start)
151   {
152     op->start (op->cb_cls);
153   }  
154 }
155
156
157 /**
158  * Checks for the readiness of an operation and schedules a operation start task
159  *
160  * @param op the operation
161  */
162 static void
163 check_readiness (struct GNUNET_TESTBED_Operation *op)
164 {   
165   unsigned int i;
166
167   if (GNUNET_SCHEDULER_NO_TASK != op->start_task_id)
168     return;  
169   for (i = 0; i < op->nqueues; i++)
170   {
171     if (0 == op->queues[i]->active)
172       return;
173   }
174   for (i = 0; i < op->nqueues; i++)
175   {
176     op->queues[i]->active--;
177   }
178   op->start_task_id = GNUNET_SCHEDULER_add_now (&call_start, op);
179 }
180
181
182 /**
183  * Create an 'operation' to be performed.
184  *
185  * @param cls closure for the callbacks
186  * @param start function to call to start the operation
187  * @param release function to call to close down the operation
188  * @return handle to the operation
189  */
190 struct GNUNET_TESTBED_Operation *
191 GNUNET_TESTBED_operation_create_ (void *cls,
192                                   OperationStart start,
193                                   OperationRelease release)
194 {
195   struct GNUNET_TESTBED_Operation *op;
196
197   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
198   op->start = start;
199   op->release = release;
200   op->cb_cls = cls;
201   op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
202   return op;
203 }
204
205
206 /**
207  * Create an operation queue.
208  *
209  * @param max_active maximum number of operations in this
210  *        queue that can be active in parallel at the same time
211  * @return handle to the queue
212  */
213 struct OperationQueue *
214 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
215 {
216   struct OperationQueue *queue;
217
218   queue = GNUNET_malloc (sizeof (struct OperationQueue));
219   queue->active = max_active;
220   return queue;
221 }
222
223
224 /**
225  * Destroy an operation queue.  The queue MUST be empty
226  * at this time.
227  *
228  * @param queue queue to destroy
229  */
230 void
231 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
232 {
233   GNUNET_break (NULL == queue->head);
234   GNUNET_break (NULL == queue->tail);
235   GNUNET_free (queue);
236 }
237
238
239 /**
240  * Add an operation to a queue.  An operation can be in multiple
241  * queues at once.  Once all queues permit the operation to become
242  * active, the operation will be activated.  The actual activation
243  * will occur in a separate task (thus allowing multiple queue 
244  * insertions to be made without having the first one instantly
245  * trigger the operation if the first queue has sufficient 
246  * resources).
247  *
248  * @param queue queue to add the operation to
249  * @param operation operation to add to the queue
250  */
251 void
252 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
253                                         struct GNUNET_TESTBED_Operation *operation)
254 {
255   struct QueueEntry *entry;
256
257   entry = GNUNET_malloc (sizeof (struct QueueEntry));
258   entry->op = operation;
259   GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
260   operation->queues =
261     GNUNET_realloc (operation->queues,
262                     sizeof (struct OperationQueue *) * (++operation->nqueues));
263   operation->queues[operation->nqueues - 1] = queue;
264   check_readiness (operation);
265 }
266
267
268 /**
269  * Remove an operation from a queue.  This can be because the
270  * oeration was active and has completed (and the resources have
271  * been released), or because the operation was cancelled and
272  * thus scheduling the operation is no longer required.
273  *
274  * @param queue queue to add the operation to
275  * @param operation operation to add to the queue
276  */
277 void
278 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
279                                         struct GNUNET_TESTBED_Operation *operation)
280 {
281   struct QueueEntry *entry;
282   struct QueueEntry *entry2;
283   
284   for (entry = queue->head; NULL != entry; entry = entry->next)
285     if (entry->op == operation)
286       break;
287   GNUNET_assert (NULL != entry);
288   if (OP_STATE_STARTED == operation->state)
289     queue->active++;
290   entry2 = entry->next;
291   GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
292   GNUNET_free (entry);
293   for (; NULL != entry2; entry2 = entry2->next)
294     if (OP_STATE_STARTED != entry2->op->state)
295       break;
296   if (NULL == entry2)
297     return;
298   check_readiness (entry2->op);
299 }
300
301
302 /**
303  * An operation is 'done' (was cancelled or finished); remove
304  * it from the queues and release associated resources.
305  *
306  * @param operation operation that finished
307  */
308 void
309 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
310 {
311   unsigned int i;
312     
313   if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
314   {
315     GNUNET_SCHEDULER_cancel (operation->start_task_id);
316     operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
317   }
318   for (i = 0; i < operation->nqueues; i++)
319     GNUNET_TESTBED_operation_queue_remove_ (operation->queues[i], operation);
320   GNUNET_free (operation->queues);
321   if (NULL != operation->release)
322     operation->release (operation->cb_cls);
323   GNUNET_free (operation);
324 }
325
326
327 /* end of testbed_api_operations.c */