2 This file is part of GNUnet.
3 (C) 2008--2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file testbed/gnunet-service-testbed.c
23 * @brief implementation of the TESTBED service
24 * @author Sree Harsha Totakura
27 #include "gnunet-service-testbed.h"
37 struct GNUNET_CONFIGURATION_Handle *our_config;
40 * The master context; generated with the first INIT message
42 struct Context *GST_context;
45 * A list of directly linked neighbours
47 struct Slave **GST_slave_list;
52 struct GNUNET_TESTBED_Host **GST_host_list;
55 * DLL head for forwarded operation contexts
57 struct ForwardedOperationContext *fopcq_head;
60 * DLL tail for forwarded operation contexts
62 struct ForwardedOperationContext *fopcq_tail;
65 * Operation queue for open file descriptors
67 struct OperationQueue *GST_opq_openfds;
70 * Timeout for operations which may take some time
72 const struct GNUNET_TIME_Relative GST_timeout;
75 * The size of the host list
77 unsigned int GST_host_list_size;
80 * The size of directly linked neighbours list
82 unsigned int GST_slave_list_size;
85 * The size of the peer list
87 unsigned int GST_peer_list_size;
90 /***********************************/
91 /* Local definitions and variables */
92 /***********************************/
95 * The message queue for sending messages to clients
100 * The message to be sent
102 struct GNUNET_MessageHeader *msg;
105 * The client to send the message to
107 struct GNUNET_SERVER_Client *client;
110 * next pointer for DLL
112 struct MessageQueue *next;
115 * prev pointer for DLL
117 struct MessageQueue *prev;
121 * Our hostname; we give this to all the peers we start
123 static char *hostname;
126 * Current Transmit Handle; NULL if no notify transmit exists currently
128 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
131 * The head for the LCF queue
133 static struct LCFContextQueue *lcfq_head;
136 * The tail for the LCF queue
138 static struct LCFContextQueue *lcfq_tail;
141 * The message queue head
143 static struct MessageQueue *mq_head;
146 * The message queue tail
148 static struct MessageQueue *mq_tail;
151 * The hashmap of shared services
153 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
158 static struct Route **route_list;
161 * The event mask for the events we listen from sub-controllers
163 static uint64_t event_mask;
166 * The size of the route list
168 static unsigned int route_list_size;
171 * The lcf_task handle
173 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
176 * The shutdown task handle
178 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
182 * Function called to notify a client about the connection begin ready to queue
183 * more data. "buf" will be NULL and "size" zero if the connection was closed
184 * for writing in the meantime.
187 * @param size number of bytes available in buf
188 * @param buf where the callee should write the message
189 * @return number of bytes written to buf
192 transmit_ready_notify (void *cls, size_t size, void *buf)
194 struct MessageQueue *mq_entry;
196 transmit_handle = NULL;
198 GNUNET_assert (NULL != mq_entry);
201 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
202 size = ntohs (mq_entry->msg->size);
203 memcpy (buf, mq_entry->msg, size);
204 GNUNET_free (mq_entry->msg);
205 GNUNET_SERVER_client_drop (mq_entry->client);
206 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
207 GNUNET_free (mq_entry);
209 if (NULL != mq_entry)
211 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
212 ntohs (mq_entry->msg->size),
213 GNUNET_TIME_UNIT_FOREVER_REL,
214 &transmit_ready_notify, NULL);
220 * Queues a message in send queue for sending to the service
222 * @param client the client to whom the queued message has to be sent
223 * @param msg the message to queue
226 GST_queue_message (struct GNUNET_SERVER_Client *client,
227 struct GNUNET_MessageHeader *msg)
229 struct MessageQueue *mq_entry;
233 type = ntohs (msg->type);
234 size = ntohs (msg->size);
235 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
236 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
237 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
239 mq_entry->client = client;
240 GNUNET_SERVER_client_keep (client);
241 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
243 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
244 if (NULL == transmit_handle)
246 GNUNET_SERVER_notify_transmit_ready (client, size,
247 GNUNET_TIME_UNIT_FOREVER_REL,
248 &transmit_ready_notify, NULL);
253 * Function to add a host to the current list of known hosts
255 * @param host the host to add
256 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
260 host_list_add (struct GNUNET_TESTBED_Host *host)
264 host_id = GNUNET_TESTBED_host_get_id_ (host);
265 if (GST_host_list_size <= host_id)
266 GST_array_grow_large_enough (GST_host_list, GST_host_list_size, host_id);
267 if (NULL != GST_host_list[host_id])
269 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
270 return GNUNET_SYSERR;
272 GST_host_list[host_id] = host;
278 * Adds a route to the route list
280 * @param route the route to add
283 route_list_add (struct Route *route)
285 if (route->dest >= route_list_size)
286 GST_array_grow_large_enough (route_list, route_list_size, route->dest);
287 GNUNET_assert (NULL == route_list[route->dest]);
288 route_list[route->dest] = route;
293 * Adds a slave to the slave array
295 * @param slave the slave controller to add
298 slave_list_add (struct Slave *slave)
300 if (slave->host_id >= GST_slave_list_size)
301 GST_array_grow_large_enough (GST_slave_list, GST_slave_list_size,
303 GNUNET_assert (NULL == GST_slave_list[slave->host_id]);
304 GST_slave_list[slave->host_id] = slave;
309 * Finds the route with directly connected host as destination through which
310 * the destination host can be reached
312 * @param host_id the id of the destination host
313 * @return the route with directly connected destination host; NULL if no route
317 GST_find_dest_route (uint32_t host_id)
321 if (route_list_size <= host_id)
323 while (NULL != (route = route_list[host_id]))
325 if (route->thru == GST_context->host_id)
327 host_id = route->thru;
334 * Routes message to a host given its host_id
336 * @param host_id the id of the destination host
337 * @param msg the message to be routed
340 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
347 * Send operation failure message to client
349 * @param client the client to which the failure message has to be sent to
350 * @param operation_id the id of the failed operation
351 * @param emsg the error message; can be NULL
354 GST_send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
355 uint64_t operation_id, const char *emsg)
357 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
361 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
362 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
364 msg = GNUNET_malloc (msize);
365 msg->header.size = htons (msize);
366 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATION_FAIL_EVENT);
367 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
368 msg->operation_id = GNUNET_htonll (operation_id);
370 memcpy (&msg[1], emsg, emsg_len);
371 GST_queue_message (client, &msg->header);
376 * Function to send generic operation success message to given client
378 * @param client the client to send the message to
379 * @param operation_id the id of the operation which was successful
382 GST_send_operation_success_msg (struct GNUNET_SERVER_Client *client,
383 uint64_t operation_id)
385 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
388 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
389 msg = GNUNET_malloc (msize);
390 msg->header.size = htons (msize);
392 htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERIC_OPERATION_SUCCESS);
393 msg->operation_id = GNUNET_htonll (operation_id);
394 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
395 GST_queue_message (client, &msg->header);
400 * Function to send a failure reponse for controller link operation
402 * @param client the client to send the message to
403 * @param operation_id the operation ID of the controller link request
404 * @param cfg the configuration with which the delegated controller is started.
405 * Can be NULL if the delegated controller is not started but just
407 * @param emsg set to an error message explaining why the controller link
408 * failed. Setting this to NULL signifies success. !This should be
409 * NULL if cfg is set!
412 send_controller_link_response (struct GNUNET_SERVER_Client *client,
413 uint64_t operation_id,
414 const struct GNUNET_CONFIGURATION_Handle
418 struct GNUNET_TESTBED_ControllerLinkResponse *msg;
424 GNUNET_assert ((NULL == cfg) || (NULL == emsg));
428 msize = sizeof (struct GNUNET_TESTBED_ControllerLinkResponse);
431 xconfig = GNUNET_TESTBED_compress_cfg_ (cfg,
434 msize += xconfig_size;
437 msize += strlen (emsg);
438 msg = GNUNET_malloc (msize);
439 msg->header.type = htons
440 (GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS_RESULT);
441 msg->header.size = htons (msize);
443 msg->success = htons (GNUNET_YES);
444 msg->operation_id = GNUNET_htonll (operation_id);
445 msg->config_size = htons ((uint16_t) config_size);
447 memcpy (&msg[1], xconfig, xconfig_size);
449 memcpy (&msg[1], emsg, strlen (emsg));
450 GST_queue_message (client, &msg->header);
454 * Callback which will be called after a host registration succeeded or failed
456 * @param cls the handle to the slave at which the registration is completed
457 * @param emsg the error message; NULL if host registration is successful
460 hr_completion (void *cls, const char *emsg);
464 * Attempts to register the next host in the host registration queue
466 * @param slave the slave controller whose host registration queue is checked
467 * for host registrations
470 register_next_host (struct Slave *slave)
472 struct HostRegistration *hr;
474 hr = slave->hr_dll_head;
475 GNUNET_assert (NULL != hr);
476 GNUNET_assert (NULL == slave->rhandle);
477 LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering host %u at %u\n",
478 GNUNET_TESTBED_host_get_id_ (hr->host),
479 GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
481 GNUNET_TESTBED_register_host (slave->controller, hr->host, hr_completion,
487 * Callback which will be called to after a host registration succeeded or failed
489 * @param cls the handle to the slave at which the registration is completed
490 * @param emsg the error message; NULL if host registration is successful
493 hr_completion (void *cls, const char *emsg)
495 struct Slave *slave = cls;
496 struct HostRegistration *hr;
498 slave->rhandle = NULL;
499 hr = slave->hr_dll_head;
500 GNUNET_assert (NULL != hr);
501 LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering host %u at %u successful\n",
502 GNUNET_TESTBED_host_get_id_ (hr->host),
503 GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
504 GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head, slave->hr_dll_tail, hr);
506 hr->cb (hr->cb_cls, emsg);
508 if (NULL != slave->hr_dll_head)
509 register_next_host (slave);
514 * Adds a host registration's request to a slave's registration queue
516 * @param slave the slave controller at which the given host has to be
518 * @param cb the host registration completion callback
519 * @param cb_cls the closure for the host registration completion callback
520 * @param host the host which has to be registered
523 GST_queue_host_registration (struct Slave *slave,
524 GNUNET_TESTBED_HostRegistrationCompletion cb,
525 void *cb_cls, struct GNUNET_TESTBED_Host *host)
527 struct HostRegistration *hr;
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "Queueing host registration for host %u at %u\n",
532 GNUNET_TESTBED_host_get_id_ (host),
533 GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
534 hr = GNUNET_malloc (sizeof (struct HostRegistration));
538 call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO;
539 GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head, slave->hr_dll_tail, hr);
540 if (GNUNET_YES == call_register)
541 register_next_host (slave);
546 * The Link Controller forwarding task
548 * @param cls the LCFContext
549 * @param tc the Task context from scheduler
552 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
556 * Completion callback for host registrations while forwarding Link Controller messages
558 * @param cls the LCFContext
559 * @param emsg the error message; NULL if host registration is successful
562 lcf_proc_cc (void *cls, const char *emsg)
564 struct LCFContext *lcf = cls;
566 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
571 goto registration_error;
572 lcf->state = DELEGATED_HOST_REGISTERED;
573 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
575 case DELEGATED_HOST_REGISTERED:
577 goto registration_error;
578 lcf->state = SLAVE_HOST_REGISTERED;
579 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
582 GNUNET_assert (0); /* Shouldn't reach here */
587 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
589 lcf->state = FINISHED;
590 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
595 * Callback to relay the reply msg of a forwarded operation back to the client
597 * @param cls ForwardedOperationContext
598 * @param msg the message to relay
601 GST_forwarded_operation_reply_relay (void *cls,
602 const struct GNUNET_MessageHeader *msg)
604 struct ForwardedOperationContext *fopc = cls;
605 struct GNUNET_MessageHeader *dup_msg;
608 msize = ntohs (msg->size);
609 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
611 dup_msg = GNUNET_copy_message (msg);
612 GST_queue_message (fopc->client, dup_msg);
613 GNUNET_SERVER_client_drop (fopc->client);
614 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
615 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
621 * Task to free resources when forwarded operation has been timedout
623 * @param cls the ForwardedOperationContext
624 * @param tc the task context from scheduler
627 GST_forwarded_operation_timeout (void *cls,
628 const struct GNUNET_SCHEDULER_TaskContext *tc)
630 struct ForwardedOperationContext *fopc = cls;
632 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
633 LOG (GNUNET_ERROR_TYPE_DEBUG, "A forwarded operation has timed out\n");
634 GST_send_operation_fail_msg (fopc->client, fopc->operation_id,
635 "A forwarded operation has timed out");
636 GNUNET_SERVER_client_drop (fopc->client);
637 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
643 * The Link Controller forwarding task
645 * @param cls the LCFContext
646 * @param tc the Task context from scheduler
649 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
653 * Task to free resources when forwarded link controllers has been timedout
655 * @param cls the LCFContext
656 * @param tc the task context from scheduler
659 lcf_forwarded_operation_timeout (void *cls,
660 const struct GNUNET_SCHEDULER_TaskContext *tc)
662 struct LCFContext *lcf = cls;
664 lcf->timeout_task = GNUNET_SCHEDULER_NO_TASK;
665 // GST_forwarded_operation_timeout (lcf->fopc, tc);
666 LOG (GNUNET_ERROR_TYPE_WARNING,
667 "A forwarded controller link operation has timed out\n");
668 send_controller_link_response (lcf->client, lcf->operation_id, NULL,
669 "A forwarded controller link operation has "
671 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
672 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
677 * The Link Controller forwarding task
679 * @param cls the LCFContext
680 * @param tc the Task context from scheduler
683 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
685 struct LCFContext *lcf = cls;
686 struct LCFContextQueue *lcfq;
688 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
693 GNUNET_TESTBED_is_host_registered_ (GST_host_list
694 [lcf->delegated_host_id],
695 lcf->gateway->controller))
697 GST_queue_host_registration (lcf->gateway, lcf_proc_cc, lcf,
698 GST_host_list[lcf->delegated_host_id]);
702 lcf->state = DELEGATED_HOST_REGISTERED;
703 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
706 case DELEGATED_HOST_REGISTERED:
708 GNUNET_TESTBED_is_host_registered_ (GST_host_list[lcf->slave_host_id],
709 lcf->gateway->controller))
711 GST_queue_host_registration (lcf->gateway, lcf_proc_cc, lcf,
712 GST_host_list[lcf->slave_host_id]);
716 lcf->state = SLAVE_HOST_REGISTERED;
717 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
720 case SLAVE_HOST_REGISTERED:
721 lcf->op = GNUNET_TESTBED_controller_link (lcf,
722 lcf->gateway->controller,
723 GST_host_list[lcf->delegated_host_id],
724 GST_host_list[lcf->slave_host_id],
726 lcf->is_subordinate);
728 GNUNET_SCHEDULER_add_delayed (GST_timeout, &lcf_forwarded_operation_timeout,
730 lcf->state = FINISHED;
734 GNUNET_assert (lcfq->lcf == lcf);
735 GNUNET_assert (NULL != lcf->cfg);
736 GNUNET_CONFIGURATION_destroy (lcf->cfg);
737 GNUNET_SERVER_client_drop (lcf->client);
738 GNUNET_TESTBED_operation_done (lcf->op);
740 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
742 if (NULL != lcfq_head)
744 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
750 * Callback for event from slave controllers
752 * @param cls struct Slave *
753 * @param event information about the event
756 slave_event_callback (void *cls,
757 const struct GNUNET_TESTBED_EventInformation *event)
759 struct RegisteredHostContext *rhc;
760 struct LCFContext *lcf;
761 struct GNUNET_CONFIGURATION_Handle *cfg;
762 struct GNUNET_TESTBED_Operation *old_op;
764 /* We currently only get here when working on RegisteredHostContexts and
766 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
768 if (CLOSURE_TYPE_RHC == rhc->type)
770 GNUNET_assert (rhc->sub_op == event->op);
774 cfg = event->details.operation_finished.generic;
775 old_op = rhc->sub_op;
776 rhc->state = RHC_LINK;
778 GNUNET_TESTBED_controller_link (rhc, rhc->gateway->controller,
779 rhc->reg_host, rhc->host, cfg,
781 GNUNET_TESTBED_operation_done (old_op);
784 LOG_DEBUG ("OL: Linking controllers successfull\n");
785 GNUNET_TESTBED_operation_done (rhc->sub_op);
787 rhc->state = RHC_OL_CONNECT;
788 GST_process_next_focc (rhc);
796 if (CLOSURE_TYPE_LCF == lcf->type)
798 GNUNET_assert (lcf->op == event->op);
799 GNUNET_assert (FINISHED == lcf->state);
800 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != lcf->timeout_task);
801 GNUNET_SCHEDULER_cancel (lcf->timeout_task);
802 if (NULL == event->details.operation_finished.emsg)
803 send_controller_link_response (lcf->client, lcf->operation_id,
804 GNUNET_TESTBED_host_get_cfg_
805 (GST_host_list[lcf->delegated_host_id]),
808 send_controller_link_response (lcf->client, lcf->operation_id,
810 event->details.operation_finished.emsg);
811 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
812 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
820 * Callback to signal successfull startup of the controller process
822 * @param cls the handle to the slave whose status is to be found here
823 * @param cfg the configuration with which the controller has been started;
824 * NULL if status is not GNUNET_OK
825 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
826 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
829 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
832 struct Slave *slave = cls;
833 struct LinkControllersContext *lcc;
836 if (GNUNET_SYSERR == status)
838 slave->controller_proc = NULL;
839 GST_slave_list[slave->host_id] = NULL;
842 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
843 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
847 GNUNET_TESTBED_controller_connect (cfg, GST_host_list[slave->host_id],
848 event_mask, &slave_event_callback,
850 if (NULL != slave->controller)
852 send_controller_link_response (lcc->client, lcc->operation_id, cfg, NULL);
856 send_controller_link_response (lcc->client, lcc->operation_id, NULL,
857 "Could not connect to delegated controller");
858 GNUNET_TESTBED_controller_stop (slave->controller_proc);
859 GST_slave_list[slave->host_id] = NULL;
867 if (NULL != lcc->client)
869 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
870 GNUNET_SERVER_client_drop (lcc->client);
881 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
884 * @param client identification of the client
885 * @param message the actual message
888 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
889 const struct GNUNET_MessageHeader *message)
891 const struct GNUNET_TESTBED_InitMessage *msg;
892 struct GNUNET_TESTBED_Host *host;
893 const char *controller_hostname;
896 if (NULL != GST_context)
898 LOG_DEBUG ("We are being connected to laterally\n");
899 GNUNET_SERVER_receive_done (client, GNUNET_OK);
902 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
903 msize = ntohs (message->size);
904 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
907 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
910 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
911 controller_hostname = (const char *) &msg[1];
912 if ('\0' != controller_hostname[msize - 1])
915 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
918 GST_context = GNUNET_malloc (sizeof (struct Context));
919 GNUNET_SERVER_client_keep (client);
920 GST_context->client = client;
921 GST_context->host_id = ntohl (msg->host_id);
922 GST_context->master_ip = GNUNET_strdup (controller_hostname);
923 LOG_DEBUG ("Our IP: %s\n", GST_context->master_ip);
924 GST_context->system =
925 GNUNET_TESTING_system_create ("testbed", GST_context->master_ip,
928 GNUNET_TESTBED_host_create_with_id (GST_context->host_id,
929 GST_context->master_ip, NULL,
931 host_list_add (host);
932 LOG_DEBUG ("Created master context with host ID: %u\n", GST_context->host_id);
933 GNUNET_SERVER_receive_done (client, GNUNET_OK);
938 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
941 * @param client identification of the client
942 * @param message the actual message
945 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
946 const struct GNUNET_MessageHeader *message)
948 struct GNUNET_TESTBED_Host *host;
949 const struct GNUNET_TESTBED_AddHostMessage *msg;
950 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
951 struct GNUNET_CONFIGURATION_Handle *host_cfg;
957 uint16_t username_length;
958 uint16_t hostname_length;
962 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
963 msize = ntohs (msg->header.size);
964 if (msize <= sizeof (struct GNUNET_TESTBED_AddHostMessage))
967 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
970 username_length = ntohs (msg->username_length);
971 hostname_length = ntohs (msg->hostname_length);
972 /* msg must contain hostname */
973 if ((msize <= (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
975 || (0 == hostname_length))
978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
981 /* msg must contain configuration */
982 if (msize <= (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
983 username_length + hostname_length))
986 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
992 if (0 != username_length)
994 username = GNUNET_malloc (username_length + 1);
995 strncpy (username, ptr, username_length);
996 ptr += username_length;
998 hostname = GNUNET_malloc (hostname_length + 1);
999 strncpy (hostname, ptr, hostname_length);
1000 ptr += hostname_length;
1001 if (NULL == (host_cfg = GNUNET_TESTBED_extract_config_ (message)))
1003 GNUNET_free_non_null (username);
1004 GNUNET_free_non_null (hostname);
1005 GNUNET_break_op (0);
1006 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1009 host_id = ntohl (msg->host_id);
1010 LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
1011 LOG_DEBUG ("-------host id: %u\n", host_id);
1012 LOG_DEBUG ("-------hostname: %s\n", hostname);
1013 if (NULL != username)
1014 LOG_DEBUG ("-------username: %s\n", username);
1016 LOG_DEBUG ("-------username: <not given>\n");
1017 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1019 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1020 host_cfg, ntohs (msg->ssh_port));
1021 GNUNET_free_non_null (username);
1022 GNUNET_free (hostname);
1023 GNUNET_CONFIGURATION_destroy (host_cfg);
1026 GNUNET_break_op (0);
1027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1030 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1031 if (GNUNET_OK != host_list_add (host))
1033 /* We are unable to add a host */
1034 emsg = "A host exists with given host-id";
1035 LOG_DEBUG ("%s: %u", emsg, host_id);
1036 GNUNET_TESTBED_host_destroy (host);
1037 reply_size += strlen (emsg) + 1;
1038 reply = GNUNET_malloc (reply_size);
1039 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1043 LOG_DEBUG ("Added host %u at %u\n", host_id, GST_context->host_id);
1044 reply = GNUNET_malloc (reply_size);
1046 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST_SUCCESS);
1047 reply->header.size = htons (reply_size);
1048 reply->host_id = htonl (host_id);
1049 GST_queue_message (client, &reply->header);
1050 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1055 * Iterator over hash map entries.
1057 * @param cls closure
1058 * @param key current key code
1059 * @param value value in the hash map
1060 * @return GNUNET_YES if we should continue to
1065 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1067 struct SharedService *queried_ss = cls;
1068 struct SharedService *ss = value;
1070 if (0 == strcmp (ss->name, queried_ss->name))
1078 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1081 * @param client identification of the client
1082 * @param message the actual message
1085 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1086 const struct GNUNET_MessageHeader *message)
1088 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1089 struct SharedService *ss;
1091 struct GNUNET_HashCode hash;
1093 uint16_t service_name_size;
1095 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1096 msg_size = ntohs (message->size);
1097 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1104 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1105 service_name = (char *) &msg[1];
1106 if ('\0' != service_name[service_name_size - 1])
1109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1112 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1113 service_name, ntohl (msg->num_peers));
1114 if (ntohl (msg->host_id) != GST_context->host_id)
1116 route_message (ntohl (msg->host_id), message);
1117 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1120 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1121 ss = GNUNET_malloc (sizeof (struct SharedService));
1122 ss->name = strdup (service_name);
1123 ss->num_shared = ntohl (msg->num_peers);
1124 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1125 if (GNUNET_SYSERR ==
1126 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1127 &ss_exists_iterator, ss))
1129 LOG (GNUNET_ERROR_TYPE_WARNING,
1130 "Service %s already configured as a shared service. "
1131 "Ignoring service sharing request \n", ss->name);
1132 GNUNET_free (ss->name);
1136 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1137 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1142 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1145 * @param client identification of the client
1146 * @param message the actual message
1149 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1150 const struct GNUNET_MessageHeader *message)
1152 const struct GNUNET_TESTBED_ControllerLinkRequest *msg;
1153 struct GNUNET_CONFIGURATION_Handle *cfg;
1154 struct LCFContextQueue *lcfq;
1155 struct Route *route;
1156 struct Route *new_route;
1157 uint32_t delegated_host_id;
1158 uint32_t slave_host_id;
1161 if (NULL == GST_context)
1164 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1167 msize = ntohs (message->size);
1168 if (sizeof (struct GNUNET_TESTBED_ControllerLinkRequest) >= msize)
1171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1174 msg = (const struct GNUNET_TESTBED_ControllerLinkRequest *) message;
1175 delegated_host_id = ntohl (msg->delegated_host_id);
1176 if (delegated_host_id == GST_context->host_id)
1179 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1183 if ((delegated_host_id >= GST_host_list_size) ||
1184 (NULL == GST_host_list[delegated_host_id]))
1186 LOG (GNUNET_ERROR_TYPE_WARNING,
1187 "Delegated host %u not registered with us\n", delegated_host_id);
1188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1191 slave_host_id = ntohl (msg->slave_host_id);
1192 if ((slave_host_id >= GST_host_list_size) ||
1193 (NULL == GST_host_list[slave_host_id]))
1195 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
1197 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1200 if (slave_host_id == delegated_host_id)
1202 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1203 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1206 cfg = GNUNET_TESTBED_extract_config_ (message); /* destroy cfg here or in lcfcontext */
1209 GNUNET_break (0); /* Configuration parsing error */
1210 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1213 if (slave_host_id == GST_context->host_id) /* Link from us */
1215 struct Slave *slave;
1216 struct LinkControllersContext *lcc;
1218 if ((delegated_host_id < GST_slave_list_size) &&
1219 (NULL != GST_slave_list[delegated_host_id]))
1222 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1225 slave = GNUNET_malloc (sizeof (struct Slave));
1226 slave->host_id = delegated_host_id;
1227 slave->reghost_map = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
1228 slave_list_add (slave);
1229 if (1 != msg->is_subordinate)
1232 GNUNET_TESTBED_controller_connect (cfg, GST_host_list[slave->host_id],
1233 event_mask, &slave_event_callback,
1235 if (NULL != slave->controller)
1236 send_controller_link_response (client,
1237 GNUNET_ntohll (msg->operation_id),
1241 send_controller_link_response (client,
1242 GNUNET_ntohll (msg->operation_id),
1244 "Could not connect to delegated controller");
1245 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1248 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1249 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1250 GNUNET_SERVER_client_keep (client);
1251 lcc->client = client;
1253 slave->controller_proc =
1254 GNUNET_TESTBED_controller_start (GST_context->master_ip,
1255 GST_host_list[slave->host_id], cfg,
1256 &slave_status_callback, slave);
1257 GNUNET_CONFIGURATION_destroy (cfg);
1258 new_route = GNUNET_malloc (sizeof (struct Route));
1259 new_route->dest = delegated_host_id;
1260 new_route->thru = GST_context->host_id;
1261 route_list_add (new_route);
1265 /* Route the request */
1266 if (slave_host_id >= route_list_size)
1268 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1272 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1273 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1274 lcfq->lcf->type = CLOSURE_TYPE_LCF;
1275 lcfq->lcf->delegated_host_id = delegated_host_id;
1276 lcfq->lcf->slave_host_id = slave_host_id;
1277 route = GST_find_dest_route (slave_host_id);
1278 GNUNET_assert (NULL != route); /* because we add routes carefully */
1279 GNUNET_assert (route->dest < GST_slave_list_size);
1280 GNUNET_assert (NULL != GST_slave_list[route->dest]);
1281 lcfq->lcf->cfg = cfg;
1282 lcfq->lcf->is_subordinate = msg->is_subordinate;
1283 lcfq->lcf->state = INIT;
1284 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1285 lcfq->lcf->gateway = GST_slave_list[route->dest];
1286 GNUNET_SERVER_client_keep (client);
1287 lcfq->lcf->client = client;
1288 if (NULL == lcfq_head)
1290 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1291 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1292 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1295 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1296 /* FIXME: Adding a new route should happen after the controllers are linked
1298 if (1 != msg->is_subordinate)
1300 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1303 if ((delegated_host_id < route_list_size) &&
1304 (NULL != route_list[delegated_host_id]))
1306 GNUNET_break_op (0); /* Are you trying to link delegated host twice
1307 * with is subordinate flag set to GNUNET_YES? */
1308 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1311 new_route = GNUNET_malloc (sizeof (struct Route));
1312 new_route->dest = delegated_host_id;
1313 new_route->thru = route->dest;
1314 route_list_add (new_route);
1315 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1320 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
1323 * @param client identification of the client
1324 * @param message the actual message
1327 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
1328 const struct GNUNET_MessageHeader *message)
1330 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
1331 struct Slave *slave;
1332 struct GNUNET_TESTBED_SlaveConfiguration *reply;
1333 const struct GNUNET_CONFIGURATION_Handle *cfg;
1337 size_t xconfig_size;
1342 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
1343 slave_id = ntohl (msg->slave_id);
1344 op_id = GNUNET_ntohll (msg->operation_id);
1345 if ((GST_slave_list_size <= slave_id) || (NULL == GST_slave_list[slave_id]))
1347 /* FIXME: Add forwardings for this type of message here.. */
1348 GST_send_operation_fail_msg (client, op_id, "Slave not found");
1349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1352 slave = GST_slave_list[slave_id];
1353 GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (GST_host_list[slave->host_id])));
1354 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1356 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1357 GNUNET_free (config);
1358 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
1359 GNUNET_break (reply_size <= UINT16_MAX);
1360 GNUNET_break (config_size <= UINT16_MAX);
1361 reply = GNUNET_realloc (xconfig, reply_size);
1362 (void) memmove (&reply[1], reply, xconfig_size);
1363 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVE_CONFIGURATION);
1364 reply->header.size = htons ((uint16_t) reply_size);
1365 reply->slave_id = msg->slave_id;
1366 reply->operation_id = msg->operation_id;
1367 reply->config_size = htons ((uint16_t) config_size);
1368 GST_queue_message (client, &reply->header);
1369 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1374 * Clears the forwarded operations queue
1379 struct ForwardedOperationContext *fopc;
1381 while (NULL != (fopc = fopcq_head))
1383 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
1384 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1385 if (GNUNET_SCHEDULER_NO_TASK != fopc->timeout_task)
1386 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
1387 GNUNET_SERVER_client_drop (fopc->client);
1390 case OP_PEER_CREATE:
1391 GNUNET_free (fopc->cls);
1393 case OP_SHUTDOWN_PEERS:
1395 struct HandlerContext_ShutdownPeers *hc = fopc->cls;
1397 GNUNET_assert (0 < hc->nslaves);
1399 if (0 == hc->nslaves)
1405 case OP_PEER_DESTROY:
1407 case OP_OVERLAY_CONNECT:
1408 case OP_LINK_CONTROLLERS:
1409 case OP_GET_SLAVE_CONFIG:
1410 case OP_MANAGE_SERVICE:
1421 * Iterator over hash map entries.
1423 * @param cls closure
1424 * @param key current key code
1425 * @param value value in the hash map
1426 * @return GNUNET_YES if we should continue to
1431 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1433 struct SharedService *ss = value;
1435 GNUNET_assert (GNUNET_YES ==
1436 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1437 GNUNET_free (ss->name);
1444 * Iterator for freeing hash map entries in a slave's reghost_map
1446 * @param cls handle to the slave
1447 * @param key current key code
1448 * @param value value in the hash map
1449 * @return GNUNET_YES if we should continue to
1454 reghost_free_iterator (void *cls, const struct GNUNET_HashCode *key,
1457 struct Slave *slave = cls;
1458 struct RegisteredHostContext *rhc = value;
1459 struct ForwardedOverlayConnectContext *focc;
1461 GNUNET_assert (GNUNET_YES ==
1462 GNUNET_CONTAINER_multihashmap_remove (slave->reghost_map, key,
1464 while (NULL != (focc = rhc->focc_dll_head))
1466 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1467 GST_cleanup_focc (focc);
1469 if (NULL != rhc->sub_op)
1470 GNUNET_TESTBED_operation_done (rhc->sub_op);
1471 if (NULL != rhc->client)
1472 GNUNET_SERVER_client_drop (rhc->client);
1473 GNUNET_free (value);
1479 * Task to clean up and shutdown nicely
1482 * @param tc the TaskContext from scheduler
1485 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1487 struct LCFContextQueue *lcfq;
1488 struct MessageQueue *mq_entry;
1491 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1492 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1493 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1495 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1496 /* cleanup any remaining forwarded operations */
1498 if (NULL != lcfq_head)
1500 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1502 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1503 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1506 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1507 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1509 GNUNET_SERVER_client_drop (lcfq->lcf->client);
1510 GNUNET_assert (NULL != lcfq->lcf->cfg);
1511 GNUNET_CONFIGURATION_destroy (lcfq->lcf->cfg);
1512 GNUNET_free (lcfq->lcf);
1513 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1519 /* Clear peer list */
1520 GST_destroy_peers ();
1521 /* Clear host list */
1522 for (id = 0; id < GST_host_list_size; id++)
1523 if (NULL != GST_host_list[id])
1524 GNUNET_TESTBED_host_destroy (GST_host_list[id]);
1525 GNUNET_free_non_null (GST_host_list);
1526 /* Clear route list */
1527 for (id = 0; id < route_list_size; id++)
1528 if (NULL != route_list[id])
1529 GNUNET_free (route_list[id]);
1530 GNUNET_free_non_null (route_list);
1531 /* Clear GST_slave_list */
1532 for (id = 0; id < GST_slave_list_size; id++)
1533 if (NULL != GST_slave_list[id])
1535 struct HostRegistration *hr_entry;
1537 while (NULL != (hr_entry = GST_slave_list[id]->hr_dll_head))
1539 GNUNET_CONTAINER_DLL_remove (GST_slave_list[id]->hr_dll_head,
1540 GST_slave_list[id]->hr_dll_tail, hr_entry);
1541 GNUNET_free (hr_entry);
1543 if (NULL != GST_slave_list[id]->rhandle)
1544 GNUNET_TESTBED_cancel_registration (GST_slave_list[id]->rhandle);
1546 GNUNET_CONTAINER_multihashmap_iterate (GST_slave_list
1548 reghost_free_iterator,
1549 GST_slave_list[id]);
1550 GNUNET_CONTAINER_multihashmap_destroy (GST_slave_list[id]->reghost_map);
1551 if (NULL != GST_slave_list[id]->controller)
1552 GNUNET_TESTBED_controller_disconnect (GST_slave_list[id]->controller);
1553 if (NULL != GST_slave_list[id]->controller_proc)
1554 GNUNET_TESTBED_controller_stop (GST_slave_list[id]->controller_proc);
1555 GNUNET_free (GST_slave_list[id]);
1557 GNUNET_free_non_null (GST_slave_list);
1558 if (NULL != GST_context)
1560 GNUNET_free_non_null (GST_context->master_ip);
1561 if (NULL != GST_context->system)
1562 GNUNET_TESTING_system_destroy (GST_context->system, GNUNET_YES);
1563 GNUNET_SERVER_client_drop (GST_context->client);
1564 GNUNET_free (GST_context);
1567 if (NULL != transmit_handle)
1568 GNUNET_SERVER_notify_transmit_ready_cancel (transmit_handle);
1569 while (NULL != (mq_entry = mq_head))
1571 GNUNET_free (mq_entry->msg);
1572 GNUNET_SERVER_client_drop (mq_entry->client);
1573 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
1574 GNUNET_free (mq_entry);
1576 GNUNET_free_non_null (hostname);
1577 GNUNET_CONFIGURATION_destroy (our_config);
1578 /* Free hello cache */
1580 GNUNET_TESTBED_operation_queue_destroy_ (GST_opq_openfds);
1581 GST_opq_openfds = NULL;
1582 GST_stats_destroy ();
1587 * Callback for client disconnect
1590 * @param client the client which has disconnected
1593 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
1595 if (NULL == GST_context)
1597 if (client == GST_context->client)
1599 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
1600 /* should not be needed as we're terminated by failure to read
1601 * from stdin, but if stdin fails for some reason, this shouldn't
1602 * hurt for now --- might need to revise this later if we ever
1603 * decide that master connections might be temporarily down
1604 * for some reason */
1605 //GNUNET_SCHEDULER_shutdown ();
1613 * @param cls closure
1614 * @param server the initialized server
1615 * @param cfg configuration to use
1618 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
1619 const struct GNUNET_CONFIGURATION_Handle *cfg)
1621 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
1622 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
1623 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST, 0},
1624 {&handle_configure_shared_service, NULL,
1625 GNUNET_MESSAGE_TYPE_TESTBED_SHARE_SERVICE, 0},
1626 {&handle_link_controllers, NULL,
1627 GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS, 0},
1628 {&GST_handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER, 0},
1629 {&GST_handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROY_PEER,
1630 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
1631 {&GST_handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_START_PEER,
1632 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
1633 {&GST_handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOP_PEER,
1634 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
1635 {&GST_handle_peer_get_config, NULL,
1636 GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_CONFIGURATION,
1637 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
1638 {&GST_handle_overlay_connect, NULL,
1639 GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT,
1640 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
1641 {&GST_handle_remote_overlay_connect, NULL,
1642 GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT, 0},
1643 {&GST_handle_manage_peer_service, NULL,
1644 GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE, 0},
1645 {&handle_slave_get_config, NULL,
1646 GNUNET_MESSAGE_TYPE_TESTBED_GET_SLAVE_CONFIGURATION,
1647 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
1648 {&GST_handle_shutdown_peers, NULL, GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS,
1649 sizeof (struct GNUNET_TESTBED_ShutdownPeersMessage)},
1653 unsigned long long num;
1656 GNUNET_CONFIGURATION_get_value_filename (cfg, "TESTBED", "LOG_FILE",
1659 GNUNET_break (GNUNET_OK == GNUNET_log_setup ("testbed", "DEBUG", logfile));
1660 GNUNET_free (logfile);
1662 GNUNET_assert (GNUNET_OK ==
1663 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
1664 "CACHE_SIZE", &num));
1665 GST_cache_init ((unsigned int) num);
1666 GNUNET_assert (GNUNET_OK ==
1667 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
1668 "MAX_OPEN_FDS", &num));
1669 GST_opq_openfds = GNUNET_TESTBED_operation_queue_create_ ((unsigned int) num);
1670 GNUNET_assert (GNUNET_OK ==
1671 GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
1672 "OPERATION_TIMEOUT",
1674 GNUNET_TIME_Relative *)
1676 GNUNET_assert (GNUNET_OK ==
1677 GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
1678 "HOSTNAME", &hostname));
1679 our_config = GNUNET_CONFIGURATION_dup (cfg);
1680 GNUNET_SERVER_add_handlers (server, message_handlers);
1681 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
1682 ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
1684 GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
1685 GNUNET_SCHEDULER_PRIORITY_IDLE,
1686 &shutdown_task, NULL);
1687 LOG_DEBUG ("Testbed startup complete\n");
1688 event_mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
1689 GST_stats_init (our_config);
1694 * The starting point of execution
1697 main (int argc, char *const *argv)
1699 //sleep (15); /* Debugging */
1700 return (GNUNET_OK ==
1701 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
1702 &testbed_run, NULL)) ? 0 : 1;
1705 /* end of gnunet-service-testbed.c */