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 it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file testbed-logger/gnunet-service-testbed-logger.c
18 * @brief service for collecting messages and writing to a file
19 * @author Sree Harsha Totakura
23 #include "gnunet_util_lib.h"
26 * Generic logging shorthand
28 #define LOG(type, ...) \
29 GNUNET_log (type, __VA_ARGS__)
32 * Debug logging shorthand
34 #define LOG_DEBUG(...) \
35 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
38 * Handle for buffered writing.
40 struct GNUNET_BIO_WriteHandle *bio;
43 * The number of connections we have
45 static unsigned int nconn;
48 * Are we shutting down?
50 static int in_shutdown;
54 * Check #GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG messages
56 * @param cls client identification of the client
57 * @param msg the actual message
58 * @return #GNUNET_OK (they are all always OK)
61 check_log_msg (void *cls,
62 const struct GNUNET_MessageHeader *msg)
69 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG messages
71 * @param cls client identification of the client
72 * @param msg the actual message
75 handle_log_msg (void *cls,
76 const struct GNUNET_MessageHeader *msg)
78 struct GNUNET_SERVICE_Client *client = cls;
81 ms = ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader);
82 GNUNET_BIO_write (bio,
85 GNUNET_SERVICE_client_continue (client);
90 * Task to clean up and shutdown nicely
95 shutdown_task (void *cls)
97 in_shutdown = GNUNET_YES;
100 /* Delay shutdown if there are active connections */
101 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
105 GNUNET_break (GNUNET_OK ==
106 GNUNET_BIO_write_close (bio));
111 * Callback called when a client connects to the service.
113 * @param cls closure for the service
114 * @param c the new client that connected to the service
115 * @param mq the message queue used to send messages to the client
119 client_connect_cb (void *cls,
120 struct GNUNET_SERVICE_Client *c,
121 struct GNUNET_MQ_Handle *mq)
123 /* FIXME: is this really what we want here? */
124 GNUNET_SERVICE_client_persist (c);
131 * Callback called when a client disconnected from the service
133 * @param cls closure for the service
134 * @param c the client that disconnected
135 * @param internal_cls should be equal to @a c
138 client_disconnect_cb (void *cls,
139 struct GNUNET_SERVICE_Client *c,
143 if (GNUNET_YES == in_shutdown)
144 GNUNET_SCHEDULER_shutdown ();
145 GNUNET_assert (c == internal_cls);
153 * @param cfg configuration to use
154 * @param service the initialized service
157 logger_run (void *cls,
158 const struct GNUNET_CONFIGURATION_Handle *cfg,
159 struct GNUNET_SERVICE_Handle *service)
168 GNUNET_CONFIGURATION_get_value_filename (cfg,
173 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
176 GNUNET_SCHEDULER_shutdown ();
180 hname_len = GNUNET_OS_get_hostname_max_length ();
181 hname = GNUNET_malloc (hname_len);
182 if (0 != gethostname (hname,
185 LOG (GNUNET_ERROR_TYPE_ERROR,
186 "Cannot get hostname. Exiting\n");
189 GNUNET_SCHEDULER_shutdown ();
192 GNUNET_asprintf (&fn,
200 if (NULL == (bio = GNUNET_BIO_write_open (fn)))
203 GNUNET_SCHEDULER_shutdown ();
207 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
209 LOG_DEBUG ("TESTBED-LOGGER startup complete\n");
214 * Define "main" method using service macro.
218 GNUNET_SERVICE_OPTION_NONE,
221 &client_disconnect_cb,
223 GNUNET_MQ_hd_var_size (log_msg,
224 GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG,
225 struct GNUNET_MessageHeader,
227 GNUNET_MQ_handler_end ());
230 /* end of gnunet-service-testbed-logger.c */