GNUnet  0.10.x
testbed_logger_api.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2008--2013, 2016 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 
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
31 
35 #define LOG(kind, ...) \
36  GNUNET_log_from(kind, "testbed-logger-api", __VA_ARGS__)
37 
38 
42 #define BUFFER_SIZE (GNUNET_MAX_MESSAGE_SIZE - sizeof(struct GNUNET_MessageHeader))
43 
52 
57 
61  void *cb_cls;
62 
67 
71  size_t buse;
72 
76  size_t bwrote;
77 
82 
87 
91  unsigned int mq_len;
92 };
93 
94 
100 static void
102 {
103  struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
105  void *cb_cls;
106  size_t bw;
107 
108  h->flush_completion_task = NULL;
109  bw = h->bwrote;
110  h->bwrote = 0;
111  cb = h->cb;
112  h->cb = NULL;
113  cb_cls = h->cb_cls;
114  h->cb_cls = NULL;
115  if (NULL != cb)
116  cb(cb_cls, bw);
117 }
118 
119 
125 static void
127 {
128  if (NULL != h->flush_completion_task)
132  h);
133 }
134 
135 
141 static void
143 
144 
150 static void
151 notify_sent(void *cls)
152 {
153  struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
154 
155  h->mq_len--;
156  if ((0 == h->mq_len) &&
157  (NULL != h->cb))
158  {
159  if (0 == h->buse)
161  else
162  dispatch_buffer(h);
163  }
164 }
165 
166 
172 static void
174 {
175  struct GNUNET_MessageHeader *msg;
176  struct GNUNET_MQ_Envelope *env;
177 
178  env = GNUNET_MQ_msg_extra(msg,
179  h->buse,
181  GNUNET_memcpy(&msg[1],
182  h->buf,
183  h->buse);
184  h->bwrote += h->buse;
185  h->buse = 0;
186  h->mq_len++;
188  &notify_sent,
189  h);
190  GNUNET_MQ_send(h->mq,
191  env);
192 }
193 
194 
201 static void
203  enum GNUNET_MQ_Error error)
204 {
205  struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
206 
207  GNUNET_break(0);
208  GNUNET_MQ_destroy(h->mq);
209  h->mq = NULL;
210 }
211 
212 
222 {
224 
226  h->mq = GNUNET_CLIENT_connect(cfg,
227  "testbed-logger",
228  NULL,
230  h);
231  if (NULL == h->mq)
232  {
233  GNUNET_free(h);
234  return NULL;
235  }
236  return h;
237 }
238 
239 
245 void
247 {
248  if (NULL != h->flush_completion_task)
249  {
251  h->flush_completion_task = NULL;
252  }
253  if (0 != h->mq_len)
255  "Disconnect lost %u logger message[s]\n",
256  h->mq_len);
257  if (NULL != h->mq)
258  {
259  GNUNET_MQ_destroy(h->mq);
260  h->mq = NULL;
261  }
262  GNUNET_free(h);
263 }
264 
265 
275 void
277  const void *data,
278  size_t size)
279 {
280  if (NULL == h->mq)
281  return;
282  while (0 != size)
283  {
284  size_t fit_size = GNUNET_MIN(size,
285  BUFFER_SIZE - h->buse);
286  GNUNET_memcpy(&h->buf[h->buse],
287  data,
288  fit_size);
289  h->buse += fit_size;
290  data += fit_size;
291  size -= fit_size;
292  if (0 != size)
293  dispatch_buffer(h);
294  }
295 }
296 
297 
305 void
308  void *cb_cls)
309 {
310  GNUNET_assert(NULL == h->cb);
311  h->cb = cb;
312  h->cb_cls = cb_cls;
313  if ((NULL == h->mq) ||
314  (0 == h->buse))
315  {
317  return;
318  }
319  dispatch_buffer(h);
320 }
321 
322 
330 void
332 {
333  if (NULL != h->flush_completion_task)
334  {
336  h->flush_completion_task = NULL;
337  }
338  h->cb = NULL;
339  h->cb_cls = NULL;
340 }
341 
342 /* End of testbed_logger_api.c */
size_t bwrote
Number of bytes wrote since last flush.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:900
struct GNUNET_BIO_WriteHandle * bw
hanlde to the file to write the load statistics to
GNUNET_MQ_Error
Error codes for the queue.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
GNUNET_TESTBED_LOGGER_FlushCompletion cb
Flush completion callback.
struct GNUNET_TESTBED_LOGGER_Handle * GNUNET_TESTBED_LOGGER_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the testbed logger service.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void(* GNUNET_TESTBED_LOGGER_FlushCompletion)(void *cls, size_t size)
Functions of this type are called to notify a successful transmission of the message to the logger se...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
void * cb_cls
Closure for cb.
size_t buse
How many bytes in buf are in use?
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
void GNUNET_MQ_notify_sent(struct GNUNET_MQ_Envelope *ev, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
Call a callback once the envelope has been sent, that is, sending it can not be canceled anymore...
Definition: mq.c:772
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:80
unsigned int mq_len
Number of entries in the MQ.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
struct GNUNET_MQ_Handle * mq
Client connection.
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
We got disconnected from the logger.
static unsigned int size
Size of the "table".
Definition: peer.c:66
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
Handle to a message queue.
Definition: mq.c:84
char buf[(GNUNET_MAX_MESSAGE_SIZE - sizeof(struct GNUNET_MessageHeader))]
Local buffer for data to be transmitted.
Connection handle for the logger service.
struct GNUNET_SCHEDULER_Task * flush_completion_task
Task to call the flush completion callback.
configuration data
Definition: configuration.c:83
static void trigger_flush_notification(struct GNUNET_TESTBED_LOGGER_Handle *h)
Schedule the flush completion notification task.
struct GNUNET_TIME_Relative retry_backoff
How long after should we retry sending a message to the service?
Entry in list of pending tasks.
Definition: scheduler.c:131
static void notify_sent(void *cls)
MQ successfully sent a message.
#define LOG(kind,...)
Generic logging shorthand.
void GNUNET_TESTBED_LOGGER_write(struct GNUNET_TESTBED_LOGGER_Handle *h, const void *data, size_t size)
Send data to be logged to the logger service.
static void call_flush_completion(void *cls)
Task to call the flush completion notification.
Header for all communications.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:351
void GNUNET_TESTBED_LOGGER_flush(struct GNUNET_TESTBED_LOGGER_Handle *h, GNUNET_TESTBED_LOGGER_FlushCompletion cb, void *cb_cls)
Flush the buffered data to the logger service.
static void dispatch_buffer(struct GNUNET_TESTBED_LOGGER_Handle *h)
Send the buffered data to the service.
uint32_t data
The data value.
#define BUFFER_SIZE
The size of the buffer we fill before sending out the message.
void GNUNET_TESTBED_LOGGER_disconnect(struct GNUNET_TESTBED_LOGGER_Handle *h)
Disconnect from the logger service.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_TESTBED_LOGGER_flush_cancel(struct GNUNET_TESTBED_LOGGER_Handle *h)
Cancel notification upon flush.
Time for relative time used by GNUnet, in microseconds.
#define GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG
Message for TESTBED LOGGER.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956