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"
33 * Context data for GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS handler
35 struct HandlerContext_ShutdownPeers
38 * The number of slave we expect to hear from since we forwarded the
39 * GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS message to them
44 * Did we observe a timeout with respect to this operation at any of the
58 struct GNUNET_CONFIGURATION_Handle *our_config;
61 * The master context; generated with the first INIT message
63 struct Context *GST_context;
66 * A list of directly linked neighbours
68 struct Slave **GST_slave_list;
71 * A list of peers we know about
73 struct Peer **GST_peer_list;
78 struct GNUNET_TESTBED_Host **GST_host_list;
81 * DLL head for forwarded operation contexts
83 struct ForwardedOperationContext *fopcq_head;
86 * DLL tail for forwarded operation contexts
88 struct ForwardedOperationContext *fopcq_tail;
91 * Operation queue for open file descriptors
93 struct OperationQueue *GST_opq_openfds;
96 * Timeout for operations which may take some time
98 const struct GNUNET_TIME_Relative GST_timeout;
101 * The size of the host list
103 unsigned int GST_host_list_size;
106 * The size of directly linked neighbours list
108 unsigned int GST_slave_list_size;
111 * The size of the peer list
113 unsigned int GST_peer_list_size;
116 /***********************************/
117 /* Local definitions and variables */
118 /***********************************/
121 * The message queue for sending messages to clients
126 * The message to be sent
128 struct GNUNET_MessageHeader *msg;
131 * The client to send the message to
133 struct GNUNET_SERVER_Client *client;
136 * next pointer for DLL
138 struct MessageQueue *next;
141 * prev pointer for DLL
143 struct MessageQueue *prev;
147 * Our hostname; we give this to all the peers we start
149 static char *hostname;
152 * Current Transmit Handle; NULL if no notify transmit exists currently
154 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
157 * The head for the LCF queue
159 static struct LCFContextQueue *lcfq_head;
162 * The tail for the LCF queue
164 static struct LCFContextQueue *lcfq_tail;
167 * The message queue head
169 static struct MessageQueue *mq_head;
172 * The message queue tail
174 static struct MessageQueue *mq_tail;
177 * The hashmap of shared services
179 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
184 static struct Route **route_list;
187 * The event mask for the events we listen from sub-controllers
189 static uint64_t event_mask;
192 * The size of the route list
194 static unsigned int route_list_size;
197 * The lcf_task handle
199 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
202 * The shutdown task handle
204 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
208 * Function called to notify a client about the connection begin ready to queue
209 * more data. "buf" will be NULL and "size" zero if the connection was closed
210 * for writing in the meantime.
213 * @param size number of bytes available in buf
214 * @param buf where the callee should write the message
215 * @return number of bytes written to buf
218 transmit_ready_notify (void *cls, size_t size, void *buf)
220 struct MessageQueue *mq_entry;
222 transmit_handle = NULL;
224 GNUNET_assert (NULL != mq_entry);
227 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
228 size = ntohs (mq_entry->msg->size);
229 memcpy (buf, mq_entry->msg, size);
230 GNUNET_free (mq_entry->msg);
231 GNUNET_SERVER_client_drop (mq_entry->client);
232 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
233 GNUNET_free (mq_entry);
235 if (NULL != mq_entry)
237 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
238 ntohs (mq_entry->msg->size),
239 GNUNET_TIME_UNIT_FOREVER_REL,
240 &transmit_ready_notify, NULL);
246 * Queues a message in send queue for sending to the service
248 * @param client the client to whom the queued message has to be sent
249 * @param msg the message to queue
252 GST_queue_message (struct GNUNET_SERVER_Client *client,
253 struct GNUNET_MessageHeader *msg)
255 struct MessageQueue *mq_entry;
259 type = ntohs (msg->type);
260 size = ntohs (msg->size);
261 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
262 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
263 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
265 mq_entry->client = client;
266 GNUNET_SERVER_client_keep (client);
267 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
269 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
270 if (NULL == transmit_handle)
272 GNUNET_SERVER_notify_transmit_ready (client, size,
273 GNUNET_TIME_UNIT_FOREVER_REL,
274 &transmit_ready_notify, NULL);
279 * Similar to GNUNET_array_grow(); however instead of calling GNUNET_array_grow()
280 * several times we call it only once. The array is also made to grow in steps
283 * @param ptr the array pointer to grow
284 * @param size the size of array
285 * @param accommodate_size the size which the array has to accommdate; after
286 * this call the array will be big enough to accommdate sizes upto
289 #define array_grow_large_enough(ptr, size, accommodate_size) \
292 unsigned int growth_size; \
293 GNUNET_assert (size <= accommodate_size); \
294 growth_size = size; \
295 while (growth_size <= accommodate_size) \
296 growth_size += LIST_GROW_STEP; \
297 GNUNET_array_grow (ptr, size, growth_size); \
298 GNUNET_assert (size > accommodate_size); \
303 * Function to add a host to the current list of known hosts
305 * @param host the host to add
306 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
310 host_list_add (struct GNUNET_TESTBED_Host *host)
314 host_id = GNUNET_TESTBED_host_get_id_ (host);
315 if (GST_host_list_size <= host_id)
316 array_grow_large_enough (GST_host_list, GST_host_list_size, host_id);
317 if (NULL != GST_host_list[host_id])
319 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
320 return GNUNET_SYSERR;
322 GST_host_list[host_id] = host;
328 * Adds a route to the route list
330 * @param route the route to add
333 route_list_add (struct Route *route)
335 if (route->dest >= route_list_size)
336 array_grow_large_enough (route_list, route_list_size, route->dest);
337 GNUNET_assert (NULL == route_list[route->dest]);
338 route_list[route->dest] = route;
343 * Adds a slave to the slave array
345 * @param slave the slave controller to add
348 slave_list_add (struct Slave *slave)
350 if (slave->host_id >= GST_slave_list_size)
351 array_grow_large_enough (GST_slave_list, GST_slave_list_size,
353 GNUNET_assert (NULL == GST_slave_list[slave->host_id]);
354 GST_slave_list[slave->host_id] = slave;
359 * Adds a peer to the peer array
361 * @param peer the peer to add
364 peer_list_add (struct Peer *peer)
366 if (peer->id >= GST_peer_list_size)
367 array_grow_large_enough (GST_peer_list, GST_peer_list_size, peer->id);
368 GNUNET_assert (NULL == GST_peer_list[peer->id]);
369 GST_peer_list[peer->id] = peer;
374 * Removes a the give peer from the peer array
376 * @param peer the peer to be removed
379 peer_list_remove (struct Peer *peer)
381 unsigned int orig_size;
384 GST_peer_list[peer->id] = NULL;
385 orig_size = GST_peer_list_size;
386 while (GST_peer_list_size >= LIST_GROW_STEP)
388 for (id = GST_peer_list_size - 1;
389 (id >= GST_peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX);
391 if (NULL != GST_peer_list[id])
393 if (id != ((GST_peer_list_size - LIST_GROW_STEP) - 1))
395 GST_peer_list_size -= LIST_GROW_STEP;
397 if (orig_size == GST_peer_list_size)
400 GNUNET_realloc (GST_peer_list,
401 sizeof (struct Peer *) * GST_peer_list_size);
406 * Finds the route with directly connected host as destination through which
407 * the destination host can be reached
409 * @param host_id the id of the destination host
410 * @return the route with directly connected destination host; NULL if no route
414 GST_find_dest_route (uint32_t host_id)
418 if (route_list_size <= host_id)
420 while (NULL != (route = route_list[host_id]))
422 if (route->thru == GST_context->host_id)
424 host_id = route->thru;
431 * Routes message to a host given its host_id
433 * @param host_id the id of the destination host
434 * @param msg the message to be routed
437 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
444 * Send operation failure message to client
446 * @param client the client to which the failure message has to be sent to
447 * @param operation_id the id of the failed operation
448 * @param emsg the error message; can be NULL
451 GST_send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
452 uint64_t operation_id, const char *emsg)
454 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
458 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
459 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
461 msg = GNUNET_malloc (msize);
462 msg->header.size = htons (msize);
463 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATION_FAIL_EVENT);
464 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
465 msg->operation_id = GNUNET_htonll (operation_id);
467 memcpy (&msg[1], emsg, emsg_len);
468 GST_queue_message (client, &msg->header);
473 * Function to send generic operation success message to given client
475 * @param client the client to send the message to
476 * @param operation_id the id of the operation which was successful
479 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
480 uint64_t operation_id)
482 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
485 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
486 msg = GNUNET_malloc (msize);
487 msg->header.size = htons (msize);
489 htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERIC_OPERATION_SUCCESS);
490 msg->operation_id = GNUNET_htonll (operation_id);
491 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
492 GST_queue_message (client, &msg->header);
497 * Function to send a failure reponse for controller link operation
499 * @param client the client to send the message to
500 * @param operation_id the operation ID of the controller link request
501 * @param cfg the configuration with which the delegated controller is started.
502 * Can be NULL if the delegated controller is not started but just
504 * @param emsg set to an error message explaining why the controller link
505 * failed. Setting this to NULL signifies success. !This should be
506 * NULL if cfg is set!
509 send_controller_link_response (struct GNUNET_SERVER_Client *client,
510 uint64_t operation_id,
511 const struct GNUNET_CONFIGURATION_Handle
515 struct GNUNET_TESTBED_ControllerLinkResponse *msg;
521 GNUNET_assert ((NULL == cfg) || (NULL == emsg));
525 msize = sizeof (struct GNUNET_TESTBED_ControllerLinkResponse);
528 xconfig = GNUNET_TESTBED_compress_cfg_ (cfg,
531 msize += xconfig_size;
534 msize += strlen (emsg);
535 msg = GNUNET_malloc (msize);
536 msg->header.type = htons
537 (GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS_RESULT);
538 msg->header.size = htons (msize);
540 msg->success = htons (GNUNET_YES);
541 msg->operation_id = GNUNET_htonll (operation_id);
542 msg->config_size = htons ((uint16_t) config_size);
544 memcpy (&msg[1], xconfig, xconfig_size);
546 memcpy (&msg[1], emsg, strlen (emsg));
547 GST_queue_message (client, &msg->header);
551 * Callback which will be called after a host registration succeeded or failed
553 * @param cls the handle to the slave at which the registration is completed
554 * @param emsg the error message; NULL if host registration is successful
557 hr_completion (void *cls, const char *emsg);
561 * Attempts to register the next host in the host registration queue
563 * @param slave the slave controller whose host registration queue is checked
564 * for host registrations
567 register_next_host (struct Slave *slave)
569 struct HostRegistration *hr;
571 hr = slave->hr_dll_head;
572 GNUNET_assert (NULL != hr);
573 GNUNET_assert (NULL == slave->rhandle);
574 LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering host %u at %u\n",
575 GNUNET_TESTBED_host_get_id_ (hr->host),
576 GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
578 GNUNET_TESTBED_register_host (slave->controller, hr->host, hr_completion,
584 * Callback which will be called to after a host registration succeeded or failed
586 * @param cls the handle to the slave at which the registration is completed
587 * @param emsg the error message; NULL if host registration is successful
590 hr_completion (void *cls, const char *emsg)
592 struct Slave *slave = cls;
593 struct HostRegistration *hr;
595 slave->rhandle = NULL;
596 hr = slave->hr_dll_head;
597 GNUNET_assert (NULL != hr);
598 LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering host %u at %u successful\n",
599 GNUNET_TESTBED_host_get_id_ (hr->host),
600 GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
601 GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head, slave->hr_dll_tail, hr);
603 hr->cb (hr->cb_cls, emsg);
605 if (NULL != slave->hr_dll_head)
606 register_next_host (slave);
611 * Adds a host registration's request to a slave's registration queue
613 * @param slave the slave controller at which the given host has to be
615 * @param cb the host registration completion callback
616 * @param cb_cls the closure for the host registration completion callback
617 * @param host the host which has to be registered
620 GST_queue_host_registration (struct Slave *slave,
621 GNUNET_TESTBED_HostRegistrationCompletion cb,
622 void *cb_cls, struct GNUNET_TESTBED_Host *host)
624 struct HostRegistration *hr;
627 LOG (GNUNET_ERROR_TYPE_DEBUG,
628 "Queueing host registration for host %u at %u\n",
629 GNUNET_TESTBED_host_get_id_ (host),
630 GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
631 hr = GNUNET_malloc (sizeof (struct HostRegistration));
635 call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO;
636 GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head, slave->hr_dll_tail, hr);
637 if (GNUNET_YES == call_register)
638 register_next_host (slave);
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 * Completion callback for host registrations while forwarding Link Controller messages
655 * @param cls the LCFContext
656 * @param emsg the error message; NULL if host registration is successful
659 lcf_proc_cc (void *cls, const char *emsg)
661 struct LCFContext *lcf = cls;
663 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
668 goto registration_error;
669 lcf->state = DELEGATED_HOST_REGISTERED;
670 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
672 case DELEGATED_HOST_REGISTERED:
674 goto registration_error;
675 lcf->state = SLAVE_HOST_REGISTERED;
676 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
679 GNUNET_assert (0); /* Shouldn't reach here */
684 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
686 lcf->state = FINISHED;
687 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
692 * Callback to relay the reply msg of a forwarded operation back to the client
694 * @param cls ForwardedOperationContext
695 * @param msg the message to relay
698 GST_forwarded_operation_reply_relay (void *cls,
699 const struct GNUNET_MessageHeader *msg)
701 struct ForwardedOperationContext *fopc = cls;
702 struct GNUNET_MessageHeader *dup_msg;
705 msize = ntohs (msg->size);
706 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
708 dup_msg = GNUNET_copy_message (msg);
709 GST_queue_message (fopc->client, dup_msg);
710 GNUNET_SERVER_client_drop (fopc->client);
711 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
712 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
718 * Task to free resources when forwarded operation has been timedout
720 * @param cls the ForwardedOperationContext
721 * @param tc the task context from scheduler
724 GST_forwarded_operation_timeout (void *cls,
725 const struct GNUNET_SCHEDULER_TaskContext *tc)
727 struct ForwardedOperationContext *fopc = cls;
729 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
730 LOG (GNUNET_ERROR_TYPE_DEBUG, "A forwarded operation has timed out\n");
731 GST_send_operation_fail_msg (fopc->client, fopc->operation_id,
732 "A forwarded operation has timed out");
733 GNUNET_SERVER_client_drop (fopc->client);
734 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
740 * The Link Controller forwarding task
742 * @param cls the LCFContext
743 * @param tc the Task context from scheduler
746 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
750 * Task to free resources when forwarded link controllers has been timedout
752 * @param cls the LCFContext
753 * @param tc the task context from scheduler
756 lcf_forwarded_operation_timeout (void *cls,
757 const struct GNUNET_SCHEDULER_TaskContext *tc)
759 struct LCFContext *lcf = cls;
761 lcf->timeout_task = GNUNET_SCHEDULER_NO_TASK;
762 // GST_forwarded_operation_timeout (lcf->fopc, tc);
763 LOG (GNUNET_ERROR_TYPE_WARNING,
764 "A forwarded controller link operation has timed out\n");
765 send_controller_link_response (lcf->client, lcf->operation_id, NULL,
766 "A forwarded controller link operation has "
768 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
769 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
774 * The Link Controller forwarding task
776 * @param cls the LCFContext
777 * @param tc the Task context from scheduler
780 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
782 struct LCFContext *lcf = cls;
783 struct LCFContextQueue *lcfq;
785 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
790 GNUNET_TESTBED_is_host_registered_ (GST_host_list
791 [lcf->delegated_host_id],
792 lcf->gateway->controller))
794 GST_queue_host_registration (lcf->gateway, lcf_proc_cc, lcf,
795 GST_host_list[lcf->delegated_host_id]);
799 lcf->state = DELEGATED_HOST_REGISTERED;
800 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
803 case DELEGATED_HOST_REGISTERED:
805 GNUNET_TESTBED_is_host_registered_ (GST_host_list[lcf->slave_host_id],
806 lcf->gateway->controller))
808 GST_queue_host_registration (lcf->gateway, lcf_proc_cc, lcf,
809 GST_host_list[lcf->slave_host_id]);
813 lcf->state = SLAVE_HOST_REGISTERED;
814 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
817 case SLAVE_HOST_REGISTERED:
818 lcf->op = GNUNET_TESTBED_controller_link (lcf,
819 lcf->gateway->controller,
820 GST_host_list[lcf->delegated_host_id],
821 GST_host_list[lcf->slave_host_id],
823 lcf->is_subordinate);
825 GNUNET_SCHEDULER_add_delayed (GST_timeout, &lcf_forwarded_operation_timeout,
827 lcf->state = FINISHED;
831 GNUNET_assert (lcfq->lcf == lcf);
832 GNUNET_assert (NULL != lcf->cfg);
833 GNUNET_CONFIGURATION_destroy (lcf->cfg);
834 GNUNET_SERVER_client_drop (lcf->client);
835 GNUNET_TESTBED_operation_done (lcf->op);
837 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
839 if (NULL != lcfq_head)
841 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
847 * Callback for event from slave controllers
849 * @param cls struct Slave *
850 * @param event information about the event
853 slave_event_callback (void *cls,
854 const struct GNUNET_TESTBED_EventInformation *event)
856 struct RegisteredHostContext *rhc;
857 struct LCFContext *lcf;
858 struct GNUNET_CONFIGURATION_Handle *cfg;
859 struct GNUNET_TESTBED_Operation *old_op;
861 /* We currently only get here when working on RegisteredHostContexts and
863 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
864 rhc = event->details.operation_finished.op_cls;
865 if (CLOSURE_TYPE_RHC == rhc->type)
867 GNUNET_assert (rhc->sub_op == event->details.operation_finished.operation);
871 cfg = event->details.operation_finished.generic;
872 old_op = rhc->sub_op;
873 rhc->state = RHC_LINK;
875 GNUNET_TESTBED_controller_link (rhc, rhc->gateway->controller,
876 rhc->reg_host, rhc->host, cfg,
878 GNUNET_TESTBED_operation_done (old_op);
881 LOG_DEBUG ("OL: Linking controllers successfull\n");
882 GNUNET_TESTBED_operation_done (rhc->sub_op);
884 rhc->state = RHC_OL_CONNECT;
885 GST_process_next_focc (rhc);
892 lcf = event->details.operation_finished.op_cls;
893 if (CLOSURE_TYPE_LCF == lcf->type)
895 GNUNET_assert (lcf->op == event->details.operation_finished.operation);
896 GNUNET_assert (FINISHED == lcf->state);
897 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != lcf->timeout_task);
898 GNUNET_SCHEDULER_cancel (lcf->timeout_task);
899 if (NULL == event->details.operation_finished.emsg)
900 send_controller_link_response (lcf->client, lcf->operation_id,
901 GNUNET_TESTBED_host_get_cfg_
902 (GST_host_list[lcf->delegated_host_id]),
905 send_controller_link_response (lcf->client, lcf->operation_id,
907 event->details.operation_finished.emsg);
908 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
909 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
917 * Callback to signal successfull startup of the controller process
919 * @param cls the handle to the slave whose status is to be found here
920 * @param cfg the configuration with which the controller has been started;
921 * NULL if status is not GNUNET_OK
922 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
923 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
926 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
929 struct Slave *slave = cls;
930 struct LinkControllersContext *lcc;
933 if (GNUNET_SYSERR == status)
935 slave->controller_proc = NULL;
936 GST_slave_list[slave->host_id] = NULL;
939 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
940 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
944 GNUNET_TESTBED_controller_connect (cfg, GST_host_list[slave->host_id],
945 event_mask, &slave_event_callback,
947 if (NULL != slave->controller)
949 send_controller_link_response (lcc->client, lcc->operation_id, cfg, NULL);
953 send_controller_link_response (lcc->client, lcc->operation_id, NULL,
954 "Could not connect to delegated controller");
955 GNUNET_TESTBED_controller_stop (slave->controller_proc);
956 GST_slave_list[slave->host_id] = NULL;
964 if (NULL != lcc->client)
966 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
967 GNUNET_SERVER_client_drop (lcc->client);
978 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
981 * @param client identification of the client
982 * @param message the actual message
985 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
986 const struct GNUNET_MessageHeader *message)
988 const struct GNUNET_TESTBED_InitMessage *msg;
989 struct GNUNET_TESTBED_Host *host;
990 const char *controller_hostname;
993 if (NULL != GST_context)
995 LOG_DEBUG ("We are being connected to laterally\n");
996 GNUNET_SERVER_receive_done (client, GNUNET_OK);
999 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1000 msize = ntohs (message->size);
1001 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1007 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1008 controller_hostname = (const char *) &msg[1];
1009 if ('\0' != controller_hostname[msize - 1])
1012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1015 GST_context = GNUNET_malloc (sizeof (struct Context));
1016 GNUNET_SERVER_client_keep (client);
1017 GST_context->client = client;
1018 GST_context->host_id = ntohl (msg->host_id);
1019 GST_context->master_ip = GNUNET_strdup (controller_hostname);
1020 LOG_DEBUG ("Our IP: %s\n", GST_context->master_ip);
1021 GST_context->system =
1022 GNUNET_TESTING_system_create ("testbed", GST_context->master_ip,
1025 GNUNET_TESTBED_host_create_with_id (GST_context->host_id,
1026 GST_context->master_ip, NULL,
1028 host_list_add (host);
1029 LOG_DEBUG ("Created master context with host ID: %u\n", GST_context->host_id);
1030 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1035 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1038 * @param client identification of the client
1039 * @param message the actual message
1042 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1043 const struct GNUNET_MessageHeader *message)
1045 struct GNUNET_TESTBED_Host *host;
1046 const struct GNUNET_TESTBED_AddHostMessage *msg;
1047 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1048 struct GNUNET_CONFIGURATION_Handle *host_cfg;
1054 uint16_t username_length;
1055 uint16_t hostname_length;
1056 uint16_t reply_size;
1059 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1060 msize = ntohs (msg->header.size);
1061 if (msize <= sizeof (struct GNUNET_TESTBED_AddHostMessage))
1063 GNUNET_break_op (0);
1064 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1067 username_length = ntohs (msg->username_length);
1068 hostname_length = ntohs (msg->hostname_length);
1069 /* msg must contain hostname */
1070 if ((msize <= (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1072 || (0 == hostname_length))
1074 GNUNET_break_op (0);
1075 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1078 /* msg must contain configuration */
1079 if (msize <= (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1080 username_length + hostname_length))
1082 GNUNET_break_op (0);
1083 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1089 if (0 != username_length)
1091 username = GNUNET_malloc (username_length + 1);
1092 strncpy (username, ptr, username_length);
1093 ptr += username_length;
1095 hostname = GNUNET_malloc (hostname_length + 1);
1096 strncpy (hostname, ptr, hostname_length);
1097 ptr += hostname_length;
1098 if (NULL == (host_cfg = GNUNET_TESTBED_extract_config_ (message)))
1100 GNUNET_free_non_null (username);
1101 GNUNET_free_non_null (hostname);
1102 GNUNET_break_op (0);
1103 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1106 host_id = ntohl (msg->host_id);
1107 LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
1108 LOG_DEBUG ("-------host id: %u\n", host_id);
1109 LOG_DEBUG ("-------hostname: %s\n", hostname);
1110 if (NULL != username)
1111 LOG_DEBUG ("-------username: %s\n", username);
1113 LOG_DEBUG ("-------username: <not given>\n");
1114 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1116 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1117 host_cfg, ntohs (msg->ssh_port));
1118 GNUNET_free_non_null (username);
1119 GNUNET_free (hostname);
1120 GNUNET_CONFIGURATION_destroy (host_cfg);
1123 GNUNET_break_op (0);
1124 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1127 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1128 if (GNUNET_OK != host_list_add (host))
1130 /* We are unable to add a host */
1131 emsg = "A host exists with given host-id";
1132 LOG_DEBUG ("%s: %u", emsg, host_id);
1133 GNUNET_TESTBED_host_destroy (host);
1134 reply_size += strlen (emsg) + 1;
1135 reply = GNUNET_malloc (reply_size);
1136 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1140 LOG_DEBUG ("Added host %u at %u\n", host_id, GST_context->host_id);
1141 reply = GNUNET_malloc (reply_size);
1143 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST_SUCCESS);
1144 reply->header.size = htons (reply_size);
1145 reply->host_id = htonl (host_id);
1146 GST_queue_message (client, &reply->header);
1147 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1152 * Iterator over hash map entries.
1154 * @param cls closure
1155 * @param key current key code
1156 * @param value value in the hash map
1157 * @return GNUNET_YES if we should continue to
1162 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1164 struct SharedService *queried_ss = cls;
1165 struct SharedService *ss = value;
1167 if (0 == strcmp (ss->name, queried_ss->name))
1175 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1178 * @param client identification of the client
1179 * @param message the actual message
1182 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1183 const struct GNUNET_MessageHeader *message)
1185 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1186 struct SharedService *ss;
1188 struct GNUNET_HashCode hash;
1190 uint16_t service_name_size;
1192 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1193 msg_size = ntohs (message->size);
1194 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1197 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1201 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1202 service_name = (char *) &msg[1];
1203 if ('\0' != service_name[service_name_size - 1])
1206 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1209 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1210 service_name, ntohl (msg->num_peers));
1211 if (ntohl (msg->host_id) != GST_context->host_id)
1213 route_message (ntohl (msg->host_id), message);
1214 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1217 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1218 ss = GNUNET_malloc (sizeof (struct SharedService));
1219 ss->name = strdup (service_name);
1220 ss->num_shared = ntohl (msg->num_peers);
1221 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1222 if (GNUNET_SYSERR ==
1223 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1224 &ss_exists_iterator, ss))
1226 LOG (GNUNET_ERROR_TYPE_WARNING,
1227 "Service %s already configured as a shared service. "
1228 "Ignoring service sharing request \n", ss->name);
1229 GNUNET_free (ss->name);
1233 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1234 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1239 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1242 * @param client identification of the client
1243 * @param message the actual message
1246 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1247 const struct GNUNET_MessageHeader *message)
1249 const struct GNUNET_TESTBED_ControllerLinkRequest *msg;
1250 struct GNUNET_CONFIGURATION_Handle *cfg;
1251 struct LCFContextQueue *lcfq;
1252 struct Route *route;
1253 struct Route *new_route;
1254 uint32_t delegated_host_id;
1255 uint32_t slave_host_id;
1258 if (NULL == GST_context)
1261 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1264 msize = ntohs (message->size);
1265 if (sizeof (struct GNUNET_TESTBED_ControllerLinkRequest) >= msize)
1268 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1271 msg = (const struct GNUNET_TESTBED_ControllerLinkRequest *) message;
1272 delegated_host_id = ntohl (msg->delegated_host_id);
1273 if (delegated_host_id == GST_context->host_id)
1276 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1277 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1280 if ((delegated_host_id >= GST_host_list_size) ||
1281 (NULL == GST_host_list[delegated_host_id]))
1283 LOG (GNUNET_ERROR_TYPE_WARNING,
1284 "Delegated host %u not registered with us\n", delegated_host_id);
1285 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1288 slave_host_id = ntohl (msg->slave_host_id);
1289 if ((slave_host_id >= GST_host_list_size) ||
1290 (NULL == GST_host_list[slave_host_id]))
1292 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
1294 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1297 if (slave_host_id == delegated_host_id)
1299 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1300 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1303 cfg = GNUNET_TESTBED_extract_config_ (message); /* destroy cfg here or in lcfcontext */
1306 GNUNET_break (0); /* Configuration parsing error */
1307 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1310 if (slave_host_id == GST_context->host_id) /* Link from us */
1312 struct Slave *slave;
1313 struct LinkControllersContext *lcc;
1315 if ((delegated_host_id < GST_slave_list_size) &&
1316 (NULL != GST_slave_list[delegated_host_id]))
1319 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1322 slave = GNUNET_malloc (sizeof (struct Slave));
1323 slave->host_id = delegated_host_id;
1324 slave->reghost_map = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
1325 slave_list_add (slave);
1326 if (1 != msg->is_subordinate)
1329 GNUNET_TESTBED_controller_connect (cfg, GST_host_list[slave->host_id],
1330 event_mask, &slave_event_callback,
1332 if (NULL != slave->controller)
1333 send_controller_link_response (client,
1334 GNUNET_ntohll (msg->operation_id),
1338 send_controller_link_response (client,
1339 GNUNET_ntohll (msg->operation_id),
1341 "Could not connect to delegated controller");
1342 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1345 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1346 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1347 GNUNET_SERVER_client_keep (client);
1348 lcc->client = client;
1350 slave->controller_proc =
1351 GNUNET_TESTBED_controller_start (GST_context->master_ip,
1352 GST_host_list[slave->host_id], cfg,
1353 &slave_status_callback, slave);
1354 GNUNET_CONFIGURATION_destroy (cfg);
1355 new_route = GNUNET_malloc (sizeof (struct Route));
1356 new_route->dest = delegated_host_id;
1357 new_route->thru = GST_context->host_id;
1358 route_list_add (new_route);
1362 /* Route the request */
1363 if (slave_host_id >= route_list_size)
1365 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1366 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1369 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1370 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1371 lcfq->lcf->type = CLOSURE_TYPE_LCF;
1372 lcfq->lcf->delegated_host_id = delegated_host_id;
1373 lcfq->lcf->slave_host_id = slave_host_id;
1374 route = GST_find_dest_route (slave_host_id);
1375 GNUNET_assert (NULL != route); /* because we add routes carefully */
1376 GNUNET_assert (route->dest < GST_slave_list_size);
1377 GNUNET_assert (NULL != GST_slave_list[route->dest]);
1378 lcfq->lcf->cfg = cfg;
1379 lcfq->lcf->is_subordinate = msg->is_subordinate;
1380 lcfq->lcf->state = INIT;
1381 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1382 lcfq->lcf->gateway = GST_slave_list[route->dest];
1383 GNUNET_SERVER_client_keep (client);
1384 lcfq->lcf->client = client;
1385 if (NULL == lcfq_head)
1387 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1388 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1389 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1392 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1393 /* FIXME: Adding a new route should happen after the controllers are linked
1395 if (1 != msg->is_subordinate)
1397 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1400 if ((delegated_host_id < route_list_size) &&
1401 (NULL != route_list[delegated_host_id]))
1403 GNUNET_break_op (0); /* Are you trying to link delegated host twice
1404 * with is subordinate flag set to GNUNET_YES? */
1405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1408 new_route = GNUNET_malloc (sizeof (struct Route));
1409 new_route->dest = delegated_host_id;
1410 new_route->thru = route->dest;
1411 route_list_add (new_route);
1412 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1417 * The task to be executed if the forwarded peer create operation has been
1420 * @param cls the FowardedOperationContext
1421 * @param tc the TaskContext from the scheduler
1424 peer_create_forward_timeout (void *cls,
1425 const struct GNUNET_SCHEDULER_TaskContext *tc)
1427 struct ForwardedOperationContext *fopc = cls;
1429 GNUNET_free (fopc->cls);
1430 GST_forwarded_operation_timeout (fopc, tc);
1435 * Callback to be called when forwarded peer create operation is successfull. We
1436 * have to relay the reply msg back to the client
1438 * @param cls ForwardedOperationContext
1439 * @param msg the peer create success message
1442 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1444 struct ForwardedOperationContext *fopc = cls;
1445 struct Peer *remote_peer;
1447 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER_SUCCESS)
1449 GNUNET_assert (NULL != fopc->cls);
1450 remote_peer = fopc->cls;
1451 peer_list_add (remote_peer);
1453 GST_forwarded_operation_reply_relay (fopc, msg);
1458 * Function to destroy a peer
1460 * @param peer the peer structure to destroy
1463 GST_destroy_peer (struct Peer *peer)
1465 GNUNET_break (0 == peer->reference_cnt);
1466 if (GNUNET_YES == peer->is_remote)
1468 peer_list_remove (peer);
1472 if (GNUNET_YES == peer->details.local.is_running)
1474 GNUNET_TESTING_peer_stop (peer->details.local.peer);
1475 peer->details.local.is_running = GNUNET_NO;
1477 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1478 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1479 peer_list_remove (peer);
1485 * Callback to be called when forwarded peer destroy operation is successfull. We
1486 * have to relay the reply msg back to the client
1488 * @param cls ForwardedOperationContext
1489 * @param msg the peer create success message
1492 peer_destroy_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1494 struct ForwardedOperationContext *fopc = cls;
1495 struct Peer *remote_peer;
1497 if (GNUNET_MESSAGE_TYPE_TESTBED_GENERIC_OPERATION_SUCCESS ==
1500 remote_peer = fopc->cls;
1501 GNUNET_assert (NULL != remote_peer);
1502 remote_peer->destroy_flag = GNUNET_YES;
1503 if (0 == remote_peer->reference_cnt)
1504 GST_destroy_peer (remote_peer);
1506 GST_forwarded_operation_reply_relay (fopc, msg);
1512 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1515 * @param client identification of the client
1516 * @param message the actual message
1519 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1520 const struct GNUNET_MessageHeader *message)
1522 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1523 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1524 struct GNUNET_CONFIGURATION_Handle *cfg;
1525 struct ForwardedOperationContext *fo_ctxt;
1526 struct Route *route;
1531 uint32_t config_size;
1537 msize = ntohs (message->size);
1538 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1540 GNUNET_break (0); /* We need configuration */
1541 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1544 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1545 host_id = ntohl (msg->host_id);
1546 peer_id = ntohl (msg->peer_id);
1547 if (UINT32_MAX == peer_id)
1549 GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1550 "Cannot create peer with given ID");
1551 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1554 if (host_id == GST_context->host_id)
1558 /* We are responsible for this peer */
1559 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1560 config_size = ntohl (msg->config_size);
1561 config = GNUNET_malloc (config_size);
1562 dest_size = config_size;
1565 uncompress ((Bytef *) config, (uLongf *) & dest_size,
1566 (const Bytef *) &msg[1], (uLong) msize)))
1568 GNUNET_break (0); /* uncompression error */
1569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1572 if (config_size != dest_size)
1574 GNUNET_break (0); /* Uncompressed config size mismatch */
1575 GNUNET_free (config);
1576 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1579 cfg = GNUNET_CONFIGURATION_create ();
1581 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1583 GNUNET_break (0); /* Configuration parsing error */
1584 GNUNET_free (config);
1585 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1588 GNUNET_free (config);
1589 GNUNET_CONFIGURATION_set_value_number (cfg, "TESTBED", "PEERID",
1590 (unsigned long long) peer_id);
1591 peer = GNUNET_malloc (sizeof (struct Peer));
1592 peer->is_remote = GNUNET_NO;
1593 peer->details.local.cfg = cfg;
1595 LOG_DEBUG ("Creating peer with id: %u\n", (unsigned int) peer->id);
1596 peer->details.local.peer =
1597 GNUNET_TESTING_peer_configure (GST_context->system,
1598 peer->details.local.cfg, peer->id,
1599 NULL /* Peer id */ ,
1601 if (NULL == peer->details.local.peer)
1603 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1610 peer->details.local.is_running = GNUNET_NO;
1611 peer_list_add (peer);
1613 GNUNET_malloc (sizeof
1614 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1615 reply->header.size =
1616 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1617 reply->header.type =
1618 htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER_SUCCESS);
1619 reply->peer_id = msg->peer_id;
1620 reply->operation_id = msg->operation_id;
1621 GST_queue_message (client, &reply->header);
1622 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1626 /* Forward peer create request */
1627 route = GST_find_dest_route (host_id);
1631 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1635 peer = GNUNET_malloc (sizeof (struct Peer));
1636 peer->is_remote = GNUNET_YES;
1638 peer->details.remote.slave = GST_slave_list[route->dest];
1639 peer->details.remote.remote_host_id = host_id;
1640 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1641 GNUNET_SERVER_client_keep (client);
1642 fo_ctxt->client = client;
1643 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1644 fo_ctxt->cls = peer; //GST_slave_list[route->dest]->controller;
1645 fo_ctxt->type = OP_PEER_CREATE;
1647 GNUNET_TESTBED_forward_operation_msg_ (GST_slave_list
1648 [route->dest]->controller,
1649 fo_ctxt->operation_id,
1651 peer_create_success_cb, fo_ctxt);
1652 fo_ctxt->timeout_task =
1653 GNUNET_SCHEDULER_add_delayed (GST_timeout, &peer_create_forward_timeout,
1655 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fo_ctxt);
1656 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1661 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1664 * @param client identification of the client
1665 * @param message the actual message
1668 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
1669 const struct GNUNET_MessageHeader *message)
1671 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1672 struct ForwardedOperationContext *fopc;
1676 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1677 peer_id = ntohl (msg->peer_id);
1678 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1679 peer_id, GNUNET_ntohll (msg->operation_id));
1680 if ((GST_peer_list_size <= peer_id) || (NULL == GST_peer_list[peer_id]))
1682 LOG (GNUNET_ERROR_TYPE_ERROR,
1683 "Asked to destroy a non existent peer with id: %u\n", peer_id);
1684 GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1685 "Peer doesn't exist");
1686 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1689 peer = GST_peer_list[peer_id];
1690 if (GNUNET_YES == peer->is_remote)
1692 /* Forward the destory message to sub controller */
1693 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1694 GNUNET_SERVER_client_keep (client);
1695 fopc->client = client;
1697 fopc->type = OP_PEER_DESTROY;
1698 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1700 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
1702 fopc->operation_id, &msg->header,
1703 &peer_destroy_success_cb, fopc);
1704 fopc->timeout_task =
1705 GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
1707 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1708 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1711 peer->destroy_flag = GNUNET_YES;
1712 if (0 == peer->reference_cnt)
1713 GST_destroy_peer (peer);
1715 LOG (GNUNET_ERROR_TYPE_DEBUG,
1716 "Delaying peer destroy as peer is currently in use\n");
1717 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1718 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1723 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1726 * @param client identification of the client
1727 * @param message the actual message
1730 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
1731 const struct GNUNET_MessageHeader *message)
1733 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1734 struct GNUNET_TESTBED_PeerEventMessage *reply;
1735 struct ForwardedOperationContext *fopc;
1739 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1740 peer_id = ntohl (msg->peer_id);
1741 if ((peer_id >= GST_peer_list_size) || (NULL == GST_peer_list[peer_id]))
1744 LOG (GNUNET_ERROR_TYPE_ERROR,
1745 "Asked to start a non existent peer with id: %u\n", peer_id);
1746 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1749 peer = GST_peer_list[peer_id];
1750 if (GNUNET_YES == peer->is_remote)
1752 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1753 GNUNET_SERVER_client_keep (client);
1754 fopc->client = client;
1755 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1756 fopc->type = OP_PEER_START;
1758 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
1760 fopc->operation_id, &msg->header,
1761 &GST_forwarded_operation_reply_relay,
1763 fopc->timeout_task =
1764 GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
1766 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1767 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1770 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
1772 GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1774 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1777 peer->details.local.is_running = GNUNET_YES;
1778 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1779 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEER_EVENT);
1780 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1781 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1782 reply->host_id = htonl (GST_context->host_id);
1783 reply->peer_id = msg->peer_id;
1784 reply->operation_id = msg->operation_id;
1785 GST_queue_message (client, &reply->header);
1786 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1791 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1794 * @param client identification of the client
1795 * @param message the actual message
1798 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
1799 const struct GNUNET_MessageHeader *message)
1801 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1802 struct GNUNET_TESTBED_PeerEventMessage *reply;
1803 struct ForwardedOperationContext *fopc;
1807 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1808 peer_id = ntohl (msg->peer_id);
1809 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PEER_STOP for peer %u\n", peer_id);
1810 if ((peer_id >= GST_peer_list_size) || (NULL == GST_peer_list[peer_id]))
1812 GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1814 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1817 peer = GST_peer_list[peer_id];
1818 if (GNUNET_YES == peer->is_remote)
1820 LOG (GNUNET_ERROR_TYPE_DEBUG, "Forwarding PEER_STOP for peer %u\n",
1822 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1823 GNUNET_SERVER_client_keep (client);
1824 fopc->client = client;
1825 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1826 fopc->type = OP_PEER_STOP;
1828 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
1830 fopc->operation_id, &msg->header,
1831 &GST_forwarded_operation_reply_relay,
1833 fopc->timeout_task =
1834 GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
1836 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1837 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1840 if (GNUNET_OK != GNUNET_TESTING_peer_kill (peer->details.local.peer))
1842 LOG (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %u failed\n", peer_id);
1843 GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1844 "Peer not running");
1845 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1848 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %u successfully stopped\n", peer_id);
1849 peer->details.local.is_running = GNUNET_NO;
1850 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1851 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEER_EVENT);
1852 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1853 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1854 reply->host_id = htonl (GST_context->host_id);
1855 reply->peer_id = msg->peer_id;
1856 reply->operation_id = msg->operation_id;
1857 GST_queue_message (client, &reply->header);
1858 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1859 GNUNET_TESTING_peer_wait (peer->details.local.peer);
1864 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1867 * @param client identification of the client
1868 * @param message the actual message
1871 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
1872 const struct GNUNET_MessageHeader *message)
1874 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1875 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1884 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1885 peer_id = ntohl (msg->peer_id);
1886 if ((peer_id >= GST_peer_list_size) || (NULL == GST_peer_list[peer_id]))
1888 GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1890 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1893 peer = GST_peer_list[peer_id];
1894 if (GNUNET_YES == peer->is_remote)
1896 struct ForwardedOperationContext *fopc;
1898 LOG_DEBUG ("Forwarding PEER_GET_CONFIG for peer: %u\n", peer_id);
1899 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1900 GNUNET_SERVER_client_keep (client);
1901 fopc->client = client;
1902 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1903 fopc->type = OP_PEER_INFO;
1905 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
1907 fopc->operation_id, &msg->header,
1908 &GST_forwarded_operation_reply_relay,
1910 fopc->timeout_task =
1911 GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
1913 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1914 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1917 LOG_DEBUG ("Received PEER_GET_CONFIG for peer: %u\n", peer_id);
1919 GNUNET_CONFIGURATION_serialize (GST_peer_list[peer_id]->details.local.cfg,
1921 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1922 GNUNET_free (config);
1925 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1926 reply = GNUNET_realloc (xconfig, msize);
1927 (void) memmove (&reply[1], reply, xc_size);
1928 reply->header.size = htons (msize);
1929 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEER_CONFIGURATION);
1930 reply->peer_id = msg->peer_id;
1931 reply->operation_id = msg->operation_id;
1932 GNUNET_TESTING_peer_get_identity (GST_peer_list[peer_id]->details.local.peer,
1933 &reply->peer_identity);
1934 reply->config_size = htons ((uint16_t) c_size);
1935 GST_queue_message (client, &reply->header);
1936 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1941 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
1944 * @param client identification of the client
1945 * @param message the actual message
1948 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
1949 const struct GNUNET_MessageHeader *message)
1951 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
1952 struct Slave *slave;
1953 struct GNUNET_TESTBED_SlaveConfiguration *reply;
1954 const struct GNUNET_CONFIGURATION_Handle *cfg;
1958 size_t xconfig_size;
1963 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
1964 slave_id = ntohl (msg->slave_id);
1965 op_id = GNUNET_ntohll (msg->operation_id);
1966 if ((GST_slave_list_size <= slave_id) || (NULL == GST_slave_list[slave_id]))
1968 /* FIXME: Add forwardings for this type of message here.. */
1969 GST_send_operation_fail_msg (client, op_id, "Slave not found");
1970 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1973 slave = GST_slave_list[slave_id];
1974 GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (GST_host_list[slave->host_id])));
1975 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1977 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1978 GNUNET_free (config);
1979 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
1980 GNUNET_break (reply_size <= UINT16_MAX);
1981 GNUNET_break (config_size <= UINT16_MAX);
1982 reply = GNUNET_realloc (xconfig, reply_size);
1983 (void) memmove (&reply[1], reply, xconfig_size);
1984 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVE_CONFIGURATION);
1985 reply->header.size = htons ((uint16_t) reply_size);
1986 reply->slave_id = msg->slave_id;
1987 reply->operation_id = msg->operation_id;
1988 reply->config_size = htons ((uint16_t) config_size);
1989 GST_queue_message (client, &reply->header);
1990 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1995 * Clears the forwarded operations queue
2000 struct ForwardedOperationContext *fopc;
2002 while (NULL != (fopc = fopcq_head))
2004 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
2005 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
2006 if (GNUNET_SCHEDULER_NO_TASK != fopc->timeout_task)
2007 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
2008 GNUNET_SERVER_client_drop (fopc->client);
2011 case OP_PEER_CREATE:
2012 GNUNET_free (fopc->cls);
2014 case OP_SHUTDOWN_PEERS:
2016 struct HandlerContext_ShutdownPeers *hc = fopc->cls;
2018 GNUNET_assert (0 < hc->nslaves);
2020 if (0 == hc->nslaves)
2026 case OP_PEER_DESTROY:
2028 case OP_OVERLAY_CONNECT:
2029 case OP_LINK_CONTROLLERS:
2030 case OP_GET_SLAVE_CONFIG:
2041 * Task run upon timeout of forwarded SHUTDOWN_PEERS operation
2043 * @param cls the ForwardedOperationContext
2044 * @param tc the scheduler task context
2047 shutdown_peers_timeout_cb (void *cls,
2048 const struct GNUNET_SCHEDULER_TaskContext *tc)
2050 struct ForwardedOperationContext *fo_ctxt = cls;
2051 struct HandlerContext_ShutdownPeers *hc;
2053 fo_ctxt->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2055 hc->timeout = GNUNET_YES;
2056 GNUNET_assert (0 < hc->nslaves);
2058 if (0 == hc->nslaves)
2059 GST_send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id,
2060 "Timeout at a slave controller");
2061 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
2062 GNUNET_SERVER_client_drop (fo_ctxt->client);
2063 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fo_ctxt);
2064 GNUNET_free (fo_ctxt);
2069 * The reply msg handler forwarded SHUTDOWN_PEERS operation. Checks if a
2070 * success reply is received from all clients and then sends the success message
2073 * @param cls ForwardedOperationContext
2074 * @param msg the message to relay
2077 shutdown_peers_reply_cb (void *cls,
2078 const struct GNUNET_MessageHeader *msg)
2080 struct ForwardedOperationContext *fo_ctxt = cls;
2081 struct HandlerContext_ShutdownPeers *hc;
2084 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != fo_ctxt->timeout_task);
2085 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
2086 fo_ctxt->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2087 GNUNET_assert (0 < hc->nslaves);
2089 if (GNUNET_MESSAGE_TYPE_TESTBED_GENERIC_OPERATION_SUCCESS !=
2091 hc->timeout = GNUNET_YES;
2092 if (0 == hc->nslaves)
2094 if (GNUNET_YES == hc->timeout)
2095 GST_send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id,
2096 "Timeout at a slave controller");
2098 send_operation_success_msg (fo_ctxt->client, fo_ctxt->operation_id);
2100 GNUNET_SERVER_client_drop (fo_ctxt->client);
2101 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fo_ctxt);
2102 GNUNET_free (fo_ctxt);
2107 * Stops and destroys all peers
2115 if (NULL == GST_peer_list)
2117 for (id = 0; id < GST_peer_list_size; id++)
2119 peer = GST_peer_list[id];
2122 /* If destroy flag is set it means that this peer should have been
2123 * destroyed by a context which we destroy before */
2124 GNUNET_break (GNUNET_NO == peer->destroy_flag);
2125 /* counter should be zero as we free all contexts before */
2126 GNUNET_break (0 == peer->reference_cnt);
2127 if ((GNUNET_NO == peer->is_remote) &&
2128 (GNUNET_YES == peer->details.local.is_running))
2129 GNUNET_TESTING_peer_kill (peer->details.local.peer);
2131 for (id = 0; id < GST_peer_list_size; id++)
2133 peer = GST_peer_list[id];
2136 if (GNUNET_NO == peer->is_remote)
2138 if (GNUNET_YES == peer->details.local.is_running)
2139 GNUNET_TESTING_peer_wait (peer->details.local.peer);
2140 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
2141 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
2145 GNUNET_free_non_null (GST_peer_list);
2146 GST_peer_list = NULL;
2147 GST_peer_list_size = 0;
2152 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS messages
2155 * @param client identification of the client
2156 * @param message the actual message
2159 handle_shutdown_peers (void *cls, struct GNUNET_SERVER_Client *client,
2160 const struct GNUNET_MessageHeader *message)
2162 const struct GNUNET_TESTBED_ShutdownPeersMessage *msg;
2163 struct HandlerContext_ShutdownPeers *hc;
2164 struct Slave *slave;
2165 struct ForwardedOperationContext *fo_ctxt;
2169 msg = (const struct GNUNET_TESTBED_ShutdownPeersMessage *) message;
2170 LOG_DEBUG ("Received SHUTDOWN_PEERS\n");
2171 /* Stop and destroy all peers */
2175 /* Forward to all slaves which we have started */
2176 op_id = GNUNET_ntohll (msg->operation_id);
2177 hc = GNUNET_malloc (sizeof (struct HandlerContext_ShutdownPeers));
2178 /* FIXME: have a better implementation where we track which slaves are
2179 started by this controller */
2180 for (cnt = 0; cnt < GST_slave_list_size; cnt++)
2182 slave = GST_slave_list[cnt];
2185 if (NULL == slave->controller_proc) /* We didn't start the slave */
2187 LOG_DEBUG ("Forwarding SHUTDOWN_PEERS\n");
2189 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2190 GNUNET_SERVER_client_keep (client);
2191 fo_ctxt->client = client;
2192 fo_ctxt->operation_id = op_id;
2194 fo_ctxt->type = OP_SHUTDOWN_PEERS;
2196 GNUNET_TESTBED_forward_operation_msg_ (slave->controller,
2197 fo_ctxt->operation_id,
2199 shutdown_peers_reply_cb,
2201 fo_ctxt->timeout_task =
2202 GNUNET_SCHEDULER_add_delayed (GST_timeout, &shutdown_peers_timeout_cb,
2204 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fo_ctxt);
2206 LOG_DEBUG ("Shutting down peers\n");
2208 if (0 == hc->nslaves)
2210 send_operation_success_msg (client, op_id);
2213 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2218 * Iterator over hash map entries.
2220 * @param cls closure
2221 * @param key current key code
2222 * @param value value in the hash map
2223 * @return GNUNET_YES if we should continue to
2228 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
2230 struct SharedService *ss = value;
2232 GNUNET_assert (GNUNET_YES ==
2233 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
2234 GNUNET_free (ss->name);
2241 * Iterator for freeing hash map entries in a slave's reghost_map
2243 * @param cls handle to the slave
2244 * @param key current key code
2245 * @param value value in the hash map
2246 * @return GNUNET_YES if we should continue to
2251 reghost_free_iterator (void *cls, const struct GNUNET_HashCode *key,
2254 struct Slave *slave = cls;
2255 struct RegisteredHostContext *rhc = value;
2256 struct ForwardedOverlayConnectContext *focc;
2258 GNUNET_assert (GNUNET_YES ==
2259 GNUNET_CONTAINER_multihashmap_remove (slave->reghost_map, key,
2261 while (NULL != (focc = rhc->focc_dll_head))
2263 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
2264 GST_cleanup_focc (focc);
2266 if (NULL != rhc->sub_op)
2267 GNUNET_TESTBED_operation_done (rhc->sub_op);
2268 if (NULL != rhc->client)
2269 GNUNET_SERVER_client_drop (rhc->client);
2270 GNUNET_free (value);
2276 * Task to clean up and shutdown nicely
2279 * @param tc the TaskContext from scheduler
2282 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2284 struct LCFContextQueue *lcfq;
2285 struct MessageQueue *mq_entry;
2288 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
2289 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
2290 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
2292 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
2293 /* cleanup any remaining forwarded operations */
2295 if (NULL != lcfq_head)
2297 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
2299 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
2300 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
2303 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2304 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2306 GNUNET_SERVER_client_drop (lcfq->lcf->client);
2307 GNUNET_assert (NULL != lcfq->lcf->cfg);
2308 GNUNET_CONFIGURATION_destroy (lcfq->lcf->cfg);
2309 GNUNET_free (lcfq->lcf);
2310 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2315 /* Clear peer list */
2317 /* Clear host list */
2318 for (id = 0; id < GST_host_list_size; id++)
2319 if (NULL != GST_host_list[id])
2320 GNUNET_TESTBED_host_destroy (GST_host_list[id]);
2321 GNUNET_free_non_null (GST_host_list);
2322 /* Clear route list */
2323 for (id = 0; id < route_list_size; id++)
2324 if (NULL != route_list[id])
2325 GNUNET_free (route_list[id]);
2326 GNUNET_free_non_null (route_list);
2327 /* Clear GST_slave_list */
2328 for (id = 0; id < GST_slave_list_size; id++)
2329 if (NULL != GST_slave_list[id])
2331 struct HostRegistration *hr_entry;
2333 while (NULL != (hr_entry = GST_slave_list[id]->hr_dll_head))
2335 GNUNET_CONTAINER_DLL_remove (GST_slave_list[id]->hr_dll_head,
2336 GST_slave_list[id]->hr_dll_tail, hr_entry);
2337 GNUNET_free (hr_entry);
2339 if (NULL != GST_slave_list[id]->rhandle)
2340 GNUNET_TESTBED_cancel_registration (GST_slave_list[id]->rhandle);
2342 GNUNET_CONTAINER_multihashmap_iterate (GST_slave_list
2344 reghost_free_iterator,
2345 GST_slave_list[id]);
2346 GNUNET_CONTAINER_multihashmap_destroy (GST_slave_list[id]->reghost_map);
2347 if (NULL != GST_slave_list[id]->controller)
2348 GNUNET_TESTBED_controller_disconnect (GST_slave_list[id]->controller);
2349 if (NULL != GST_slave_list[id]->controller_proc)
2350 GNUNET_TESTBED_controller_stop (GST_slave_list[id]->controller_proc);
2351 GNUNET_free (GST_slave_list[id]);
2353 GNUNET_free_non_null (GST_slave_list);
2354 if (NULL != GST_context)
2356 GNUNET_free_non_null (GST_context->master_ip);
2357 if (NULL != GST_context->system)
2358 GNUNET_TESTING_system_destroy (GST_context->system, GNUNET_YES);
2359 GNUNET_SERVER_client_drop (GST_context->client);
2360 GNUNET_free (GST_context);
2363 if (NULL != transmit_handle)
2364 GNUNET_SERVER_notify_transmit_ready_cancel (transmit_handle);
2365 while (NULL != (mq_entry = mq_head))
2367 GNUNET_free (mq_entry->msg);
2368 GNUNET_SERVER_client_drop (mq_entry->client);
2369 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
2370 GNUNET_free (mq_entry);
2372 GNUNET_free_non_null (hostname);
2373 GNUNET_CONFIGURATION_destroy (our_config);
2374 /* Free hello cache */
2376 GNUNET_TESTBED_operation_queue_destroy_ (GST_opq_openfds);
2377 GST_opq_openfds = NULL;
2382 * Callback for client disconnect
2385 * @param client the client which has disconnected
2388 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2390 if (NULL == GST_context)
2392 if (client == GST_context->client)
2394 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2395 /* should not be needed as we're terminated by failure to read
2396 * from stdin, but if stdin fails for some reason, this shouldn't
2397 * hurt for now --- might need to revise this later if we ever
2398 * decide that master connections might be temporarily down
2399 * for some reason */
2400 //GNUNET_SCHEDULER_shutdown ();
2408 * @param cls closure
2409 * @param server the initialized server
2410 * @param cfg configuration to use
2413 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
2414 const struct GNUNET_CONFIGURATION_Handle *cfg)
2416 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
2417 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2418 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST, 0},
2419 {&handle_configure_shared_service, NULL,
2420 GNUNET_MESSAGE_TYPE_TESTBED_SHARE_SERVICE, 0},
2421 {&handle_link_controllers, NULL,
2422 GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS, 0},
2423 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER, 0},
2424 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROY_PEER,
2425 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2426 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_START_PEER,
2427 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2428 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOP_PEER,
2429 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2430 {&handle_peer_get_config, NULL,
2431 GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_CONFIGURATION,
2432 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2433 {&GST_handle_overlay_connect, NULL,
2434 GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT,
2435 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2436 {&GST_handle_remote_overlay_connect, NULL,
2437 GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT, 0},
2438 {&handle_slave_get_config, NULL,
2439 GNUNET_MESSAGE_TYPE_TESTBED_GET_SLAVE_CONFIGURATION,
2440 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
2441 {&handle_shutdown_peers, NULL, GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS,
2442 sizeof (struct GNUNET_TESTBED_ShutdownPeersMessage)},
2446 unsigned long long num;
2449 GNUNET_CONFIGURATION_get_value_filename (cfg, "TESTBED", "LOG_FILE",
2452 GNUNET_break (GNUNET_OK == GNUNET_log_setup ("testbed", "DEBUG", logfile));
2453 GNUNET_free (logfile);
2455 GNUNET_assert (GNUNET_OK ==
2456 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
2457 "CACHE_SIZE", &num));
2458 GST_cache_init ((unsigned int) num);
2459 GNUNET_assert (GNUNET_OK ==
2460 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
2461 "MAX_OPEN_FDS", &num));
2462 GST_opq_openfds = GNUNET_TESTBED_operation_queue_create_ ((unsigned int) num);
2463 GNUNET_assert (GNUNET_OK ==
2464 GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
2465 "OPERATION_TIMEOUT",
2467 GNUNET_TIME_Relative *)
2469 GNUNET_assert (GNUNET_OK ==
2470 GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
2471 "HOSTNAME", &hostname));
2472 our_config = GNUNET_CONFIGURATION_dup (cfg);
2473 GNUNET_SERVER_add_handlers (server, message_handlers);
2474 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
2475 ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
2477 GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
2478 GNUNET_SCHEDULER_PRIORITY_IDLE,
2479 &shutdown_task, NULL);
2480 LOG_DEBUG ("Testbed startup complete\n");
2481 event_mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
2486 * The starting point of execution
2489 main (int argc, char *const *argv)
2491 //sleep (15); /* Debugging */
2492 return (GNUNET_OK ==
2493 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
2494 &testbed_run, NULL)) ? 0 : 1;
2497 /* end of gnunet-service-testbed.c */