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 Affero 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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file testbed-logger/gnunet-service-testbed-logger.c
21 * @brief service for collecting messages and writing to a file
22 * @author Sree Harsha Totakura
26 #include "gnunet_util_lib.h"
29 * Generic logging shorthand
31 #define LOG(type, ...) \
32 GNUNET_log (type, __VA_ARGS__)
35 * Debug logging shorthand
37 #define LOG_DEBUG(...) \
38 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
41 * Handle for buffered writing.
43 struct GNUNET_BIO_WriteHandle *bio;
46 * The number of connections we have
48 static unsigned int nconn;
51 * Are we shutting down?
53 static int in_shutdown;
57 * Check #GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG messages
59 * @param cls client identification of the client
60 * @param msg the actual message
61 * @return #GNUNET_OK (they are all always OK)
64 check_log_msg (void *cls,
65 const struct GNUNET_MessageHeader *msg)
72 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG messages
74 * @param cls client identification of the client
75 * @param msg the actual message
78 handle_log_msg (void *cls,
79 const struct GNUNET_MessageHeader *msg)
81 struct GNUNET_SERVICE_Client *client = cls;
84 ms = ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader);
85 GNUNET_BIO_write (bio,
88 GNUNET_SERVICE_client_continue (client);
93 * Task to clean up and shutdown nicely
98 shutdown_task (void *cls)
100 in_shutdown = GNUNET_YES;
103 /* Delay shutdown if there are active connections */
104 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
108 GNUNET_break (GNUNET_OK ==
109 GNUNET_BIO_write_close (bio));
114 * Callback called when a client connects to the service.
116 * @param cls closure for the service
117 * @param c the new client that connected to the service
118 * @param mq the message queue used to send messages to the client
122 client_connect_cb (void *cls,
123 struct GNUNET_SERVICE_Client *c,
124 struct GNUNET_MQ_Handle *mq)
126 /* FIXME: is this really what we want here? */
127 GNUNET_SERVICE_client_persist (c);
134 * Callback called when a client disconnected from the service
136 * @param cls closure for the service
137 * @param c the client that disconnected
138 * @param internal_cls should be equal to @a c
141 client_disconnect_cb (void *cls,
142 struct GNUNET_SERVICE_Client *c,
146 if (GNUNET_YES == in_shutdown)
147 GNUNET_SCHEDULER_shutdown ();
148 GNUNET_assert (c == internal_cls);
156 * @param cfg configuration to use
157 * @param service the initialized service
160 logger_run (void *cls,
161 const struct GNUNET_CONFIGURATION_Handle *cfg,
162 struct GNUNET_SERVICE_Handle *service)
171 GNUNET_CONFIGURATION_get_value_filename (cfg,
176 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
179 GNUNET_SCHEDULER_shutdown ();
183 hname_len = GNUNET_OS_get_hostname_max_length ();
184 hname = GNUNET_malloc (hname_len);
185 if (0 != gethostname (hname,
188 LOG (GNUNET_ERROR_TYPE_ERROR,
189 "Cannot get hostname. Exiting\n");
192 GNUNET_SCHEDULER_shutdown ();
195 GNUNET_asprintf (&fn,
203 if (NULL == (bio = GNUNET_BIO_write_open (fn)))
206 GNUNET_SCHEDULER_shutdown ();
210 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
212 LOG_DEBUG ("TESTBED-LOGGER startup complete\n");
217 * Define "main" method using service macro.
221 GNUNET_SERVICE_OPTION_NONE,
224 &client_disconnect_cb,
226 GNUNET_MQ_hd_var_size (log_msg,
227 GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG,
228 struct GNUNET_MessageHeader,
230 GNUNET_MQ_handler_end ());
233 /* end of gnunet-service-testbed-logger.c */