aboutsummaryrefslogtreecommitdiff
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.c183
1 files changed, 91 insertions, 92 deletions
diff --git a/src/testbed-logger/testbed_logger_api.c b/src/testbed-logger/testbed_logger_api.c
index 5a550e876..6e383e3c7 100644
--- a/src/testbed-logger/testbed_logger_api.c
+++ b/src/testbed-logger/testbed_logger_api.c
@@ -11,7 +11,7 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
@@ -33,19 +33,18 @@
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 GNUNET_MessageHeader))
43 43
44/** 44/**
45 * Connection handle for the logger service 45 * Connection handle for the logger service
46 */ 46 */
47struct GNUNET_TESTBED_LOGGER_Handle 47struct GNUNET_TESTBED_LOGGER_Handle {
48{
49 /** 48 /**
50 * Client connection 49 * Client connection
51 */ 50 */
@@ -99,7 +98,7 @@ struct GNUNET_TESTBED_LOGGER_Handle
99 * @param cls the logger handle 98 * @param cls the logger handle
100 */ 99 */
101static void 100static void
102call_flush_completion (void *cls) 101call_flush_completion(void *cls)
103{ 102{
104 struct GNUNET_TESTBED_LOGGER_Handle *h = cls; 103 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
105 GNUNET_TESTBED_LOGGER_FlushCompletion cb; 104 GNUNET_TESTBED_LOGGER_FlushCompletion cb;
@@ -114,7 +113,7 @@ call_flush_completion (void *cls)
114 cb_cls = h->cb_cls; 113 cb_cls = h->cb_cls;
115 h->cb_cls = NULL; 114 h->cb_cls = NULL;
116 if (NULL != cb) 115 if (NULL != cb)
117 cb (cb_cls, bw); 116 cb(cb_cls, bw);
118} 117}
119 118
120 119
@@ -124,13 +123,13 @@ call_flush_completion (void *cls)
124 * @param h logger handle 123 * @param h logger handle
125 */ 124 */
126static void 125static void
127trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h) 126trigger_flush_notification(struct GNUNET_TESTBED_LOGGER_Handle *h)
128{ 127{
129 if (NULL != h->flush_completion_task) 128 if (NULL != h->flush_completion_task)
130 GNUNET_SCHEDULER_cancel (h->flush_completion_task); 129 GNUNET_SCHEDULER_cancel(h->flush_completion_task);
131 h->flush_completion_task 130 h->flush_completion_task
132 = GNUNET_SCHEDULER_add_now (&call_flush_completion, 131 = GNUNET_SCHEDULER_add_now(&call_flush_completion,
133 h); 132 h);
134} 133}
135 134
136 135
@@ -140,7 +139,7 @@ trigger_flush_notification (struct GNUNET_TESTBED_LOGGER_Handle *h)
140 * @param h the logger handle 139 * @param h the logger handle
141 */ 140 */
142static void 141static void
143dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h); 142dispatch_buffer(struct GNUNET_TESTBED_LOGGER_Handle *h);
144 143
145 144
146/** 145/**
@@ -149,19 +148,19 @@ dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h);
149 * @param cls our handle 148 * @param cls our handle
150 */ 149 */
151static void 150static void
152notify_sent (void *cls) 151notify_sent(void *cls)
153{ 152{
154 struct GNUNET_TESTBED_LOGGER_Handle *h = cls; 153 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
155 154
156 h->mq_len--; 155 h->mq_len--;
157 if ( (0 == h->mq_len) && 156 if ((0 == h->mq_len) &&
158 (NULL != h->cb) ) 157 (NULL != h->cb))
159 { 158 {
160 if (0 == h->buse) 159 if (0 == h->buse)
161 trigger_flush_notification (h); 160 trigger_flush_notification(h);
162 else 161 else
163 dispatch_buffer (h); 162 dispatch_buffer(h);
164 } 163 }
165} 164}
166 165
167 166
@@ -171,25 +170,25 @@ notify_sent (void *cls)
171 * @param h the logger handle 170 * @param h the logger handle
172 */ 171 */
173static void 172static void
174dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h) 173dispatch_buffer(struct GNUNET_TESTBED_LOGGER_Handle *h)
175{ 174{
176 struct GNUNET_MessageHeader *msg; 175 struct GNUNET_MessageHeader *msg;
177 struct GNUNET_MQ_Envelope *env; 176 struct GNUNET_MQ_Envelope *env;
178 177
179 env = GNUNET_MQ_msg_extra (msg, 178 env = GNUNET_MQ_msg_extra(msg,
180 h->buse, 179 h->buse,
181 GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG); 180 GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG);
182 GNUNET_memcpy (&msg[1], 181 GNUNET_memcpy(&msg[1],
183 h->buf, 182 h->buf,
184 h->buse); 183 h->buse);
185 h->bwrote += h->buse; 184 h->bwrote += h->buse;
186 h->buse = 0; 185 h->buse = 0;
187 h->mq_len++; 186 h->mq_len++;
188 GNUNET_MQ_notify_sent (env, 187 GNUNET_MQ_notify_sent(env,
189 &notify_sent, 188 &notify_sent,
190 h); 189 h);
191 GNUNET_MQ_send (h->mq, 190 GNUNET_MQ_send(h->mq,
192 env); 191 env);
193} 192}
194 193
195 194
@@ -200,13 +199,13 @@ dispatch_buffer (struct GNUNET_TESTBED_LOGGER_Handle *h)
200 * @param error error code 199 * @param error error code
201 */ 200 */
202static void 201static void
203mq_error_handler (void *cls, 202mq_error_handler(void *cls,
204 enum GNUNET_MQ_Error error) 203 enum GNUNET_MQ_Error error)
205{ 204{
206 struct GNUNET_TESTBED_LOGGER_Handle *h = cls; 205 struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
207 206
208 GNUNET_break (0); 207 GNUNET_break(0);
209 GNUNET_MQ_destroy (h->mq); 208 GNUNET_MQ_destroy(h->mq);
210 h->mq = NULL; 209 h->mq = NULL;
211} 210}
212 211
@@ -219,21 +218,21 @@ mq_error_handler (void *cls,
219 * upon any error 218 * upon any error
220 */ 219 */
221struct GNUNET_TESTBED_LOGGER_Handle * 220struct GNUNET_TESTBED_LOGGER_Handle *
222GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) 221GNUNET_TESTBED_LOGGER_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
223{ 222{
224 struct GNUNET_TESTBED_LOGGER_Handle *h; 223 struct GNUNET_TESTBED_LOGGER_Handle *h;
225 224
226 h = GNUNET_new (struct GNUNET_TESTBED_LOGGER_Handle); 225 h = GNUNET_new(struct GNUNET_TESTBED_LOGGER_Handle);
227 h->mq = GNUNET_CLIENT_connect (cfg, 226 h->mq = GNUNET_CLIENT_connect(cfg,
228 "testbed-logger", 227 "testbed-logger",
229 NULL, 228 NULL,
230 &mq_error_handler, 229 &mq_error_handler,
231 h); 230 h);
232 if (NULL == h->mq) 231 if (NULL == h->mq)
233 { 232 {
234 GNUNET_free (h); 233 GNUNET_free(h);
235 return NULL; 234 return NULL;
236 } 235 }
237 return h; 236 return h;
238} 237}
239 238
@@ -244,23 +243,23 @@ GNUNET_TESTBED_LOGGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
244 * @param h the logger handle 243 * @param h the logger handle
245 */ 244 */
246void 245void
247GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h) 246GNUNET_TESTBED_LOGGER_disconnect(struct GNUNET_TESTBED_LOGGER_Handle *h)
248{ 247{
249 if (NULL != h->flush_completion_task) 248 if (NULL != h->flush_completion_task)
250 { 249 {
251 GNUNET_SCHEDULER_cancel (h->flush_completion_task); 250 GNUNET_SCHEDULER_cancel(h->flush_completion_task);
252 h->flush_completion_task = NULL; 251 h->flush_completion_task = NULL;
253 } 252 }
254 if (0 != h->mq_len) 253 if (0 != h->mq_len)
255 LOG (GNUNET_ERROR_TYPE_WARNING, 254 LOG(GNUNET_ERROR_TYPE_WARNING,
256 "Disconnect lost %u logger message[s]\n", 255 "Disconnect lost %u logger message[s]\n",
257 h->mq_len); 256 h->mq_len);
258 if (NULL != h->mq) 257 if (NULL != h->mq)
259 { 258 {
260 GNUNET_MQ_destroy (h->mq); 259 GNUNET_MQ_destroy(h->mq);
261 h->mq = NULL; 260 h->mq = NULL;
262 } 261 }
263 GNUNET_free (h); 262 GNUNET_free(h);
264} 263}
265 264
266 265
@@ -274,25 +273,25 @@ GNUNET_TESTBED_LOGGER_disconnect (struct GNUNET_TESTBED_LOGGER_Handle *h)
274 * @param size how many bytes of @a data to send 273 * @param size how many bytes of @a data to send
275 */ 274 */
276void 275void
277GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h, 276GNUNET_TESTBED_LOGGER_write(struct GNUNET_TESTBED_LOGGER_Handle *h,
278 const void *data, 277 const void *data,
279 size_t size) 278 size_t size)
280{ 279{
281 if (NULL == h->mq) 280 if (NULL == h->mq)
282 return; 281 return;
283 while (0 != size) 282 while (0 != size)
284 { 283 {
285 size_t fit_size = GNUNET_MIN (size, 284 size_t fit_size = GNUNET_MIN(size,
286 BUFFER_SIZE - h->buse); 285 BUFFER_SIZE - h->buse);
287 GNUNET_memcpy (&h->buf[h->buse], 286 GNUNET_memcpy(&h->buf[h->buse],
288 data, 287 data,
289 fit_size); 288 fit_size);
290 h->buse += fit_size; 289 h->buse += fit_size;
291 data += fit_size; 290 data += fit_size;
292 size -= fit_size; 291 size -= fit_size;
293 if (0 != size) 292 if (0 != size)
294 dispatch_buffer (h); 293 dispatch_buffer(h);
295 } 294 }
296} 295}
297 296
298 297
@@ -304,20 +303,20 @@ GNUNET_TESTBED_LOGGER_write (struct GNUNET_TESTBED_LOGGER_Handle *h,
304 * @param cb_cls the closure for the above callback 303 * @param cb_cls the closure for the above callback
305 */ 304 */
306void 305void
307GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h, 306GNUNET_TESTBED_LOGGER_flush(struct GNUNET_TESTBED_LOGGER_Handle *h,
308 GNUNET_TESTBED_LOGGER_FlushCompletion cb, 307 GNUNET_TESTBED_LOGGER_FlushCompletion cb,
309 void *cb_cls) 308 void *cb_cls)
310{ 309{
311 GNUNET_assert (NULL == h->cb); 310 GNUNET_assert(NULL == h->cb);
312 h->cb = cb; 311 h->cb = cb;
313 h->cb_cls = cb_cls; 312 h->cb_cls = cb_cls;
314 if ( (NULL == h->mq) || 313 if ((NULL == h->mq) ||
315 (0 == h->buse) ) 314 (0 == h->buse))
316 { 315 {
317 trigger_flush_notification (h); 316 trigger_flush_notification(h);
318 return; 317 return;
319 } 318 }
320 dispatch_buffer (h); 319 dispatch_buffer(h);
321} 320}
322 321
323 322
@@ -329,13 +328,13 @@ GNUNET_TESTBED_LOGGER_flush (struct GNUNET_TESTBED_LOGGER_Handle *h,
329 * @param h the logger handle 328 * @param h the logger handle
330 */ 329 */
331void 330void
332GNUNET_TESTBED_LOGGER_flush_cancel (struct GNUNET_TESTBED_LOGGER_Handle *h) 331GNUNET_TESTBED_LOGGER_flush_cancel(struct GNUNET_TESTBED_LOGGER_Handle *h)
333{ 332{
334 if (NULL != h->flush_completion_task) 333 if (NULL != h->flush_completion_task)
335 { 334 {
336 GNUNET_SCHEDULER_cancel (h->flush_completion_task); 335 GNUNET_SCHEDULER_cancel(h->flush_completion_task);
337 h->flush_completion_task = NULL; 336 h->flush_completion_task = NULL;
338 } 337 }
339 h->cb = NULL; 338 h->cb = NULL;
340 h->cb_cls = NULL; 339 h->cb_cls = NULL;
341} 340}