controller hostname in init
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed.c
1 /*
2   This file is part of GNUnet.
3   (C) 2012 Christian Grothoff (and other contributing authors)
4
5   GNUnet is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published
7   by the Free Software Foundation; either version 2, 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/gnunet-service-testbed.c
23  * @brief implementation of the TESTBED service
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_service_lib.h"
29 #include "gnunet_server_lib.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_hello_lib.h"
33 #include <zlib.h>
34
35 #include "gnunet_testbed_service.h"
36 #include "testbed.h"
37 #include "testbed_api.h"
38 #include "testbed_api_hosts.h"
39 #include "gnunet_testing_lib-new.h"
40
41 /**
42  * Generic logging
43  */
44 #define LOG(kind,...)                           \
45   GNUNET_log (kind, __VA_ARGS__)
46
47 /**
48  * Debug logging
49  */
50 #define LOG_DEBUG(...)                          \
51   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
52
53
54 #define LIST_GROW_STEP 10
55
56 struct Context
57 {
58   /**
59    * The client handle associated with this context
60    */
61   struct GNUNET_SERVER_Client *client;
62
63   /**
64    * The network address of the master controller
65    */
66   char *master_ip;
67
68   /**
69    * The TESTING system handle for starting peers locally
70    */
71   struct GNUNET_TESTING_System *system;
72   
73   /**
74    * Event mask of event to be responded in this context
75    */
76   uint64_t event_mask;
77
78   /**
79    * Our host id according to this context
80    */
81   uint32_t host_id;
82 };
83
84
85 /**
86  * The message queue for sending messages to clients
87  */
88 struct MessageQueue
89 {
90   /**
91    * The message to be sent
92    */
93   struct GNUNET_MessageHeader *msg;
94
95   /**
96    * The client to send the message to
97    */
98   struct GNUNET_SERVER_Client *client;
99   
100   /**
101    * next pointer for DLL
102    */
103   struct MessageQueue *next;
104   
105   /**
106    * prev pointer for DLL
107    */
108   struct MessageQueue *prev;
109 };
110
111
112 /**
113  * The structure for identifying a shared service
114  */
115 struct SharedService
116 {
117   /**
118    * The name of the shared service
119    */
120   char *name;
121
122   /**
123    * Number of shared peers per instance of the shared service
124    */
125   uint32_t num_shared;
126
127   /**
128    * Number of peers currently sharing the service
129    */
130   uint32_t num_sharing;
131 };
132
133
134 /**
135  * A routing entry
136  */
137 struct Route
138 {
139   /**
140    * destination host
141    */
142   uint32_t dest;
143
144   /**
145    * The host destination is reachable thru
146    */
147   uint32_t thru;
148 };
149
150
151 /**
152  * Structure representing a connected(directly-linked) controller
153  */
154 struct Slave
155 {
156   /**
157    * The controller process handle if we had started the controller
158    */
159   struct GNUNET_TESTBED_ControllerProc *controller_proc;
160
161   /**
162    * The controller handle
163    */
164   struct GNUNET_TESTBED_Controller *controller;
165
166   /**
167    * The id of the host this controller is running on
168    */
169   uint32_t host_id;
170 };
171
172
173 /**
174  * States of LCFContext
175  */
176 enum LCFContextState
177   {
178     /**
179      * The Context has been initialized; Nothing has been done on it
180      */
181     INIT,
182
183     /**
184      * Delegated host has been registered at the forwarding controller
185      */
186     DELEGATED_HOST_REGISTERED,
187     
188     /**
189      * The slave host has been registred at the forwarding controller
190      */
191     SLAVE_HOST_REGISTERED,
192
193     /**
194      * The context has been finished (may have error)
195      */
196     FINISHED
197
198   };
199
200
201 /**
202  * Link controllers request forwarding context
203  */
204 struct LCFContext
205 {
206   /**
207    * The serialized and compressed configuration
208    */
209   char *sxcfg;
210
211   /**
212    * The gateway which will pass the link message to delegated host
213    */
214   struct Slave *gateway;
215
216   /**
217    * The host registration handle while registered hosts in this context
218    */
219   struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
220
221   /**
222    * The size of the compressed serialized configuration
223    */
224   size_t sxcfg_size;
225
226   /**
227    * The size of the uncompressed configuration
228    */
229   size_t scfg_size;
230
231   /**
232    * Should the delegated host be started by the slave host?
233    */
234   int is_subordinate;
235
236   /**
237    * The state of this context
238    */
239   enum LCFContextState state;
240
241   /**
242    * The delegated host
243    */
244   uint32_t delegated_host_id;
245
246   /**
247    * The slave host
248    */
249   uint32_t slave_host_id;
250
251 };
252
253
254 /**
255  * Structure of a queue entry in LCFContext request queue
256  */
257 struct LCFContextQueue
258 {
259   /**
260    * The LCFContext
261    */
262   struct LCFContext *lcf;
263
264   /**
265    * Head prt for DLL
266    */
267   struct LCFContextQueue *next;
268
269   /**
270    * Tail ptr for DLL
271    */
272   struct LCFContextQueue *prev;
273 };
274
275
276 /**
277  * A locally started peer
278  */
279 struct Peer
280 {
281   /**
282    * The peer handle from testing API
283    */
284   struct GNUNET_TESTING_Peer *peer;
285
286   /**
287    * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
288    * configured with
289    */
290   struct GNUNET_CONFIGURATION_Handle *cfg;
291
292   /**
293    * Our local reference id for this peer
294    */
295   uint32_t id;
296
297 };
298
299
300 /**
301  * State information for overlay connect context
302  */
303 enum OCCState
304   {
305     /**
306      * Initial state
307      */
308     OCC_STATE_INIT,
309
310     /**
311      * Peer 1 has connected to peer0
312      */
313     OCC_STATE_PEER0_SUCCESS,
314
315     /**
316      * Peer 2 has connected to peer1
317      */
318     OCC_STATE_PEER1_SUCCESS,
319
320   };
321
322
323 /**
324  * Context information for connecting 2 peers in overlay
325  */
326 struct OverlayConnectContext
327 {
328   /**
329    * The client which has requested for overlay connection
330    */
331   struct GNUNET_SERVER_Client *client;
332
333   /**
334    * the peer which has to connect to the other peer
335    */
336   struct Peer *peer;
337
338   /**
339    * The other peer
340    */
341   struct Peer *other_peer;
342   
343   /**
344    * Transport handle of the first peer to offer second peer's HELLO
345    */
346   struct GNUNET_TRANSPORT_Handle *p1th;
347
348   /**
349    * Transport handle of other peer to get its HELLO
350    */
351   struct GNUNET_TRANSPORT_Handle *p2th;
352
353   /**
354    * Core handles of the peer which has to connect to other peer
355    */
356   struct GNUNET_CORE_Handle *ch;
357
358   /**
359    * HELLO of the other peer
360    */
361   struct GNUNET_MessageHeader *hello;
362   
363   /**
364    * Get hello handle for the other peer
365    */
366   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
367
368   /**
369    * The error message we send if this overlay connect operation has timed out
370    */
371   char *emsg;
372   
373   /**
374    * The peer identity of the first peer
375    */
376   struct GNUNET_PeerIdentity peer_identity;
377
378   /**
379    * The peer identity of the other peer
380    */
381   struct GNUNET_PeerIdentity other_peer_identity;
382
383   /**
384    * The id of the operation responsible for creating this context
385    */
386   uint64_t op_id;
387
388   /**
389    * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
390    * connect to peer 2
391    */
392   GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
393   
394   /**
395    * The id of the overlay connect timeout task
396    */
397   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
398
399   /**
400    * State information for determining whose HELLOs have been successfully
401    * exchanged
402    */
403   enum OCCState state;
404
405 };
406
407
408 /**
409  * The master context; generated with the first INIT message
410  */
411 static struct Context *master_context;
412
413 /***********/
414 /* Handles */
415 /***********/
416
417 /**
418  * Current Transmit Handle; NULL if no notify transmit exists currently
419  */
420 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
421
422 /****************/
423 /* Lists & Maps */
424 /****************/
425
426 /**
427  * The head for the LCF queue
428  */
429 static struct LCFContextQueue *lcfq_head;
430
431 /**
432  * The tail for the LCF queue
433  */
434 static struct LCFContextQueue *lcfq_tail;
435
436 /**
437  * The message queue head
438  */
439 static struct MessageQueue *mq_head;
440
441 /**
442  * The message queue tail
443  */
444 static struct MessageQueue *mq_tail;
445
446 /**
447  * Array of host list
448  */
449 static struct GNUNET_TESTBED_Host **host_list;
450
451 /**
452  * A list of routes
453  */
454 static struct Route **route_list;
455
456 /**
457  * A list of directly linked neighbours
458  */
459 static struct Slave **slave_list;
460
461 /**
462  * A list of peers we own locally
463  */
464 static struct Peer **peer_list;
465
466 /**
467  * The hashmap of shared services
468  */
469 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
470
471 /**
472  * The size of the host list
473  */
474 static uint32_t host_list_size;
475
476 /**
477  * The size of the route list
478  */
479 static uint32_t route_list_size;
480
481 /**
482  * The size of directly linked neighbours list
483  */
484 static uint32_t slave_list_size;
485
486 /**
487  * The size of the peer list
488  */
489 static uint32_t peer_list_size;
490
491 /*********/
492 /* Tasks */
493 /*********/
494
495 /**
496  * The lcf_task handle
497  */
498 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
499
500 /**
501  * The shutdown task handle
502  */
503 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
504
505
506 /**
507  * Function called to notify a client about the connection begin ready to queue
508  * more data.  "buf" will be NULL and "size" zero if the connection was closed
509  * for writing in the meantime.
510  *
511  * @param cls NULL
512  * @param size number of bytes available in buf
513  * @param buf where the callee should write the message
514  * @return number of bytes written to buf
515  */
516 static size_t
517 transmit_ready_notify (void *cls, size_t size, void *buf)
518 {
519   struct MessageQueue *mq_entry;
520
521   transmit_handle = NULL;
522   mq_entry = mq_head;
523   GNUNET_assert (NULL != mq_entry);
524   if (0 == size)
525     return 0;
526   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
527   size = ntohs (mq_entry->msg->size);
528   memcpy (buf, mq_entry->msg, size);
529   GNUNET_free (mq_entry->msg);
530   GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
531   GNUNET_free (mq_entry);
532   mq_entry = mq_head;
533   if (NULL != mq_entry)
534     transmit_handle = 
535       GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
536                                            ntohs (mq_entry->msg->size),
537                                            GNUNET_TIME_UNIT_FOREVER_REL,
538                                            &transmit_ready_notify, NULL);
539   return size;
540 }
541
542
543 /**
544  * Queues a message in send queue for sending to the service
545  *
546  * @param client the client to whom the queued message has to be sent
547  * @param msg the message to queue
548  */
549 static void
550 queue_message (struct GNUNET_SERVER_Client *client,
551                struct GNUNET_MessageHeader *msg)
552 {
553   struct MessageQueue *mq_entry;
554   uint16_t type;
555   uint16_t size;
556
557   type = ntohs (msg->type);
558   size = ntohs (msg->size);
559   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
560                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));                 
561   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
562   mq_entry->msg = msg;
563   mq_entry->client = client;
564   LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
565               ntohs (msg->size));
566   GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
567   if (NULL == transmit_handle)
568     transmit_handle = 
569       GNUNET_SERVER_notify_transmit_ready (client, size,
570                                            GNUNET_TIME_UNIT_FOREVER_REL,
571                                            &transmit_ready_notify, NULL);
572 }
573
574
575 /**
576  * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
577  *
578  * @param ptr the memory block to realloc
579  * @param size the size of ptr
580  * @param new_size the size to which ptr has to be realloc'ed
581  * @return the newly reallocated memory block
582  */
583 static void *
584 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
585 {
586   ptr = GNUNET_realloc (ptr, new_size);
587   if (new_size > size)
588     ptr = memset (ptr + size, 0, new_size - size);
589   return ptr;
590 }
591
592
593 /**
594  * Function to add a host to the current list of known hosts
595  *
596  * @param host the host to add 
597  * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
598  *           already in use
599  */
600 static int
601 host_list_add (struct GNUNET_TESTBED_Host *host)
602 {
603   uint32_t host_id;
604
605   host_id = GNUNET_TESTBED_host_get_id_ (host);
606   if (host_list_size <= host_id)
607   {
608     host_list = 
609       TESTBED_realloc (host_list, 
610                        sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
611                        sizeof (struct GNUNET_TESTBED_Host *) *
612                        (host_list_size + LIST_GROW_STEP));
613     host_list_size += LIST_GROW_STEP;
614   }
615   if (NULL != host_list[host_id])
616   {
617     LOG_DEBUG ("A host with id: %u already exists\n", host_id);
618     return GNUNET_SYSERR;
619   }
620   host_list[host_id] = host;
621   return GNUNET_OK;
622 }
623
624
625 /**
626  * Adds a route to the route list
627  *
628  * @param route the route to add
629  */
630 static void
631 route_list_add (struct Route *route)
632 {
633   if (route->dest >= route_list_size)
634   {
635     route_list = 
636       TESTBED_realloc (route_list, 
637                        sizeof (struct Route *) * route_list_size,
638                        sizeof (struct Route *) * 
639                        (route_list_size + LIST_GROW_STEP));
640     route_list_size += LIST_GROW_STEP;
641   }
642   GNUNET_assert (NULL == route_list[route->dest]);
643   route_list[route->dest] = route;
644 }
645
646
647 /**
648  * Adds a slave to the slave array
649  *
650  * @param slave the slave controller to add
651  */
652 static void
653 slave_list_add (struct Slave *slave)
654 {
655   if (slave->host_id  >= slave_list_size)
656   {
657     slave_list = TESTBED_realloc (slave_list, 
658                                   sizeof (struct Slave *) *slave_list_size,
659                                   sizeof (struct Slave *) *
660                                   (slave_list_size + LIST_GROW_STEP));
661     slave_list_size += LIST_GROW_STEP;
662   }
663   GNUNET_assert (NULL == slave_list[slave->host_id]);
664   slave_list[slave->host_id] = slave;
665 }
666
667
668 /**
669  * Adds a peer to the peer array
670  *
671  * @param peer the peer to add
672  */
673 static void
674 peer_list_add (struct Peer *peer)
675 {
676   if (peer->id  >= peer_list_size)
677   {
678     peer_list = TESTBED_realloc (peer_list, 
679                                  sizeof (struct Peer *) * peer_list_size,
680                                  sizeof (struct Peer *) *
681                                  (peer_list_size + LIST_GROW_STEP));
682     peer_list_size += LIST_GROW_STEP;
683   }
684   GNUNET_assert (NULL == peer_list[peer->id]);
685   peer_list[peer->id] = peer;
686 }
687
688
689 /**
690  * Removes a the give peer from the peer array
691  *
692  * @param peer the peer to be removed
693  */
694 static void
695 peer_list_remove (struct Peer *peer)
696 {
697   uint32_t id;
698
699   peer_list[peer->id] = NULL;
700   while (peer_list_size >= LIST_GROW_STEP)
701   {
702     for (id = peer_list_size - 1;
703          id > peer_list_size - LIST_GROW_STEP; id--)
704       if (NULL != peer_list[id])
705         break;
706     if (id != peer_list_size - LIST_GROW_STEP)
707       break;
708     peer_list_size -= LIST_GROW_STEP;
709   }
710   peer_list = GNUNET_realloc (peer_list, sizeof (struct GNUNET_TESTBED_Peer*)
711                               * peer_list_size);
712 }
713
714
715 /**
716  * Routes message to a host given its host_id
717  *
718  * @param host_id the id of the destination host
719  * @param msg the message to be routed
720  */
721 static void
722 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
723 {
724   GNUNET_break (0);
725 }
726
727
728 /**
729  * Send operation failure message to client
730  *
731  * @param client the client to which the failure message has to be sent to
732  * @param operation_id the id of the failed operation
733  * @param emsg the error message; can be NULL
734  */
735 static void
736 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
737                          uint64_t operation_id,
738                          const char *emsg)
739 {
740   struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
741   uint16_t msize;
742   uint16_t emsg_len;
743   
744   msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);  
745   emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
746   msize += emsg_len;
747   msg = GNUNET_malloc (msize);
748   msg->header.size = htons (msize);
749   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONEVENT);
750   msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
751   msg->operation_id = GNUNET_htonll (operation_id);
752   if (0 != emsg_len)
753     memcpy (&msg[1], emsg, emsg_len);
754   queue_message (client, &msg->header);
755 }
756
757
758 /**
759  * The  Link Controller forwarding task
760  *
761  * @param cls the LCFContext
762  * @param tc the Task context from scheduler
763  */
764 static void
765 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
766
767
768 /**
769  * Completion callback for host registrations while forwarding Link Controller messages
770  *
771  * @param cls the LCFContext
772  * @param emsg the error message; NULL if host registration is successful
773  */
774 static void
775 lcf_proc_cc (void *cls, const char *emsg)
776 {
777   struct LCFContext *lcf = cls;
778
779   lcf->rhandle = NULL;
780   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
781   switch (lcf->state)
782   {
783   case INIT:
784     if (NULL != emsg)
785       goto registration_error;
786     lcf->state = DELEGATED_HOST_REGISTERED;
787     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
788     break;
789   case DELEGATED_HOST_REGISTERED:
790      if (NULL != emsg)
791       goto registration_error;
792      lcf->state = SLAVE_HOST_REGISTERED;
793      lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
794      break;
795   default:
796     GNUNET_assert (0);          /* Shouldn't reach here */
797   }  
798   return;
799
800  registration_error:
801   LOG (GNUNET_ERROR_TYPE_WARNING, 
802        "Host registration failed with message: %s\n", emsg);
803   lcf->state = FINISHED;
804   lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
805 }
806
807
808 /**
809  * The  Link Controller forwarding task
810  *
811  * @param cls the LCFContext
812  * @param tc the Task context from scheduler
813  */
814 static void
815 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
816 {
817   struct LCFContext *lcf = cls;
818   struct LCFContextQueue *lcfq;
819
820   lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
821   switch (lcf->state)
822   {
823   case INIT:
824     if (GNUNET_NO ==
825         GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
826                                             lcf->gateway->controller))
827     {
828       lcf->rhandle =
829         GNUNET_TESTBED_register_host (lcf->gateway->controller,
830                                       host_list[lcf->delegated_host_id],
831                                       lcf_proc_cc, lcf);
832     }
833     else
834     {
835       lcf->state = DELEGATED_HOST_REGISTERED;
836       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
837     }
838     break;
839   case DELEGATED_HOST_REGISTERED:
840     if (GNUNET_NO ==
841         GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
842                                             lcf->gateway->controller))
843     {
844       lcf->rhandle =
845         GNUNET_TESTBED_register_host (lcf->gateway->controller,
846                                       host_list[lcf->slave_host_id],
847                                       lcf_proc_cc, lcf);
848     }
849     else
850     {
851       lcf->state = SLAVE_HOST_REGISTERED;
852       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
853     }
854     break;
855   case SLAVE_HOST_REGISTERED:
856     GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
857                                       host_list[lcf->delegated_host_id],
858                                       host_list[lcf->slave_host_id],
859                                       lcf->sxcfg, lcf->sxcfg_size,
860                                       lcf->scfg_size,
861                                       lcf->is_subordinate);
862     lcf->state = FINISHED;
863     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
864     break;
865   case FINISHED:
866     lcfq = lcfq_head;
867     GNUNET_assert (lcfq->lcf == lcf);
868     GNUNET_free (lcf->sxcfg);
869     GNUNET_free (lcf);
870     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
871     GNUNET_free (lcfq);
872     if (NULL != lcfq_head)
873       lcf_proc_task_id = 
874         GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
875   }
876 }
877
878
879 /**
880  * Callback for event from slave controllers
881  *
882  * @param cls struct Slave *
883  * @param event information about the event
884  */
885 static void 
886 slave_event_callback(void *cls,
887                      const struct GNUNET_TESTBED_EventInformation *event)
888 {
889   GNUNET_break (0);
890 }
891
892
893 /**
894  * Callback to signal successfull startup of the controller process
895  *
896  * @param cls the closure from GNUNET_TESTBED_controller_start()
897  * @param cfg the configuration with which the controller has been started;
898  *          NULL if status is not GNUNET_OK
899  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
900  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
901  */
902 static void 
903 slave_status_callback (void *cls, 
904                        const struct GNUNET_CONFIGURATION_Handle *cfg,
905                        int status)
906 {
907   struct Slave *slave = cls;
908
909   if (GNUNET_SYSERR == status)
910   {
911     slave->controller_proc = NULL;
912     LOG (GNUNET_ERROR_TYPE_WARNING,
913          "Unexpected slave shutdown\n");
914     GNUNET_SCHEDULER_shutdown ();       /* We too shutdown */
915     return;
916   }
917   slave->controller =
918     GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
919                                        master_context->event_mask,
920                                        &slave_event_callback, slave);
921 }
922
923
924 /**
925  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
926  *
927  * @param cls NULL
928  * @param client identification of the client
929  * @param message the actual message
930  */
931 static void 
932 handle_init (void *cls,
933              struct GNUNET_SERVER_Client *client,
934              const struct GNUNET_MessageHeader *message)
935 {
936   const struct GNUNET_TESTBED_InitMessage *msg;
937   struct GNUNET_TESTBED_Host *host;
938   const char *controller_hostname;
939   uint16_t msize;
940
941   if (NULL != master_context)
942   {
943     GNUNET_break (0);
944     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
945     return;
946   }
947   msg = (const struct GNUNET_TESTBED_InitMessage *) message;
948   msize = ntohs (message->size);
949   if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
950   {
951     GNUNET_break (0);
952     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
953     return;
954   }
955   msize -= sizeof (struct GNUNET_TESTBED_InitMessage);  
956   controller_hostname = (const char *) &msg[1];
957   if ('\0' != controller_hostname[msize - 1])
958   {
959     GNUNET_break (0);
960     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
961     return;
962   }    
963   master_context = GNUNET_malloc (sizeof (struct Context));
964   master_context->client = client;
965   master_context->host_id = ntohl (msg->host_id);
966   master_context->master_ip = GNUNET_strdup (controller_hostname);  
967   LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
968   master_context->system = 
969     GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
970   host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
971                                              NULL, NULL, 0);
972   host_list_add (host);
973   master_context->event_mask = GNUNET_ntohll (msg->event_mask);
974   GNUNET_SERVER_client_keep (client);
975   LOG_DEBUG ("Created master context with host ID: %u\n",
976              master_context->host_id);
977   GNUNET_SERVER_receive_done (client, GNUNET_OK);
978 }
979
980
981 /**
982  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
983  *
984  * @param cls NULL
985  * @param client identification of the client
986  * @param message the actual message
987  */
988 static void 
989 handle_add_host (void *cls,
990                  struct GNUNET_SERVER_Client *client,
991                  const struct GNUNET_MessageHeader *message)
992 {
993   struct GNUNET_TESTBED_Host *host;
994   const struct GNUNET_TESTBED_AddHostMessage *msg;
995   struct GNUNET_TESTBED_HostConfirmedMessage *reply;
996   char *username;
997   char *hostname;
998   char *emsg;
999   uint32_t host_id;
1000   uint16_t username_length;
1001   uint16_t hostname_length;
1002   uint16_t reply_size;
1003   uint16_t msize;
1004   
1005   msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1006   msize = ntohs (msg->header.size);
1007   username = (char *) &(msg[1]);
1008   username_length = ntohs (msg->user_name_length);
1009   GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1010                           + username_length + 1)); /* msg must contain hostname */
1011   if (0 != username_length)
1012     GNUNET_assert ('\0' == username[username_length]);
1013   username_length = (0 == username_length) ? 0 : username_length + 1;              
1014   hostname = username + username_length;
1015   hostname_length = msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1016                              + username_length);
1017   GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1018   GNUNET_assert (strlen (hostname) == hostname_length - 1);
1019   host_id = ntohl (msg->host_id);
1020   LOG_DEBUG ("Received ADDHOST message\n");
1021   LOG_DEBUG ("-------host id: %u\n", host_id);
1022   if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
1023   if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
1024   else LOG_DEBUG ("-------username: NULL\n");
1025   LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1026   host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1027                                              ntohs (msg->ssh_port));
1028   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1029   reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1030   if (GNUNET_OK != host_list_add (host))
1031   {    
1032     /* We are unable to add a host */  
1033     emsg = "A host exists with given host-id";
1034     LOG_DEBUG ("%s: %u", emsg, host_id);
1035     GNUNET_TESTBED_host_destroy (host);
1036     reply_size += strlen (emsg) + 1;
1037     reply = GNUNET_malloc (reply_size);
1038     memcpy (&reply[1], emsg, strlen (emsg) + 1);
1039   }
1040   else
1041     reply = GNUNET_malloc (reply_size);  
1042   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1043   reply->header.size = htons (reply_size);
1044   reply->host_id = htonl (host_id);  
1045   queue_message (client, &reply->header);
1046 }
1047
1048
1049 /**
1050  * Iterator over hash map entries.
1051  *
1052  * @param cls closure
1053  * @param key current key code
1054  * @param value value in the hash map
1055  * @return GNUNET_YES if we should continue to
1056  *         iterate,
1057  *         GNUNET_NO if not.
1058  */
1059 int ss_exists_iterator (void *cls,
1060                         const struct GNUNET_HashCode * key,
1061                         void *value)
1062 {
1063   struct SharedService *queried_ss = cls;
1064   struct SharedService *ss = value;
1065
1066   if (0 == strcmp (ss->name, queried_ss->name))
1067     return GNUNET_NO;
1068   else
1069     return GNUNET_YES;
1070 }
1071
1072
1073 /**
1074  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1075  *
1076  * @param cls NULL
1077  * @param client identification of the client
1078  * @param message the actual message
1079  */
1080 static void 
1081 handle_configure_shared_service (void *cls,
1082                                  struct GNUNET_SERVER_Client *client,
1083                                  const struct GNUNET_MessageHeader *message)
1084 {
1085   const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1086   struct SharedService *ss;
1087   char *service_name;
1088   struct GNUNET_HashCode hash;
1089   uint16_t msg_size;
1090   uint16_t service_name_size;
1091     
1092   msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1093   msg_size = ntohs (message->size);
1094   if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1095   {
1096     GNUNET_break (0);
1097     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1098     return;
1099   }
1100   service_name_size = msg_size - 
1101     sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1102   service_name = (char *) &msg[1];
1103   if ('\0' != service_name[service_name_size - 1])
1104   {
1105     GNUNET_break (0);
1106     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1107     return;
1108   }
1109   LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1110              service_name, ntohl (msg->num_peers));
1111   if (ntohl (msg->host_id) != master_context->host_id)
1112   {
1113     route_message (ntohl (msg->host_id), message);
1114     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1115     return;
1116   }
1117   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1118   ss = GNUNET_malloc (sizeof (struct SharedService));
1119   ss->name = strdup (service_name);
1120   ss->num_shared = ntohl (msg->num_peers);
1121   GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1122   if (GNUNET_SYSERR == 
1123       GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1124                                                   &ss_exists_iterator, ss))
1125   {
1126     LOG (GNUNET_ERROR_TYPE_WARNING,
1127          "Service %s already configured as a shared service. "
1128          "Ignoring service sharing request \n", ss->name);
1129     GNUNET_free (ss->name);
1130     GNUNET_free (ss);
1131     return;
1132   }
1133   GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1134                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);  
1135 }
1136
1137
1138 /**
1139  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1140  *
1141  * @param cls NULL
1142  * @param client identification of the client
1143  * @param message the actual message
1144  */
1145 static void 
1146 handle_link_controllers (void *cls,
1147                          struct GNUNET_SERVER_Client *client,
1148                          const struct GNUNET_MessageHeader *message)
1149 {
1150   const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1151   struct GNUNET_CONFIGURATION_Handle *cfg;
1152   struct LCFContextQueue *lcfq;
1153   struct Route *route;
1154   struct Route *new_route;
1155   char *config;  
1156   uLongf dest_size;
1157   size_t config_size;
1158   uint32_t delegated_host_id;
1159   uint32_t slave_host_id;
1160   uint16_t msize;
1161    
1162   if (NULL == master_context)
1163   {
1164     GNUNET_break (0);
1165     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1166     return;
1167   }
1168   msize = ntohs (message->size);
1169   if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1170   {
1171     GNUNET_break (0);
1172     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1173     return;
1174   }
1175   msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1176   delegated_host_id = ntohl (msg->delegated_host_id);
1177   if (delegated_host_id == master_context->host_id)
1178   {
1179     GNUNET_break (0);
1180     LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1181     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1182     return;
1183   }
1184   if ((delegated_host_id >= host_list_size) || 
1185       (NULL == host_list[delegated_host_id]))
1186   {
1187     LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1188     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1189     return;
1190   }
1191   slave_host_id = ntohl (msg->slave_host_id);
1192   if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1193   {
1194     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1195     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1196     return;
1197   }
1198   if (slave_host_id == delegated_host_id)
1199   {
1200     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1201     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1202     return;
1203   }
1204   msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1205   config_size = ntohs (msg->config_size);
1206   
1207   if (slave_host_id == master_context->host_id) /* Link from us */
1208   {
1209     struct Slave *slave;
1210
1211     if ((delegated_host_id < slave_list_size) && 
1212         (NULL != slave_list[delegated_host_id])) /* We have already added */
1213     {
1214       LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1215            delegated_host_id);
1216       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1217       return;
1218     }    
1219     config = GNUNET_malloc (config_size);
1220     dest_size = (uLongf) config_size;    
1221     if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1222                             (const Bytef *) &msg[1], (uLong) msize))
1223     {
1224       GNUNET_break (0);           /* Compression error */
1225       GNUNET_free (config);
1226       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1227       return;
1228     }
1229     if (config_size == dest_size)
1230     {
1231       LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1232       GNUNET_free (config);
1233       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1234       return;
1235     }
1236     cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1237     if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1238                                                        GNUNET_NO))
1239     {
1240       GNUNET_break (0);           /* Configuration parsing error */
1241       GNUNET_free (config);
1242       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1243       return;
1244     }
1245     GNUNET_free (config);
1246     if ((delegated_host_id < slave_list_size) &&
1247         (NULL != slave_list[delegated_host_id]))
1248     {
1249       GNUNET_break (0);           /* Configuration parsing error */
1250       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1251       return;
1252     }
1253     slave = GNUNET_malloc (sizeof (struct Slave));
1254     slave->host_id = delegated_host_id;    
1255     slave_list_add (slave);    
1256     if (1 == msg->is_subordinate)
1257     {
1258       slave->controller_proc =
1259         GNUNET_TESTBED_controller_start (master_context->master_ip,
1260                                          host_list[slave->host_id],
1261                                          cfg, &slave_status_callback,
1262                                          slave);
1263     }
1264     else {
1265       slave->controller = 
1266         GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1267                                            master_context->event_mask,
1268                                            &slave_event_callback, slave);
1269     }
1270     GNUNET_CONFIGURATION_destroy (cfg);
1271     new_route = GNUNET_malloc (sizeof (struct Route));
1272     new_route->dest = delegated_host_id;
1273     new_route->thru = master_context->host_id;
1274     route_list_add (new_route);
1275     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1276     return;
1277   }
1278
1279   /* Route the request */
1280   if (slave_host_id >= route_list_size)
1281   {
1282     LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1283     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1284     return;
1285   }
1286   lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1287   lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1288   lcfq->lcf->delegated_host_id = delegated_host_id;
1289   lcfq->lcf->slave_host_id = slave_host_id;
1290   while (NULL != (route = route_list[slave_host_id]))
1291   {
1292     if (route->thru == master_context->host_id)
1293       break;
1294     slave_host_id = route->thru;
1295   }
1296   GNUNET_assert (NULL != route); /* because we add routes carefully */
1297   GNUNET_assert (route->dest < slave_list_size);
1298   GNUNET_assert (NULL != slave_list[route->dest]);  
1299   lcfq->lcf->is_subordinate =
1300     (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1301   lcfq->lcf->state = INIT;
1302   lcfq->lcf->gateway = slave_list[route->dest];
1303   lcfq->lcf->sxcfg_size = msize;
1304   lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1305   lcfq->lcf->scfg_size = config_size;
1306   (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1307   if (NULL == lcfq_head)
1308   {
1309     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1310     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1311     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1312   }
1313   else
1314     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1315   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1316   new_route = GNUNET_malloc (sizeof (struct Route));
1317   new_route->dest = delegated_host_id;
1318   new_route->thru = route->dest;
1319   route_list_add (new_route);
1320 }
1321
1322
1323 /**
1324  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1325  *
1326  * @param cls NULL
1327  * @param client identification of the client
1328  * @param message the actual message
1329  */
1330 static void 
1331 handle_peer_create (void *cls,
1332                     struct GNUNET_SERVER_Client *client,
1333                     const struct GNUNET_MessageHeader *message)
1334 {
1335   const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1336   struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1337   struct GNUNET_CONFIGURATION_Handle *cfg;
1338   char *config;
1339   size_t dest_size;
1340   int ret;
1341   uint32_t config_size;
1342   uint16_t msize;
1343   
1344
1345   msize = ntohs (message->size);
1346   if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1347   {
1348     GNUNET_break (0);           /* We need configuration */
1349     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1350     return;
1351   }
1352   msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1353   if (ntohl (msg->host_id) == master_context->host_id)
1354   {
1355     struct Peer *peer;
1356     char *emsg;
1357     
1358     /* We are responsible for this peer */
1359     msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1360     config_size = ntohl (msg->config_size);    
1361     config = GNUNET_malloc (config_size);
1362     dest_size = config_size;
1363     if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1364                                    (const Bytef *) &msg[1], (uLong) msize)))
1365     {
1366       GNUNET_break (0);           /* uncompression error */
1367       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1368       return;
1369     }
1370     if (config_size != dest_size)
1371     {
1372       GNUNET_break (0);/* Uncompressed config size mismatch */
1373       GNUNET_free (config);
1374       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1375       return;
1376     }
1377     cfg = GNUNET_CONFIGURATION_create ();
1378     if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1379                                                        GNUNET_NO))
1380     {
1381       GNUNET_break (0);           /* Configuration parsing error */
1382       GNUNET_free (config);
1383       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1384       return;
1385     }
1386     GNUNET_free (config);
1387     peer = GNUNET_malloc (sizeof (struct Peer));
1388     peer->cfg = cfg;
1389     peer->id = ntohl (msg->peer_id);
1390     LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1391     peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1392                                                 peer->id,
1393                                                 NULL /* Peer id */,
1394                                                 &emsg);
1395     if (NULL == peer->peer)
1396     {
1397       LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1398       GNUNET_free (emsg);
1399       GNUNET_free (peer);
1400       GNUNET_break (0);
1401       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1402       return;
1403     }
1404     peer_list_add (peer);
1405     reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1406     reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1407     reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1408     reply->peer_id = msg->peer_id;
1409     reply->operation_id = msg->operation_id;
1410     queue_message (client, &reply->header);
1411     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1412     return;
1413   }
1414
1415   /* FIXME: Forward the peer to other host */
1416   GNUNET_break (0);
1417   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1418 }
1419
1420
1421 /**
1422  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1423  *
1424  * @param cls NULL
1425  * @param client identification of the client
1426  * @param message the actual message
1427  */
1428 static void 
1429 handle_peer_destroy (void *cls,
1430                      struct GNUNET_SERVER_Client *client,
1431                      const struct GNUNET_MessageHeader *message)
1432 {
1433   const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1434   struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1435   struct Peer *peer;
1436   uint32_t peer_id;
1437   uint16_t reply_size;
1438   
1439   msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1440   peer_id = ntohl (msg->peer_id);
1441   LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1442              peer_id, GNUNET_ntohll (msg->operation_id));  
1443   if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1444   {
1445     GNUNET_break (0);
1446     /* FIXME: Reply with failure event message or forward to slave controller */
1447     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1448     return;
1449   }
1450   peer = peer_list[peer_id];
1451   GNUNET_TESTING_peer_destroy (peer->peer);
1452   GNUNET_CONFIGURATION_destroy (peer->cfg);
1453   peer_list_remove (peer);
1454   GNUNET_free (peer);
1455   reply_size = 
1456     sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1457   reply = GNUNET_malloc (reply_size);
1458   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1459   reply->header.size = htons (reply_size);
1460   reply->operation_id = msg->operation_id;
1461   reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1462   queue_message (client, &reply->header);
1463   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1464 }
1465
1466
1467 /**
1468  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1469  *
1470  * @param cls NULL
1471  * @param client identification of the client
1472  * @param message the actual message
1473  */
1474 static void 
1475 handle_peer_start (void *cls,
1476                    struct GNUNET_SERVER_Client *client,
1477                    const struct GNUNET_MessageHeader *message)
1478 {
1479   const struct GNUNET_TESTBED_PeerStartMessage *msg;
1480   struct GNUNET_TESTBED_PeerEventMessage *reply;
1481   uint32_t peer_id;
1482
1483   msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1484   peer_id = ntohl (msg->peer_id);
1485   if ((peer_id >= peer_list_size) 
1486       || (NULL == peer_list[peer_id]))
1487   {
1488     GNUNET_break (0);
1489     /* FIXME: reply with failure message or forward to slave controller */
1490     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1491     return;
1492   }
1493   if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1494   {
1495     /* FIXME: return FAILURE message */
1496     GNUNET_break (0);
1497     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1498     return;
1499   }
1500   reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1501   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1502   reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1503   reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1504   reply->host_id = htonl (master_context->host_id);
1505   reply->peer_id = msg->peer_id;
1506   reply->operation_id = msg->operation_id;
1507   queue_message (client, &reply->header);
1508   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1509 }
1510
1511
1512 /**
1513  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1514  *
1515  * @param cls NULL
1516  * @param client identification of the client
1517  * @param message the actual message
1518  */
1519 static void 
1520 handle_peer_stop (void *cls,
1521                   struct GNUNET_SERVER_Client *client,
1522                   const struct GNUNET_MessageHeader *message)
1523 {
1524   const struct GNUNET_TESTBED_PeerStopMessage *msg;
1525   struct GNUNET_TESTBED_PeerEventMessage *reply;
1526   uint32_t peer_id;
1527
1528   msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1529   peer_id = ntohl (msg->peer_id);
1530   if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1531   {
1532     GNUNET_break (0);           /* FIXME: route to slave? */
1533     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1534     return;
1535   }
1536   if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1537   {
1538     /* FIXME: return FAILURE message */
1539     GNUNET_break (0);
1540     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1541     return;
1542   }
1543   reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1544   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1545   reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1546   reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1547   reply->host_id = htonl (master_context->host_id);
1548   reply->peer_id = msg->peer_id;
1549   reply->operation_id = msg->operation_id;
1550   queue_message (client, &reply->header);
1551   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1552 }
1553
1554
1555 /**
1556  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1557  *
1558  * @param cls NULL
1559  * @param client identification of the client
1560  * @param message the actual message
1561  */
1562 static void 
1563 handle_peer_get_config (void *cls,
1564                         struct GNUNET_SERVER_Client *client,
1565                         const struct GNUNET_MessageHeader *message)
1566 {
1567   const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1568   struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1569   char *config;
1570   char *xconfig;
1571   size_t c_size;
1572   size_t xc_size;  
1573   uint32_t peer_id;
1574   uint16_t msize;
1575   
1576   msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1577   peer_id = ntohl (msg->peer_id);
1578   if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1579   {
1580     /* FIXME: return FAILURE message */
1581     GNUNET_break (0);
1582     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1583   }
1584   config = GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->cfg,
1585                                            &c_size);
1586   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1587   GNUNET_free (config);
1588   msize = xc_size + sizeof (struct
1589                             GNUNET_TESTBED_PeerConfigurationInformationMessage);
1590   reply = GNUNET_realloc (xconfig, msize);
1591   (void) memmove (&reply[1], reply, xc_size);
1592   reply->header.size = htons (msize);
1593   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1594   reply->peer_id = msg->peer_id;
1595   reply->operation_id = msg->operation_id;
1596   GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->peer,
1597                                     &reply->peer_identity);
1598   reply->config_size = htons ((uint16_t) c_size);
1599   queue_message (client, &reply->header);
1600   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1601 }
1602
1603
1604 /**
1605  * Task for cleaing up overlay connect context structure
1606  *
1607  * @param cls the overlay connect context
1608  * @param tc the task context
1609  */
1610 static void
1611 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1612 {
1613   struct OverlayConnectContext *occ = cls;
1614
1615   GNUNET_free_non_null (occ->emsg);
1616   GNUNET_free_non_null (occ->hello);
1617   if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1618     GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1619   if (NULL != occ->ch)
1620     GNUNET_CORE_disconnect (occ->ch);
1621   if (NULL != occ->ghh)
1622     GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1623   if (NULL != occ->p1th)
1624     GNUNET_TRANSPORT_disconnect (occ->p1th);
1625   if (NULL != occ->p2th)
1626     GNUNET_TRANSPORT_disconnect (occ->p2th);
1627   GNUNET_free (occ);
1628 }
1629
1630
1631 /**
1632  * Task which will be run when overlay connect request has been timed out
1633  *
1634  * @param 
1635  * @return 
1636  */
1637 static void
1638 timeout_overlay_connect (void *cls,
1639                          const struct GNUNET_SCHEDULER_TaskContext *tc)
1640 {
1641   struct OverlayConnectContext *occ = cls;
1642
1643   occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1644   send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
1645   GNUNET_SERVER_client_drop (occ->client);
1646   occ_cleanup (occ, tc);
1647 }
1648
1649
1650
1651 /**
1652  * Function called to notify transport users that another
1653  * peer connected to us.
1654  *
1655  * @param cls closure
1656  * @param new_peer the peer that connected
1657  * @param ats performance data
1658  * @param ats_count number of entries in ats (excluding 0-termination)
1659  */
1660 static void 
1661 overlay_connect_notify (void *cls,
1662                         const struct GNUNET_PeerIdentity * new_peer,
1663                         const struct GNUNET_ATS_Information * ats,
1664                         unsigned int ats_count)
1665 {
1666   struct OverlayConnectContext *occ = cls;
1667   struct GNUNET_TESTBED_ConnectionEventMessage *msg;
1668   char *new_peer_str;
1669   char *other_peer_str;
1670
1671   LOG_DEBUG ("Overlay connect notify\n");
1672   if (0 == memcmp (new_peer, &occ->peer_identity, 
1673                    sizeof (struct GNUNET_PeerIdentity)))
1674     return;
1675   new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
1676   other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1677   if (0 != memcmp (new_peer, &occ->other_peer_identity,
1678                    sizeof (struct GNUNET_PeerIdentity)))
1679   {
1680     LOG_DEBUG ("Unexpected peer %4s connected to peer %4s\n",
1681                new_peer_str, other_peer_str);
1682     GNUNET_free (new_peer_str);
1683     GNUNET_free (other_peer_str);
1684     return;
1685   }
1686   LOG_DEBUG ("Peer %4s connected to peer %4s\n", new_peer_str, other_peer_str);
1687   GNUNET_free (new_peer_str);
1688   GNUNET_free (other_peer_str);
1689   if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1690   {
1691     GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1692     occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1693   }
1694   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
1695   GNUNET_SCHEDULER_cancel (occ->timeout_task);
1696   occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1697   GNUNET_free_non_null (occ->emsg);
1698   occ->emsg = NULL;
1699   GNUNET_TRANSPORT_disconnect (occ->p1th);
1700   occ->p1th = NULL;
1701   /* Peer 1 has connected connect to peer2 - now send overlay connect success message */
1702   LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
1703   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
1704   msg->header.size = htons (sizeof (struct
1705                                     GNUNET_TESTBED_ConnectionEventMessage));
1706   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
1707   msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
1708   msg->peer1 = htonl (occ->peer->id);
1709   msg->peer2 = htonl (occ->other_peer->id);
1710   msg->operation_id = GNUNET_htonll (occ->op_id);
1711   queue_message (occ->client, &msg->header);
1712   GNUNET_SERVER_client_drop (occ->client);
1713   GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
1714 }
1715
1716
1717 static void
1718 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1719 {
1720   struct OverlayConnectContext *occ = cls;
1721   char *other_peer_str;
1722
1723   occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1724   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1725     return;
1726   GNUNET_assert (NULL != occ->hello);
1727   other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1728   LOG_DEBUG ("Offering HELLO of %s to %s\n", other_peer_str, GNUNET_i2s (&occ->peer_identity));
1729   GNUNET_free (other_peer_str);
1730   GNUNET_TRANSPORT_offer_hello (occ->p1th, occ->hello, NULL, NULL);
1731   GNUNET_TRANSPORT_try_connect (occ->p1th, &occ->other_peer_identity);
1732   occ->send_hello_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1733                                                        &send_hello, occ);
1734 }
1735
1736 /**
1737  * Test for checking whether HELLO message is empty
1738  *
1739  * @param cls empty flag to set
1740  * @param address the HELLO
1741  * @param expiration expiration of the HELLO
1742  * @return 
1743  */
1744 static int
1745 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
1746               struct GNUNET_TIME_Absolute expiration)
1747 {
1748   int *empty = cls;
1749
1750   *empty = GNUNET_NO;
1751   return GNUNET_OK;
1752 }
1753
1754
1755 /**
1756  * Function called whenever there is an update to the
1757  * HELLO of peers in the OverlayConnectClosure
1758  *
1759  * @param cls closure
1760  * @param hello our updated HELLO
1761  */
1762 static void 
1763 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
1764 {
1765   struct OverlayConnectContext *occ = cls;
1766   int empty;
1767   uint16_t msize;
1768   
1769   msize = ntohs (hello->size);
1770   if (msize < 0)
1771   {
1772     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1773                 "HELLO message of peer %s is of size 0\n",
1774                 &occ->other_peer_identity);
1775     return;
1776   }
1777   empty = GNUNET_YES;
1778   (void) 
1779     GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) hello,
1780                                     GNUNET_NO, &test_address, &empty);
1781   if (GNUNET_YES == empty)
1782   {
1783     LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->other_peer_identity));
1784     return;
1785   }
1786   LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->other_peer_identity));
1787   occ->hello = GNUNET_malloc (msize);
1788   memcpy (occ->hello, hello, msize);
1789   GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1790   occ->ghh = NULL;
1791   GNUNET_TRANSPORT_disconnect (occ->p2th);
1792   occ->p2th = NULL;
1793   GNUNET_free_non_null (occ->emsg);  
1794   occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
1795   occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
1796 }
1797
1798
1799 /**
1800  * Function called after GNUNET_CORE_connect has succeeded (or failed
1801  * for good).  Note that the private key of the peer is intentionally
1802  * not exposed here; if you need it, your process should try to read
1803  * the private key file directly (which should work if you are
1804  * authorized...).
1805  *
1806  * @param cls closure
1807  * @param server handle to the server, NULL if we failed
1808  * @param my_identity ID of this peer, NULL if we failed
1809  */
1810 static void 
1811 core_startup_cb (void *cls, struct GNUNET_CORE_Handle * server,
1812                  const struct GNUNET_PeerIdentity *my_identity)
1813 {
1814   struct OverlayConnectContext *occ = cls;
1815
1816   GNUNET_free_non_null (occ->emsg);
1817   occ->emsg = NULL;
1818   memcpy (&occ->peer_identity, my_identity, sizeof (struct GNUNET_PeerIdentity));
1819   occ->p1th =
1820     GNUNET_TRANSPORT_connect (occ->peer->cfg, &occ->peer_identity, NULL, NULL,
1821                               NULL, NULL);
1822   /* Connect to the transport of 2nd peer and get its HELLO message */
1823   GNUNET_TESTING_peer_get_identity (occ->other_peer->peer,
1824                                     &occ->other_peer_identity);
1825   occ->p2th = 
1826     GNUNET_TRANSPORT_connect (occ->other_peer->cfg, &occ->other_peer_identity,
1827                               NULL, NULL, NULL, NULL);
1828   if ((NULL == occ->p1th) || (NULL == occ->p2th))
1829   {
1830     occ->emsg = GNUNET_strdup ("Cannot connect to TRANSPORTs of peers");
1831     goto send_failure;
1832   }
1833   LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s
1834              (&occ->other_peer_identity));
1835   occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
1836   occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p2th, &hello_update_cb, occ);
1837   return;
1838
1839  send_failure:
1840   GNUNET_SCHEDULER_cancel (occ->timeout_task);
1841   occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);  
1842 }
1843
1844
1845 /**
1846  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
1847  *
1848  * @param cls NULL
1849  * @param client identification of the client
1850  * @param message the actual message
1851  */
1852 static void 
1853 handle_overlay_connect (void *cls,
1854                         struct GNUNET_SERVER_Client *client,
1855                         const struct GNUNET_MessageHeader *message)
1856 {
1857   const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
1858   struct OverlayConnectContext *occ;
1859   struct GNUNET_CORE_MessageHandler no_handlers[] = {
1860     {NULL, 0, 0}
1861   };
1862   uint32_t p1;
1863   uint32_t p2;
1864
1865   msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
1866   p1 = ntohl (msg->peer1);
1867   p2 = ntohl (msg->peer2);
1868   GNUNET_assert (p1 < peer_list_size);
1869   GNUNET_assert (NULL != peer_list[p1]);
1870   GNUNET_assert (p2 < peer_list_size);
1871   GNUNET_assert (NULL != peer_list[p2]);
1872   occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
1873   GNUNET_SERVER_client_keep (client);
1874   occ->client = client;
1875   occ->state = OCC_STATE_INIT;
1876   occ->peer = peer_list[p1];
1877   occ->other_peer = peer_list[p2];
1878   occ->op_id = GNUNET_ntohll (msg->operation_id);
1879   occ->timeout_task =
1880     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1881                                   (GNUNET_TIME_UNIT_SECONDS, 30),
1882                                   &timeout_overlay_connect, occ);   
1883   /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
1884   occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
1885   occ->ch = 
1886     GNUNET_CORE_connect (occ->peer->cfg, occ, &core_startup_cb,
1887                          &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
1888                          GNUNET_NO, no_handlers);
1889   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1890 }
1891
1892
1893 /**
1894  * Iterator over hash map entries.
1895  *
1896  * @param cls closure
1897  * @param key current key code
1898  * @param value value in the hash map
1899  * @return GNUNET_YES if we should continue to
1900  *         iterate,
1901  *         GNUNET_NO if not.
1902  */
1903 static int 
1904 ss_map_free_iterator (void *cls,
1905                       const struct GNUNET_HashCode * key, void *value)
1906 {
1907   struct SharedService *ss = value;
1908
1909   GNUNET_assert (GNUNET_YES ==
1910                  GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1911   GNUNET_free (ss->name);
1912   GNUNET_free (ss);
1913   return GNUNET_YES;
1914 }
1915
1916
1917 /**
1918  * Task to clean up and shutdown nicely
1919  *
1920  * @param cls NULL
1921  * @param tc the TaskContext from scheduler
1922  */
1923 static void
1924 shutdown_task (void *cls,
1925                const struct GNUNET_SCHEDULER_TaskContext *tc)
1926 {
1927   struct LCFContextQueue *lcfq;
1928   uint32_t id;
1929
1930   shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1931   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1932   (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1933                                                 NULL);
1934   GNUNET_CONTAINER_multihashmap_destroy (ss_map);  
1935   if (NULL != lcfq_head)
1936   {
1937     if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1938     {
1939       GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1940       lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1941     }
1942     if (NULL != lcfq_head->lcf->rhandle)
1943       GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1944   }
1945   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1946   for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1947   {
1948     GNUNET_free (lcfq->lcf->sxcfg);
1949     GNUNET_free (lcfq->lcf);
1950     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1951     GNUNET_free (lcfq);
1952   }
1953   /* Clear peer list */
1954   for (id = 0; id < peer_list_size; id++)
1955     if (NULL != peer_list[id])
1956     {
1957       GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1958       GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1959       GNUNET_free (peer_list[id]);
1960     }
1961   GNUNET_free_non_null (peer_list);
1962   /* Clear host list */
1963   for (id = 0; id < host_list_size; id++)
1964     if (NULL != host_list[id])
1965       GNUNET_TESTBED_host_destroy (host_list[id]);
1966   GNUNET_free_non_null (host_list);
1967   /* Clear route list */
1968   for (id = 0; id < route_list_size; id++)
1969     if (NULL != route_list[id])
1970       GNUNET_free (route_list[id]);
1971   GNUNET_free_non_null (route_list);
1972   /* Clear slave_list */
1973   for (id = 0; id < slave_list_size; id++)
1974     if (NULL != slave_list[id])
1975     {
1976       GNUNET_assert (NULL != slave_list[id]->controller);
1977       GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1978       if (NULL != slave_list[id]->controller_proc)
1979         GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1980     }
1981   if (NULL != master_context)
1982   {  
1983     GNUNET_free_non_null (master_context->master_ip);
1984     if (NULL != master_context->system)
1985       GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1986     GNUNET_free (master_context);
1987     master_context = NULL;
1988   }
1989 }
1990
1991
1992 /**
1993  * Callback for client disconnect
1994  *
1995  * @param cls NULL
1996  * @param client the client which has disconnected
1997  */
1998 static void
1999 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2000 {
2001   if (NULL == master_context)
2002     return;
2003   if (client == master_context->client)
2004   {
2005     LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2006     GNUNET_SERVER_client_drop (client);
2007     /* should not be needed as we're terminated by failure to read
2008        from stdin, but if stdin fails for some reason, this shouldn't 
2009        hurt for now --- might need to revise this later if we ever
2010        decide that master connections might be temporarily down 
2011        for some reason */
2012     //GNUNET_SCHEDULER_shutdown ();
2013   }
2014 }
2015
2016
2017 /**
2018  * Testbed setup
2019  *
2020  * @param cls closure
2021  * @param server the initialized server
2022  * @param cfg configuration to use
2023  */
2024 static void 
2025 testbed_run (void *cls,
2026              struct GNUNET_SERVER_Handle *server,
2027              const struct GNUNET_CONFIGURATION_Handle *cfg)
2028 {
2029   static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
2030     {
2031       {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2032       {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2033       {&handle_configure_shared_service, NULL,
2034        GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2035       {&handle_link_controllers, NULL,
2036        GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2037       {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2038       {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2039        sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2040       {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2041        sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2042       {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2043        sizeof (struct GNUNET_TESTBED_PeerStopMessage)},      
2044       {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2045        sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2046       {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2047        sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2048       {NULL}
2049     };
2050
2051   GNUNET_SERVER_add_handlers (server,
2052                               message_handlers);
2053   GNUNET_SERVER_disconnect_notify (server,
2054                                    &client_disconnect_cb,
2055                                    NULL);
2056   ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2057   shutdown_task_id = 
2058     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2059                                   &shutdown_task,
2060                                   NULL);
2061   LOG_DEBUG ("Testbed startup complete\n");
2062 }
2063
2064
2065 /**
2066  * The starting point of execution
2067  */
2068 int main (int argc, char *const *argv)
2069 {
2070   //sleep (30);                 /* Debugging */
2071   return
2072     (GNUNET_OK ==
2073      GNUNET_SERVICE_run (argc,
2074                          argv,
2075                          "testbed",
2076                          GNUNET_SERVICE_OPTION_NONE,
2077                          &testbed_run,
2078                          NULL)) ? 0 : 1;
2079 }