2 This file is part of GNUnet.
3 Copyright (C) 2008--2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file testbed/gnunet-service-testbed-logger.c
23 * @brief service for collecting messages and writing to a file
24 * @author Sree Harsha Totakura
28 #include "gnunet_util_lib.h"
31 * Generic logging shorthand
33 #define LOG(type, ...) \
34 GNUNET_log (type, __VA_ARGS__)
37 * Debug logging shorthand
39 #define LOG_DEBUG(...) \
40 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
43 * The message queue for sending messages to clients
48 * The message to be sent
50 struct GNUNET_MessageHeader *msg;
53 * The client to send the message to
55 struct GNUNET_SERVER_Client *client;
58 * next pointer for DLL
60 struct MessageQueue *next;
63 * prev pointer for DLL
65 struct MessageQueue *prev;
69 * The message queue head
71 static struct MessageQueue *mq_head;
74 * The message queue tail
76 static struct MessageQueue *mq_tail;
79 * Handle for buffered writing.
81 struct GNUNET_BIO_WriteHandle *bio;
84 * The number of connections we have
86 static unsigned int nconn;
89 * Are we shutting down?
91 static int in_shutdown;
95 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
98 * @param client identification of the client
99 * @param msg the actual message
102 handle_log_msg (void *cls,
103 struct GNUNET_SERVER_Client *client,
104 const struct GNUNET_MessageHeader *msg)
108 ms = ntohs (msg->size);
109 ms -= sizeof (struct GNUNET_MessageHeader);
110 GNUNET_BIO_write (bio, &msg[1], ms);
111 GNUNET_SERVER_receive_done (client, GNUNET_OK);
116 * Task to clean up and shutdown nicely
121 shutdown_task (void *cls)
123 struct MessageQueue *mq_entry;
125 in_shutdown = GNUNET_YES;
128 /* Delay shutdown if there are active connections */
129 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
132 while (NULL != (mq_entry = mq_head))
134 GNUNET_free (mq_entry->msg);
135 GNUNET_SERVER_client_drop (mq_entry->client);
136 GNUNET_CONTAINER_DLL_remove (mq_head,
139 GNUNET_free (mq_entry);
141 GNUNET_break (GNUNET_OK == GNUNET_BIO_write_close (bio));
146 x * Functions with this signature are called whenever a client
147 * is disconnected on the network level.
150 * @param client identification of the client; NULL
151 * for the last call when the server is destroyed
154 client_disconnected (void *cls,
155 struct GNUNET_SERVER_Client *client)
159 GNUNET_break (0 == nconn);
163 if (GNUNET_YES == in_shutdown)
164 GNUNET_SCHEDULER_shutdown ();
169 * Functions with this signature are called whenever a client
170 * is connected on the network level.
173 * @param client identification of the client
176 client_connected (void *cls,
177 struct GNUNET_SERVER_Client *client)
181 GNUNET_break (0 == nconn);
184 GNUNET_SERVER_client_persist_ (client);
193 * @param server the initialized server
194 * @param cfg configuration to use
197 logger_run (void *cls,
198 struct GNUNET_SERVER_Handle *server,
199 const struct GNUNET_CONFIGURATION_Handle *cfg)
201 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
202 {&handle_log_msg, NULL, GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG, 0},
212 GNUNET_CONFIGURATION_get_value_filename (cfg,
217 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
220 GNUNET_SCHEDULER_shutdown ();
224 hname_len = GNUNET_OS_get_hostname_max_length ();
225 hname = GNUNET_malloc (hname_len);
226 if (0 != gethostname (hname, hname_len))
228 LOG (GNUNET_ERROR_TYPE_ERROR,
229 "Cannot get hostname. Exiting\n");
232 GNUNET_SCHEDULER_shutdown ();
235 GNUNET_asprintf (&fn,
243 if (NULL == (bio = GNUNET_BIO_write_open (fn)))
246 GNUNET_SCHEDULER_shutdown ();
250 GNUNET_SERVER_add_handlers (server, message_handlers);
251 GNUNET_SERVER_connect_notify (server, &client_connected, NULL);
252 GNUNET_SERVER_disconnect_notify (server, &client_disconnected, NULL);
253 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
254 LOG_DEBUG ("TESTBED-LOGGER startup complete\n");
259 * The starting point of execution
262 main (int argc, char *const *argv)
265 GNUNET_SERVICE_run (argc, argv, "testbed-logger",
266 GNUNET_SERVICE_OPTION_NONE,
267 &logger_run, NULL)) ? 0 : 1;
270 /* end of gnunet-service-testbed-logger.c */