- test config fixes for adaptive overlay connects
[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 controller event mask
281    */
282   uint64_t event_mask;
283
284   /**
285    * Did we start the receive loop yet?
286    */
287   int in_receive;
288
289   /**
290    * Did we create the host for this?
291    */
292   int aux_host;
293
294   /**
295    * The number of parallel overlay connects we do currently
296    */
297   unsigned int num_parallel_connects;
298
299   /**
300    * The threshold for the number of parallel overlay connects we do
301    */
302   unsigned int num_parallel_connects_threshold;
303
304   /**
305    * The operation id counter. use current value and increment
306    */
307   uint32_t operation_counter;
308
309 };
310
311
312 /**
313  * Queues a message in send queue for sending to the service
314  *
315  * @param controller the handle to the controller
316  * @param msg the message to queue
317  */
318 void
319 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
320                                struct GNUNET_MessageHeader *msg);
321
322
323 /**
324  * Compresses given configuration using zlib compress
325  *
326  * @param config the serialized configuration
327  * @param size the size of config
328  * @param xconfig will be set to the compressed configuration (memory is fresly
329  *          allocated)
330  * @return the size of the xconfig
331  */
332 size_t
333 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
334                                  char **xconfig);
335
336
337 /**
338  * Adds an operation to the queue of operations
339  *
340  * @param op the operation to add
341  */
342 void
343 GNUNET_TESTBED_operation_add_ (struct GNUNET_TESTBED_Operation *op);
344
345
346 /**
347  * Creates a helper initialization message. Only for testing.
348  *
349  * @param cname the ip address of the controlling host
350  * @param hostname the hostname of the destination this message is intended for
351  * @param cfg the configuration that has to used to start the testbed service
352  *          thru helper
353  * @return the initialization message
354  */
355 struct GNUNET_TESTBED_HelperInit *
356 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
357                                         const char *hostname,
358                                         const struct GNUNET_CONFIGURATION_Handle
359                                         *cfg);
360
361
362 /**
363  * Sends the given message as an operation. The given callback is called when a
364  * reply for the operation is available.  Call
365  * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
366  * operation context if the cc hasn't been called
367  *
368  * @param controller the controller to which the message has to be sent
369  * @param operation_id the operation id of the message
370  * @param msg the message to send
371  * @param cc the callback to call when reply is available
372  * @param cc_cls the closure for the above callback
373  * @return the operation context which can be used to cancel the forwarded
374  *           operation
375  */
376 struct OperationContext *
377 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
378                                        *controller, uint64_t operation_id,
379                                        const struct GNUNET_MessageHeader *msg,
380                                        GNUNET_CLIENT_MessageHandler cc,
381                                        void *cc_cls);
382
383 /**
384  * Function to cancel an operation created by simply forwarding an operation
385  * message.
386  *
387  * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
388  */
389 void
390 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc);
391
392
393 /**
394  * Generates configuration by uncompressing configuration in given message. The
395  * given message should be of the following types:
396  * GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG,
397  * GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG
398  *
399  * @param msg the message containing compressed configuration
400  * @return handle to the parsed configuration
401  */
402 struct GNUNET_CONFIGURATION_Handle *
403 GNUNET_TESTBED_extract_config_ (const struct GNUNET_MessageHeader *msg);
404
405
406 /**
407  * Checks the integrity of the OpeationFailureEventMessage and if good returns
408  * the error message it contains.
409  *
410  * @param msg the OperationFailureEventMessage
411  * @return the error message
412  */
413 const char *
414 GNUNET_TESTBED_parse_error_string_ (const struct
415                                     GNUNET_TESTBED_OperationFailureEventMessage
416                                     *msg);
417
418
419 /**
420  * Function to return the operation id for a controller. The operation id is
421  * created from the controllers host id and its internal operation counter.
422  *
423  * @param controller the handle to the controller whose operation id has to be incremented
424  * @return the incremented operation id.
425  */
426 uint64_t
427 GNUNET_TESTBED_get_next_op_id (struct GNUNET_TESTBED_Controller *controller);
428
429
430 /**
431  * Like GNUNET_TESTBED_get_slave_config(), however without the host registration
432  * check. Another difference is that this function takes the id of the slave
433  * host.
434  *
435  * @param op_cls the closure for the operation
436  * @param master the handle to master controller
437  * @param slave_host_id id of the host where the slave controller is running to
438  *          the slave_host should remain valid until this operation is cancelled
439  *          or marked as finished
440  * @return the operation handle;
441  */
442 struct GNUNET_TESTBED_Operation *
443 GNUNET_TESTBED_get_slave_config_ (void *op_cls,
444                                   struct GNUNET_TESTBED_Controller *master,
445                                   uint32_t slave_host_id);
446
447
448 /**
449  * Same as the GNUNET_TESTBED_controller_link_2, but with ids for delegated host
450  * and slave host
451  *
452  * @param op_cls the operation closure for the event which is generated to
453  *          signal success or failure of this operation
454  * @param master handle to the master controller who creates the association
455  * @param delegated_host_id id of the host to which requests should be delegated
456  * @param slave_host_id id of the host which is used to run the slave controller
457  * @param sxcfg serialized and compressed configuration
458  * @param sxcfg_size the size sxcfg
459  * @param scfg_size the size of uncompressed serialized configuration
460  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
461  *          be started by the slave controller; GNUNET_NO if the slave
462  *          controller has to connect to the already started delegated
463  *          controller via TCP/IP
464  * @return the operation handle
465  */
466 struct GNUNET_TESTBED_Operation *
467 GNUNET_TESTBED_controller_link_2_ (void *op_cls,
468                                    struct GNUNET_TESTBED_Controller *master,
469                                    uint32_t delegated_host_id,
470                                    uint32_t slave_host_id,
471                                    const char *sxcfg, size_t sxcfg_size,
472                                    size_t scfg_size, int is_subordinate);
473
474
475 /**
476  * Same as the GNUNET_TESTBED_controller_link, but with ids for delegated host
477  * and slave host
478  *
479  * @param op_cls the operation closure for the event which is generated to
480  *          signal success or failure of this operation
481  * @param master handle to the master controller who creates the association
482  * @param delegated_host_id id of the host to which requests should be
483  *          delegated; cannot be NULL
484  * @param slave_host_id id of the host which should connect to controller
485  *          running on delegated host ; use NULL to make the master controller
486  *          connect to the delegated host
487  * @param slave_cfg configuration to use for the slave controller
488  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
489  *          be started by the slave controller; GNUNET_NO if the slave
490  *          controller has to connect to the already started delegated
491  *          controller via TCP/IP
492  * @return the operation handle
493  */
494 struct GNUNET_TESTBED_Operation *
495 GNUNET_TESTBED_controller_link_ (void *op_cls,
496                                 struct GNUNET_TESTBED_Controller *master,
497                                 uint32_t delegated_host_id,
498                                 uint32_t slave_host_id,
499                                 const struct GNUNET_CONFIGURATION_Handle
500                                 *slave_cfg,
501                                  int is_subordinate);
502
503 #endif
504 /* end of testbed_api.h */