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 
48 {
53 
58 
62  void *cb_cls;
63 
68 
72  size_t buse;
73 
77  size_t bwrote;
78 
83 
88 
92  unsigned int mq_len;
93 };
94 
95 
101 static void
103 {
104  struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
106  void *cb_cls;
107  size_t bw;
108 
109  h->flush_completion_task = NULL;
110  bw = h->bwrote;
111  h->bwrote = 0;
112  cb = h->cb;
113  h->cb = NULL;
114  cb_cls = h->cb_cls;
115  h->cb_cls = NULL;
116  if (NULL != cb)
117  cb (cb_cls, bw);
118 }
119 
120 
126 static void
128 {
129  if (NULL != h->flush_completion_task)
133  h);
134 }
135 
136 
142 static void
144 
145 
151 static void
152 notify_sent (void *cls)
153 {
154  struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
155 
156  h->mq_len--;
157  if ( (0 == h->mq_len) &&
158  (NULL != h->cb) )
159  {
160  if (0 == h->buse)
162  else
163  dispatch_buffer (h);
164  }
165 }
166 
167 
173 static void
175 {
176  struct GNUNET_MessageHeader *msg;
177  struct GNUNET_MQ_Envelope *env;
178 
179  env = GNUNET_MQ_msg_extra (msg,
180  h->buse,
182  GNUNET_memcpy (&msg[1],
183  h->buf,
184  h->buse);
185  h->bwrote += h->buse;
186  h->buse = 0;
187  h->mq_len++;
189  &notify_sent,
190  h);
191  GNUNET_MQ_send (h->mq,
192  env);
193 }
194 
195 
202 static void
203 mq_error_handler (void *cls,
204  enum GNUNET_MQ_Error error)
205 {
206  struct GNUNET_TESTBED_LOGGER_Handle *h = cls;
207 
208  GNUNET_break (0);
209  GNUNET_MQ_destroy (h->mq);
210  h->mq = NULL;
211 }
212 
213 
223 {
225 
227  h->mq = GNUNET_CLIENT_connect (cfg,
228  "testbed-logger",
229  NULL,
231  h);
232  if (NULL == h->mq)
233  {
234  GNUNET_free (h);
235  return NULL;
236  }
237  return h;
238 }
239 
240 
246 void
248 {
249  if (NULL != h->flush_completion_task)
250  {
252  h->flush_completion_task = NULL;
253  }
254  if (0 != h->mq_len)
256  "Disconnect lost %u logger message[s]\n",
257  h->mq_len);
258  if (NULL != h->mq)
259  {
260  GNUNET_MQ_destroy (h->mq);
261  h->mq = NULL;
262  }
263  GNUNET_free (h);
264 }
265 
266 
276 void
278  const void *data,
279  size_t size)
280 {
281  if (NULL == h->mq)
282  return;
283  while (0 != size)
284  {
285  size_t fit_size = GNUNET_MIN (size,
286  BUFFER_SIZE - h->buse);
287  GNUNET_memcpy (&h->buf[h->buse],
288  data,
289  fit_size);
290  h->buse += fit_size;
291  data += fit_size;
292  size -= fit_size;
293  if (0 != size)
294  dispatch_buffer (h);
295  }
296 }
297 
298 
306 void
309  void *cb_cls)
310 {
311  GNUNET_assert (NULL == h->cb);
312  h->cb = cb;
313  h->cb_cls = cb_cls;
314  if ( (NULL == h->mq) ||
315  (0 == h->buse) )
316  {
318  return;
319  }
320  dispatch_buffer (h);
321 }
322 
323 
331 void
333 {
334  if (NULL != h->flush_completion_task)
335  {
337  h->flush_completion_task = NULL;
338  }
339  h->cb = NULL;
340  h->cb_cls = NULL;
341 }
342 
343 /* 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:901
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_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
#define GNUNET_memcpy(dst, src, n)
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:1273
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:83
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: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:965