summaryrefslogtreecommitdiff
path: root/src/testbed-logger/testbed_logger_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testbed-logger/testbed_logger_api.c')
-rw-r--r--src/testbed-logger/testbed_logger_api.c174
1 files changed, 88 insertions, 86 deletions
diff --git a/src/testbed-logger/testbed_logger_api.c b/src/testbed-logger/testbed_logger_api.c
index 6e383e3c7..58f3d36ce 100644
--- a/src/testbed-logger/testbed_logger_api.c
+++ b/src/testbed-logger/testbed_logger_api.c
@@ -33,18 +33,20 @@
33 * Generic logging shorthand 33 * Generic logging shorthand
34 */ 34 */
35#define LOG(kind, ...) \ 35#define LOG(kind, ...) \
36 GNUNET_log_from(kind, "testbed-logger-api", __VA_ARGS__) 36 GNUNET_log_from (kind, "testbed-logger-api", __VA_ARGS__)
37 37
38 38
39/** 39/**
40 * The size of the buffer we fill before sending out the message 40 * The size of the buffer we fill before sending out the message
41 */ 41 */
42#define BUFFER_SIZE (GNUNET_MAX_MESSAGE_SIZE - sizeof(struct GNUNET_MessageHeader)) 42#define BUFFER_SIZE (GNUNET_MAX_MESSAGE_SIZE - sizeof(struct \
43 GNUNET_MessageHeader))
43 44
44/** 45/**
45 * Connection handle for the logger service 46 * Connection handle for the logger service
46 */ 47 */
47struct GNUNET_TESTBED_LOGGER_Handle { 48struct GNUNET_TESTBED_LOGGER_Handle
49{
48 /** 50 /**
49 * Client connection 51 * Client connection
50 */ 52 */
@@ -98,7 +100,7 @@ struct GNUNET_TESTBED_LOGGER_Handle {
98 * @param cls the logger handle 100 * @param cls the logger handle
99 */ 101 */
100static void 102static void
101call_flush_completion(void *cls) 103call_flush_completion (void *cls)
102{ 104{
103 struct GNUNET_TESTBED_LOGGER_Handle *h = cls; 105 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
104 GNUNET_TESTBED_LOGGER_FlushCompletion cb; 106 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
@@ -113,7 +115,7 @@ call_flush_completion(void *cls)
113 cb_cls = h->cb_cls; 115 cb_cls = h->cb_cls;
114 h->cb_cls = NULL; 116 h->cb_cls = NULL;
115 if (NULL != cb) 117 if (NULL != cb)
116 cb(cb_cls, bw); 118 cb (cb_cls, bw);
117} 119}
118 120
119 121
@@ -123,13 +125,13 @@ call_flush_completion(void *cls)
123 * @param h logger handle 125 * @param h logger handle
124 */ 126 */
125static void 127static void
126trigger_flush_notification(struct GNUNET_TESTBED_LOGGER_Handle *h) 128trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
127{ 129{
128 if (NULL != h->flush_completion_task) 130 if (NULL != h->flush_completion_task)
129 GNUNET_SCHEDULER_cancel(h->flush_completion_task); 131 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
130 h->flush_completion_task 132 h->flush_completion_task
131 = GNUNET_SCHEDULER_add_now(&call_flush_completion, 133 = GNUNET_SCHEDULER_add_now (&call_flush_completion,
132 h); 134 h);
133} 135}
134 136
135 137
@@ -139,7 +141,7 @@ trigger_flush_notification(struct GNUNET_TESTBED_LOGGER_Handle *h)
139 * @param h the logger handle 141 * @param h the logger handle
140 */ 142 */
141static void 143static void
142dispatch_buffer(struct GNUNET_TESTBED_LOGGER_Handle *h); 144dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h);
143 145
144 146
145/** 147/**
@@ -148,19 +150,19 @@ dispatch_buffer(struct GNUNET_TESTBED_LOGGER_Handle *h);
148 * @param cls our handle 150 * @param cls our handle
149 */ 151 */
150static void 152static void
151notify_sent(void *cls) 153notify_sent (void *cls)
152{ 154{
153 struct GNUNET_TESTBED_LOGGER_Handle *h = cls; 155 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
154 156
155 h->mq_len--; 157 h->mq_len--;
156 if ((0 == h->mq_len) && 158 if ((0 == h->mq_len) &&
157 (NULL != h->cb)) 159 (NULL != h->cb))
158 { 160 {
159 if (0 == h->buse) 161 if (0 == h->buse)
160 trigger_flush_notification(h); 162 trigger_flush_notification (h);
161 else 163 else
162 dispatch_buffer(h); 164 dispatch_buffer (h);
163 } 165 }
164} 166}
165 167
166 168
@@ -170,25 +172,25 @@ notify_sent(void *cls)
170 * @param h the logger handle 172 * @param h the logger handle
171 */ 173 */
172static void 174static void
173dispatch_buffer(struct GNUNET_TESTBED_LOGGER_Handle *h) 175dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
174{ 176{
175 struct GNUNET_MessageHeader *msg; 177 struct GNUNET_MessageHeader *msg;
176 struct GNUNET_MQ_Envelope *env; 178 struct GNUNET_MQ_Envelope *env;
177 179
178 env = GNUNET_MQ_msg_extra(msg, 180 env = GNUNET_MQ_msg_extra (msg,
179 h->buse, 181 h->buse,
180 GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG); 182 GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
181 GNUNET_memcpy(&msg[1], 183 GNUNET_memcpy (&msg[1],
182 h->buf, 184 h->buf,
183 h->buse); 185 h->buse);
184 h->bwrote += h->buse; 186 h->bwrote += h->buse;
185 h->buse = 0; 187 h->buse = 0;
186 h->mq_len++; 188 h->mq_len++;
187 GNUNET_MQ_notify_sent(env, 189 GNUNET_MQ_notify_sent (env,
188 &notify_sent, 190 &notify_sent,
189 h); 191 h);
190 GNUNET_MQ_send(h->mq, 192 GNUNET_MQ_send (h->mq,
191 env); 193 env);
192} 194}
193 195
194 196
@@ -199,13 +201,13 @@ dispatch_buffer(struct GNUNET_TESTBED_LOGGER_Handle *h)
199 * @param error error code 201 * @param error error code
200 */ 202 */
201static void 203static void
202mq_error_handler(void *cls, 204mq_error_handler (void *cls,
203 enum GNUNET_MQ_Error error) 205 enum GNUNET_MQ_Error error)
204{ 206{
205 struct GNUNET_TESTBED_LOGGER_Handle *h = cls; 207 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
206 208
207 GNUNET_break(0); 209 GNUNET_break (0);
208 GNUNET_MQ_destroy(h->mq); 210 GNUNET_MQ_destroy (h->mq);
209 h->mq = NULL; 211 h->mq = NULL;
210} 212}
211 213
@@ -218,21 +220,21 @@ mq_error_handler(void *cls,
218 * upon any error 220 * upon any error
219 */ 221 */
220struct GNUNET_TESTBED_LOGGER_Handle * 222struct GNUNET_TESTBED_LOGGER_Handle *
221GNUNET_TESTBED_LOGGER_connect(const struct GNUNET_CONFIGURATION_Handle *cfg) 223GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
222{ 224{
223 struct GNUNET_TESTBED_LOGGER_Handle *h; 225 struct GNUNET_TESTBED_LOGGER_Handle *h;
224 226
225 h = GNUNET_new(struct GNUNET_TESTBED_LOGGER_Handle); 227 h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle);
226 h->mq = GNUNET_CLIENT_connect(cfg, 228 h->mq = GNUNET_CLIENT_connect (cfg,
227 "testbed-logger", 229 "testbed-logger",
228 NULL, 230 NULL,
229 &mq_error_handler, 231 &mq_error_handler,
230 h); 232 h);
231 if (NULL == h->mq) 233 if (NULL == h->mq)
232 { 234 {
233 GNUNET_free(h); 235 GNUNET_free (h);
234 return NULL; 236 return NULL;
235 } 237 }
236 return h; 238 return h;
237} 239}
238 240
@@ -243,23 +245,23 @@ GNUNET_TESTBED_LOGGER_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
243 * @param h the logger handle 245 * @param h the logger handle
244 */ 246 */
245void 247void
246GNUNET_TESTBED_LOGGER_disconnect(struct GNUNET_TESTBED_LOGGER_Handle *h) 248GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
247{ 249{
248 if (NULL != h->flush_completion_task) 250 if (NULL != h->flush_completion_task)
249 { 251 {
250 GNUNET_SCHEDULER_cancel(h->flush_completion_task); 252 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
251 h->flush_completion_task = NULL; 253 h->flush_completion_task = NULL;
252 } 254 }
253 if (0 != h->mq_len) 255 if (0 != h->mq_len)
254 LOG(GNUNET_ERROR_TYPE_WARNING, 256 LOG (GNUNET_ERROR_TYPE_WARNING,
255 "Disconnect lost %u logger message[s]\n", 257 "Disconnect lost %u logger message[s]\n",
256 h->mq_len); 258 h->mq_len);
257 if (NULL != h->mq) 259 if (NULL != h->mq)
258 { 260 {
259 GNUNET_MQ_destroy(h->mq); 261 GNUNET_MQ_destroy (h->mq);
260 h->mq = NULL; 262 h->mq = NULL;
261 } 263 }
262 GNUNET_free(h); 264 GNUNET_free (h);
263} 265}
264 266
265 267
@@ -273,25 +275,25 @@ GNUNET_TESTBED_LOGGER_disconnect(struct GNUNET_TESTBED_LOGGER_Handle *h)
273 * @param size how many bytes of @a data to send 275 * @param size how many bytes of @a data to send
274 */ 276 */
275void 277void
276GNUNET_TESTBED_LOGGER_write(struct GNUNET_TESTBED_LOGGER_Handle *h, 278GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
277 const void *data, 279 const void *data,
278 size_t size) 280 size_t size)
279{ 281{
280 if (NULL == h->mq) 282 if (NULL == h->mq)
281 return; 283 return;
282 while (0 != size) 284 while (0 != size)
283 { 285 {
284 size_t fit_size = GNUNET_MIN(size, 286 size_t fit_size = GNUNET_MIN (size,
285 BUFFER_SIZE - h->buse); 287 BUFFER_SIZE - h->buse);
286 GNUNET_memcpy(&h->buf[h->buse], 288 GNUNET_memcpy (&h->buf[h->buse],
287 data, 289 data,
288 fit_size); 290 fit_size);
289 h->buse += fit_size; 291 h->buse += fit_size;
290 data += fit_size; 292 data += fit_size;
291 size -= fit_size; 293 size -= fit_size;
292 if (0 != size) 294 if (0 != size)
293 dispatch_buffer(h); 295 dispatch_buffer (h);
294 } 296 }
295} 297}
296 298
297 299
@@ -303,20 +305,20 @@ GNUNET_TESTBED_LOGGER_write(struct GNUNET_TESTBED_LOGGER_Handle *h,
303 * @param cb_cls the closure for the above callback 305 * @param cb_cls the closure for the above callback
304 */ 306 */
305void 307void
306GNUNET_TESTBED_LOGGER_flush(struct GNUNET_TESTBED_LOGGER_Handle *h, 308GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
307 GNUNET_TESTBED_LOGGER_FlushCompletion cb, 309 GNUNET_TESTBED_LOGGER_FlushCompletion cb,
308 void *cb_cls) 310 void *cb_cls)
309{ 311{
310 GNUNET_assert(NULL == h->cb); 312 GNUNET_assert (NULL == h->cb);
311 h->cb = cb; 313 h->cb = cb;
312 h->cb_cls = cb_cls; 314 h->cb_cls = cb_cls;
313 if ((NULL == h->mq) || 315 if ((NULL == h->mq) ||
314 (0 == h->buse)) 316 (0 == h->buse))
315 { 317 {
316 trigger_flush_notification(h); 318 trigger_flush_notification (h);
317 return; 319 return;
318 } 320 }
319 dispatch_buffer(h); 321 dispatch_buffer (h);
320} 322}
321 323
322 324
@@ -328,13 +330,13 @@ GNUNET_TESTBED_LOGGER_flush(struct GNUNET_TESTBED_LOGGER_Handle *h,
328 * @param h the logger handle 330 * @param h the logger handle
329 */ 331 */
330void 332void
331GNUNET_TESTBED_LOGGER_flush_cancel(struct GNUNET_TESTBED_LOGGER_Handle *h) 333GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h)
332{ 334{
333 if (NULL != h->flush_completion_task) 335 if (NULL != h->flush_completion_task)
334 { 336 {
335 GNUNET_SCHEDULER_cancel(h->flush_completion_task); 337 GNUNET_SCHEDULER_cancel (h->flush_completion_task);
336 h->flush_completion_task = NULL; 338 h->flush_completion_task = NULL;
337 } 339 }
338 h->cb = NULL; 340 h->cb = NULL;
339 h->cb_cls = NULL; 341 h->cb_cls = NULL;
340} 342}