state information for OperationContext
[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_PEERCONFIG 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_config (struct GNUNET_TESTBED_Controller *c,
414                     const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
415 {
416   struct OperationContext *opc;
417   struct GNUNET_TESTBED_Peer *peer;
418   struct PeerInfoData *data;
419   struct PeerInfoData2 *response_data;
420   struct GNUNET_TESTBED_EventInformation info;
421   uint64_t op_id;
422   
423   op_id = GNUNET_ntohll (msg->operation_id);
424   if (NULL == (opc = find_opc (c, op_id)))
425   {
426     LOG_DEBUG ("Operation not found\n");
427     return GNUNET_YES;
428   }
429   data = opc->data;
430   GNUNET_assert (NULL != data);
431   peer = data->peer;
432   GNUNET_assert (NULL != peer);
433   GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
434   if (0 == (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
435   {
436     LOG_DEBUG ("Skipping operation callback as flag not set\n");
437     return GNUNET_YES;
438   }
439   response_data = GNUNET_malloc (sizeof (struct PeerInfoData2));
440   response_data->pit = data->pit;
441   GNUNET_free (data);
442   opc->data = NULL;
443   info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
444   info.details.operation_finished.operation = opc->op;
445   info.details.operation_finished.op_cls = NULL;
446   info.details.operation_finished.emsg = NULL;
447   info.details.operation_finished.pit = response_data->pit;
448   switch (response_data->pit)
449   {
450   case GNUNET_TESTBED_PIT_IDENTITY:
451     {
452       struct GNUNET_PeerIdentity *peer_identity;
453
454       peer_identity = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
455       (void) memcpy (peer_identity, &msg->peer_identity, 
456                      sizeof (struct GNUNET_PeerIdentity));
457       response_data->details.peer_identity = peer_identity;      
458       info.details.operation_finished.op_result.pid = peer_identity;
459     }
460     break;
461   case GNUNET_TESTBED_PIT_CONFIGURATION:
462     {
463       struct GNUNET_CONFIGURATION_Handle *cfg;
464       char *config;
465       uLong config_size;
466       int ret;
467       uint16_t msize;
468       
469       config_size = (uLong) ntohs (msg->config_size);
470       config = GNUNET_malloc (config_size);
471       msize = ntohs (msg->header.size);
472       msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
473       if (Z_OK != (ret = uncompress ((Bytef *) config, &config_size,
474                                      (const Bytef *) &msg[1], (uLong) msize)))
475         GNUNET_assert (0);
476       cfg = GNUNET_CONFIGURATION_create (); /* Freed in oprelease_peer_getinfo */
477       GNUNET_assert (GNUNET_OK == 
478                      GNUNET_CONFIGURATION_deserialize (cfg, config,
479                                                        (size_t) config_size,
480                                                        GNUNET_NO));
481       GNUNET_free (config);
482       response_data->details.cfg = cfg;
483       info.details.operation_finished.op_result.cfg = cfg;
484     }
485     break;
486   case GNUNET_TESTBED_PIT_GENERIC:
487     GNUNET_assert (0);          /* never reach here */
488     break;
489   }
490   opc->data = response_data;
491   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
492   opc->state = OPC_STATE_FINISHED;
493   c->cc (c->cc_cls, &info);  
494   return GNUNET_YES;
495 }
496
497
498 /**
499  * Handler for messages from controller (testbed service)
500  *
501  * @param cls the controller handler
502  * @param msg message received, NULL on timeout or fatal error
503  */
504 static void 
505 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
506 {
507   struct GNUNET_TESTBED_Controller *c = cls;
508   int status;
509   uint16_t msize;
510
511   c->in_receive = GNUNET_NO;
512   /* FIXME: Add checks for message integrity */
513   if (NULL == msg)
514   {
515     LOG_DEBUG ("Receive timed out or connection to service dropped\n");
516     return;
517   }
518   status = GNUNET_OK;
519   msize = ntohs (msg->size);
520   switch (ntohs (msg->type))
521   {
522   case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
523     GNUNET_assert (msize >= sizeof (struct
524                                     GNUNET_TESTBED_HostConfirmedMessage));
525     status =
526       handle_addhostconfirm (c, (const struct GNUNET_TESTBED_HostConfirmedMessage *) msg);
527     break;
528   case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
529     GNUNET_assert 
530       (msize == sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage));
531     status =
532       handle_opsuccess (c, (const struct
533                             GNUNET_TESTBED_GenericOperationSuccessEventMessage
534                             *) msg);
535     break;
536   case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
537     GNUNET_assert (msize == 
538                    sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
539     status =
540       handle_peer_create_success 
541       (c, (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *)msg);
542     break;
543   case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
544     GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
545     status =
546       handle_peer_event (c, (const struct GNUNET_TESTBED_PeerEventMessage *) msg);
547     
548     break;
549   case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
550     GNUNET_assert (msize >= 
551                    sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage));
552     status = 
553       handle_peer_config 
554       (c, (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
555   msg);
556     break;
557   default:
558     GNUNET_break (0);
559   }
560   if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
561   {
562     c->in_receive = GNUNET_YES;
563     GNUNET_CLIENT_receive (c->client, &message_handler, c,
564                            GNUNET_TIME_UNIT_FOREVER_REL);    
565   }
566 }
567
568
569 /**
570  * Function called to notify a client about the connection begin ready to queue
571  * more data.  "buf" will be NULL and "size" zero if the connection was closed
572  * for writing in the meantime.
573  *
574  * @param cls closure
575  * @param size number of bytes available in buf
576  * @param buf where the callee should write the message
577  * @return number of bytes written to buf
578  */
579 static size_t
580 transmit_ready_notify (void *cls, size_t size, void *buf)
581 {
582   struct GNUNET_TESTBED_Controller *c = cls;
583   struct MessageQueue *mq_entry;
584
585   c->th = NULL;
586   mq_entry = c->mq_head;
587   GNUNET_assert (NULL != mq_entry);
588   if ((0 == size) && (NULL == buf)) /* Timeout */
589   {
590     LOG_DEBUG ("Message sending timed out -- retrying\n");
591     c->th =
592       GNUNET_CLIENT_notify_transmit_ready (c->client,
593                                            ntohs (mq_entry->msg->size),
594                                            TIMEOUT_REL,
595                                            GNUNET_YES, &transmit_ready_notify,
596                                            c);
597     return 0;
598   }
599   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
600   size = ntohs (mq_entry->msg->size);  
601   memcpy (buf, mq_entry->msg, size);
602   LOG_DEBUG ("Message of type: %u and size: %u sent\n",
603              ntohs (mq_entry->msg->type), size);
604   GNUNET_free (mq_entry->msg);
605   GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
606   GNUNET_free (mq_entry);
607   mq_entry = c->mq_head;
608   if (NULL != mq_entry)
609     c->th = 
610       GNUNET_CLIENT_notify_transmit_ready (c->client,
611                                            ntohs (mq_entry->msg->size),
612                                            TIMEOUT_REL,
613                                            GNUNET_YES, &transmit_ready_notify,
614                                            c);
615   if (GNUNET_NO == c->in_receive)
616   {
617     c->in_receive = GNUNET_YES;
618     GNUNET_CLIENT_receive (c->client, &message_handler, c,
619                            GNUNET_TIME_UNIT_FOREVER_REL);
620   }
621   return size;
622 }
623
624
625 /**
626  * Queues a message in send queue for sending to the service
627  *
628  * @param controller the handle to the controller
629  * @param msg the message to queue
630  */
631 void
632 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
633                                struct GNUNET_MessageHeader *msg)
634 {
635   struct MessageQueue *mq_entry;
636   uint16_t type;
637   uint16_t size;
638
639   type = ntohs (msg->type);
640   size = ntohs (msg->size);
641   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
642                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));                 
643   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
644   mq_entry->msg = msg;
645   LOG (GNUNET_ERROR_TYPE_DEBUG,
646        "Queueing message of type %u, size %u for sending\n", type,
647        ntohs (msg->size));
648   GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
649                                     mq_entry);
650   if (NULL == controller->th)
651     controller->th = 
652       GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
653                                            TIMEOUT_REL,
654                                            GNUNET_YES, &transmit_ready_notify,
655                                            controller);
656 }
657
658
659 /**
660  * Handle for controller process
661  */
662 struct GNUNET_TESTBED_ControllerProc
663 {
664   /**
665    * The process handle
666    */
667   struct GNUNET_HELPER_Handle *helper;
668
669   /**
670    * The host where the helper is run
671    */
672   struct GNUNET_TESTBED_Host *host;
673
674   /**
675    * The controller error callback
676    */
677   GNUNET_TESTBED_ControllerStatusCallback cb;
678
679   /**
680    * The closure for the above callback
681    */
682   void *cls;
683
684   /**
685    * The send handle for the helper
686    */
687   struct GNUNET_HELPER_SendHandle *shandle;
688
689   /**
690    * The message corresponding to send handle
691    */
692   struct GNUNET_MessageHeader *msg;
693
694   /**
695    * The port number for ssh; used for helpers starting ssh
696    */
697   char *port;
698
699   /**
700    * The ssh destination string; used for helpers starting ssh
701    */
702   char *dst;
703
704   /**
705    * The configuration of the running testbed service
706    */
707   struct GNUNET_CONFIGURATION_Handle *cfg;
708
709 };
710
711
712 /**
713  * Functions with this signature are called whenever a
714  * complete message is received by the tokenizer.
715  *
716  * Do not call GNUNET_SERVER_mst_destroy in callback
717  *
718  * @param cls closure
719  * @param client identification of the client
720  * @param message the actual message
721  *
722  * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
723  */
724 static int helper_mst (void *cls, void *client,
725                        const struct GNUNET_MessageHeader *message)
726 {
727   struct GNUNET_TESTBED_ControllerProc *cp = cls;
728   const struct GNUNET_TESTBED_HelperReply *msg;
729   const char *hostname;
730   char *config;
731   uLongf config_size;
732   uLongf xconfig_size;
733     
734   msg = (const struct GNUNET_TESTBED_HelperReply *) message;
735   GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) 
736                  < ntohs (msg->header.size));
737   GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY 
738                  == ntohs (msg->header.type));
739   config_size = (uLongf) ntohs (msg->config_size);
740   xconfig_size = (uLongf) (ntohs (msg->header.size)
741                            - sizeof (struct GNUNET_TESTBED_HelperReply));
742   config = GNUNET_malloc (config_size);
743   GNUNET_assert (Z_OK == uncompress ((Bytef *) config, &config_size,
744                                      (const Bytef *) &msg[1], xconfig_size));
745   GNUNET_assert (NULL == cp->cfg);
746   cp->cfg = GNUNET_CONFIGURATION_create ();
747   GNUNET_assert (GNUNET_CONFIGURATION_deserialize (cp->cfg, config, 
748                                                    config_size, GNUNET_NO));
749   GNUNET_free (config);
750   if ((NULL == cp->host) || 
751       (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
752     hostname = "localhost";
753   /* Change the hostname so that we can connect to it */
754   GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname", 
755                                          hostname);
756   cp->cb (cp->cls, cp->cfg, GNUNET_OK);
757   return GNUNET_OK;
758 }
759
760
761 /**
762  * Continuation function from GNUNET_HELPER_send()
763  * 
764  * @param cls closure
765  * @param result GNUNET_OK on success,
766  *               GNUNET_NO if helper process died
767  *               GNUNET_SYSERR during GNUNET_HELPER_stop
768  */
769 static void 
770 clear_msg (void *cls, int result)
771 {
772   struct GNUNET_TESTBED_ControllerProc *cp = cls;
773   
774   GNUNET_assert (NULL != cp->shandle);
775   cp->shandle = NULL;
776   GNUNET_free (cp->msg);
777 }
778
779
780 /**
781  * Callback that will be called when the helper process dies. This is not called
782  * when the helper process is stoped using GNUNET_HELPER_stop()
783  *
784  * @param cls the closure from GNUNET_HELPER_start()
785  */
786 static void 
787 helper_exp_cb (void *cls)
788 {
789   struct GNUNET_TESTBED_ControllerProc *cp = cls;
790   GNUNET_TESTBED_ControllerStatusCallback cb;
791   void *cb_cls;
792
793   cb = cp->cb;
794   cb_cls = cp->cls;
795   GNUNET_TESTBED_controller_stop (cp);
796   if (NULL != cb)
797     cb (cb_cls, NULL, GNUNET_SYSERR);
798 }
799
800
801 /**
802  * Starts a controller process at the host. FIXME: add controller start callback
803  * with the configuration with which the controller is started
804  *
805  * @param controller_ip the ip address of the controller. Will be set as TRUSTED
806  *          host when starting testbed controller at host
807  * @param host the host where the controller has to be started; NULL for
808  *          localhost
809  * @param cfg template configuration to use for the remote controller; the
810  *          remote controller will be started with a slightly modified
811  *          configuration (port numbers, unix domain sockets and service home
812  *          values are changed as per TESTING library on the remote host)
813  * @param cb function called when the controller is successfully started or
814  *          dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
815  *          called if cb is called with GNUNET_SYSERR as status. Will never be
816  *          called in the same task as 'GNUNET_TESTBED_controller_start'
817  *          (synchronous errors will be signalled by returning NULL). This
818  *          parameter cannot be NULL.
819  * @param cls closure for above callbacks
820  * @return the controller process handle, NULL on errors
821  */
822 struct GNUNET_TESTBED_ControllerProc *
823 GNUNET_TESTBED_controller_start (const char *controller_ip,
824                                  struct GNUNET_TESTBED_Host *host,
825                                  const struct GNUNET_CONFIGURATION_Handle *cfg,
826                                  GNUNET_TESTBED_ControllerStatusCallback cb,
827                                  void *cls)
828 {
829   struct GNUNET_TESTBED_ControllerProc *cp;
830   struct GNUNET_TESTBED_HelperInit *msg;
831   
832   cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
833   if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
834   {
835     char * const binary_argv[] = {
836       "gnunet-testbed-helper", NULL
837     };
838
839     cp->helper = GNUNET_HELPER_start ("gnunet-testbed-helper", binary_argv, 
840                                       &helper_mst, &helper_exp_cb, cp);
841   }
842   else
843   {
844     char *remote_args[6 + 1];
845     unsigned int argp;
846     const char *username;
847     const char *hostname;
848
849     username = GNUNET_TESTBED_host_get_username_ (host);
850     hostname = GNUNET_TESTBED_host_get_hostname_ (host);
851     GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
852     if (NULL == username)
853       GNUNET_asprintf (&cp->dst, "%s", hostname);
854     else 
855       GNUNET_asprintf (&cp->dst, "%s@%s", hostname, username);
856     argp = 0;
857     remote_args[argp++] = "ssh";
858     remote_args[argp++] = "-p";
859     remote_args[argp++] = cp->port;
860     remote_args[argp++] = "-q";
861     remote_args[argp++] = cp->dst;
862     remote_args[argp++] = "gnunet-testbed-helper";
863     remote_args[argp++] = NULL;
864     GNUNET_assert (argp == 6 + 1);
865     cp->helper = GNUNET_HELPER_start ("ssh", remote_args,
866                                       &helper_mst, &helper_exp_cb, cp);
867   }
868   if (NULL == cp->helper)
869   {
870     GNUNET_free_non_null (cp->port);
871     GNUNET_free_non_null (cp->dst);
872     GNUNET_free (cp);
873     return NULL;
874   }
875   cp->host = host;
876   cp->cb = cb;
877   cp->cls = cls;
878   msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
879   cp->msg = &msg->header;
880   cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO,
881                                     &clear_msg, cp);
882   if (NULL == cp->shandle)
883   {
884     GNUNET_free (msg);
885     GNUNET_TESTBED_controller_stop (cp);
886     return NULL;
887   }
888   return cp;
889 }
890
891
892 /**
893  * Stop the controller process (also will terminate all peers and controllers
894  * dependent on this controller).  This function blocks until the testbed has
895  * been fully terminated (!).
896  *
897  * @param cproc the controller process handle
898  */
899 void
900 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
901 {
902   if (NULL != cproc->shandle)
903     GNUNET_HELPER_send_cancel (cproc->shandle);
904   GNUNET_HELPER_stop (cproc->helper);
905   if (NULL != cproc->cfg)
906     GNUNET_CONFIGURATION_destroy (cproc->cfg);
907   GNUNET_free_non_null (cproc->port);
908   GNUNET_free_non_null (cproc->dst);
909   GNUNET_free (cproc);
910 }
911
912
913 /**
914  * Start a controller process using the given configuration at the
915  * given host.
916  *
917  * @param cfg configuration to use
918  * @param host host to run the controller on; This should be the same host if
919  *          the controller was previously started with
920  *          GNUNET_TESTBED_controller_start; NULL for localhost
921  * @param event_mask bit mask with set of events to call 'cc' for;
922  *                   or-ed values of "1LL" shifted by the
923  *                   respective 'enum GNUNET_TESTBED_EventType'
924  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
925  * @param cc controller callback to invoke on events
926  * @param cc_cls closure for cc
927  * @return handle to the controller
928  */
929 struct GNUNET_TESTBED_Controller *
930 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
931                                    struct GNUNET_TESTBED_Host *host,
932                                    uint64_t event_mask,
933                                    GNUNET_TESTBED_ControllerCallback cc,
934                                    void *cc_cls)
935 {
936   struct GNUNET_TESTBED_Controller *controller;
937   struct GNUNET_TESTBED_InitMessage *msg;
938   unsigned long long max_parallel_peer_create;
939
940   if (GNUNET_OK !=
941       GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
942                                              "MAX_PARALLEL_PEER_CREATE",
943                                              &max_parallel_peer_create))
944   {
945     GNUNET_break (0);
946     return NULL;
947   }                                                          
948   controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
949   controller->cc = cc;
950   controller->cc_cls = cc_cls;
951   controller->event_mask = event_mask;
952   controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
953   controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);  
954   if (NULL == controller->client)
955   {
956     GNUNET_TESTBED_controller_disconnect (controller);
957     return NULL;
958   }
959   if (NULL == host)
960   {
961     host = GNUNET_TESTBED_host_create_by_id_ (0);
962     if (NULL == host)
963     {
964       LOG (GNUNET_ERROR_TYPE_WARNING,
965            "Treating NULL host as localhost. Multiple references to localhost "
966            "may break when localhost freed before calling disconnect \n");
967       host = GNUNET_TESTBED_host_lookup_by_id_ (0);
968     }
969     else
970     {
971       controller->aux_host = GNUNET_YES;
972     }
973   }
974   GNUNET_assert (NULL != host);
975   controller->opq_peer_create =
976     GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
977                                             max_parallel_peer_create);
978   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
979   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
980   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
981   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
982   msg->event_mask = GNUNET_htonll (controller->event_mask);
983   GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *)
984                                  msg);
985   
986   return controller;
987 }
988
989
990 /**
991  * Configure shared services at a controller.  Using this function,
992  * you can specify that certain services (such as "resolver")
993  * should not be run for each peer but instead be shared
994  * across N peers on the specified host.  This function
995  * must be called before any peers are created at the host.
996  * 
997  * @param controller controller to configure
998  * @param service_name name of the service to share
999  * @param num_peers number of peers that should share one instance
1000  *        of the specified service (1 for no sharing is the default),
1001  *        use 0 to disable the service
1002  */
1003 void
1004 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
1005                                              const char *service_name,
1006                                              uint32_t num_peers)
1007 {
1008   struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1009   uint16_t service_name_size;
1010   uint16_t msg_size;
1011   
1012   service_name_size = strlen (service_name) + 1;
1013   msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
1014     + service_name_size;
1015   msg = GNUNET_malloc (msg_size);
1016   msg->header.size = htons (msg_size);
1017   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1018   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1019   msg->num_peers = htonl (num_peers);
1020   memcpy (&msg[1], service_name, service_name_size);
1021   GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1022 }
1023
1024
1025 /**
1026  * disconnects from the controller.
1027  *
1028  * @param controller handle to controller to stop
1029  */
1030 void
1031 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
1032 {
1033   struct MessageQueue *mq_entry;
1034
1035   if (NULL != controller->th)
1036     GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1037  /* Clear the message queue */
1038   while (NULL != (mq_entry = controller->mq_head))
1039   {
1040     GNUNET_CONTAINER_DLL_remove (controller->mq_head,
1041                                  controller->mq_tail,
1042                                  mq_entry);
1043     GNUNET_free (mq_entry->msg);
1044     GNUNET_free (mq_entry);
1045   }
1046   if (NULL != controller->client)
1047     GNUNET_CLIENT_disconnect (controller->client);
1048   GNUNET_CONFIGURATION_destroy (controller->cfg);
1049   if (GNUNET_YES == controller->aux_host)
1050     GNUNET_TESTBED_host_destroy (controller->host);
1051   GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_peer_create);
1052   GNUNET_free (controller);
1053 }
1054
1055
1056 /**
1057  * Register a host with the controller
1058  *
1059  * @param controller the controller handle
1060  * @param host the host to register
1061  * @param cc the completion callback to call to inform the status of
1062  *          registration. After calling this callback the registration handle
1063  *          will be invalid. Cannot be NULL.
1064  * @param cc_cls the closure for the cc
1065  * @return handle to the host registration which can be used to cancel the
1066  *           registration 
1067  */
1068 struct GNUNET_TESTBED_HostRegistrationHandle *
1069 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1070                               struct GNUNET_TESTBED_Host *host,
1071                               GNUNET_TESTBED_HostRegistrationCompletion cc,
1072                               void *cc_cls)
1073 {
1074   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1075   struct GNUNET_TESTBED_AddHostMessage *msg;
1076   const char *username;
1077   const char *hostname;
1078   uint16_t msg_size;
1079   uint16_t user_name_length;
1080
1081   if (NULL != controller->rh)
1082     return NULL;
1083   hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1084   if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1085   {
1086     LOG (GNUNET_ERROR_TYPE_WARNING,
1087          "Host hostname: %s already registered\n",
1088          (NULL == hostname) ? "localhost" : hostname);
1089     return NULL;
1090   }  
1091   rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1092   rh->host = host;
1093   rh->c = controller;
1094   GNUNET_assert (NULL != cc);
1095   rh->cc = cc;
1096   rh->cc_cls = cc_cls;
1097   controller->rh = rh;
1098   username = GNUNET_TESTBED_host_get_username_ (host);
1099   msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1100   user_name_length = 0;
1101   if (NULL != username)
1102   {
1103     user_name_length = strlen (username) + 1;
1104     msg_size += user_name_length;
1105   }
1106   /* FIXME: what happens when hostname is NULL? localhost */
1107   GNUNET_assert (NULL != hostname);
1108   msg_size += strlen (hostname) + 1;
1109   msg = GNUNET_malloc (msg_size);
1110   msg->header.size = htons (msg_size);
1111   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1112   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1113   msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1114   msg->user_name_length = htons (user_name_length);
1115   if (NULL != username)
1116     memcpy (&msg[1], username, user_name_length);
1117   strcpy (((void *) &msg[1]) + user_name_length, hostname);
1118   GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1119   return rh;
1120 }
1121
1122
1123 /**
1124  * Cancel the pending registration. Note that if the registration message is
1125  * already sent to the service the cancellation has only the effect that the
1126  * registration completion callback for the registration is never called.
1127  *
1128  * @param handle the registration handle to cancel
1129  */
1130 void
1131 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1132                                     *handle)
1133 {
1134   if (handle != handle->c->rh)
1135   {
1136     GNUNET_break (0);
1137     return;
1138   }
1139   handle->c->rh = NULL;
1140   GNUNET_free (handle);  
1141 }
1142
1143
1144 /**
1145  * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1146  * serialized and compressed
1147  *
1148  * @param master handle to the master controller who creates the association
1149  * @param delegated_host requests to which host should be delegated; cannot be NULL
1150  * @param slave_host which host is used to run the slave controller; use NULL to
1151  *          make the master controller connect to the delegated host
1152  * @param sxcfg serialized and compressed configuration
1153  * @param sxcfg_size the size scfg
1154  * @param scfg_size the size of uncompressed serialized configuration
1155  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1156  *          be started by the master controller; GNUNET_NO if we are just
1157  *          allowed to use the slave via TCP/IP
1158  */
1159 void
1160 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1161                                   struct GNUNET_TESTBED_Host *delegated_host,
1162                                   struct GNUNET_TESTBED_Host *slave_host,
1163                                   const char *sxcfg,
1164                                   size_t sxcfg_size,
1165                                   size_t scfg_size,
1166                                   int is_subordinate)
1167 {
1168   struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1169   uint16_t msg_size;
1170
1171   GNUNET_assert (GNUNET_YES == 
1172                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1173   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1174     GNUNET_assert (GNUNET_YES == 
1175                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1176   msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1177   msg = GNUNET_malloc (msg_size);
1178   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);  
1179   msg->header.size = htons (msg_size);
1180   msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1181   msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_ 
1182                               ((NULL != slave_host) ? slave_host : master->host));
1183   msg->config_size = htons ((uint16_t) scfg_size);
1184   msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1185   memcpy (&msg[1], sxcfg, sxcfg_size);
1186   GNUNET_TESTBED_queue_message_ (master, (struct GNUNET_MessageHeader *) msg);
1187 }
1188
1189
1190 /**
1191  * Compresses given configuration using zlib compress
1192  *
1193  * @param config the serialized configuration
1194  * @param size the size of config
1195  * @param xconfig will be set to the compressed configuration (memory is fresly
1196  *          allocated) 
1197  * @return the size of the xconfig
1198  */
1199 size_t
1200 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1201                                  char **xconfig)
1202 {
1203   size_t xsize;
1204   
1205   xsize = compressBound ((uLong) size);
1206   *xconfig = GNUNET_malloc (xsize);
1207   GNUNET_assert (Z_OK ==
1208                  compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
1209                             (const Bytef *) config, (uLongf) size, 
1210                             Z_BEST_SPEED));
1211   return xsize;
1212 }
1213                                 
1214
1215 /**
1216  * Create a link from slave controller to delegated controller. Whenever the
1217  * master controller is asked to start a peer at the delegated controller the
1218  * request will be routed towards slave controller (if a route exists). The
1219  * slave controller will then route it to the delegated controller. The
1220  * configuration of the slave controller is given and to be used to either
1221  * create the slave controller or to connect to an existing slave controller
1222  * process.  'is_subordinate' specifies if the given slave controller should be
1223  * started and managed by the master controller, or if the slave already has a
1224  * master and this is just a secondary master that is also allowed to use the
1225  * existing slave.
1226  *
1227  * @param master handle to the master controller who creates the association
1228  * @param delegated_host requests to which host should be delegated
1229  * @param slave_host which host is used to run the slave controller 
1230  * @param slave_cfg configuration to use for the slave controller
1231  * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1232  *                       by the master controller; GNUNET_NO if we are just
1233  *                       allowed to use the slave via TCP/IP
1234  */
1235 void
1236 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1237                                 struct GNUNET_TESTBED_Host *delegated_host,
1238                                 struct GNUNET_TESTBED_Host *slave_host,
1239                                 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
1240                                 int is_subordinate)
1241 {
1242   char *config;
1243   char *cconfig;
1244   size_t cc_size;
1245   size_t config_size;  
1246   
1247   GNUNET_assert (GNUNET_YES == 
1248                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1249   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1250     GNUNET_assert (GNUNET_YES == 
1251                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1252   config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1253   cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1254   GNUNET_free (config);
1255   GNUNET_assert ((UINT16_MAX -
1256                   sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
1257                   >= cc_size); /* Configuration doesn't fit in 1 message */
1258   GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1259                                     (const char *) cconfig,
1260                                     cc_size, config_size, is_subordinate);
1261   GNUNET_free (cconfig);
1262 }
1263
1264
1265 /**
1266  * Ask the testbed controller to write the current overlay topology to
1267  * a file.  Naturally, the file will only contain a snapshot as the
1268  * topology may evolve all the time.
1269  *
1270  * @param controller overlay controller to inspect
1271  * @param filename name of the file the topology should
1272  *        be written to.
1273  */
1274 void
1275 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
1276                                                const char *filename)
1277 {
1278   GNUNET_break (0);
1279 }
1280
1281
1282 /**
1283  * Creates a helper initialization message. Only for testing.
1284  *
1285  * @param cname the ip address of the controlling host
1286  * @param cfg the configuration that has to used to start the testbed service
1287  *          thru helper
1288  * @return the initialization message
1289  */
1290 struct GNUNET_TESTBED_HelperInit *
1291 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1292                                          const struct GNUNET_CONFIGURATION_Handle *cfg)
1293 {
1294   struct GNUNET_TESTBED_HelperInit *msg;
1295   char *config;
1296   char *xconfig;
1297   size_t config_size;
1298   size_t xconfig_size;
1299   uint16_t cname_len;
1300   uint16_t msg_size;
1301
1302   config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1303   GNUNET_assert (NULL != config);
1304   xconfig_size =
1305     GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1306   GNUNET_free (config);
1307   cname_len = strlen (cname);
1308   msg_size = xconfig_size + cname_len + 1 + 
1309     sizeof (struct GNUNET_TESTBED_HelperInit);
1310   msg = GNUNET_realloc (xconfig, msg_size);
1311   (void) memmove ( ((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1312   msg->header.size = htons (msg_size);
1313   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1314   msg->cname_size = htons (cname_len);
1315   msg->config_size = htons (config_size);
1316   (void) strcpy ((char *) &msg[1], cname);
1317   return msg;
1318 }
1319
1320
1321 /**
1322  * Cancel a pending operation.  Releases all resources
1323  * of the operation and will ensure that no event
1324  * is generated for the operation.  Does NOT guarantee
1325  * that the operation will be fully undone (or that
1326  * nothing ever happened).  
1327  * 
1328  * @param operation operation to cancel
1329  */
1330 void
1331 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1332 {
1333   GNUNET_CONTAINER_DLL_remove (operation->controller->op_head,
1334                                operation->controller->op_tail,
1335                                operation);
1336   GNUNET_TESTBED_operation_done (operation);
1337 }
1338
1339
1340 /**
1341  * Signal that the information from an operation has been fully
1342  * processed.  This function MUST be called for each event
1343  * of type 'operation_finished' to fully remove the operation
1344  * from the operation queue.  After calling this function, the
1345  * 'op_result' becomes invalid (!).
1346  * 
1347  * @param operation operation to signal completion for
1348  */
1349 void
1350 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1351 {
1352   switch (operation->type)
1353   {
1354   case OP_PEER_CREATE:
1355   case OP_PEER_DESTROY:
1356   case OP_PEER_START:
1357   case OP_PEER_STOP:
1358   case OP_PEER_INFO:
1359     GNUNET_TESTBED_operation_release_ (operation);
1360     return;
1361   case OP_OVERLAY_CONNECT:
1362     GNUNET_free_non_null (operation->data);
1363     break;
1364   }
1365   GNUNET_free (operation);
1366 }
1367
1368
1369 /* end of testbed_api.c */