GNUnet  0.20.0
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)
131  GNUNET_SCHEDULER_cancel (h->flush_completion_task);
132  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 
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);
211  h->mq = NULL;
212 }
213 
214 
224 {
226 
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  {
252  GNUNET_SCHEDULER_cancel (h->flush_completion_task);
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  {
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 
300 void
303  void *cb_cls)
304 {
305  GNUNET_assert (NULL == h->cb);
306  h->cb = cb;
307  h->cb_cls = cb_cls;
308  if ((NULL == h->mq) ||
309  (0 == h->buse))
310  {
312  return;
313  }
314  dispatch_buffer (h);
315 }
316 
317 
325 void
327 {
328  if (NULL != h->flush_completion_task)
329  {
330  GNUNET_SCHEDULER_cancel (h->flush_completion_task);
331  h->flush_completion_task = NULL;
332  }
333  h->cb = NULL;
334  h->cb_cls = NULL;
335 }
336 
337 
338 /* End of testbed_logger_api.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
uint32_t data
The data value.
struct GNUNET_BIO_WriteHandle * bw
handle to the file to write the load statistics to
API for submitting data to the testbed logger service.
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
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MIN(a, b)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_WARNING
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
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:304
#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:63
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:638
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:683
#define GNUNET_MESSAGE_TYPE_TESTBED_LOGGER_MSG
Message for TESTBED LOGGER.
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:1299
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975
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.
void GNUNET_TESTBED_LOGGER_flush_cancel(struct GNUNET_TESTBED_LOGGER_Handle *h)
Cancel notification upon flush.
struct GNUNET_TESTBED_LOGGER_Handle * GNUNET_TESTBED_LOGGER_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the testbed logger service.
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...
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.
void GNUNET_TESTBED_LOGGER_disconnect(struct GNUNET_TESTBED_LOGGER_Handle *h)
Disconnect from the logger service.
static unsigned int size
Size of the "table".
Definition: peer.c:68
struct GNUNET_MQ_Handle * mq
Our connection to the ARM service.
Definition: arm_api.c:107
Handle to a message queue.
Definition: mq.c:87
Header for all communications.
Entry in list of pending tasks.
Definition: scheduler.c:136
Connection handle for the logger service.
struct GNUNET_TIME_Relative retry_backoff
How long after should we retry sending a message to the service?
struct GNUNET_MQ_Handle * mq
Client connection.
size_t buse
How many bytes in buf are in use?
unsigned int mq_len
Number of entries in the MQ.
GNUNET_TESTBED_LOGGER_FlushCompletion cb
Flush completion callback.
void * cb_cls
Closure for cb.
char buf[(GNUNET_MAX_MESSAGE_SIZE - sizeof(struct GNUNET_MessageHeader))]
Local buffer for data to be transmitted.
struct GNUNET_SCHEDULER_Task * flush_completion_task
Task to call the flush completion callback.
size_t bwrote
Number of bytes wrote since last flush.
Time for relative time used by GNUnet, in microseconds.
#define BUFFER_SIZE
The size of the buffer we fill before sending out the message.
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
We got disconnected from the logger.
static void notify_sent(void *cls)
MQ successfully sent a message.
static void dispatch_buffer(struct GNUNET_TESTBED_LOGGER_Handle *h)
Send the buffered data to the service.
#define LOG(kind,...)
Generic logging shorthand.
static void call_flush_completion(void *cls)
Task to call the flush completion notification.
static void trigger_flush_notification(struct GNUNET_TESTBED_LOGGER_Handle *h)
Schedule the flush completion notification task.