- remove dead code
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed-logger.c
1 /*
2   This file is part of GNUnet.
3   (C) 2008--2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file testbed/gnunet-service-testbed-logger.c
23  * @brief service for collecting messages and writing to a file
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29
30 /**
31  * Generic logging shorthand
32  */
33 #define LOG(type, ...)                         \
34   GNUNET_log (type, __VA_ARGS__)
35
36 /**
37  * Debug logging shorthand
38  */
39 #define LOG_DEBUG(...)                          \
40   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
41
42 /**
43  * The message queue for sending messages to clients
44  */
45 struct MessageQueue
46 {
47   /**
48    * The message to be sent
49    */
50   struct GNUNET_MessageHeader *msg;
51
52   /**
53    * The client to send the message to
54    */
55   struct GNUNET_SERVER_Client *client;
56
57   /**
58    * next pointer for DLL
59    */
60   struct MessageQueue *next;
61
62   /**
63    * prev pointer for DLL
64    */
65   struct MessageQueue *prev;
66 };
67
68 /**
69  * The message queue head
70  */
71 static struct MessageQueue *mq_head;
72
73 /**
74  * The message queue tail
75  */
76 static struct MessageQueue *mq_tail;
77
78 /**
79  * Handle for buffered writing.
80  */
81 struct GNUNET_BIO_WriteHandle *bio;
82
83 /**
84  * The shutdown task handle
85  */
86 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
87
88 /**
89  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
90  *
91  * @param cls NULL
92  * @param client identification of the client
93  * @param msg the actual message
94  */
95 static void
96 handle_log_msg (void *cls, struct GNUNET_SERVER_Client *client,
97                 const struct GNUNET_MessageHeader *msg)
98 {
99   uint16_t ms;
100
101   ms = ntohs (msg->size);
102   ms -= sizeof (struct GNUNET_MessageHeader);
103   GNUNET_BIO_write (bio, &msg[1], ms);
104   GNUNET_SERVER_receive_done (client, GNUNET_OK);
105 }
106
107
108 /**
109  * Task to clean up and shutdown nicely
110  *
111  * @param cls NULL
112  * @param tc the TaskContext from scheduler
113  */
114 static void
115 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   struct MessageQueue *mq_entry;
118
119   shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
120   while (NULL != (mq_entry = mq_head))
121   {
122     GNUNET_free (mq_entry->msg);
123     GNUNET_SERVER_client_drop (mq_entry->client);
124     GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
125     GNUNET_free (mq_entry);
126   }
127   GNUNET_break (GNUNET_OK == GNUNET_BIO_write_close (bio));
128 }
129
130
131 /**
132  * Testbed setup
133  *
134  * @param cls closure
135  * @param server the initialized server
136  * @param cfg configuration to use
137  */
138 static void
139 logger_run (void *cls, struct GNUNET_SERVER_Handle *server,
140              const struct GNUNET_CONFIGURATION_Handle *cfg)
141 {
142   static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
143     {&handle_log_msg, NULL, GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG, 0},
144     {NULL, NULL, 0, 0}
145   };
146   char *dir;
147   char *fn;
148   char *hname;
149   size_t hname_len;
150   pid_t pid;
151
152   if (GNUNET_OK !=
153       GNUNET_CONFIGURATION_get_value_filename (cfg, "TESTBED-LOGGER", "DIR",
154                                                &dir))
155   {
156     LOG (GNUNET_ERROR_TYPE_ERROR, "Not logging directory definied.  Exiting\n");
157     GNUNET_SCHEDULER_shutdown ();
158     return;
159   }
160   pid = getpid ();
161   hname_len = GNUNET_OS_get_hostname_max_length ();
162   hname = GNUNET_malloc (hname_len);
163   if (0 != gethostname (hname, hname_len))
164   {
165     LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot get hostname.  Exiting\n");
166     GNUNET_free (hname);
167     GNUNET_free (dir);
168     GNUNET_SCHEDULER_shutdown ();
169     return;
170   }
171   (void) GNUNET_asprintf (&fn, "%s/%.*s_%jd.dat", dir, hname_len, hname,
172                           (intmax_t) pid);
173   GNUNET_free (hname);
174   GNUNET_free (dir);
175   if (NULL == (bio = GNUNET_BIO_write_open (fn)))
176   {
177     GNUNET_free (fn);
178     GNUNET_SCHEDULER_shutdown ();
179     return;
180   }
181   GNUNET_free (fn);
182   GNUNET_SERVER_add_handlers (server, message_handlers);
183   shutdown_task_id =
184       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
185                                     &shutdown_task, NULL);
186   LOG_DEBUG ("TESTBED-LOGGER startup complete\n");
187 }
188
189
190 /**
191  * The starting point of execution
192  */
193 int
194 main (int argc, char *const *argv)
195 {
196   //sleep (15);                 /* Debugging */
197   return (GNUNET_OK ==
198           GNUNET_SERVICE_run (argc, argv, "testbed-logger",
199                               GNUNET_SERVICE_OPTION_NONE,
200                               &logger_run, NULL)) ? 0 : 1;
201 }
202
203 /* end of gnunet-service-testbed.c */