testbed api test case and fixes
[oweals/gnunet.git] / src / testbed / testbed_api.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.c
23  * @brief API for accessing the GNUnet testing service.
24  *        This library is supposed to make it easier to write
25  *        testcases and script large-scale benchmarks.
26  * @author Christian Grothoff
27  */
28 #include "platform.h"
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_hello_lib.h"
34 #include <zlib.h>
35
36 #include "testbed.h"
37 #include "testbed_api_hosts.h"
38
39 /**
40  * Generic logging shorthand
41  */
42 #define LOG(kind, ...)                          \
43   GNUNET_log_from (kind, "testbed-api", __VA_ARGS__);
44
45 /**
46  * Debug logging
47  */
48 #define LOG_DEBUG(...)                          \
49   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
50
51 /**
52  * Relative time seconds shorthand
53  */
54 #define TIME_REL_SECS(sec) \
55   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
56
57
58 /**
59  * Default server message sending retry timeout
60  */
61 #define TIMEOUT_REL TIME_REL_SECS(1)
62
63
64 /**
65  * The message queue for sending messages to the controller service
66  */
67 struct MessageQueue
68 {
69   /**
70    * The message to be sent
71    */
72   struct GNUNET_MessageHeader *msg;
73
74   /**
75    * next pointer for DLL
76    */
77   struct MessageQueue *next;
78   
79   /**
80    * prev pointer for DLL
81    */
82   struct MessageQueue *prev;
83 };
84
85
86 /**
87  * Structure for a controller link
88  */
89 struct ControllerLink
90 {
91   /**
92    * The next ptr for DLL
93    */
94   struct ControllerLink *next;
95
96   /**
97    * The prev ptr for DLL
98    */
99   struct ControllerLink *prev;
100
101   /**
102    * The host which will be referred in the peer start request. This is the
103    * host where the peer should be started
104    */
105   struct GNUNET_TESTBED_Host *delegated_host;
106
107   /**
108    * The host which will contacted to delegate the peer start request
109    */
110   struct GNUNET_TESTBED_Host *slave_host;
111
112   /**
113    * The configuration to be used to connect to slave host
114    */
115   const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
116
117   /**
118    * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
119    * if we are just allowed to use the slave via TCP/IP
120    */
121   int is_subordinate;
122 };
123
124
125 /**
126  * Handle to interact with a GNUnet testbed controller.  Each
127  * controller has at least one master handle which is created when the
128  * controller is created; this master handle interacts with the
129  * controller process, destroying it destroys the controller (by
130  * closing stdin of the controller process).  Additionally,
131  * controllers can interact with each other (in a P2P fashion); those
132  * links are established via TCP/IP on the controller's service port.
133  */
134 struct GNUNET_TESTBED_Controller
135 {
136
137   /**
138    * The host where the controller is running
139    */
140   struct GNUNET_TESTBED_Host *host;
141
142   /**
143    * The controller callback
144    */
145   GNUNET_TESTBED_ControllerCallback cc;
146
147   /**
148    * The closure for controller callback
149    */
150   void *cc_cls;
151
152   /**
153    * The configuration to use while connecting to controller
154    */
155   struct GNUNET_CONFIGURATION_Handle *cfg;
156
157   /**
158    * The client connection handle to the controller service
159    */
160   struct GNUNET_CLIENT_Connection *client;
161   
162   /**
163    * The head of the message queue
164    */
165   struct MessageQueue *mq_head;
166
167   /**
168    * The tail of the message queue
169    */
170   struct MessageQueue *mq_tail;
171
172   /**
173    * The head of the ControllerLink list
174    */
175   struct ControllerLink *cl_head;
176
177   /**
178    * The tail of the ControllerLink list
179    */
180   struct ControllerLink *cl_tail;
181
182   /**
183    * The client transmit handle
184    */
185   struct GNUNET_CLIENT_TransmitHandle *th;
186
187   /**
188    * The host registration handle; NULL if no current registration requests are
189    * present 
190    */
191   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
192
193   /**
194    * The controller event mask
195    */
196   uint64_t event_mask;
197
198   /**
199    * Did we start the receive loop yet?
200    */
201   int in_receive;
202
203   /**
204    * Did we create the host for this?
205    */
206   int aux_host;
207 };
208
209
210 /**
211  * handle for host registration
212  */
213 struct GNUNET_TESTBED_HostRegistrationHandle
214 {
215   /**
216    * The host being registered
217    */
218   struct GNUNET_TESTBED_Host *host;
219
220   /**
221    * The controller at which this host is being registered
222    */
223   struct GNUNET_TESTBED_Controller *c;
224
225   /**
226    * The Registartion completion callback
227    */
228   GNUNET_TESTBED_HostRegistrationCompletion cc;
229
230   /**
231    * The closure for above callback
232    */
233   void *cc_cls;
234 };
235
236
237 /**
238  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
239  * controller (testbed service)
240  *
241  * @param c the controller handler
242  * @param msg message received
243  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
244  *           not
245  */
246 static int
247 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
248                        const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
249 {
250   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
251   char *emsg;
252   uint16_t msg_size;
253
254   rh = c->rh;
255   if (NULL == rh)
256   {  
257     return GNUNET_OK;    
258   }
259   if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
260   {
261     LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
262                GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
263     return GNUNET_OK;
264   }
265   c->rh = NULL;
266   msg_size = ntohs (msg->header.size);
267   if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
268   {
269     LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
270     GNUNET_TESTBED_mark_host_registered_at_  (rh->host, c);
271     rh->cc (rh->cc_cls, NULL);
272     GNUNET_free (rh);
273     return GNUNET_OK;
274   } 
275   /* We have an error message */
276   emsg = (char *) &msg[1];
277   if ('\0' != emsg[msg_size - 
278                    sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
279   {
280     GNUNET_break (0);
281     GNUNET_free (rh);
282     return GNUNET_NO;
283   }  
284   LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
285        ntohl (msg->host_id), emsg);
286   rh->cc (rh->cc_cls, emsg);
287   GNUNET_free (rh);
288   return GNUNET_OK;
289 }
290
291
292 /**
293  * Handler for messages from controller (testbed service)
294  *
295  * @param cls the controller handler
296  * @param msg message received, NULL on timeout or fatal error
297  */
298 static void 
299 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
300 {
301   struct GNUNET_TESTBED_Controller *c = cls;  
302   int status;
303
304   c->in_receive = GNUNET_NO;
305   /* FIXME: Add checks for message integrity */
306   if (NULL == msg)
307   {
308     LOG_DEBUG ("Receive timed out or connection to service dropped\n");
309     return;
310   }
311   status = GNUNET_OK;
312   switch (ntohs (msg->type))
313   {
314   case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
315     status =
316       handle_addhostconfirm (c, (const struct
317                                  GNUNET_TESTBED_HostConfirmedMessage *) msg);   
318     break;
319   default:
320     GNUNET_break (0);
321   }
322   if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
323   {
324     c->in_receive = GNUNET_YES;
325     GNUNET_CLIENT_receive (c->client, &message_handler, c,
326                            GNUNET_TIME_UNIT_FOREVER_REL);    
327   }
328 }
329
330 /**
331  * Function called to notify a client about the connection begin ready to queue
332  * more data.  "buf" will be NULL and "size" zero if the connection was closed
333  * for writing in the meantime.
334  *
335  * @param cls closure
336  * @param size number of bytes available in buf
337  * @param buf where the callee should write the message
338  * @return number of bytes written to buf
339  */
340 static size_t
341 transmit_ready_notify (void *cls, size_t size, void *buf)
342 {
343   struct GNUNET_TESTBED_Controller *c = cls;
344   struct MessageQueue *mq_entry;
345
346   c->th = NULL;
347   mq_entry = c->mq_head;
348   GNUNET_assert (NULL != mq_entry);
349   if ((0 == size) && (NULL == buf)) /* Timeout */
350   {
351     LOG_DEBUG ("Message sending timed out -- retrying\n");
352     c->th =
353       GNUNET_CLIENT_notify_transmit_ready (c->client,
354                                            ntohs (mq_entry->msg->size),
355                                            TIMEOUT_REL,
356                                            GNUNET_YES, &transmit_ready_notify,
357                                            c);
358     return 0;
359   }
360   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
361   size = ntohs (mq_entry->msg->size);  
362   memcpy (buf, mq_entry->msg, size);
363   LOG_DEBUG ("Message of type: %u and size: %u sent\n",
364              ntohs (mq_entry->msg->type), size);
365   GNUNET_free (mq_entry->msg);
366   GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
367   GNUNET_free (mq_entry);
368   mq_entry = c->mq_head;
369   if (NULL != mq_entry)
370     c->th = 
371       GNUNET_CLIENT_notify_transmit_ready (c->client,
372                                            ntohs (mq_entry->msg->size),
373                                            TIMEOUT_REL,
374                                            GNUNET_YES, &transmit_ready_notify,
375                                            c);
376   if (GNUNET_NO == c->in_receive)
377   {
378     c->in_receive = GNUNET_YES;
379     GNUNET_CLIENT_receive (c->client, &message_handler, c,
380                            GNUNET_TIME_UNIT_FOREVER_REL);
381   }
382   return size;
383 }
384
385
386 /**
387  * Queues a message in send queue for sending to the service
388  *
389  * @param controller the handle to the controller
390  * @param msg the message to queue
391  */
392 static void
393 queue_message (struct GNUNET_TESTBED_Controller *controller,
394                struct GNUNET_MessageHeader *msg)
395 {
396   struct MessageQueue *mq_entry;
397   uint16_t type;
398   uint16_t size;
399
400   type = ntohs (msg->type);
401   size = ntohs (msg->size);
402   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
403                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));                 
404   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
405   mq_entry->msg = msg;
406   LOG (GNUNET_ERROR_TYPE_DEBUG,
407        "Queueing message of type %u, size %u for sending\n", type,
408        ntohs (msg->size));
409   GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
410                                     mq_entry);
411   if (NULL == controller->th)
412     controller->th = 
413       GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
414                                            TIMEOUT_REL,
415                                            GNUNET_YES, &transmit_ready_notify,
416                                            controller);
417 }
418
419
420 /**
421  * Handle for controller process
422  */
423 struct GNUNET_TESTBED_ControllerProc
424 {
425   /**
426    * The helper handle
427    */
428   struct GNUNET_TESTBED_HelperHandle *helper;
429
430 };
431
432
433 /**
434  * Starts a controller process at the host
435  *
436  * @param host the host where the controller has to be started; NULL for localhost
437  * @return the controller process handle
438  */
439 struct GNUNET_TESTBED_ControllerProc *
440 GNUNET_TESTBED_controller_start (struct GNUNET_TESTBED_Host *host)
441 {
442   struct GNUNET_TESTBED_ControllerProc *cproc;
443   char * const binary_argv[] = {
444     "gnunet-service-testbed",
445     "gnunet-service-testbed",
446     NULL
447   };
448
449   cproc = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
450   cproc->helper = GNUNET_TESTBED_host_run_ (host, binary_argv);
451   if (NULL == cproc->helper)
452   {
453     GNUNET_free (cproc);
454     return NULL;
455   }
456   return cproc;
457 }
458
459
460 /**
461  * Stop the controller process (also will terminate all peers and controllers
462  * dependent on this controller).  This function blocks until the testbed has
463  * been fully terminated (!).
464  *
465  * @param cproc the controller process handle
466  */
467 void
468 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
469 {
470   GNUNET_TESTBED_host_stop_ (cproc->helper);
471   GNUNET_free (cproc);
472 }
473
474
475 /**
476  * Start a controller process using the given configuration at the
477  * given host.
478  *
479  * @param cfg configuration to use
480  * @param host host to run the controller on; This should be the same host if
481  *          the controller was previously started with
482  *          GNUNET_TESTBED_controller_start; NULL for localhost
483  * @param event_mask bit mask with set of events to call 'cc' for;
484  *                   or-ed values of "1LL" shifted by the
485  *                   respective 'enum GNUNET_TESTBED_EventType'
486  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
487  * @param cc controller callback to invoke on events
488  * @param cc_cls closure for cc
489  * @return handle to the controller
490  */
491 struct GNUNET_TESTBED_Controller *
492 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
493                                    struct GNUNET_TESTBED_Host *host,
494                                    uint64_t event_mask,
495                                    GNUNET_TESTBED_ControllerCallback cc,
496                                    void *cc_cls)
497 {
498   struct GNUNET_TESTBED_Controller *controller;
499   struct GNUNET_TESTBED_InitMessage *msg;
500
501   controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
502   controller->cc = cc;
503   controller->cc_cls = cc_cls;
504   controller->event_mask = event_mask;
505   controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
506   controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);  
507   if (NULL == controller->client)
508   {
509     GNUNET_TESTBED_controller_disconnect (controller);
510     return NULL;
511   }
512   if (NULL == host)
513   {
514     host = GNUNET_TESTBED_host_create_by_id_ (0);
515     if (NULL == host)
516     {
517       LOG (GNUNET_ERROR_TYPE_WARNING,
518            "Treating NULL host as localhost. Multiple references to localhost. "
519            " May break when localhost freed before calling disconnect \n");
520       host = GNUNET_TESTBED_host_lookup_by_id_ (0);
521     }
522     else
523     {
524       controller->aux_host = GNUNET_YES;
525     }
526   }
527   GNUNET_assert (NULL != host);
528   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
529   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
530   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
531   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
532   msg->event_mask = GNUNET_htonll (controller->event_mask);
533   queue_message (controller, (struct GNUNET_MessageHeader *) msg);
534   return controller;
535 }
536
537
538 /**
539  * Configure shared services at a controller.  Using this function,
540  * you can specify that certain services (such as "resolver")
541  * should not be run for each peer but instead be shared
542  * across N peers on the specified host.  This function
543  * must be called before any peers are created at the host.
544  * 
545  * @param controller controller to configure
546  * @param service_name name of the service to share
547  * @param num_peers number of peers that should share one instance
548  *        of the specified service (1 for no sharing is the default),
549  *        use 0 to disable the service
550  */
551 void
552 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
553                                              const char *service_name,
554                                              uint32_t num_peers)
555 {
556   struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
557   uint16_t service_name_size;
558   uint16_t msg_size;
559   
560   service_name_size = strlen (service_name) + 1;
561   msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
562     + service_name_size;
563   msg = GNUNET_malloc (msg_size);
564   msg->header.size = htons (msg_size);
565   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
566   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
567   msg->num_peers = htonl (num_peers);
568   memcpy (&msg[1], service_name, service_name_size);
569   queue_message (controller, (struct GNUNET_MessageHeader *) msg);
570 }
571
572
573 /**
574  * disconnects from the controller.
575  *
576  * @param controller handle to controller to stop
577  */
578 void
579 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
580 {
581   struct MessageQueue *mq_entry;
582
583   if (NULL != controller->th)
584     GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
585  /* Clear the message queue */
586   while (NULL != (mq_entry = controller->mq_head))
587   {
588     GNUNET_CONTAINER_DLL_remove (controller->mq_head,
589                                  controller->mq_tail,
590                                  mq_entry);
591     GNUNET_free (mq_entry->msg);
592     GNUNET_free (mq_entry);
593   }
594   if (NULL != controller->client)
595     GNUNET_CLIENT_disconnect (controller->client);
596   GNUNET_CONFIGURATION_destroy (controller->cfg);
597   if (GNUNET_YES == controller->aux_host)
598     GNUNET_TESTBED_host_destroy (controller->host);
599   GNUNET_free (controller);
600 }
601
602
603 /**
604  * Register a host with the controller
605  *
606  * @param controller the controller handle
607  * @param host the host to register
608  * @param cc the completion callback to call to inform the status of
609  *          registration. After calling this callback the registration handle
610  *          will be invalid. Cannot be NULL.
611  * @param cc_cls the closure for the cc
612  * @return handle to the host registration which can be used to cancel the
613  *           registration 
614  */
615 struct GNUNET_TESTBED_HostRegistrationHandle *
616 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
617                               struct GNUNET_TESTBED_Host *host,
618                               GNUNET_TESTBED_HostRegistrationCompletion cc,
619                               void *cc_cls)
620 {
621   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
622   struct GNUNET_TESTBED_AddHostMessage *msg;
623   const char *username;
624   const char *hostname;
625   uint16_t msg_size;
626   uint16_t user_name_length;
627
628   if (NULL != controller->rh)
629     return NULL;
630   hostname = GNUNET_TESTBED_host_get_hostname_ (host);
631   if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
632   {
633     LOG (GNUNET_ERROR_TYPE_WARNING,
634          "Host hostname: %s already registered\n",
635          (NULL == hostname) ? "localhost" : hostname);
636     return NULL;
637   }  
638   rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
639   rh->host = host;
640   rh->c = controller;
641   GNUNET_assert (NULL != cc);
642   rh->cc = cc;
643   rh->cc_cls = cc_cls;
644   controller->rh = rh;
645   username = GNUNET_TESTBED_host_get_username_ (host);
646   msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
647   user_name_length = 0;
648   if (NULL != username)
649   {
650     user_name_length = strlen (username) + 1;
651     msg_size += user_name_length;
652   }
653   /* FIXME: what happens when hostname is NULL? localhost */
654   GNUNET_assert (NULL != hostname);
655   msg_size += strlen (hostname) + 1;
656   msg = GNUNET_malloc (msg_size);
657   msg->header.size = htons (msg_size);
658   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
659   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
660   msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
661   msg->user_name_length = htons (user_name_length);
662   if (NULL != username)
663     memcpy (&msg[1], username, user_name_length);
664   strcpy (((void *) &msg[1]) + user_name_length, hostname);
665   queue_message (controller, (struct GNUNET_MessageHeader *) msg);
666   return rh;
667 }
668
669
670 /**
671  * Cancel the pending registration. Note that if the registration message is
672  * already sent to the service the cancellation has only the effect that the
673  * registration completion callback for the registration is never called.
674  *
675  * @param handle the registration handle to cancel
676  */
677 void
678 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
679                                     *handle)
680 {
681   if (handle != handle->c->rh)
682   {
683     GNUNET_break (0);
684     return;
685   }
686   handle->c->rh = NULL;
687   GNUNET_free (handle);  
688 }
689
690
691 /**
692  * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
693  * serialized and compressed
694  *
695  * @param master handle to the master controller who creates the association
696  * @param delegated_host requests to which host should be delegated; cannot be NULL
697  * @param slave_host which host is used to run the slave controller; use NULL to
698  *          make the master controller connect to the delegated host
699  * @param sxcfg serialized and compressed configuration
700  * @param sxcfg_size the size scfg
701  * @param scfg_size the size of uncompressed serialized configuration
702  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
703  *          be started by the master controller; GNUNET_NO if we are just
704  *          allowed to use the slave via TCP/IP
705  */
706 void
707 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
708                                   struct GNUNET_TESTBED_Host *delegated_host,
709                                   struct GNUNET_TESTBED_Host *slave_host,
710                                   const char *sxcfg,
711                                   size_t sxcfg_size,
712                                   size_t scfg_size,
713                                   int is_subordinate)
714 {
715   struct GNUNET_TESTBED_ControllerLinkMessage *msg;
716   uint16_t msg_size;
717
718   GNUNET_assert (GNUNET_YES == 
719                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
720   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
721     GNUNET_assert (GNUNET_YES == 
722                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
723   msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
724   msg = GNUNET_malloc (msg_size);
725   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);  
726   msg->header.size = htons (msg_size);
727   msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
728   msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_ 
729                               ((NULL != slave_host) ? slave_host : master->host));
730   msg->config_size = htons ((uint16_t) scfg_size);
731   msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
732   memcpy (&msg[1], sxcfg, sxcfg_size);
733   queue_message (master, (struct GNUNET_MessageHeader *) msg);
734 }
735
736
737 /**
738  * Create a link from slave controller to delegated controller. Whenever the
739  * master controller is asked to start a peer at the delegated controller the
740  * request will be routed towards slave controller (if a route exists). The
741  * slave controller will then route it to the delegated controller. The
742  * configuration of the slave controller is given and to be used to either
743  * create the slave controller or to connect to an existing slave controller
744  * process.  'is_subordinate' specifies if the given slave controller should be
745  * started and managed by the master controller, or if the slave already has a
746  * master and this is just a secondary master that is also allowed to use the
747  * existing slave.
748  *
749  * @param master handle to the master controller who creates the association
750  * @param delegated_host requests to which host should be delegated
751  * @param slave_host which host is used to run the slave controller 
752  * @param slave_cfg configuration to use for the slave controller
753  * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
754  *                       by the master controller; GNUNET_NO if we are just
755  *                       allowed to use the slave via TCP/IP
756  */
757 void
758 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
759                                 struct GNUNET_TESTBED_Host *delegated_host,
760                                 struct GNUNET_TESTBED_Host *slave_host,
761                                 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
762                                 int is_subordinate)
763 {
764   char *config;
765   Bytef *cconfig;
766   uLongf cc_size;
767   size_t config_size;  
768   
769   GNUNET_assert (GNUNET_YES == 
770                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
771   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
772     GNUNET_assert (GNUNET_YES == 
773                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
774   config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
775   cc_size = compressBound ((uLong) config_size);
776   cconfig = GNUNET_malloc (cc_size);
777   GNUNET_assert (Z_OK ==
778                  compress2 (cconfig, &cc_size, 
779                             (Bytef *) config, config_size, Z_BEST_SPEED));
780   GNUNET_free (config);
781   GNUNET_assert ((UINT16_MAX -
782                   sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
783                   >= cc_size); /* Configuration doesn't fit in 1 message */
784   GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
785                                     (const char *) cconfig,
786                                     cc_size, config_size, is_subordinate);
787   GNUNET_free (cconfig);
788 }
789
790
791 /**
792  * Ask the testbed controller to write the current overlay topology to
793  * a file.  Naturally, the file will only contain a snapshot as the
794  * topology may evolve all the time.
795  *
796  * @param controller overlay controller to inspect
797  * @param filename name of the file the topology should
798  *        be written to.
799  */
800 void
801 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
802                                                const char *filename)
803 {
804   GNUNET_break (0);
805 }
806
807
808
809 /* end of testbed_api.c */