glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / testbed / testbed_api.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 GNUnet e.V.
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14  */
15
16 /**
17  * @file testbed/testbed_api.h
18  * @brief Interface for functions internally exported from testbed_api.c
19  * @author Sree Harsha Totakura
20  */
21
22 #ifndef TESTBED_API_H
23 #define TESTBED_API_H
24
25 #include "gnunet_util_lib.h"
26 #include "gnunet_testbed_service.h"
27 #include "testbed.h"
28 #include "testbed_helper.h"
29
30 /**
31  * Testbed Helper binary name
32  */
33 #define HELPER_TESTBED_BINARY "gnunet-helper-testbed"
34
35
36 /**
37  * Enumeration of operations
38  */
39 enum OperationType
40 {
41     /**
42      * Peer create operation
43      */
44   OP_PEER_CREATE,
45
46     /**
47      * Peer start operation
48      */
49   OP_PEER_START,
50
51     /**
52      * Peer stop operation
53      */
54   OP_PEER_STOP,
55
56     /**
57      * Peer destroy operation
58      */
59   OP_PEER_DESTROY,
60
61     /**
62      * Get peer information operation
63      */
64   OP_PEER_INFO,
65
66   /**
67    * Reconfigure a peer
68    */
69   OP_PEER_RECONFIGURE,
70
71     /**
72      * Overlay connection operation
73      */
74   OP_OVERLAY_CONNECT,
75
76     /**
77      * Forwarded operation
78      */
79   OP_FORWARDED,
80
81     /**
82      * Link controllers operation
83      */
84   OP_LINK_CONTROLLERS,
85
86   /**
87    * Get slave config operation
88    */
89   OP_GET_SLAVE_CONFIG,
90
91   /**
92    * Stop and destroy all peers
93    */
94   OP_SHUTDOWN_PEERS,
95
96   /**
97    * Start/stop service at a peer
98    */
99   OP_MANAGE_SERVICE
100 };
101
102
103
104 /**
105  * Enumeration of states of OperationContext
106  */
107 enum OperationContextState
108 {
109   /**
110    * The initial state where the associated operation has just been created
111    * and is waiting in the operation queues to be started
112    */
113   OPC_STATE_INIT = 0,
114
115   /**
116    * The operation has been started. It may occupy some resources which are to
117    * be freed if cancelled.
118    */
119   OPC_STATE_STARTED,
120
121   /**
122    * The operation has finished. The end results of this operation may occupy
123    * some resources which are to be freed by operation_done
124    */
125   OPC_STATE_FINISHED
126 };
127
128
129 /**
130  * Context information for GNUNET_TESTBED_Operation
131  */
132 struct OperationContext
133 {
134   /**
135    * The controller to which this operation context belongs to
136    */
137   struct GNUNET_TESTBED_Controller *c;
138
139   /**
140    * The operation
141    */
142   struct GNUNET_TESTBED_Operation *op;
143
144   /**
145    * The operation closure
146    */
147   void *op_cls;
148
149   /**
150    * Data relevant to the operation
151    */
152   void *data;
153
154   /**
155    * The id of the opearation
156    */
157   uint64_t id;
158
159   /**
160    * The type of operation
161    */
162   enum OperationType type;
163
164   /**
165    * The state of the operation
166    */
167   enum OperationContextState state;
168 };
169
170
171 /**
172  * Operation empty callback
173  *
174  * @param cls closure
175  */
176 typedef void
177 (*TESTBED_opcq_empty_cb) (void *cls);
178
179
180 /**
181  * Handle to interact with a GNUnet testbed controller.  Each
182  * controller has at least one master handle which is created when the
183  * controller is created; this master handle interacts with the
184  * controller process, destroying it destroys the controller (by
185  * closing stdin of the controller process).  Additionally,
186  * controllers can interact with each other (in a P2P fashion); those
187  * links are established via TCP/IP on the controller's service port.
188  */
189 struct GNUNET_TESTBED_Controller
190 {
191   /**
192    * The host where the controller is running
193    */
194   struct GNUNET_TESTBED_Host *host;
195
196   /**
197    * The controller callback
198    */
199   GNUNET_TESTBED_ControllerCallback cc;
200
201   /**
202    * The closure for controller callback
203    */
204   void *cc_cls;
205
206   /**
207    * The configuration to use while connecting to controller
208    */
209   struct GNUNET_CONFIGURATION_Handle *cfg;
210
211   /**
212    * The message queue to the controller service
213    */
214   struct GNUNET_MQ_Handle *mq;
215
216   /**
217    * The host registration handle; NULL if no current registration requests are
218    * present
219    */
220   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
221
222   /**
223    * The map of active operation contexts
224    */
225   struct GNUNET_CONTAINER_MultiHashMap32 *opc_map;
226
227   /**
228    * If this callback is not NULL, schedule it as a task when opc_map gets empty
229    */
230   TESTBED_opcq_empty_cb opcq_empty_cb;
231
232   /**
233    * Closure for the above task
234    */
235   void *opcq_empty_cls;
236
237   /**
238    * Operation queue for simultaneous operations
239    */
240   struct OperationQueue *opq_parallel_operations;
241
242   /**
243    * Operation queue for simultaneous service connections
244    */
245   struct OperationQueue *opq_parallel_service_connections;
246
247   /**
248    * Operation queue for simultaneous topology configuration operations
249    */
250   struct OperationQueue *opq_parallel_topology_config_operations;
251
252   /**
253    * handle for hashtable of barrier handles, values are
254    * of type `struct GNUNET_TESTBED_Barrier`.
255    */
256   struct GNUNET_CONTAINER_MultiHashMap *barrier_map;
257
258   /**
259    * The controller event mask
260    */
261   uint64_t event_mask;
262
263   /**
264    * The operation id counter. use current value and increment
265    */
266   uint32_t operation_counter;
267
268 };
269
270
271 /**
272  * Handle for barrier
273  */
274 struct GNUNET_TESTBED_Barrier
275 {
276   /**
277    * hashcode identifying this barrier in the hashmap
278    */
279   struct GNUNET_HashCode key;
280
281   /**
282    * The controller handle given while initiliasing this barrier
283    */
284   struct GNUNET_TESTBED_Controller *c;
285
286   /**
287    * The name of the barrier
288    */
289   char *name;
290
291   /**
292    * The continuation callback to call when we have a status update on this
293    */
294   GNUNET_TESTBED_barrier_status_cb cb;
295
296   /**
297    * the closure for the above callback
298    */
299   void *cls;
300
301   /**
302    * Should the barrier crossed status message be echoed back to the controller?
303    */
304   int echo;
305 };
306
307
308
309 /**
310  * Queues a message in send queue for sending to the service
311  *
312  * @param controller the handle to the controller
313  * @param msg the message to queue
314  * @deprecated
315  */
316 void
317 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
318                                struct GNUNET_MessageHeader *msg);
319
320
321 /**
322  * Inserts the given operation context into the operation context map of the
323  * given controller.  Creates the operation context map if one does not exist
324  * for the controller
325  *
326  * @param c the controller
327  * @param opc the operation context to be inserted
328  */
329 void
330 GNUNET_TESTBED_insert_opc_ (struct GNUNET_TESTBED_Controller *c,
331                             struct OperationContext *opc);
332
333
334 /**
335  * Removes the given operation context from the operation context map of the
336  * given controller
337  *
338  * @param c the controller
339  * @param opc the operation context to remove
340  */
341 void
342 GNUNET_TESTBED_remove_opc_ (const struct GNUNET_TESTBED_Controller *c,
343                             struct OperationContext *opc);
344
345
346 /**
347  * Compresses given configuration using zlib compress
348  *
349  * @param config the serialized configuration
350  * @param size the size of config
351  * @param xconfig will be set to the compressed configuration (memory is fresly
352  *          allocated)
353  * @return the size of the xconfig
354  */
355 size_t
356 GNUNET_TESTBED_compress_config_ (const char *config,
357                                  size_t size,
358                                  char **xconfig);
359
360
361 /**
362  * Function to serialize and compress using zlib a configuration through a
363  * configuration handle
364  *
365  * @param cfg the configuration
366  * @param size the size of configuration when serialize.  Will be set on success.
367  * @param xsize the sizeo of the compressed configuration.  Will be set on success.
368  * @return the serialized and compressed configuration
369  */
370 char *
371 GNUNET_TESTBED_compress_cfg_ (const struct GNUNET_CONFIGURATION_Handle *cfg,
372                               size_t *size,
373                               size_t *xsize);
374
375
376 /**
377  * Creates a helper initialization message. This function is here because we
378  * want to use this in testing
379  *
380  * @param trusted_ip the ip address of the controller which will be set as TRUSTED
381  *          HOST(all connections form this ip are permitted by the testbed) when
382  *          starting testbed controller at host. This can either be a single ip
383  *          address or a network address in CIDR notation.
384  * @param hostname the hostname of the destination this message is intended for
385  * @param cfg the configuration that has to used to start the testbed service
386  *          thru helper
387  * @return the initialization message
388  */
389 struct GNUNET_TESTBED_HelperInit *
390 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
391                                         const char *hostname,
392                                         const struct GNUNET_CONFIGURATION_Handle *cfg);
393
394
395 /**
396  * Sends the given message as an operation. The given callback is called when a
397  * reply for the operation is available.  Call
398  * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
399  * operation context if the cc hasn't been called
400  *
401  * @param controller the controller to which the message has to be sent
402  * @param operation_id the operation id of the message
403  * @param msg the message to send
404  * @param cc the callback to call when reply is available
405  * @param cc_cls the closure for the above callback
406  * @return the operation context which can be used to cancel the forwarded
407  *           operation
408  */
409 struct OperationContext *
410 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller *controller,
411                                        uint64_t operation_id,
412                                        const struct GNUNET_MessageHeader *msg,
413                                        GNUNET_MQ_MessageCallback cc,
414                                        void *cc_cls);
415
416 /**
417  * Function to cancel an operation created by simply forwarding an operation
418  * message.
419  *
420  * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
421  */
422 void
423 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc);
424
425
426 /**
427  * Generates configuration by uncompressing configuration in given message. The
428  * given message should be of the following types:
429  * #GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG,
430  * #GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG
431  *
432  * @param msg the message containing compressed configuration
433  * @return handle to the parsed configuration
434  */
435 struct GNUNET_CONFIGURATION_Handle *
436 GNUNET_TESTBED_extract_config_ (const struct GNUNET_MessageHeader *msg);
437
438
439 /**
440  * Checks the integrity of the OpeationFailureEventMessage and if good returns
441  * the error message it contains.
442  *
443  * @param msg the OperationFailureEventMessage
444  * @return the error message
445  */
446 const char *
447 GNUNET_TESTBED_parse_error_string_ (const struct GNUNET_TESTBED_OperationFailureEventMessage *msg);
448
449
450 /**
451  * Function to return the operation id for a controller. The operation id is
452  * created from the controllers host id and its internal operation counter.
453  *
454  * @param controller the handle to the controller whose operation id has to be incremented
455  * @return the incremented operation id.
456  */
457 uint64_t
458 GNUNET_TESTBED_get_next_op_id (struct GNUNET_TESTBED_Controller *controller);
459
460
461 /**
462  * Like GNUNET_TESTBED_get_slave_config(), however without the host registration
463  * check. Another difference is that this function takes the id of the slave
464  * host.
465  *
466  * @param op_cls the closure for the operation
467  * @param master the handle to master controller
468  * @param slave_host_id id of the host where the slave controller is running to
469  *          the slave_host should remain valid until this operation is cancelled
470  *          or marked as finished
471  * @return the operation handle;
472  */
473 struct GNUNET_TESTBED_Operation *
474 GNUNET_TESTBED_get_slave_config_ (void *op_cls,
475                                   struct GNUNET_TESTBED_Controller *master,
476                                   uint32_t slave_host_id);
477
478
479
480 /**
481  * Initialise a barrier and call the given callback when the required percentage
482  * of peers (quorum) reach the barrier OR upon error.
483  *
484  * @param controller the handle to the controller
485  * @param name identification name of the barrier
486  * @param quorum the percentage of peers that is required to reach the barrier.
487  *   Peers signal reaching a barrier by calling
488  *   GNUNET_TESTBED_barrier_reached().
489  * @param cb the callback to call when the barrier is reached or upon error.
490  *   Cannot be NULL.
491  * @param cls closure for the above callback
492  * @param echo #GNUNET_YES to echo the barrier crossed status message back to the
493  *   controller
494  * @return barrier handle; NULL upon error
495  */
496 struct GNUNET_TESTBED_Barrier *
497 GNUNET_TESTBED_barrier_init_ (struct GNUNET_TESTBED_Controller *controller,
498                               const char *name,
499                               unsigned int quorum,
500                               GNUNET_TESTBED_barrier_status_cb cb,
501                               void *cls,
502                               int echo);
503
504
505 /**
506  * Remove a barrier and it was the last one in the barrier hash map, destroy the
507  * hash map
508  *
509  * @param barrier the barrier to remove
510  */
511 void
512 GNUNET_TESTBED_barrier_remove_ (struct GNUNET_TESTBED_Barrier *barrier);
513
514
515
516 #endif
517 /* end of testbed_api.h */