GNUnet  0.11.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 \
43  GNUNET_MessageHeader))
44 
49 {
54 
59 
63  void *cb_cls;
64 
69 
73  size_t buse;
74 
78  size_t bwrote;
79 
84 
89 
93  unsigned int mq_len;
94 };
95 
96 
102 static void
104 {
105  struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
107  void *cb_cls;
108  size_t bw;
109 
110  h->flush_completion_task = NULL;
111  bw = h->bwrote;
112  h->bwrote = 0;
113  cb = h->cb;
114  h->cb = NULL;
115  cb_cls = h->cb_cls;
116  h->cb_cls = NULL;
117  if (NULL != cb)
118  cb (cb_cls, bw);
119 }
120 
121 
127 static void
129 {
130  if (NULL != h->flush_completion_task)
134  h);
135 }
136 
137 
143 static void
145 
146 
152 static void
153 notify_sent (void *cls)
154 {
155  struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
156 
157  h->mq_len--;
158  if ((0 == h->mq_len) &&
159  (NULL != h->cb))
160  {
161  if (0 == h->buse)
163  else
164  dispatch_buffer (h);
165  }
166 }
167 
168 
174 static void
176 {
177  struct GNUNET_MessageHeader *msg;
178  struct GNUNET_MQ_Envelope *env;
179 
180  env = GNUNET_MQ_msg_extra (msg,
181  h->buse,
183  GNUNET_memcpy (&msg[1],
184  h->buf,
185  h->buse);
186  h->bwrote += h->buse;
187  h->buse = 0;
188  h->mq_len++;
190  &notify_sent,
191  h);
192  GNUNET_MQ_send (h->mq,
193  env);
194 }
195 
196 
203 static void
204 mq_error_handler (void *cls,
205  enum GNUNET_MQ_Error error)
206 {
207  struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
208 
209  GNUNET_break (0);
210  GNUNET_MQ_destroy (h->mq);
211  h->mq = NULL;
212 }
213 
214 
224 {
226 
228  h->mq = GNUNET_CLIENT_connect (cfg,
229  "testbed-logger",
230  NULL,
232  h);
233  if (NULL == h->mq)
234  {
235  GNUNET_free (h);
236  return NULL;
237  }
238  return h;
239 }
240 
241 
247 void
249 {
250  if (NULL != h->flush_completion_task)
251  {
253  h->flush_completion_task = NULL;
254  }
255  if (0 != h->mq_len)
257  "Disconnect lost %u logger message[s]\n",
258  h->mq_len);
259  if (NULL != h->mq)
260  {
261  GNUNET_MQ_destroy (h->mq);
262  h->mq = NULL;
263  }
264  GNUNET_free (h);
265 }
266 
267 
277 void
279  const void *data,
280  size_t size)
281 {
282  if (NULL == h->mq)
283  return;
284  while (0 != size)
285  {
286  size_t fit_size = GNUNET_MIN (size,
287  BUFFER_SIZE - h->buse);
288  GNUNET_memcpy (&h->buf[h->buse],
289  data,
290  fit_size);
291  h->buse += fit_size;
292  data += fit_size;
293  size -= fit_size;
294  if (0 != size)
295  dispatch_buffer (h);
296  }
297 }
298 
299 
307 void
310  void *cb_cls)
311 {
312  GNUNET_assert (NULL == h->cb);
313  h->cb = cb;
314  h->cb_cls = cb_cls;
315  if ((NULL == h->mq) ||
316  (0 == h->buse))
317  {
319  return;
320  }
321  dispatch_buffer (h);
322 }
323 
324 
332 void
334 {
335  if (NULL != h->flush_completion_task)
336  {
338  h->flush_completion_task = NULL;
339  }
340  h->cb = NULL;
341  h->cb_cls = NULL;
342 }
343 
344 
345 /* End of testbed_logger_api.c */
size_t bwrote
Number of bytes wrote since last flush.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
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:1057
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:99
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:774
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:1280
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:80
unsigned int mq_len
Number of entries in the MQ.
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:67
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
Handle to a message queue.
Definition: mq.c:85
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:85
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:134
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:824
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:353
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:966