aboutsummaryrefslogtreecommitdiff
path: root/src/testbed-logger
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-03-16 03:27:16 +0100
committerChristian Grothoff <christian@grothoff.org>2017-03-16 03:27:16 +0100
commitb3d3b62e704f85b0af505a9d074504d5fea93e7e (patch)
tree21d1b395091ea0029a7e501b5e667ac8bfb86af7 /src/testbed-logger
parentcd475225e4fd83fcc16b77ea1294c11428447209 (diff)
downloadgnunet-b3d3b62e704f85b0af505a9d074504d5fea93e7e.tar.gz
gnunet-b3d3b62e704f85b0af505a9d074504d5fea93e7e.zip
migrate testbed-logger to new service API
Diffstat (limited to 'src/testbed-logger')
-rw-r--r--src/testbed-logger/gnunet-service-testbed-logger.c179
-rw-r--r--src/testbed-logger/test_testbed_logger_api.c4
2 files changed, 76 insertions, 107 deletions
diff --git a/src/testbed-logger/gnunet-service-testbed-logger.c b/src/testbed-logger/gnunet-service-testbed-logger.c
index 1c250b306..f915e70af 100644
--- a/src/testbed-logger/gnunet-service-testbed-logger.c
+++ b/src/testbed-logger/gnunet-service-testbed-logger.c
@@ -40,42 +40,6 @@
40 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__) 40 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
41 41
42/** 42/**
43 * The message queue for sending messages to clients
44 */
45struct 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 */
71static struct MessageQueue *mq_head;
72
73/**
74 * The message queue tail
75 */
76static struct MessageQueue *mq_tail;
77
78/**
79 * Handle for buffered writing. 43 * Handle for buffered writing.
80 */ 44 */
81struct GNUNET_BIO_WriteHandle *bio; 45struct GNUNET_BIO_WriteHandle *bio;
@@ -92,23 +56,38 @@ static int in_shutdown;
92 56
93 57
94/** 58/**
95 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages 59 * Check #GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG messages
96 * 60 *
97 * @param cls NULL 61 * @param cls client identification of the client
98 * @param client identification of the client 62 * @param msg the actual message
63 * @return #GNUNET_OK (they are all always OK)
64 */
65static int
66check_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
99 * @param msg the actual message 77 * @param msg the actual message
100 */ 78 */
101static void 79static void
102handle_log_msg (void *cls, 80handle_log_msg (void *cls,
103 struct GNUNET_SERVER_Client *client,
104 const struct GNUNET_MessageHeader *msg) 81 const struct GNUNET_MessageHeader *msg)
105{ 82{
83 struct GNUNET_SERVICE_Client *client = cls;
106 uint16_t ms; 84 uint16_t ms;
107 85
108 ms = ntohs (msg->size); 86 ms = ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader);
109 ms -= sizeof (struct GNUNET_MessageHeader); 87 GNUNET_BIO_write (bio,
110 GNUNET_BIO_write (bio, &msg[1], ms); 88 &msg[1],
111 GNUNET_SERVER_receive_done (client, GNUNET_OK); 89 ms);
90 GNUNET_SERVICE_client_continue (client);
112} 91}
113 92
114 93
@@ -120,69 +99,55 @@ handle_log_msg (void *cls,
120static void 99static void
121shutdown_task (void *cls) 100shutdown_task (void *cls)
122{ 101{
123 struct MessageQueue *mq_entry;
124
125 in_shutdown = GNUNET_YES; 102 in_shutdown = GNUNET_YES;
126 if (0 != nconn) 103 if (0 != nconn)
127 { 104 {
128 /* Delay shutdown if there are active connections */ 105 /* Delay shutdown if there are active connections */
129 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); 106 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
107 NULL);
130 return; 108 return;
131 } 109 }
132 while (NULL != (mq_entry = mq_head)) 110 GNUNET_break (GNUNET_OK ==
133 { 111 GNUNET_BIO_write_close (bio));
134 GNUNET_free (mq_entry->msg);
135 GNUNET_SERVER_client_drop (mq_entry->client);
136 GNUNET_CONTAINER_DLL_remove (mq_head,
137 mq_tail,
138 mq_entry);
139 GNUNET_free (mq_entry);
140 }
141 GNUNET_break (GNUNET_OK == GNUNET_BIO_write_close (bio));
142} 112}
143 113
144 114
145/** 115/**
146x * Functions with this signature are called whenever a client 116 * Callback called when a client connects to the service.
147 * is disconnected on the network level.
148 * 117 *
149 * @param cls closure 118 * @param cls closure for the service
150 * @param client identification of the client; NULL 119 * @param c the new client that connected to the service
151 * for the last call when the server is destroyed 120 * @param mq the message queue used to send messages to the client
121 * @return @a c
152 */ 122 */
153static void 123static void *
154client_disconnected (void *cls, 124client_connect_cb (void *cls,
155 struct GNUNET_SERVER_Client *client) 125 struct GNUNET_SERVICE_Client *c,
126 struct GNUNET_MQ_Handle *mq)
156{ 127{
157 if (NULL == client) 128 /* FIXME: is this really what we want here? */
158 { 129 GNUNET_SERVICE_client_persist (c);
159 GNUNET_break (0 == nconn); 130 nconn++;
160 return; 131 return c;
161 }
162 nconn--;
163 if (GNUNET_YES == in_shutdown)
164 GNUNET_SCHEDULER_shutdown ();
165} 132}
166 133
167 134
168/** 135/**
169 * Functions with this signature are called whenever a client 136 * Callback called when a client disconnected from the service
170 * is connected on the network level.
171 * 137 *
172 * @param cls closure 138 * @param cls closure for the service
173 * @param client identification of the client 139 * @param c the client that disconnected
140 * @param internal_cls should be equal to @a c
174 */ 141 */
175static void 142static void
176client_connected (void *cls, 143client_disconnect_cb (void *cls,
177 struct GNUNET_SERVER_Client *client) 144 struct GNUNET_SERVICE_Client *c,
145 void *internal_cls)
178{ 146{
179 if (NULL == client) 147 nconn--;
180 { 148 if (GNUNET_YES == in_shutdown)
181 GNUNET_break (0 == nconn); 149 GNUNET_SCHEDULER_shutdown ();
182 return; 150 GNUNET_assert (c == internal_cls);
183 }
184 GNUNET_SERVER_client_persist_ (client);
185 nconn++;
186} 151}
187 152
188 153
@@ -190,18 +155,14 @@ client_connected (void *cls,
190 * Testbed setup 155 * Testbed setup
191 * 156 *
192 * @param cls closure 157 * @param cls closure
193 * @param server the initialized server
194 * @param cfg configuration to use 158 * @param cfg configuration to use
159 * @param service the initialized service
195 */ 160 */
196static void 161static void
197logger_run (void *cls, 162logger_run (void *cls,
198 struct GNUNET_SERVER_Handle *server, 163 const struct GNUNET_CONFIGURATION_Handle *cfg,
199 const struct GNUNET_CONFIGURATION_Handle *cfg) 164 struct GNUNET_SERVICE_Handle *service)
200{ 165{
201 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
202 {&handle_log_msg, NULL, GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG, 0},
203 {NULL, NULL, 0, 0}
204 };
205 char *dir; 166 char *dir;
206 char *fn; 167 char *fn;
207 char *hname; 168 char *hname;
@@ -223,7 +184,8 @@ logger_run (void *cls,
223 pid = getpid (); 184 pid = getpid ();
224 hname_len = GNUNET_OS_get_hostname_max_length (); 185 hname_len = GNUNET_OS_get_hostname_max_length ();
225 hname = GNUNET_malloc (hname_len); 186 hname = GNUNET_malloc (hname_len);
226 if (0 != gethostname (hname, hname_len)) 187 if (0 != gethostname (hname,
188 hname_len))
227 { 189 {
228 LOG (GNUNET_ERROR_TYPE_ERROR, 190 LOG (GNUNET_ERROR_TYPE_ERROR,
229 "Cannot get hostname. Exiting\n"); 191 "Cannot get hostname. Exiting\n");
@@ -247,24 +209,27 @@ logger_run (void *cls,
247 return; 209 return;
248 } 210 }
249 GNUNET_free (fn); 211 GNUNET_free (fn);
250 GNUNET_SERVER_add_handlers (server, message_handlers); 212 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
251 GNUNET_SERVER_connect_notify (server, &client_connected, NULL); 213 NULL);
252 GNUNET_SERVER_disconnect_notify (server, &client_disconnected, NULL);
253 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
254 LOG_DEBUG ("TESTBED-LOGGER startup complete\n"); 214 LOG_DEBUG ("TESTBED-LOGGER startup complete\n");
255} 215}
256 216
257 217
258/** 218/**
259 * The starting point of execution 219 * Define "main" method using service macro.
260 */ 220 */
261int 221GNUNET_SERVICE_MAIN
262main (int argc, char *const *argv) 222("testbed-logger",
263{ 223 GNUNET_SERVICE_OPTION_NONE,
264 return (GNUNET_OK == 224 &logger_run,
265 GNUNET_SERVICE_run (argc, argv, "testbed-logger", 225 &client_connect_cb,
266 GNUNET_SERVICE_OPTION_NONE, 226 &client_disconnect_cb,
267 &logger_run, NULL)) ? 0 : 1; 227 NULL,
268} 228 GNUNET_MQ_hd_var_size (log_msg,
229 GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG,
230 struct GNUNET_MessageHeader,
231 NULL),
232 GNUNET_MQ_handler_end ());
233
269 234
270/* end of gnunet-service-testbed-logger.c */ 235/* end of gnunet-service-testbed-logger.c */
diff --git a/src/testbed-logger/test_testbed_logger_api.c b/src/testbed-logger/test_testbed_logger_api.c
index 0ebe0c3f4..e627feeb4 100644
--- a/src/testbed-logger/test_testbed_logger_api.c
+++ b/src/testbed-logger/test_testbed_logger_api.c
@@ -258,11 +258,15 @@ main (int argc, char **argv)
258 GNUNET_log_setup ("test-testbed-logger-api", 258 GNUNET_log_setup ("test-testbed-logger-api",
259 "WARNING", 259 "WARNING",
260 NULL); 260 NULL);
261 GNUNET_break (GNUNET_OK ==
262 GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
261 ret = GNUNET_TESTING_service_run ("test-testbed-logger", 263 ret = GNUNET_TESTING_service_run ("test-testbed-logger",
262 "testbed-logger", 264 "testbed-logger",
263 "test_testbed_logger_api.conf", 265 "test_testbed_logger_api.conf",
264 &test_main, 266 &test_main,
265 NULL); 267 NULL);
268 GNUNET_break (GNUNET_OK ==
269 GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
266 if (0 != ret) 270 if (0 != ret)
267 return 1; 271 return 1;
268 if (GNUNET_OK != result) 272 if (GNUNET_OK != result)