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