peer start/stop with new operations handling
[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   for (i = 0; i < op->nqueues; i++)
168   {
169     if (0 == op->queues[i]->active)
170       return;
171   }
172   for (i = 0; i < op->nqueues; i++)
173   {
174     op->queues[i]->active--;
175   }
176   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == op->start_task_id);
177   op->start_task_id = GNUNET_SCHEDULER_add_now (&call_start, op);  
178 }
179
180
181 /**
182  * Create an 'operation' to be performed.
183  *
184  * @param cls closure for the callbacks
185  * @param start function to call to start the operation
186  * @param release function to call to close down the operation
187  * @return handle to the operation
188  */
189 struct GNUNET_TESTBED_Operation *
190 GNUNET_TESTBED_operation_create_ (void *cls,
191                                   OperationStart start,
192                                   OperationRelease release)
193 {
194   struct GNUNET_TESTBED_Operation *op;
195
196   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
197   op->start = start;
198   op->release = release;
199   op->cb_cls = cls;
200   return op;  
201 }
202
203
204 /**
205  * Create an operation queue.
206  *
207  * @param max_active maximum number of operations in this
208  *        queue that can be active in parallel at the same time
209  * @return handle to the queue
210  */
211 struct OperationQueue *
212 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
213 {
214   struct OperationQueue *queue;
215
216   queue = GNUNET_malloc (sizeof (struct OperationQueue));
217   queue->active = max_active;
218   return queue;
219 }
220
221
222 /**
223  * Destroy an operation queue.  The queue MUST be empty
224  * at this time.
225  *
226  * @param queue queue to destroy
227  */
228 void
229 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
230 {
231   GNUNET_break (NULL == queue->head);
232   GNUNET_break (NULL == queue->tail);
233   GNUNET_free (queue);
234 }
235
236
237 /**
238  * Add an operation to a queue.  An operation can be in multiple
239  * queues at once.  Once all queues permit the operation to become
240  * active, the operation will be activated.  The actual activation
241  * will occur in a separate task (thus allowing multiple queue 
242  * insertions to be made without having the first one instantly
243  * trigger the operation if the first queue has sufficient 
244  * resources).
245  *
246  * @param queue queue to add the operation to
247  * @param operation operation to add to the queue
248  */
249 void
250 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
251                                         struct GNUNET_TESTBED_Operation *operation)
252 {
253   struct QueueEntry *entry;
254
255   entry = GNUNET_malloc (sizeof (struct QueueEntry));
256   entry->op = operation;
257   GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
258   operation->queues =
259     GNUNET_realloc (operation->queues,
260                     sizeof (struct OperationQueue *) * (++operation->nqueues));
261   operation->queues[operation->nqueues - 1] = queue;
262   check_readiness (operation);
263 }
264
265
266 /**
267  * Remove an operation from a queue.  This can be because the
268  * oeration was active and has completed (and the resources have
269  * been released), or because the operation was cancelled and
270  * thus scheduling the operation is no longer required.
271  *
272  * @param queue queue to add the operation to
273  * @param operation operation to add to the queue
274  */
275 void
276 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
277                                         struct GNUNET_TESTBED_Operation *operation)
278 {
279   struct QueueEntry *entry;
280   struct QueueEntry *entry2;
281   
282   for (entry = queue->head; NULL != entry; entry = entry->next)
283     if (entry->op == operation)
284       break;
285   GNUNET_assert (NULL != entry);
286   if (OP_STATE_STARTED == operation->state)
287     queue->active++;
288   entry2 = entry->next;
289   GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
290   GNUNET_free (entry);
291   for (; NULL != entry2; entry2 = entry2->next)
292     if (OP_STATE_STARTED != entry2->op->state)
293       break;
294   if (NULL == entry2)
295     return;
296   check_readiness (entry2->op);
297 }
298
299
300 /**
301  * An operation is 'done' (was cancelled or finished); remove
302  * it from the queues and release associated resources.
303  *
304  * @param operation operation that finished
305  */
306 void
307 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
308 {
309   unsigned int i;
310     
311   if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
312   {
313     GNUNET_SCHEDULER_cancel (operation->start_task_id);
314     operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
315   }
316   for (i = 0; i < operation->nqueues; i++)
317     GNUNET_TESTBED_operation_queue_remove_ (operation->queues[i], operation);
318   GNUNET_free (operation->queues);
319   if (NULL != operation->release)
320     operation->release (operation->cb_cls);
321   GNUNET_free (operation);
322 }
323
324
325 /* end of testbed_api_operations.c */