diff options
Diffstat (limited to 'src/testbed-logger/gnunet-service-testbed-logger.c')
-rw-r--r-- | src/testbed-logger/gnunet-service-testbed-logger.c | 236 |
1 files changed, 0 insertions, 236 deletions
diff --git a/src/testbed-logger/gnunet-service-testbed-logger.c b/src/testbed-logger/gnunet-service-testbed-logger.c deleted file mode 100644 index bc2f0abe0..000000000 --- a/src/testbed-logger/gnunet-service-testbed-logger.c +++ /dev/null | |||
@@ -1,236 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2008--2013 GNUnet e.V. | ||
4 | |||
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. | ||
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 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
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 | ||
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 | * Handle for buffered writing. | ||
44 | */ | ||
45 | struct GNUNET_BIO_WriteHandle *bio; | ||
46 | |||
47 | /** | ||
48 | * The number of connections we have | ||
49 | */ | ||
50 | static unsigned int nconn; | ||
51 | |||
52 | /** | ||
53 | * Are we shutting down? | ||
54 | */ | ||
55 | static int in_shutdown; | ||
56 | |||
57 | |||
58 | /** | ||
59 | * Check #GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG messages | ||
60 | * | ||
61 | * @param cls client identification of the client | ||
62 | * @param msg the actual message | ||
63 | * @return #GNUNET_OK (they are all always OK) | ||
64 | */ | ||
65 | static int | ||
66 | check_log_msg (void *cls, | ||
67 | const struct GNUNET_MessageHeader *msg) | ||
68 | { | ||
69 | return GNUNET_OK; | ||
70 | } | ||
71 | |||
72 | |||
73 | /** | ||
74 | * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG messages | ||
75 | * | ||
76 | * @param cls client identification of the client | ||
77 | * @param msg the actual message | ||
78 | */ | ||
79 | static void | ||
80 | handle_log_msg (void *cls, | ||
81 | const struct GNUNET_MessageHeader *msg) | ||
82 | { | ||
83 | struct GNUNET_SERVICE_Client *client = cls; | ||
84 | uint16_t ms; | ||
85 | |||
86 | ms = ntohs (msg->size) - sizeof(struct GNUNET_MessageHeader); | ||
87 | GNUNET_BIO_write (bio, | ||
88 | "testbed-logger-handle-log-msg", | ||
89 | &msg[1], | ||
90 | ms); | ||
91 | GNUNET_SERVICE_client_continue (client); | ||
92 | } | ||
93 | |||
94 | |||
95 | /** | ||
96 | * Task to clean up and shutdown nicely | ||
97 | * | ||
98 | * @param cls NULL | ||
99 | */ | ||
100 | static void | ||
101 | shutdown_task (void *cls) | ||
102 | { | ||
103 | in_shutdown = GNUNET_YES; | ||
104 | if (0 != nconn) | ||
105 | { | ||
106 | /* Delay shutdown if there are active connections */ | ||
107 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, | ||
108 | NULL); | ||
109 | return; | ||
110 | } | ||
111 | GNUNET_break (GNUNET_OK == | ||
112 | GNUNET_BIO_write_close (bio, NULL)); | ||
113 | } | ||
114 | |||
115 | |||
116 | /** | ||
117 | * Callback called when a client connects to the service. | ||
118 | * | ||
119 | * @param cls closure for the service | ||
120 | * @param c the new client that connected to the service | ||
121 | * @param mq the message queue used to send messages to the client | ||
122 | * @return @a c | ||
123 | */ | ||
124 | static void * | ||
125 | client_connect_cb (void *cls, | ||
126 | struct GNUNET_SERVICE_Client *c, | ||
127 | struct GNUNET_MQ_Handle *mq) | ||
128 | { | ||
129 | /* FIXME: is this really what we want here? */ | ||
130 | GNUNET_SERVICE_client_persist (c); | ||
131 | nconn++; | ||
132 | return c; | ||
133 | } | ||
134 | |||
135 | |||
136 | /** | ||
137 | * Callback called when a client disconnected from the service | ||
138 | * | ||
139 | * @param cls closure for the service | ||
140 | * @param c the client that disconnected | ||
141 | * @param internal_cls should be equal to @a c | ||
142 | */ | ||
143 | static void | ||
144 | client_disconnect_cb (void *cls, | ||
145 | struct GNUNET_SERVICE_Client *c, | ||
146 | void *internal_cls) | ||
147 | { | ||
148 | nconn--; | ||
149 | if (GNUNET_YES == in_shutdown) | ||
150 | GNUNET_SCHEDULER_shutdown (); | ||
151 | GNUNET_assert (c == internal_cls); | ||
152 | } | ||
153 | |||
154 | |||
155 | /** | ||
156 | * Testbed setup | ||
157 | * | ||
158 | * @param cls closure | ||
159 | * @param cfg configuration to use | ||
160 | * @param service the initialized service | ||
161 | */ | ||
162 | static void | ||
163 | logger_run (void *cls, | ||
164 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
165 | struct GNUNET_SERVICE_Handle *service) | ||
166 | { | ||
167 | char *dir; | ||
168 | char *fn; | ||
169 | char *hname; | ||
170 | size_t hname_len; | ||
171 | pid_t pid; | ||
172 | |||
173 | if (GNUNET_OK != | ||
174 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
175 | "TESTBED-LOGGER", | ||
176 | "DIR", | ||
177 | &dir)) | ||
178 | { | ||
179 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
180 | "TESTBED-LOGGER", | ||
181 | "DIR"); | ||
182 | GNUNET_SCHEDULER_shutdown (); | ||
183 | return; | ||
184 | } | ||
185 | pid = getpid (); | ||
186 | hname_len = GNUNET_OS_get_hostname_max_length (); | ||
187 | hname = GNUNET_malloc (hname_len); | ||
188 | if (0 != gethostname (hname, | ||
189 | hname_len)) | ||
190 | { | ||
191 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
192 | "Cannot get hostname. Exiting\n"); | ||
193 | GNUNET_free (hname); | ||
194 | GNUNET_free (dir); | ||
195 | GNUNET_SCHEDULER_shutdown (); | ||
196 | return; | ||
197 | } | ||
198 | GNUNET_asprintf (&fn, | ||
199 | "%s/%.*s_%jd.dat", | ||
200 | dir, | ||
201 | (int) hname_len, | ||
202 | hname, | ||
203 | (intmax_t) pid); | ||
204 | GNUNET_free (hname); | ||
205 | GNUNET_free (dir); | ||
206 | if (NULL == (bio = GNUNET_BIO_write_open_file (fn))) | ||
207 | { | ||
208 | GNUNET_free (fn); | ||
209 | GNUNET_SCHEDULER_shutdown (); | ||
210 | return; | ||
211 | } | ||
212 | GNUNET_free (fn); | ||
213 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, | ||
214 | NULL); | ||
215 | LOG_DEBUG ("TESTBED-LOGGER startup complete\n"); | ||
216 | } | ||
217 | |||
218 | |||
219 | /** | ||
220 | * Define "main" method using service macro. | ||
221 | */ | ||
222 | GNUNET_SERVICE_MAIN | ||
223 | ("testbed-logger", | ||
224 | GNUNET_SERVICE_OPTION_NONE, | ||
225 | &logger_run, | ||
226 | &client_connect_cb, | ||
227 | &client_disconnect_cb, | ||
228 | NULL, | ||
229 | GNUNET_MQ_hd_var_size (log_msg, | ||
230 | GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG, | ||
231 | struct GNUNET_MessageHeader, | ||
232 | NULL), | ||
233 | GNUNET_MQ_handler_end ()); | ||
234 | |||
235 | |||
236 | /* end of gnunet-service-testbed-logger.c */ | ||