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-logger/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 * Handle for buffered writing.
45 struct GNUNET_BIO_WriteHandle *bio;
48 * The number of connections we have
50 static unsigned int nconn;
53 * Are we shutting down?
55 static int in_shutdown;
59 * Check #GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG messages
61 * @param cls client identification of the client
62 * @param msg the actual message
63 * @return #GNUNET_OK (they are all always OK)
66 check_log_msg (void *cls,
67 const struct GNUNET_MessageHeader *msg)
74 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG messages
76 * @param cls client identification of the client
77 * @param msg the actual message
80 handle_log_msg (void *cls,
81 const struct GNUNET_MessageHeader *msg)
83 struct GNUNET_SERVICE_Client *client = cls;
86 ms = ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader);
87 GNUNET_BIO_write (bio,
90 GNUNET_SERVICE_client_continue (client);
95 * Task to clean up and shutdown nicely
100 shutdown_task (void *cls)
102 in_shutdown = GNUNET_YES;
105 /* Delay shutdown if there are active connections */
106 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
110 GNUNET_break (GNUNET_OK ==
111 GNUNET_BIO_write_close (bio));
116 * Callback called when a client connects to the service.
118 * @param cls closure for the service
119 * @param c the new client that connected to the service
120 * @param mq the message queue used to send messages to the client
124 client_connect_cb (void *cls,
125 struct GNUNET_SERVICE_Client *c,
126 struct GNUNET_MQ_Handle *mq)
128 /* FIXME: is this really what we want here? */
129 GNUNET_SERVICE_client_persist (c);
136 * Callback called when a client disconnected from the service
138 * @param cls closure for the service
139 * @param c the client that disconnected
140 * @param internal_cls should be equal to @a c
143 client_disconnect_cb (void *cls,
144 struct GNUNET_SERVICE_Client *c,
148 if (GNUNET_YES == in_shutdown)
149 GNUNET_SCHEDULER_shutdown ();
150 GNUNET_assert (c == internal_cls);
158 * @param cfg configuration to use
159 * @param service the initialized service
162 logger_run (void *cls,
163 const struct GNUNET_CONFIGURATION_Handle *cfg,
164 struct GNUNET_SERVICE_Handle *service)
173 GNUNET_CONFIGURATION_get_value_filename (cfg,
178 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
181 GNUNET_SCHEDULER_shutdown ();
185 hname_len = GNUNET_OS_get_hostname_max_length ();
186 hname = GNUNET_malloc (hname_len);
187 if (0 != gethostname (hname,
190 LOG (GNUNET_ERROR_TYPE_ERROR,
191 "Cannot get hostname. Exiting\n");
194 GNUNET_SCHEDULER_shutdown ();
197 GNUNET_asprintf (&fn,
205 if (NULL == (bio = GNUNET_BIO_write_open (fn)))
208 GNUNET_SCHEDULER_shutdown ();
212 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
214 LOG_DEBUG ("TESTBED-LOGGER startup complete\n");
219 * Define "main" method using service macro.
223 GNUNET_SERVICE_OPTION_NONE,
226 &client_disconnect_cb,
228 GNUNET_MQ_hd_var_size (log_msg,
229 GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG,
230 struct GNUNET_MessageHeader,
232 GNUNET_MQ_handler_end ());
235 /* end of gnunet-service-testbed-logger.c */