2 This file is part of GNUnet.
3 (C) 2012 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
28 #include "gnunet_service_lib.h"
29 #include "gnunet_server_lib.h"
32 #include "gnunet_testbed_service.h"
34 #define LOG(kind,...) \
35 GNUNET_log (kind, __VA_ARGS__)
41 * The client handle associated with this context
43 struct GNUNET_SERVER_Client *client;
46 * Event mask of event to be responded in this context
51 * Our host id according to this context
58 * The master context; generated with the first INIT message
60 static struct Context *master_context;
63 * The shutdown task handle
65 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
69 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
72 * @param client identification of the client
73 * @param message the actual message
76 handle_init (void *cls,
77 struct GNUNET_SERVER_Client *client,
78 const struct GNUNET_MessageHeader *message)
80 const struct GNUNET_TESTBED_InitMessage *msg;
82 if (NULL != master_context)
85 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
88 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
89 master_context = GNUNET_malloc (sizeof (struct Context));
90 master_context->client = client;
91 master_context->host_id = ntohl (msg->host_id);
92 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
93 GNUNET_SERVER_client_keep (client);
94 LOG (GNUNET_ERROR_TYPE_DEBUG,
95 "Created master context with host ID: %u\n", master_context->host_id);
96 GNUNET_SERVER_receive_done (client, GNUNET_OK);
101 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
104 * @param client identification of the client
105 * @param message the actual message
108 handle_addhost (void *cls,
109 struct GNUNET_SERVER_Client *client,
110 const struct GNUNET_MessageHeader *message)
112 struct GNUNET_TESTBED_Host *host;
113 const struct GNUNET_TESTBED_AddHostMessage *msg;
116 uint16_t username_length;
117 uint16_t hostname_length;
119 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
120 username_length = ntohs (msg->user_name_length);
121 username_length = (0 == username_length) ? 0 : username_length + 1;
122 username = (char *) &(msg[1]);
123 hostname = username + username_length;
124 if (ntohs (message->size) <=
125 (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length))
128 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
131 hostname_length = ntohs (message->size)
132 - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
133 if (strlen (hostname) != hostname_length)
136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
139 host = GNUNET_TESTBED_host_create (hostname, username, ntohs
141 /* Store host in a hashmap? But the host_id will be different */
145 * Task to clean up and shutdown nicely
148 * @param tc the TaskContext from scheduler
151 shutdown_task (void *cls,
152 const struct GNUNET_SCHEDULER_TaskContext *tc)
154 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
155 GNUNET_free_non_null (master_context);
160 * Callback for client disconnect
163 * @param client the client which has disconnected
166 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
168 if (NULL == master_context)
170 if (client == master_context->client)
172 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
173 GNUNET_SERVER_client_drop (client);
174 GNUNET_SCHEDULER_cancel (shutdown_task_id);
176 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
185 * @param server the initialized server
186 * @param cfg configuration to use
189 testbed_run (void *cls,
190 struct GNUNET_SERVER_Handle *server,
191 const struct GNUNET_CONFIGURATION_Handle *cfg)
193 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
195 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
196 sizeof (struct GNUNET_TESTBED_InitMessage)},
197 {&handle_addhost, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
201 GNUNET_SERVER_add_handlers (server,
203 GNUNET_SERVER_disconnect_notify (server,
204 &client_disconnect_cb,
207 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
214 * The starting point of execution
216 int main (int argc, char *const *argv)
220 GNUNET_SERVICE_run (argc,
223 GNUNET_SERVICE_OPTION_NONE,