-reducing testing peers and links
[oweals/gnunet.git] / src / testbed / testbed_api.h
1 /*
2       This file is part of GNUnet
3       (C) 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.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_testbed_service.h"
31 #include "testbed.h"
32
33
34 /**
35  * Testbed Helper binary name
36  */
37 #define HELPER_TESTBED_BINARY "gnunet-helper-testbed"
38
39
40 /**
41  * Enumeration of operations
42  */
43 enum OperationType
44 {
45     /**
46      * Peer create operation
47      */
48   OP_PEER_CREATE,
49
50     /**
51      * Peer start operation
52      */
53   OP_PEER_START,
54
55     /**
56      * Peer stop operation
57      */
58   OP_PEER_STOP,
59
60     /**
61      * Peer destroy operation
62      */
63   OP_PEER_DESTROY,
64
65     /**
66      * Get peer information operation
67      */
68   OP_PEER_INFO,
69
70     /**
71      * Overlay connection operation
72      */
73   OP_OVERLAY_CONNECT,
74
75     /**
76      * Forwarded operation
77      */
78   OP_FORWARDED,
79
80     /**
81      * Link controllers operation
82      */
83   OP_LINK_CONTROLLERS,
84
85   /**
86    * Get slave config operation
87    */
88   OP_GET_SLAVE_CONFIG
89
90 };
91
92
93 /**
94  * The message queue for sending messages to the controller service
95  */
96 struct MessageQueue;
97
98 /**
99  * Structure for a controller link
100  */
101 struct ControllerLink;
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    * next ptr for DLL
136    */
137   struct OperationContext *next;
138
139   /**
140    * prev ptr for DLL
141    */
142   struct OperationContext *prev;
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  * Handle to interact with a GNUnet testbed controller.  Each
183  * controller has at least one master handle which is created when the
184  * controller is created; this master handle interacts with the
185  * controller process, destroying it destroys the controller (by
186  * closing stdin of the controller process).  Additionally,
187  * controllers can interact with each other (in a P2P fashion); those
188  * links are established via TCP/IP on the controller's service port.
189  */
190 struct GNUNET_TESTBED_Controller
191 {
192
193   /**
194    * The host where the controller is running
195    */
196   struct GNUNET_TESTBED_Host *host;
197
198   /**
199    * The controller callback
200    */
201   GNUNET_TESTBED_ControllerCallback cc;
202
203   /**
204    * The closure for controller callback
205    */
206   void *cc_cls;
207
208   /**
209    * The configuration to use while connecting to controller
210    */
211   struct GNUNET_CONFIGURATION_Handle *cfg;
212
213   /**
214    * The client connection handle to the controller service
215    */
216   struct GNUNET_CLIENT_Connection *client;
217
218   /**
219    * The head of the message queue
220    */
221   struct MessageQueue *mq_head;
222
223   /**
224    * The tail of the message queue
225    */
226   struct MessageQueue *mq_tail;
227
228   /**
229    * The head of the ControllerLink list
230    */
231   struct ControllerLink *cl_head;
232
233   /**
234    * The tail of the ControllerLink list
235    */
236   struct ControllerLink *cl_tail;
237
238   /**
239    * The client transmit handle
240    */
241   struct GNUNET_CLIENT_TransmitHandle *th;
242
243   /**
244    * The host registration handle; NULL if no current registration requests are
245    * present
246    */
247   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
248
249   /**
250    * The head of the opeartion context queue
251    */
252   struct OperationContext *ocq_head;
253
254   /**
255    * The tail of the operation context queue
256    */
257   struct OperationContext *ocq_tail;
258
259   /**
260    * Operation queue for simultaneous operations
261    */
262   struct OperationQueue *opq_parallel_operations;
263
264   /**
265    * Operation queue for simultaneous service connections
266    */
267   struct OperationQueue *opq_parallel_service_connections;
268
269   /**
270    * Operation queue for simultaneous topology configuration operations
271    */
272   struct OperationQueue *opq_parallel_topology_config_operations;
273
274   /**
275    * Operation queue for simultaneous overlay connect operations
276    */
277   struct OperationQueue *opq_parallel_overlay_connect_operations;
278
279   /**
280    * The operation id counter. use current value and increment
281    */
282   uint32_t operation_counter;
283
284   /**
285    * The controller event mask
286    */
287   uint64_t event_mask;
288
289   /**
290    * Did we start the receive loop yet?
291    */
292   int in_receive;
293
294   /**
295    * Did we create the host for this?
296    */
297   int aux_host;
298 };
299
300
301 /**
302  * Queues a message in send queue for sending to the service
303  *
304  * @param controller the handle to the controller
305  * @param msg the message to queue
306  */
307 void
308 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
309                                struct GNUNET_MessageHeader *msg);
310
311
312 /**
313  * Compresses given configuration using zlib compress
314  *
315  * @param config the serialized configuration
316  * @param size the size of config
317  * @param xconfig will be set to the compressed configuration (memory is fresly
318  *          allocated)
319  * @return the size of the xconfig
320  */
321 size_t
322 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
323                                  char **xconfig);
324
325
326 /**
327  * Adds an operation to the queue of operations
328  *
329  * @param op the operation to add
330  */
331 void
332 GNUNET_TESTBED_operation_add_ (struct GNUNET_TESTBED_Operation *op);
333
334
335 /**
336  * Creates a helper initialization message. Only for testing.
337  *
338  * @param cname the ip address of the controlling host
339  * @param hostname the hostname of the destination this message is intended for
340  * @param cfg the configuration that has to used to start the testbed service
341  *          thru helper
342  * @return the initialization message
343  */
344 struct GNUNET_TESTBED_HelperInit *
345 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
346                                         const char *hostname,
347                                         const struct GNUNET_CONFIGURATION_Handle
348                                         *cfg);
349
350
351 /**
352  * Sends the given message as an operation. The given callback is called when a
353  * reply for the operation is available.  Call
354  * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
355  * operation context if the cc hasn't been called
356  *
357  * @param controller the controller to which the message has to be sent
358  * @param operation_id the operation id of the message
359  * @param msg the message to send
360  * @param cc the callback to call when reply is available
361  * @param cc_cls the closure for the above callback
362  * @return the operation context which can be used to cancel the forwarded
363  *           operation
364  */
365 struct OperationContext *
366 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
367                                        *controller, uint64_t operation_id,
368                                        const struct GNUNET_MessageHeader *msg,
369                                        GNUNET_CLIENT_MessageHandler cc,
370                                        void *cc_cls);
371
372 /**
373  * Function to cancel an operation created by simply forwarding an operation
374  * message.
375  *
376  * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
377  */
378 void
379 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc);
380
381
382 /**
383  * Generates configuration by uncompressing configuration in given message. The
384  * given message should be of the following types:
385  * GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG,
386  * GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG
387  *
388  * @param msg the message containing compressed configuration
389  * @return handle to the parsed configuration
390  */
391 struct GNUNET_CONFIGURATION_Handle *
392 GNUNET_TESTBED_extract_config_ (const struct GNUNET_MessageHeader *msg);
393
394
395 /**
396  * Checks the integrity of the OpeationFailureEventMessage and if good returns
397  * the error message it contains.
398  *
399  * @param msg the OperationFailureEventMessage
400  * @return the error message
401  */
402 const char *
403 GNUNET_TESTBED_parse_error_string_ (const struct
404                                     GNUNET_TESTBED_OperationFailureEventMessage
405                                     *msg);
406
407
408 /**
409  * Function to return the operation id for a controller. The operation id is
410  * created from the controllers host id and its internal operation counter.
411  *
412  * @param controller the handle to the controller whose operation id has to be incremented
413  * @return the incremented operation id.
414  */
415 uint64_t
416 GNUNET_TESTBED_get_next_op_id (struct GNUNET_TESTBED_Controller *controller);
417
418
419 /**
420  * Like GNUNET_TESTBED_get_slave_config(), however without the host registration
421  * check. Another difference is that this function takes the id of the slave
422  * host.
423  *
424  * @param op_cls the closure for the operation
425  * @param master the handle to master controller
426  * @param slave_host_id id of the host where the slave controller is running to
427  *          the slave_host should remain valid until this operation is cancelled
428  *          or marked as finished
429  * @return the operation handle;
430  */
431 struct GNUNET_TESTBED_Operation *
432 GNUNET_TESTBED_get_slave_config_ (void *op_cls,
433                                   struct GNUNET_TESTBED_Controller *master,
434                                   uint32_t slave_host_id);
435
436
437 /**
438  * Same as the GNUNET_TESTBED_controller_link_2, but with ids for delegated host
439  * and slave host
440  *
441  * @param op_cls the operation closure for the event which is generated to
442  *          signal success or failure of this operation
443  * @param master handle to the master controller who creates the association
444  * @param delegated_host_id id of the host to which requests should be delegated
445  * @param slave_host_id id of the host which is used to run the slave controller
446  * @param sxcfg serialized and compressed configuration
447  * @param sxcfg_size the size sxcfg
448  * @param scfg_size the size of uncompressed serialized configuration
449  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
450  *          be started by the slave controller; GNUNET_NO if the slave
451  *          controller has to connect to the already started delegated
452  *          controller via TCP/IP
453  * @return the operation handle
454  */
455 struct GNUNET_TESTBED_Operation *
456 GNUNET_TESTBED_controller_link_2_ (void *op_cls,
457                                    struct GNUNET_TESTBED_Controller *master,
458                                    uint32_t delegated_host_id,
459                                    uint32_t slave_host_id,
460                                    const char *sxcfg, size_t sxcfg_size,
461                                    size_t scfg_size, int is_subordinate);
462
463
464 /**
465  * Same as the GNUNET_TESTBED_controller_link, but with ids for delegated host
466  * and slave host
467  *
468  * @param op_cls the operation closure for the event which is generated to
469  *          signal success or failure of this operation
470  * @param master handle to the master controller who creates the association
471  * @param delegated_host_id id of the host to which requests should be
472  *          delegated; cannot be NULL
473  * @param slave_host_id id of the host which should connect to controller
474  *          running on delegated host ; use NULL to make the master controller
475  *          connect to the delegated host
476  * @param slave_cfg configuration to use for the slave controller
477  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
478  *          be started by the slave controller; GNUNET_NO if the slave
479  *          controller has to connect to the already started delegated
480  *          controller via TCP/IP
481  * @return the operation handle
482  */
483 struct GNUNET_TESTBED_Operation *
484 GNUNET_TESTBED_controller_link_ (void *op_cls,
485                                 struct GNUNET_TESTBED_Controller *master,
486                                 uint32_t delegated_host_id,
487                                 uint32_t slave_host_id,
488                                 const struct GNUNET_CONFIGURATION_Handle
489                                 *slave_cfg,
490                                  int is_subordinate);
491
492 #endif
493 /* end of testbed_api.h */