working version of peer connect
[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  * @author Sree Harsha Totakura
28  */
29
30
31 #include "platform.h"
32 #include "gnunet_testbed_service.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_constants.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_hello_lib.h"
37 #include <zlib.h>
38
39 #include "testbed.h"
40 #include "testbed_api.h"
41 #include "testbed_api_hosts.h"
42 #include "testbed_api_peers.h"
43 #include "testbed_api_operations.h"
44
45 /**
46  * Generic logging shorthand
47  */
48 #define LOG(kind, ...)                          \
49   GNUNET_log_from (kind, "testbed-api", __VA_ARGS__);
50
51 /**
52  * Debug logging
53  */
54 #define LOG_DEBUG(...)                          \
55   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
56
57 /**
58  * Relative time seconds shorthand
59  */
60 #define TIME_REL_SECS(sec) \
61   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
62
63
64 /**
65  * Default server message sending retry timeout
66  */
67 #define TIMEOUT_REL TIME_REL_SECS(1)
68
69
70 /**
71  * The message queue for sending messages to the controller service
72  */
73 struct MessageQueue
74 {
75   /**
76    * The message to be sent
77    */
78   struct GNUNET_MessageHeader *msg;
79
80   /**
81    * next pointer for DLL
82    */
83   struct MessageQueue *next;
84   
85   /**
86    * prev pointer for DLL
87    */
88   struct MessageQueue *prev;
89 };
90
91
92 /**
93  * Structure for a controller link
94  */
95 struct ControllerLink
96 {
97   /**
98    * The next ptr for DLL
99    */
100   struct ControllerLink *next;
101
102   /**
103    * The prev ptr for DLL
104    */
105   struct ControllerLink *prev;
106
107   /**
108    * The host which will be referred in the peer start request. This is the
109    * host where the peer should be started
110    */
111   struct GNUNET_TESTBED_Host *delegated_host;
112
113   /**
114    * The host which will contacted to delegate the peer start request
115    */
116   struct GNUNET_TESTBED_Host *slave_host;
117
118   /**
119    * The configuration to be used to connect to slave host
120    */
121   const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
122
123   /**
124    * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
125    * if we are just allowed to use the slave via TCP/IP
126    */
127   int is_subordinate;
128 };
129
130
131 /**
132  * handle for host registration
133  */
134 struct GNUNET_TESTBED_HostRegistrationHandle
135 {
136   /**
137    * The host being registered
138    */
139   struct GNUNET_TESTBED_Host *host;
140
141   /**
142    * The controller at which this host is being registered
143    */
144   struct GNUNET_TESTBED_Controller *c;
145
146   /**
147    * The Registartion completion callback
148    */
149   GNUNET_TESTBED_HostRegistrationCompletion cc;
150
151   /**
152    * The closure for above callback
153    */
154   void *cc_cls;
155 };
156
157
158 /**
159  * Returns the operation context with the given id if found in the Operation
160  * context queues of the controller
161  *
162  * @param c the controller whose queues are searched
163  * @param id the id which has to be checked
164  * @return the matching operation context; NULL if no match found
165  */
166 static struct OperationContext *
167 find_opc (const struct GNUNET_TESTBED_Controller *c, const uint64_t id)
168 {
169   struct OperationContext *opc;
170
171   for (opc = c->ocq_head; NULL != opc; opc = opc->next)
172   {
173     if (id == opc->id)
174       return opc;
175   }
176   return NULL;
177 }
178
179
180 /**
181  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
182  * controller (testbed service)
183  *
184  * @param c the controller handler
185  * @param msg message received
186  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
187  *           not
188  */
189 static int
190 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
191                        const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
192 {
193   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
194   char *emsg;
195   uint16_t msg_size;
196
197   rh = c->rh;
198   if (NULL == rh)
199   {  
200     return GNUNET_OK;    
201   }
202   if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
203   {
204     LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
205                GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
206     return GNUNET_OK;
207   }
208   c->rh = NULL;
209   msg_size = ntohs (msg->header.size);
210   if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
211   {
212     LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
213     GNUNET_TESTBED_mark_host_registered_at_  (rh->host, c);
214     rh->cc (rh->cc_cls, NULL);
215     GNUNET_free (rh);
216     return GNUNET_OK;
217   } 
218   /* We have an error message */
219   emsg = (char *) &msg[1];
220   if ('\0' != emsg[msg_size - 
221                    sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
222   {
223     GNUNET_break (0);
224     GNUNET_free (rh);
225     return GNUNET_NO;
226   }  
227   LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
228        ntohl (msg->host_id), emsg);
229   rh->cc (rh->cc_cls, emsg);
230   GNUNET_free (rh);
231   return GNUNET_OK;
232 }
233
234
235 /**
236  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
237  * controller (testbed service)
238  *
239  * @param c the controller handler
240  * @param msg message received
241  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
242  *           not
243  */
244 static int
245 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
246                   const struct
247                   GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
248 {
249   struct OperationContext *opc;
250   struct GNUNET_TESTBED_EventInformation *event;
251   uint64_t op_id;
252   
253   op_id = GNUNET_ntohll (msg->operation_id);
254   LOG_DEBUG ("Operation %ul successful\n", op_id);
255   if (NULL == (opc = find_opc (c, op_id)))
256   {
257     LOG_DEBUG ("Operation not found\n");
258     return GNUNET_YES;
259   }
260   event = NULL;
261   if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
262     event = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_EventInformation));
263   if (NULL != event)
264     event->type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
265   switch (opc->type)
266   {
267   case OP_PEER_DESTROY:
268     {
269       struct GNUNET_TESTBED_Peer *peer;
270       
271       if (NULL != event)
272       {
273         event->details.operation_finished.operation = opc->op;
274         event->details.operation_finished.op_cls = NULL;
275         event->details.operation_finished.emsg = NULL;
276         event->details.operation_finished.pit = GNUNET_TESTBED_PIT_GENERIC;
277         event->details.operation_finished.op_result.generic = NULL;
278       }
279       peer = opc->data;
280       GNUNET_free (peer);
281       opc->data = NULL;
282       //PEERDESTROYDATA
283     }
284     break;
285   default:
286     GNUNET_assert (0);
287   }
288   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
289   opc->state = OPC_STATE_FINISHED;
290   if (NULL != event)
291   {
292     if (NULL != c->cc)
293       c->cc (c->cc_cls, event);
294     GNUNET_free (event);
295   }  
296   return GNUNET_YES;  
297 }
298
299
300 /**
301  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
302  * controller (testbed service)
303  *
304  * @param c the controller handler
305  * @param msg message received
306  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
307  *           not
308  */
309 static int
310 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
311                             const struct
312                             GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
313 {
314   struct OperationContext *opc;
315   struct PeerCreateData *data;
316   struct GNUNET_TESTBED_Peer *peer;
317   GNUNET_TESTBED_PeerCreateCallback cb;
318   void *cls;
319   uint64_t op_id;
320
321   GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage)
322                  == ntohs (msg->header.size));
323   op_id = GNUNET_ntohll (msg->operation_id);
324   if (NULL == (opc = find_opc (c, op_id)))
325   {
326     LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
327     return GNUNET_YES;
328   }
329   GNUNET_assert (OP_PEER_CREATE == opc->type);
330   GNUNET_assert (NULL != opc->data);
331   data = opc->data;
332   GNUNET_assert (NULL != data->peer);
333   peer = data->peer;
334   GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
335   peer->state = PS_CREATED;
336   cb = data->cb;
337   cls = data->cls;
338   GNUNET_free (opc->data);  
339   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
340   opc->state = OPC_STATE_FINISHED;
341   if (NULL != cb)
342     cb (cls, peer, NULL);
343   return GNUNET_YES;
344 }
345
346
347 /**
348  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
349  * controller (testbed service)
350  *
351  * @param c the controller handler
352  * @param msg message received
353  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
354  *           not
355  */
356 static int
357 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
358                    const struct GNUNET_TESTBED_PeerEventMessage *msg)
359 {
360   struct OperationContext *opc;
361   struct GNUNET_TESTBED_Peer *peer;
362   struct GNUNET_TESTBED_EventInformation event;
363   uint64_t op_id;
364
365   GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage)
366                  == ntohs (msg->header.size));
367   op_id = GNUNET_ntohll (msg->operation_id);
368   if (NULL == (opc = find_opc (c, op_id)))
369   {
370     LOG_DEBUG ("Operation not found\n");
371     return GNUNET_YES;
372   }
373   GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
374   peer = opc->data;
375   GNUNET_assert (NULL != peer);
376   event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
377   switch (event.type)
378   {
379   case GNUNET_TESTBED_ET_PEER_START:
380     peer->state = PS_STARTED;
381     event.details.peer_start.host = peer->host;
382     event.details.peer_start.peer = peer;
383     break;
384   case GNUNET_TESTBED_ET_PEER_STOP:
385     peer->state = PS_STOPPED;    
386     event.details.peer_stop.peer = peer;  
387     break;
388   default:
389     GNUNET_assert (0);          /* We should never reach this state */
390   }
391   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
392   opc->state = OPC_STATE_FINISHED;
393   if (0 != ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP)
394             & c->event_mask))
395   {
396     if (NULL != c->cc)
397       c->cc (c->cc_cls, &event);
398   }    
399   return GNUNET_YES;
400 }
401
402
403 /**
404  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT message from
405  * controller (testbed service)
406  *
407  * @param c the controller handler
408  * @param msg message received
409  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
410  *           not
411  */
412 static int
413 handle_peer_conevent (struct GNUNET_TESTBED_Controller *c,
414                       const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
415 {
416   struct OperationContext *opc;
417   struct OverlayConnectData *data;
418   struct GNUNET_TESTBED_EventInformation event;
419   uint64_t op_id;
420
421   op_id = GNUNET_ntohll (msg->operation_id);
422   if (NULL == (opc = find_opc (c, op_id)))
423   {
424     LOG_DEBUG ("Operation not found\n");
425     return GNUNET_YES;
426   }
427   data = opc->data;
428   GNUNET_assert (NULL != data);
429   GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id)
430                   && (ntohl (msg->peer2) == data->p2->unique_id));
431   event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
432   switch (event.type)
433   {
434   case GNUNET_TESTBED_ET_CONNECT:
435     event.details.peer_connect.peer1 = data->p1;
436     event.details.peer_connect.peer2 = data->p2;
437     break;
438   case GNUNET_TESTBED_ET_DISCONNECT:
439     GNUNET_assert (0);          /* FIXME: implement */
440     break;
441   default:
442     GNUNET_assert (0);          /* Should never reach here */
443     break;
444   }
445   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
446   opc->state = OPC_STATE_FINISHED;
447   GNUNET_free (data);
448   if (0 != ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT)
449             & c->event_mask))
450   {
451     if (NULL != c->cc)
452       c->cc (c->cc_cls, &event);
453   }
454   return GNUNET_YES;
455 }
456
457
458 /**
459  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
460  * controller (testbed service)
461  *
462  * @param c the controller handler
463  * @param msg message received
464  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
465  *           not
466  */
467 static int
468 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
469                     const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
470 {
471   struct OperationContext *opc;
472   struct GNUNET_TESTBED_Peer *peer;
473   struct PeerInfoData *data;
474   struct PeerInfoData2 *response_data;
475   struct GNUNET_TESTBED_EventInformation info;
476   uint64_t op_id;
477   
478   op_id = GNUNET_ntohll (msg->operation_id);
479   if (NULL == (opc = find_opc (c, op_id)))
480   {
481     LOG_DEBUG ("Operation not found\n");
482     return GNUNET_YES;
483   }
484   data = opc->data;
485   GNUNET_assert (NULL != data);
486   peer = data->peer;
487   GNUNET_assert (NULL != peer);
488   GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
489   if (0 == (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
490   {
491     LOG_DEBUG ("Skipping operation callback as flag not set\n");
492     return GNUNET_YES;
493   }
494   response_data = GNUNET_malloc (sizeof (struct PeerInfoData2));
495   response_data->pit = data->pit;
496   GNUNET_free (data);
497   opc->data = NULL;
498   info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
499   info.details.operation_finished.operation = opc->op;
500   info.details.operation_finished.op_cls = NULL;
501   info.details.operation_finished.emsg = NULL;
502   info.details.operation_finished.pit = response_data->pit;
503   switch (response_data->pit)
504   {
505   case GNUNET_TESTBED_PIT_IDENTITY:
506     {
507       struct GNUNET_PeerIdentity *peer_identity;
508
509       peer_identity = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
510       (void) memcpy (peer_identity, &msg->peer_identity, 
511                      sizeof (struct GNUNET_PeerIdentity));
512       response_data->details.peer_identity = peer_identity;      
513       info.details.operation_finished.op_result.pid = peer_identity;
514     }
515     break;
516   case GNUNET_TESTBED_PIT_CONFIGURATION:
517     {
518       struct GNUNET_CONFIGURATION_Handle *cfg;
519       char *config;
520       uLong config_size;
521       int ret;
522       uint16_t msize;
523       
524       config_size = (uLong) ntohs (msg->config_size);
525       config = GNUNET_malloc (config_size);
526       msize = ntohs (msg->header.size);
527       msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
528       if (Z_OK != (ret = uncompress ((Bytef *) config, &config_size,
529                                      (const Bytef *) &msg[1], (uLong) msize)))
530         GNUNET_assert (0);
531       cfg = GNUNET_CONFIGURATION_create (); /* Freed in oprelease_peer_getinfo */
532       GNUNET_assert (GNUNET_OK == 
533                      GNUNET_CONFIGURATION_deserialize (cfg, config,
534                                                        (size_t) config_size,
535                                                        GNUNET_NO));
536       GNUNET_free (config);
537       response_data->details.cfg = cfg;
538       info.details.operation_finished.op_result.cfg = cfg;
539     }
540     break;
541   case GNUNET_TESTBED_PIT_GENERIC:
542     GNUNET_assert (0);          /* never reach here */
543     break;
544   }
545   opc->data = response_data;
546   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
547   opc->state = OPC_STATE_FINISHED;
548   c->cc (c->cc_cls, &info);  
549   return GNUNET_YES;
550 }
551
552
553 /**
554  * Handler for messages from controller (testbed service)
555  *
556  * @param cls the controller handler
557  * @param msg message received, NULL on timeout or fatal error
558  */
559 static void 
560 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
561 {
562   struct GNUNET_TESTBED_Controller *c = cls;
563   int status;
564   uint16_t msize;
565
566   c->in_receive = GNUNET_NO;
567   /* FIXME: Add checks for message integrity */
568   if (NULL == msg)
569   {
570     LOG_DEBUG ("Receive timed out or connection to service dropped\n");
571     return;
572   }
573   status = GNUNET_OK;
574   msize = ntohs (msg->size);
575   switch (ntohs (msg->type))
576   {
577   case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
578     GNUNET_assert (msize >= sizeof (struct
579                                     GNUNET_TESTBED_HostConfirmedMessage));
580     status =
581       handle_addhostconfirm (c, (const struct GNUNET_TESTBED_HostConfirmedMessage *) msg);
582     break;
583   case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
584     GNUNET_assert 
585       (msize == sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage));
586     status =
587       handle_opsuccess (c, (const struct
588                             GNUNET_TESTBED_GenericOperationSuccessEventMessage
589                             *) msg);
590     break;
591   case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
592     GNUNET_assert (msize == 
593                    sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
594     status =
595       handle_peer_create_success 
596       (c, (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *)msg);
597     break;
598   case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
599     GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
600     status =
601       handle_peer_event (c, (const struct GNUNET_TESTBED_PeerEventMessage *) msg);
602     
603     break;
604   case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
605     GNUNET_assert (msize >= 
606                    sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage));
607     status = 
608       handle_peer_config 
609       (c, (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
610   msg);
611     break;
612   case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT:
613     GNUNET_assert (msize ==
614                    sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
615     status = 
616       handle_peer_conevent (c, (const struct
617                                 GNUNET_TESTBED_ConnectionEventMessage *) msg);
618   default:
619     GNUNET_break (0);
620   }
621   if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
622   {
623     c->in_receive = GNUNET_YES;
624     GNUNET_CLIENT_receive (c->client, &message_handler, c,
625                            GNUNET_TIME_UNIT_FOREVER_REL);    
626   }
627 }
628
629
630 /**
631  * Function called to notify a client about the connection begin ready to queue
632  * more data.  "buf" will be NULL and "size" zero if the connection was closed
633  * for writing in the meantime.
634  *
635  * @param cls closure
636  * @param size number of bytes available in buf
637  * @param buf where the callee should write the message
638  * @return number of bytes written to buf
639  */
640 static size_t
641 transmit_ready_notify (void *cls, size_t size, void *buf)
642 {
643   struct GNUNET_TESTBED_Controller *c = cls;
644   struct MessageQueue *mq_entry;
645
646   c->th = NULL;
647   mq_entry = c->mq_head;
648   GNUNET_assert (NULL != mq_entry);
649   if ((0 == size) && (NULL == buf)) /* Timeout */
650   {
651     LOG_DEBUG ("Message sending timed out -- retrying\n");
652     c->th =
653       GNUNET_CLIENT_notify_transmit_ready (c->client,
654                                            ntohs (mq_entry->msg->size),
655                                            TIMEOUT_REL,
656                                            GNUNET_YES, &transmit_ready_notify,
657                                            c);
658     return 0;
659   }
660   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
661   size = ntohs (mq_entry->msg->size);  
662   memcpy (buf, mq_entry->msg, size);
663   LOG_DEBUG ("Message of type: %u and size: %u sent\n",
664              ntohs (mq_entry->msg->type), size);
665   GNUNET_free (mq_entry->msg);
666   GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
667   GNUNET_free (mq_entry);
668   mq_entry = c->mq_head;
669   if (NULL != mq_entry)
670     c->th = 
671       GNUNET_CLIENT_notify_transmit_ready (c->client,
672                                            ntohs (mq_entry->msg->size),
673                                            TIMEOUT_REL,
674                                            GNUNET_YES, &transmit_ready_notify,
675                                            c);
676   if (GNUNET_NO == c->in_receive)
677   {
678     c->in_receive = GNUNET_YES;
679     GNUNET_CLIENT_receive (c->client, &message_handler, c,
680                            GNUNET_TIME_UNIT_FOREVER_REL);
681   }
682   return size;
683 }
684
685
686 /**
687  * Queues a message in send queue for sending to the service
688  *
689  * @param controller the handle to the controller
690  * @param msg the message to queue
691  */
692 void
693 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
694                                struct GNUNET_MessageHeader *msg)
695 {
696   struct MessageQueue *mq_entry;
697   uint16_t type;
698   uint16_t size;
699
700   type = ntohs (msg->type);
701   size = ntohs (msg->size);
702   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
703                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));                 
704   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
705   mq_entry->msg = msg;
706   LOG (GNUNET_ERROR_TYPE_DEBUG,
707        "Queueing message of type %u, size %u for sending\n", type,
708        ntohs (msg->size));
709   GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
710                                     mq_entry);
711   if (NULL == controller->th)
712     controller->th = 
713       GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
714                                            TIMEOUT_REL,
715                                            GNUNET_YES, &transmit_ready_notify,
716                                            controller);
717 }
718
719
720 /**
721  * Handle for controller process
722  */
723 struct GNUNET_TESTBED_ControllerProc
724 {
725   /**
726    * The process handle
727    */
728   struct GNUNET_HELPER_Handle *helper;
729
730   /**
731    * The host where the helper is run
732    */
733   struct GNUNET_TESTBED_Host *host;
734
735   /**
736    * The controller error callback
737    */
738   GNUNET_TESTBED_ControllerStatusCallback cb;
739
740   /**
741    * The closure for the above callback
742    */
743   void *cls;
744
745   /**
746    * The send handle for the helper
747    */
748   struct GNUNET_HELPER_SendHandle *shandle;
749
750   /**
751    * The message corresponding to send handle
752    */
753   struct GNUNET_MessageHeader *msg;
754
755   /**
756    * The port number for ssh; used for helpers starting ssh
757    */
758   char *port;
759
760   /**
761    * The ssh destination string; used for helpers starting ssh
762    */
763   char *dst;
764
765   /**
766    * The configuration of the running testbed service
767    */
768   struct GNUNET_CONFIGURATION_Handle *cfg;
769
770 };
771
772
773 /**
774  * Functions with this signature are called whenever a
775  * complete message is received by the tokenizer.
776  *
777  * Do not call GNUNET_SERVER_mst_destroy in callback
778  *
779  * @param cls closure
780  * @param client identification of the client
781  * @param message the actual message
782  *
783  * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
784  */
785 static int helper_mst (void *cls, void *client,
786                        const struct GNUNET_MessageHeader *message)
787 {
788   struct GNUNET_TESTBED_ControllerProc *cp = cls;
789   const struct GNUNET_TESTBED_HelperReply *msg;
790   const char *hostname;
791   char *config;
792   uLongf config_size;
793   uLongf xconfig_size;
794     
795   msg = (const struct GNUNET_TESTBED_HelperReply *) message;
796   GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) 
797                  < ntohs (msg->header.size));
798   GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY 
799                  == ntohs (msg->header.type));
800   config_size = (uLongf) ntohs (msg->config_size);
801   xconfig_size = (uLongf) (ntohs (msg->header.size)
802                            - sizeof (struct GNUNET_TESTBED_HelperReply));
803   config = GNUNET_malloc (config_size);
804   GNUNET_assert (Z_OK == uncompress ((Bytef *) config, &config_size,
805                                      (const Bytef *) &msg[1], xconfig_size));
806   GNUNET_assert (NULL == cp->cfg);
807   cp->cfg = GNUNET_CONFIGURATION_create ();
808   GNUNET_assert (GNUNET_CONFIGURATION_deserialize (cp->cfg, config, 
809                                                    config_size, GNUNET_NO));
810   GNUNET_free (config);
811   if ((NULL == cp->host) || 
812       (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
813     hostname = "localhost";
814   /* Change the hostname so that we can connect to it */
815   GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname", 
816                                          hostname);
817   cp->cb (cp->cls, cp->cfg, GNUNET_OK);
818   return GNUNET_OK;
819 }
820
821
822 /**
823  * Continuation function from GNUNET_HELPER_send()
824  * 
825  * @param cls closure
826  * @param result GNUNET_OK on success,
827  *               GNUNET_NO if helper process died
828  *               GNUNET_SYSERR during GNUNET_HELPER_stop
829  */
830 static void 
831 clear_msg (void *cls, int result)
832 {
833   struct GNUNET_TESTBED_ControllerProc *cp = cls;
834   
835   GNUNET_assert (NULL != cp->shandle);
836   cp->shandle = NULL;
837   GNUNET_free (cp->msg);
838 }
839
840
841 /**
842  * Callback that will be called when the helper process dies. This is not called
843  * when the helper process is stoped using GNUNET_HELPER_stop()
844  *
845  * @param cls the closure from GNUNET_HELPER_start()
846  */
847 static void 
848 helper_exp_cb (void *cls)
849 {
850   struct GNUNET_TESTBED_ControllerProc *cp = cls;
851   GNUNET_TESTBED_ControllerStatusCallback cb;
852   void *cb_cls;
853
854   cb = cp->cb;
855   cb_cls = cp->cls;
856   GNUNET_TESTBED_controller_stop (cp);
857   if (NULL != cb)
858     cb (cb_cls, NULL, GNUNET_SYSERR);
859 }
860
861
862 /**
863  * Starts a controller process at the host. FIXME: add controller start callback
864  * with the configuration with which the controller is started
865  *
866  * @param controller_ip the ip address of the controller. Will be set as TRUSTED
867  *          host when starting testbed controller at host
868  * @param host the host where the controller has to be started; NULL for
869  *          localhost
870  * @param cfg template configuration to use for the remote controller; the
871  *          remote controller will be started with a slightly modified
872  *          configuration (port numbers, unix domain sockets and service home
873  *          values are changed as per TESTING library on the remote host)
874  * @param cb function called when the controller is successfully started or
875  *          dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
876  *          called if cb is called with GNUNET_SYSERR as status. Will never be
877  *          called in the same task as 'GNUNET_TESTBED_controller_start'
878  *          (synchronous errors will be signalled by returning NULL). This
879  *          parameter cannot be NULL.
880  * @param cls closure for above callbacks
881  * @return the controller process handle, NULL on errors
882  */
883 struct GNUNET_TESTBED_ControllerProc *
884 GNUNET_TESTBED_controller_start (const char *controller_ip,
885                                  struct GNUNET_TESTBED_Host *host,
886                                  const struct GNUNET_CONFIGURATION_Handle *cfg,
887                                  GNUNET_TESTBED_ControllerStatusCallback cb,
888                                  void *cls)
889 {
890   struct GNUNET_TESTBED_ControllerProc *cp;
891   struct GNUNET_TESTBED_HelperInit *msg;
892   
893   cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
894   if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
895   {
896     char * const binary_argv[] = {
897       "gnunet-testbed-helper", NULL
898     };
899
900     cp->helper = GNUNET_HELPER_start ("gnunet-testbed-helper", binary_argv, 
901                                       &helper_mst, &helper_exp_cb, cp);
902   }
903   else
904   {
905     char *remote_args[6 + 1];
906     unsigned int argp;
907     const char *username;
908     const char *hostname;
909
910     username = GNUNET_TESTBED_host_get_username_ (host);
911     hostname = GNUNET_TESTBED_host_get_hostname_ (host);
912     GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
913     if (NULL == username)
914       GNUNET_asprintf (&cp->dst, "%s", hostname);
915     else 
916       GNUNET_asprintf (&cp->dst, "%s@%s", hostname, username);
917     argp = 0;
918     remote_args[argp++] = "ssh";
919     remote_args[argp++] = "-p";
920     remote_args[argp++] = cp->port;
921     remote_args[argp++] = "-q";
922     remote_args[argp++] = cp->dst;
923     remote_args[argp++] = "gnunet-testbed-helper";
924     remote_args[argp++] = NULL;
925     GNUNET_assert (argp == 6 + 1);
926     cp->helper = GNUNET_HELPER_start ("ssh", remote_args,
927                                       &helper_mst, &helper_exp_cb, cp);
928   }
929   if (NULL == cp->helper)
930   {
931     GNUNET_free_non_null (cp->port);
932     GNUNET_free_non_null (cp->dst);
933     GNUNET_free (cp);
934     return NULL;
935   }
936   cp->host = host;
937   cp->cb = cb;
938   cp->cls = cls;
939   msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
940   cp->msg = &msg->header;
941   cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO,
942                                     &clear_msg, cp);
943   if (NULL == cp->shandle)
944   {
945     GNUNET_free (msg);
946     GNUNET_TESTBED_controller_stop (cp);
947     return NULL;
948   }
949   return cp;
950 }
951
952
953 /**
954  * Stop the controller process (also will terminate all peers and controllers
955  * dependent on this controller).  This function blocks until the testbed has
956  * been fully terminated (!).
957  *
958  * @param cproc the controller process handle
959  */
960 void
961 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
962 {
963   if (NULL != cproc->shandle)
964     GNUNET_HELPER_send_cancel (cproc->shandle);
965   GNUNET_HELPER_stop (cproc->helper);
966   if (NULL != cproc->cfg)
967     GNUNET_CONFIGURATION_destroy (cproc->cfg);
968   GNUNET_free_non_null (cproc->port);
969   GNUNET_free_non_null (cproc->dst);
970   GNUNET_free (cproc);
971 }
972
973
974 /**
975  * Start a controller process using the given configuration at the
976  * given host.
977  *
978  * @param cfg configuration to use
979  * @param host host to run the controller on; This should be the same host if
980  *          the controller was previously started with
981  *          GNUNET_TESTBED_controller_start; NULL for localhost
982  * @param event_mask bit mask with set of events to call 'cc' for;
983  *                   or-ed values of "1LL" shifted by the
984  *                   respective 'enum GNUNET_TESTBED_EventType'
985  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
986  * @param cc controller callback to invoke on events
987  * @param cc_cls closure for cc
988  * @return handle to the controller
989  */
990 struct GNUNET_TESTBED_Controller *
991 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
992                                    struct GNUNET_TESTBED_Host *host,
993                                    uint64_t event_mask,
994                                    GNUNET_TESTBED_ControllerCallback cc,
995                                    void *cc_cls)
996 {
997   struct GNUNET_TESTBED_Controller *controller;
998   struct GNUNET_TESTBED_InitMessage *msg;
999   unsigned long long max_parallel_peer_create;
1000
1001   if (GNUNET_OK !=
1002       GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1003                                              "MAX_PARALLEL_PEER_CREATE",
1004                                              &max_parallel_peer_create))
1005   {
1006     GNUNET_break (0);
1007     return NULL;
1008   }                                                          
1009   controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1010   controller->cc = cc;
1011   controller->cc_cls = cc_cls;
1012   controller->event_mask = event_mask;
1013   controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1014   controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);  
1015   if (NULL == controller->client)
1016   {
1017     GNUNET_TESTBED_controller_disconnect (controller);
1018     return NULL;
1019   }
1020   if (NULL == host)
1021   {
1022     host = GNUNET_TESTBED_host_create_by_id_ (0);
1023     if (NULL == host)
1024     {
1025       LOG (GNUNET_ERROR_TYPE_WARNING,
1026            "Treating NULL host as localhost. Multiple references to localhost "
1027            "may break when localhost freed before calling disconnect \n");
1028       host = GNUNET_TESTBED_host_lookup_by_id_ (0);
1029     }
1030     else
1031     {
1032       controller->aux_host = GNUNET_YES;
1033     }
1034   }
1035   GNUNET_assert (NULL != host);
1036   controller->opq_peer_create =
1037     GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1038                                             max_parallel_peer_create);
1039   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
1040   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1041   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
1042   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1043   msg->event_mask = GNUNET_htonll (controller->event_mask);
1044   GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *)
1045                                  msg);
1046   
1047   return controller;
1048 }
1049
1050
1051 /**
1052  * Configure shared services at a controller.  Using this function,
1053  * you can specify that certain services (such as "resolver")
1054  * should not be run for each peer but instead be shared
1055  * across N peers on the specified host.  This function
1056  * must be called before any peers are created at the host.
1057  * 
1058  * @param controller controller to configure
1059  * @param service_name name of the service to share
1060  * @param num_peers number of peers that should share one instance
1061  *        of the specified service (1 for no sharing is the default),
1062  *        use 0 to disable the service
1063  */
1064 void
1065 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
1066                                              const char *service_name,
1067                                              uint32_t num_peers)
1068 {
1069   struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1070   uint16_t service_name_size;
1071   uint16_t msg_size;
1072   
1073   service_name_size = strlen (service_name) + 1;
1074   msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
1075     + service_name_size;
1076   msg = GNUNET_malloc (msg_size);
1077   msg->header.size = htons (msg_size);
1078   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1079   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1080   msg->num_peers = htonl (num_peers);
1081   memcpy (&msg[1], service_name, service_name_size);
1082   GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1083 }
1084
1085
1086 /**
1087  * disconnects from the controller.
1088  *
1089  * @param controller handle to controller to stop
1090  */
1091 void
1092 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
1093 {
1094   struct MessageQueue *mq_entry;
1095
1096   if (NULL != controller->th)
1097     GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1098  /* Clear the message queue */
1099   while (NULL != (mq_entry = controller->mq_head))
1100   {
1101     GNUNET_CONTAINER_DLL_remove (controller->mq_head,
1102                                  controller->mq_tail,
1103                                  mq_entry);
1104     GNUNET_free (mq_entry->msg);
1105     GNUNET_free (mq_entry);
1106   }
1107   if (NULL != controller->client)
1108     GNUNET_CLIENT_disconnect (controller->client);
1109   GNUNET_CONFIGURATION_destroy (controller->cfg);
1110   if (GNUNET_YES == controller->aux_host)
1111     GNUNET_TESTBED_host_destroy (controller->host);
1112   GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_peer_create);
1113   GNUNET_free (controller);
1114 }
1115
1116
1117 /**
1118  * Register a host with the controller
1119  *
1120  * @param controller the controller handle
1121  * @param host the host to register
1122  * @param cc the completion callback to call to inform the status of
1123  *          registration. After calling this callback the registration handle
1124  *          will be invalid. Cannot be NULL.
1125  * @param cc_cls the closure for the cc
1126  * @return handle to the host registration which can be used to cancel the
1127  *           registration 
1128  */
1129 struct GNUNET_TESTBED_HostRegistrationHandle *
1130 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1131                               struct GNUNET_TESTBED_Host *host,
1132                               GNUNET_TESTBED_HostRegistrationCompletion cc,
1133                               void *cc_cls)
1134 {
1135   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1136   struct GNUNET_TESTBED_AddHostMessage *msg;
1137   const char *username;
1138   const char *hostname;
1139   uint16_t msg_size;
1140   uint16_t user_name_length;
1141
1142   if (NULL != controller->rh)
1143     return NULL;
1144   hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1145   if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1146   {
1147     LOG (GNUNET_ERROR_TYPE_WARNING,
1148          "Host hostname: %s already registered\n",
1149          (NULL == hostname) ? "localhost" : hostname);
1150     return NULL;
1151   }  
1152   rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1153   rh->host = host;
1154   rh->c = controller;
1155   GNUNET_assert (NULL != cc);
1156   rh->cc = cc;
1157   rh->cc_cls = cc_cls;
1158   controller->rh = rh;
1159   username = GNUNET_TESTBED_host_get_username_ (host);
1160   msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1161   user_name_length = 0;
1162   if (NULL != username)
1163   {
1164     user_name_length = strlen (username) + 1;
1165     msg_size += user_name_length;
1166   }
1167   /* FIXME: what happens when hostname is NULL? localhost */
1168   GNUNET_assert (NULL != hostname);
1169   msg_size += strlen (hostname) + 1;
1170   msg = GNUNET_malloc (msg_size);
1171   msg->header.size = htons (msg_size);
1172   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1173   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1174   msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1175   msg->user_name_length = htons (user_name_length);
1176   if (NULL != username)
1177     memcpy (&msg[1], username, user_name_length);
1178   strcpy (((void *) &msg[1]) + user_name_length, hostname);
1179   GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1180   return rh;
1181 }
1182
1183
1184 /**
1185  * Cancel the pending registration. Note that if the registration message is
1186  * already sent to the service the cancellation has only the effect that the
1187  * registration completion callback for the registration is never called.
1188  *
1189  * @param handle the registration handle to cancel
1190  */
1191 void
1192 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1193                                     *handle)
1194 {
1195   if (handle != handle->c->rh)
1196   {
1197     GNUNET_break (0);
1198     return;
1199   }
1200   handle->c->rh = NULL;
1201   GNUNET_free (handle);  
1202 }
1203
1204
1205 /**
1206  * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1207  * serialized and compressed
1208  *
1209  * @param master handle to the master controller who creates the association
1210  * @param delegated_host requests to which host should be delegated; cannot be NULL
1211  * @param slave_host which host is used to run the slave controller; use NULL to
1212  *          make the master controller connect to the delegated host
1213  * @param sxcfg serialized and compressed configuration
1214  * @param sxcfg_size the size scfg
1215  * @param scfg_size the size of uncompressed serialized configuration
1216  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1217  *          be started by the master controller; GNUNET_NO if we are just
1218  *          allowed to use the slave via TCP/IP
1219  */
1220 void
1221 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1222                                   struct GNUNET_TESTBED_Host *delegated_host,
1223                                   struct GNUNET_TESTBED_Host *slave_host,
1224                                   const char *sxcfg,
1225                                   size_t sxcfg_size,
1226                                   size_t scfg_size,
1227                                   int is_subordinate)
1228 {
1229   struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1230   uint16_t msg_size;
1231
1232   GNUNET_assert (GNUNET_YES == 
1233                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1234   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1235     GNUNET_assert (GNUNET_YES == 
1236                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1237   msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1238   msg = GNUNET_malloc (msg_size);
1239   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);  
1240   msg->header.size = htons (msg_size);
1241   msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1242   msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_ 
1243                               ((NULL != slave_host) ? slave_host : master->host));
1244   msg->config_size = htons ((uint16_t) scfg_size);
1245   msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1246   memcpy (&msg[1], sxcfg, sxcfg_size);
1247   GNUNET_TESTBED_queue_message_ (master, (struct GNUNET_MessageHeader *) msg);
1248 }
1249
1250
1251 /**
1252  * Compresses given configuration using zlib compress
1253  *
1254  * @param config the serialized configuration
1255  * @param size the size of config
1256  * @param xconfig will be set to the compressed configuration (memory is fresly
1257  *          allocated) 
1258  * @return the size of the xconfig
1259  */
1260 size_t
1261 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1262                                  char **xconfig)
1263 {
1264   size_t xsize;
1265   
1266   xsize = compressBound ((uLong) size);
1267   *xconfig = GNUNET_malloc (xsize);
1268   GNUNET_assert (Z_OK ==
1269                  compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
1270                             (const Bytef *) config, (uLongf) size, 
1271                             Z_BEST_SPEED));
1272   return xsize;
1273 }
1274                                 
1275
1276 /**
1277  * Create a link from slave controller to delegated controller. Whenever the
1278  * master controller is asked to start a peer at the delegated controller the
1279  * request will be routed towards slave controller (if a route exists). The
1280  * slave controller will then route it to the delegated controller. The
1281  * configuration of the slave controller is given and to be used to either
1282  * create the slave controller or to connect to an existing slave controller
1283  * process.  'is_subordinate' specifies if the given slave controller should be
1284  * started and managed by the master controller, or if the slave already has a
1285  * master and this is just a secondary master that is also allowed to use the
1286  * existing slave.
1287  *
1288  * @param master handle to the master controller who creates the association
1289  * @param delegated_host requests to which host should be delegated
1290  * @param slave_host which host is used to run the slave controller 
1291  * @param slave_cfg configuration to use for the slave controller
1292  * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1293  *                       by the master controller; GNUNET_NO if we are just
1294  *                       allowed to use the slave via TCP/IP
1295  */
1296 void
1297 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1298                                 struct GNUNET_TESTBED_Host *delegated_host,
1299                                 struct GNUNET_TESTBED_Host *slave_host,
1300                                 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
1301                                 int is_subordinate)
1302 {
1303   char *config;
1304   char *cconfig;
1305   size_t cc_size;
1306   size_t config_size;  
1307   
1308   GNUNET_assert (GNUNET_YES == 
1309                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1310   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1311     GNUNET_assert (GNUNET_YES == 
1312                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1313   config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1314   cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1315   GNUNET_free (config);
1316   GNUNET_assert ((UINT16_MAX -
1317                   sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
1318                   >= cc_size); /* Configuration doesn't fit in 1 message */
1319   GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1320                                     (const char *) cconfig,
1321                                     cc_size, config_size, is_subordinate);
1322   GNUNET_free (cconfig);
1323 }
1324
1325
1326 /**
1327  * Ask the testbed controller to write the current overlay topology to
1328  * a file.  Naturally, the file will only contain a snapshot as the
1329  * topology may evolve all the time.
1330  *
1331  * @param controller overlay controller to inspect
1332  * @param filename name of the file the topology should
1333  *        be written to.
1334  */
1335 void
1336 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
1337                                                const char *filename)
1338 {
1339   GNUNET_break (0);
1340 }
1341
1342
1343 /**
1344  * Creates a helper initialization message. Only for testing.
1345  *
1346  * @param cname the ip address of the controlling host
1347  * @param cfg the configuration that has to used to start the testbed service
1348  *          thru helper
1349  * @return the initialization message
1350  */
1351 struct GNUNET_TESTBED_HelperInit *
1352 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1353                                          const struct GNUNET_CONFIGURATION_Handle *cfg)
1354 {
1355   struct GNUNET_TESTBED_HelperInit *msg;
1356   char *config;
1357   char *xconfig;
1358   size_t config_size;
1359   size_t xconfig_size;
1360   uint16_t cname_len;
1361   uint16_t msg_size;
1362
1363   config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1364   GNUNET_assert (NULL != config);
1365   xconfig_size =
1366     GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1367   GNUNET_free (config);
1368   cname_len = strlen (cname);
1369   msg_size = xconfig_size + cname_len + 1 + 
1370     sizeof (struct GNUNET_TESTBED_HelperInit);
1371   msg = GNUNET_realloc (xconfig, msg_size);
1372   (void) memmove ( ((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1373   msg->header.size = htons (msg_size);
1374   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1375   msg->cname_size = htons (cname_len);
1376   msg->config_size = htons (config_size);
1377   (void) strcpy ((char *) &msg[1], cname);
1378   return msg;
1379 }
1380
1381
1382 /**
1383  * Cancel a pending operation.  Releases all resources
1384  * of the operation and will ensure that no event
1385  * is generated for the operation.  Does NOT guarantee
1386  * that the operation will be fully undone (or that
1387  * nothing ever happened).  
1388  * 
1389  * @param operation operation to cancel
1390  */
1391 void
1392 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1393 {
1394   GNUNET_CONTAINER_DLL_remove (operation->controller->op_head,
1395                                operation->controller->op_tail,
1396                                operation);
1397   GNUNET_TESTBED_operation_done (operation);
1398 }
1399
1400
1401 /**
1402  * Signal that the information from an operation has been fully
1403  * processed.  This function MUST be called for each event
1404  * of type 'operation_finished' to fully remove the operation
1405  * from the operation queue.  After calling this function, the
1406  * 'op_result' becomes invalid (!).
1407  * 
1408  * @param operation operation to signal completion for
1409  */
1410 void
1411 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1412 {
1413   switch (operation->type)
1414   {
1415   case OP_PEER_CREATE:
1416   case OP_PEER_DESTROY:
1417   case OP_PEER_START:
1418   case OP_PEER_STOP:
1419   case OP_PEER_INFO:
1420   case OP_OVERLAY_CONNECT:
1421     GNUNET_TESTBED_operation_release_ (operation);
1422     return;
1423   default:
1424     GNUNET_assert (0);
1425     break;
1426   }
1427 }
1428
1429
1430 /* end of testbed_api.c */