GNUnet  0.11.x
Data Structures | Functions | Variables
gnunet-service-statistics.c File Reference

program that tracks statistics More...

#include "platform.h"
#include "gnunet_bio_lib.h"
#include "gnunet_container_lib.h"
#include "gnunet_disk_lib.h"
#include "gnunet_getopt_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_service_lib.h"
#include "gnunet_statistics_service.h"
#include "gnunet_strings_lib.h"
#include "gnunet_time_lib.h"
#include "statistics.h"
Include dependency graph for gnunet-service-statistics.c:

Go to the source code of this file.

Data Structures

struct  WatchEntry
 Watch entry. More...
 
struct  StatsEntry
 Entry in the statistics list. More...
 
struct  SubsystemEntry
 We keep the statistics organized by subsystem for faster lookup during SET operations. More...
 
struct  ClientEntry
 Information about one of our clients. More...
 

Functions

static void save ()
 Write persistent statistics to disk. More...
 
static void transmit (struct ClientEntry *ce, const struct StatsEntry *e)
 Transmit the given stats value. More...
 
static void * client_connect_cb (void *cls, struct GNUNET_SERVICE_Client *c, struct GNUNET_MQ_Handle *mq)
 Callback called when a client connects to the service. More...
 
static int check_get (void *cls, const struct GNUNET_MessageHeader *message)
 Check integrity of GET-message. More...
 
static void handle_get (void *cls, const struct GNUNET_MessageHeader *message)
 Handle GET-message. More...
 
static void notify_change (struct StatsEntry *se)
 Notify all clients listening about a change to a value. More...
 
static struct SubsystemEntryfind_subsystem_entry (struct ClientEntry *ce, const char *service)
 Find the subsystem entry of the given name for the specified client. More...
 
static struct StatsEntryfind_stat_entry (struct SubsystemEntry *se, const char *name)
 Find the statistics entry of the given subsystem. More...
 
static int check_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
 Check format of SET-message. More...
 
static void handle_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
 Handle SET-message. More...
 
static int check_watch (void *cls, const struct GNUNET_MessageHeader *message)
 Check integrity of WATCH-message. More...
 
static void handle_watch (void *cls, const struct GNUNET_MessageHeader *message)
 Handle WATCH-message. More...
 
static void handle_disconnect (void *cls, const struct GNUNET_MessageHeader *message)
 Handle DISCONNECT-message. More...
 
static void do_shutdown ()
 Actually perform the shutdown. More...
 
static void shutdown_task (void *cls)
 Task run during shutdown. More...
 
static void client_disconnect_cb (void *cls, struct GNUNET_SERVICE_Client *client, void *app_cls)
 A client disconnected. More...
 
static int inject_message (void *cls, const struct GNUNET_MessageHeader *message)
 We've read a struct GNUNET_STATISTICS_SetMessage * from disk. More...
 
static void load ()
 Load persistent values from disk. More...
 
static void run (void *cls, const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SERVICE_Handle *service)
 Process statistics requests. More...
 
 GNUNET_SERVICE_MAIN ("statistics", GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, &run, &client_connect_cb, &client_disconnect_cb, NULL, GNUNET_MQ_hd_var_size(set, GNUNET_MESSAGE_TYPE_STATISTICS_SET, struct GNUNET_STATISTICS_SetMessage, NULL), GNUNET_MQ_hd_var_size(get, GNUNET_MESSAGE_TYPE_STATISTICS_GET, struct GNUNET_MessageHeader, NULL), GNUNET_MQ_hd_var_size(watch, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, struct GNUNET_MessageHeader, NULL), GNUNET_MQ_hd_fixed_size(disconnect, GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT, struct GNUNET_MessageHeader, NULL), GNUNET_MQ_handler_end())
 Define "main" method using service macro. More...
 

Variables

static const struct GNUNET_CONFIGURATION_Handlecfg
 Our configuration. More...
 
static struct SubsystemEntrysub_head
 Head of linked list of subsystems with active statistics. More...
 
static struct SubsystemEntrysub_tail
 Tail of linked list of subsystems with active statistics. More...
 
static unsigned int client_count
 Number of connected clients. More...
 
static struct GNUNET_NotificationContextnc
 Our notification context. More...
 
static uint32_t uidgen
 Counter used to generate unique values. More...
 
static int in_shutdown
 Set to GNUNET_YES if we are shutting down as soon as possible. More...
 

Detailed Description

program that tracks statistics

Author
Christian Grothoff

Definition in file gnunet-service-statistics.c.

Function Documentation

◆ save()

static void save ( )
static

Write persistent statistics to disk.

Definition at line 243 of file gnunet-service-statistics.c.

References _, fn, GNUNET_assert, GNUNET_BIO_write(), GNUNET_BIO_write_close(), GNUNET_BIO_write_open(), GNUNET_break, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONTAINER_DLL_remove, GNUNET_DISK_directory_create_for_file(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_free_non_null, GNUNET_htonll(), GNUNET_log, GNUNET_log_config_missing(), GNUNET_log_strerror_file, GNUNET_malloc, GNUNET_MESSAGE_TYPE_STATISTICS_SET, GNUNET_OK, GNUNET_STATISTICS_SETFLAG_PERSISTENT, GNUNET_STRINGS_buffer_fill(), GNUNET_STATISTICS_SetMessage::header, msg, StatsEntry::name, StatsEntry::persistent, SubsystemEntry::service, size, GNUNET_MessageHeader::size, SubsystemEntry::stat_head, SubsystemEntry::stat_tail, GNUNET_MessageHeader::type, StatsEntry::value, and wh.

Referenced by do_shutdown(), and GNUNET_STRINGS_parse_ipv6_policy().

244 {
245  struct SubsystemEntry *se;
246  struct StatsEntry *pos;
247  char *fn;
248  struct GNUNET_BIO_WriteHandle *wh;
249  uint16_t size;
250  unsigned long long total;
251  size_t nlen;
252  size_t slen;
254 
256  "STATISTICS",
257  "DATABASE",
258  &fn))
259  {
261  "STATISTICS",
262  "DATABASE");
263  return;
264  }
266  wh = GNUNET_BIO_write_open (fn);
267  total = 0;
268  while (NULL != (se = sub_head))
269  {
271  slen = strlen (se->service) + 1;
272  while (NULL != (pos = se->stat_head))
273  {
275  if ((pos->persistent) && (NULL != wh))
276  {
277  nlen = strlen (pos->name) + 1;
278  size = sizeof(struct GNUNET_STATISTICS_SetMessage) + nlen + slen;
279  GNUNET_assert (size < UINT16_MAX);
280  msg = GNUNET_malloc (size);
281 
282  msg->header.size = htons ((uint16_t) size);
284  GNUNET_assert (nlen + slen ==
285  GNUNET_STRINGS_buffer_fill ((char *) &msg[1],
286  nlen + slen,
287  2,
288  se->service,
289  pos->name));
290  msg->flags =
292  msg->value = GNUNET_htonll (pos->value);
293  if (GNUNET_OK != GNUNET_BIO_write (wh, msg, size))
294  {
296  if (GNUNET_OK != GNUNET_BIO_write_close (wh))
298  wh = NULL;
299  }
300  else
301  {
302  total += size;
303  }
304  GNUNET_free (msg);
305  }
306  GNUNET_free (pos);
307  }
308  GNUNET_free (se);
309  }
310  if (NULL != wh)
311  {
312  if (GNUNET_OK != GNUNET_BIO_write_close (wh))
314  if (0 == total)
315  GNUNET_break (0 == unlink (fn));
316  else
318  _ ("Wrote %llu bytes of statistics to `%s'\n"),
319  total,
320  fn);
321  }
323 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct StatsEntry * stat_head
Head of list of values kept for this subsystem.
#define GNUNET_MESSAGE_TYPE_STATISTICS_SET
Set a statistical value.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
const char * service
Name of the subsystem this entry is for, allocated at the end of this struct, do not free()...
uint64_t value
Our value.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:684
Message to set a statistic.
Definition: statistics.h:91
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
We keep the statistics organized by subsystem for faster lookup during SET operations.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static struct SubsystemEntry * sub_tail
Tail of linked list of subsystems with active statistics.
static char * fn
Filename of the unique file.
size_t GNUNET_STRINGS_buffer_fill(char *buffer, size_t size, unsigned int count,...)
Fill a buffer of the given size with count 0-terminated strings (given as varargs).
Definition: strings.c:65
int persistent
Is this value persistent?
Handle for buffered writing.
Definition: bio.c:379
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:35
static unsigned int size
Size of the "table".
Definition: peer.c:67
Entry in the statistics list.
static struct GNUNET_TESTBED_BarrierWaitHandle * wh
Our barrier wait handle.
static struct SubsystemEntry * sub_head
Head of linked list of subsystems with active statistics.
int GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h)
Close an open file for writing.
Definition: bio.c:438
const char * name
Name for the value stored by this entry, allocated at the end of this struct.
uint32_t flags
0 for absolute value, 1 for relative value; 2 to make persistent (see GNUNET_STATISTICS_SETFLAG_*).
Definition: statistics.h:102
struct StatsEntry * stat_tail
Tail of list of values kept for this subsystem.
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open(const char *fn)
Open a file for writing.
Definition: bio.c:410
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_STATISTICS_SET.
Definition: statistics.h:96
#define GNUNET_log(kind,...)
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
int GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: bio.c:483
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_STATISTICS_SETFLAG_PERSISTENT
The value being set is to be persistent (note that this bit can be combined with GNUNET_STATISTICS_SE...
Definition: statistics.h:83
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ transmit()

static void transmit ( struct ClientEntry ce,
const struct StatsEntry e 
)
static

Transmit the given stats value.

Parameters
clientreceiver of the value
evalue to transmit

Definition at line 333 of file gnunet-service-statistics.c.

References env, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_htonll(), GNUNET_log, GNUNET_MAX_MESSAGE_SIZE, GNUNET_MESSAGE_TYPE_STATISTICS_VALUE, GNUNET_MQ_msg_extra, GNUNET_MQ_send(), GNUNET_STATISTICS_PERSIST_BIT, GNUNET_STRINGS_buffer_fill(), m, ClientEntry::mq, StatsEntry::name, StatsEntry::persistent, SubsystemEntry::service, size, StatsEntry::subsystem, GNUNET_STATISTICS_ReplyMessage::uid, StatsEntry::uid, GNUNET_STATISTICS_ReplyMessage::value, and StatsEntry::value.

Referenced by handle_get().

334 {
335  struct GNUNET_MQ_Envelope *env;
337  size_t size;
338 
339  size = strlen (e->subsystem->service) + 1 + strlen (e->name) + 1;
342  m->uid = htonl (e->uid);
343  if (e->persistent)
344  m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
345  m->value = GNUNET_htonll (e->value);
346  GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
347  size,
348  2,
349  e->subsystem->service,
350  e->name));
352  "Transmitting value for `%s:%s' (%d): %llu\n",
353  e->subsystem->service,
354  e->name,
355  e->persistent,
356  (unsigned long long) e->value);
357  GNUNET_MQ_send (ce->mq, env);
358 }
const char * service
Name of the subsystem this entry is for, allocated at the end of this struct, do not free()...
uint64_t value
Our value.
Statistics message.
Definition: statistics.h:40
uint32_t uid
Unique numerical identifier for the value (will not change during the same client-session).
Definition: statistics.h:53
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_MESSAGE_TYPE_STATISTICS_VALUE
Response to a STATISTICS_GET message (with value).
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:104
#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
size_t GNUNET_STRINGS_buffer_fill(char *buffer, size_t size, unsigned int count,...)
Fill a buffer of the given size with count 0-terminated strings (given as varargs).
Definition: strings.c:65
int persistent
Is this value persistent?
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:35
static unsigned int size
Size of the "table".
Definition: peer.c:67
struct GNUNET_MQ_Handle * mq
Queue for transmissions to client.
#define GNUNET_STATISTICS_PERSIST_BIT
Flag for the struct GNUNET_STATISTICS_ReplyMessage UID only.
Definition: statistics.h:65
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
const char * name
Name for the value stored by this entry, allocated at the end of this struct.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
#define GNUNET_log(kind,...)
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
uint32_t uid
Unique ID.
struct SubsystemEntry * subsystem
Subsystem this entry belongs to.
uint64_t value
The value.
Definition: statistics.h:58
Here is the call graph for this function:
Here is the caller graph for this function:

◆ client_connect_cb()

static void* client_connect_cb ( void *  cls,
struct GNUNET_SERVICE_Client c,
struct GNUNET_MQ_Handle mq 
)
static

Callback called when a client connects to the service.

Parameters
clsclosure for the service
cthe new client that connected to the service
mqthe message queue used to send messages to the client
Returns
c

Definition at line 370 of file gnunet-service-statistics.c.

References WatchEntry::ce, ClientEntry::client, client_count, GNUNET_new, GNUNET_notification_context_add(), mq, and ClientEntry::mq.

Referenced by run().

373 {
374  struct ClientEntry *ce;
375 
376  ce = GNUNET_new (struct ClientEntry);
377  ce->client = c;
378  ce->mq = mq;
379  client_count++;
381  return ce;
382 }
static unsigned int client_count
Number of connected clients.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void GNUNET_notification_context_add(struct GNUNET_NotificationContext *nc, struct GNUNET_MQ_Handle *mq)
Add a subscriber to the notification context.
Definition: nc.c:160
struct GNUNET_SERVICE_Client * client
Handle identifying the client.
Information about one of our clients.
struct GNUNET_MQ_Handle * mq
Queue for transmissions to client.
static struct GNUNET_NotificationContext * nc
Our notification context.
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_get()

static int check_get ( void *  cls,
const struct GNUNET_MessageHeader message 
)
static

Check integrity of GET-message.

Parameters
clsidentification of the client
messagethe actual message
Returns
GNUNET_OK if message is well-formed

Definition at line 393 of file gnunet-service-statistics.c.

References GNUNET_break, GNUNET_OK, GNUNET_STRINGS_buffer_tokenize(), GNUNET_SYSERR, name, service, size, and GNUNET_MessageHeader::size.

394 {
395  const char *service;
396  const char *name;
397  size_t size;
398 
399  size = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
400  if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
401  size,
402  2,
403  &service,
404  &name))
405  {
406  GNUNET_break (0);
407  return GNUNET_SYSERR;
408  }
409  return GNUNET_OK;
410 }
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
unsigned int GNUNET_STRINGS_buffer_tokenize(const char *buffer, size_t size, unsigned int count,...)
Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count ...
Definition: strings.c:135
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:67
const char * name
Header for all communications.
Here is the call graph for this function:

◆ handle_get()

static void handle_get ( void *  cls,
const struct GNUNET_MessageHeader message 
)
static

Handle GET-message.

Parameters
clsidentification of the client
messagethe actual message

Definition at line 420 of file gnunet-service-statistics.c.

References WatchEntry::ce, ClientEntry::client, end, env, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_MESSAGE_TYPE_STATISTICS_END, GNUNET_MQ_msg, GNUNET_MQ_send(), GNUNET_SERVICE_client_continue(), GNUNET_STRINGS_buffer_tokenize(), ClientEntry::mq, StatsEntry::name, name, StatsEntry::next, SubsystemEntry::next, SubsystemEntry::service, service, size, GNUNET_MessageHeader::size, SubsystemEntry::stat_head, and transmit().

421 {
422  struct ClientEntry *ce = cls;
423  struct GNUNET_MQ_Envelope *env;
424  struct GNUNET_MessageHeader *end;
425  const char *service;
426  const char *name;
427  size_t slen;
428  size_t nlen;
429  struct SubsystemEntry *se;
430  struct StatsEntry *pos;
431  size_t size;
432 
433  size = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
434  GNUNET_assert (size ==
435  GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
436  size,
437  2,
438  &service,
439  &name));
440  slen = strlen (service);
441  nlen = strlen (name);
443  "Received request for statistics on `%s:%s'\n",
444  slen ? service : "*",
445  nlen ? name : "*");
446  for (se = sub_head; NULL != se; se = se->next)
447  {
448  if (! ((0 == slen) || (0 == strcmp (service, se->service))))
449  continue;
450  for (pos = se->stat_head; NULL != pos; pos = pos->next)
451  {
452  if (! ((0 == nlen) || (0 == strcmp (name, pos->name))))
453  continue;
454  transmit (ce, pos);
455  }
456  }
458  GNUNET_MQ_send (ce->mq, env);
460 }
struct StatsEntry * stat_head
Head of list of values kept for this subsystem.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
const char * service
Name of the subsystem this entry is for, allocated at the end of this struct, do not free()...
#define GNUNET_MESSAGE_TYPE_STATISTICS_END
Response to a STATISTICS_GET message (end of value stream).
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
struct StatsEntry * next
This is a linked list.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
struct GNUNET_SERVICE_Client * client
Handle identifying the client.
struct SubsystemEntry * next
Subsystems are kept in a DLL.
unsigned int GNUNET_STRINGS_buffer_tokenize(const char *buffer, size_t size, unsigned int count,...)
Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count ...
Definition: strings.c:135
We keep the statistics organized by subsystem for faster lookup during SET operations.
Information about one of our clients.
static void transmit(struct ClientEntry *ce, const struct StatsEntry *e)
Transmit the given stats value.
static unsigned int size
Size of the "table".
Definition: peer.c:67
struct GNUNET_MQ_Handle * mq
Queue for transmissions to client.
Entry in the statistics list.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static struct SubsystemEntry * sub_head
Head of linked list of subsystems with active statistics.
const char * name
Name for the value stored by this entry, allocated at the end of this struct.
const char * name
#define GNUNET_log(kind,...)
Header for all communications.
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_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2243
Here is the call graph for this function:

◆ notify_change()

static void notify_change ( struct StatsEntry se)
static

Notify all clients listening about a change to a value.

Parameters
sevalue that changed

Definition at line 469 of file gnunet-service-statistics.c.

References WatchEntry::ce, env, GNUNET_STATISTICS_WatchValueMessage::flags, GNUNET_htonll(), GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE, GNUNET_MQ_msg, GNUNET_MQ_send(), GNUNET_STATISTICS_SETFLAG_PERSISTENT, GNUNET_YES, WatchEntry::last_value, WatchEntry::last_value_set, ClientEntry::mq, WatchEntry::next, StatsEntry::persistent, GNUNET_STATISTICS_WatchValueMessage::reserved, StatsEntry::value, GNUNET_STATISTICS_WatchValueMessage::value, StatsEntry::we_head, WatchEntry::wid, and GNUNET_STATISTICS_WatchValueMessage::wid.

Referenced by handle_set(), and handle_watch().

470 {
471  struct GNUNET_MQ_Envelope *env;
473  struct WatchEntry *pos;
474 
475  for (pos = se->we_head; NULL != pos; pos = pos->next)
476  {
477  if (GNUNET_YES == pos->last_value_set)
478  {
479  if (pos->last_value == se->value)
480  continue;
481  }
482  else
483  {
484  pos->last_value_set = GNUNET_YES;
485  }
487  wvm->flags =
489  wvm->wid = htonl (pos->wid);
490  wvm->reserved = htonl (0);
491  wvm->value = GNUNET_htonll (se->value);
492  GNUNET_MQ_send (pos->ce->mq, env);
493  pos->last_value = se->value;
494  }
495 }
uint64_t value
Our value.
struct WatchEntry * we_head
Watch context for changes to this value, or NULL for none.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
uint32_t flags
0 for absolute value, 1 for relative value; 2 to make persistent (see GNUNET_STATISTICS_SETFLAG_*).
Definition: statistics.h:126
uint32_t reserved
Reserved (always 0).
Definition: statistics.h:139
Message transmitted if a watched value changes.
Definition: statistics.h:115
uint32_t wid
Unique watch number for this client and this watched value.
int last_value_set
Is last_value valid GNUNET_NO : last_value is n/a, GNUNET_YES: last_value is valid.
#define GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE
Changes to a watched value.
int persistent
Is this value persistent?
uint32_t wid
Unique watch identification number (watch requests are enumerated in the order they are received...
Definition: statistics.h:134
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:35
uint64_t last_value
Last value we communicated to the client for this watch entry.
struct GNUNET_MQ_Handle * mq
Queue for transmissions to client.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
struct ClientEntry * ce
For which client is this watch entry?
#define GNUNET_YES
Definition: gnunet_common.h:77
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
struct WatchEntry * next
Watch entries are kept in a linked list.
#define GNUNET_STATISTICS_SETFLAG_PERSISTENT
The value being set is to be persistent (note that this bit can be combined with GNUNET_STATISTICS_SE...
Definition: statistics.h:83
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_subsystem_entry()

static struct SubsystemEntry* find_subsystem_entry ( struct ClientEntry ce,
const char *  service 
)
static

Find the subsystem entry of the given name for the specified client.

Parameters
ceclient looking for the subsystem, may contain a hint to find the entry faster, can be NULL
servicename of the subsystem to look for
Returns
subsystem entry, never NULL (subsystem entry is created if necessary)

Definition at line 507 of file gnunet-service-statistics.c.

References GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_malloc, GNUNET_memcpy, SubsystemEntry::next, SubsystemEntry::service, and ClientEntry::subsystem.

Referenced by handle_set(), and handle_watch().

508 {
509  size_t slen;
510  struct SubsystemEntry *se;
511 
512  if (NULL != ce)
513  se = ce->subsystem;
514  else
515  se = NULL;
516  if ((NULL == se) || (0 != strcmp (service, se->service)))
517  {
518  for (se = sub_head; NULL != se; se = se->next)
519  if (0 == strcmp (service, se->service))
520  break;
521  if (NULL != ce)
522  ce->subsystem = se;
523  }
524  if (NULL != se)
525  return se;
527  "Allocating new subsystem entry `%s'\n",
528  service);
529  slen = strlen (service) + 1;
530  se = GNUNET_malloc (sizeof(struct SubsystemEntry) + slen);
531  GNUNET_memcpy (&se[1], service, slen);
532  se->service = (const char *) &se[1];
534  if (NULL != ce)
535  ce->subsystem = se;
536  return se;
537 }
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
const char * service
Name of the subsystem this entry is for, allocated at the end of this struct, do not free()...
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
struct SubsystemEntry * subsystem
Which subsystem is this client writing to (SET/UPDATE)?
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
struct SubsystemEntry * next
Subsystems are kept in a DLL.
We keep the statistics organized by subsystem for faster lookup during SET operations.
static struct SubsystemEntry * sub_tail
Tail of linked list of subsystems with active statistics.
static struct SubsystemEntry * sub_head
Head of linked list of subsystems with active statistics.
#define GNUNET_log(kind,...)
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the caller graph for this function:

◆ find_stat_entry()

static struct StatsEntry* find_stat_entry ( struct SubsystemEntry se,
const char *  name 
)
static

Find the statistics entry of the given subsystem.

Parameters
subsystemsubsystem to look in
namename of the entry to look for
Returns
statistis entry, or NULL if not found

Definition at line 548 of file gnunet-service-statistics.c.

References StatsEntry::name, StatsEntry::next, and SubsystemEntry::stat_head.

Referenced by handle_set(), and handle_watch().

549 {
550  struct StatsEntry *pos;
551 
552  for (pos = se->stat_head; NULL != pos; pos = pos->next)
553  if (0 == strcmp (name, pos->name))
554  return pos;
555  return NULL;
556 }
struct StatsEntry * stat_head
Head of list of values kept for this subsystem.
struct StatsEntry * next
This is a linked list.
Entry in the statistics list.
const char * name
Name for the value stored by this entry, allocated at the end of this struct.
const char * name
Here is the caller graph for this function:

◆ check_set()

static int check_set ( void *  cls,
const struct GNUNET_STATISTICS_SetMessage msg 
)
static

Check format of SET-message.

Parameters
clsthe struct ClientEntry
messagethe actual message
Returns
GNUNET_OK if message is well-formed

Definition at line 567 of file gnunet-service-statistics.c.

References GNUNET_break, GNUNET_OK, GNUNET_STRINGS_buffer_tokenize(), GNUNET_SYSERR, GNUNET_STATISTICS_SetMessage::header, name, service, and GNUNET_MessageHeader::size.

Referenced by inject_message().

568 {
569  const char *service;
570  const char *name;
571  size_t msize;
572 
573  msize = ntohs (msg->header.size) - sizeof(*msg);
574  if (msize != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
575  msize,
576  2,
577  &service,
578  &name))
579  {
580  GNUNET_break (0);
581  return GNUNET_SYSERR;
582  }
583  return GNUNET_OK;
584 }
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
unsigned int GNUNET_STRINGS_buffer_tokenize(const char *buffer, size_t size, unsigned int count,...)
Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count ...
Definition: strings.c:135
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
const char * name
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_STATISTICS_SET.
Definition: statistics.h:96
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_set()

static void handle_set ( void *  cls,
const struct GNUNET_STATISTICS_SetMessage msg 
)
static

Handle SET-message.

Parameters
clsthe struct ClientEntry
messagethe actual message

Definition at line 594 of file gnunet-service-statistics.c.

References WatchEntry::ce, ClientEntry::client, delta, find_stat_entry(), find_subsystem_entry(), GNUNET_assert, GNUNET_break, GNUNET_CONTAINER_DLL_insert, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_malloc, GNUNET_memcpy, GNUNET_NO, GNUNET_ntohll(), GNUNET_SERVICE_client_continue(), GNUNET_STATISTICS_SETFLAG_PERSISTENT, GNUNET_STATISTICS_SETFLAG_RELATIVE, GNUNET_STRINGS_buffer_tokenize(), GNUNET_YES, GNUNET_STATISTICS_SetMessage::header, StatsEntry::name, name, notify_change(), StatsEntry::persistent, service, StatsEntry::set, size, GNUNET_MessageHeader::size, SubsystemEntry::stat_head, SubsystemEntry::stat_tail, StatsEntry::subsystem, StatsEntry::uid, uidgen, StatsEntry::value, and value.

Referenced by inject_message().

595 {
596  struct ClientEntry *ce = cls;
597  const char *service;
598  const char *name;
599  size_t nlen;
600  uint16_t msize;
601  uint16_t size;
602  struct SubsystemEntry *se;
603  struct StatsEntry *pos;
604  uint32_t flags;
605  uint64_t value;
606  int64_t delta;
607  int changed;
608  int initial_set;
609 
610  msize = ntohs (msg->header.size);
611  size = msize - sizeof(struct GNUNET_STATISTICS_SetMessage);
612  GNUNET_assert (size == GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
613  size,
614  2,
615  &service,
616  &name));
617  se = find_subsystem_entry (ce, service);
618  flags = ntohl (msg->flags);
619  value = GNUNET_ntohll (msg->value);
621  "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
622  service,
623  name,
624  (unsigned int) flags,
625  (unsigned long long) value);
626  pos = find_stat_entry (se, name);
627  if (NULL != pos)
628  {
629  initial_set = 0;
630  if (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE))
631  {
632  changed = (pos->value != value);
633  pos->value = value;
634  }
635  else
636  {
637  delta = (int64_t) value;
638  if ((delta < 0) && (pos->value < -delta))
639  {
640  changed = (0 != pos->value);
641  pos->value = 0;
642  }
643  else
644  {
645  changed = (0 != delta);
646  GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
647  pos->value += delta;
648  }
649  }
650  if (GNUNET_NO == pos->set)
651  {
652  pos->set = GNUNET_YES;
653  initial_set = 1;
654  }
655  pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
656  if (pos != se->stat_head)
657  {
658  /* move to front for faster setting next time! */
661  }
663  "Statistic `%s:%s' updated to value %llu (%d).\n",
664  service,
665  name,
666  (unsigned long long) pos->value,
667  pos->persistent);
668  if ((changed) || (1 == initial_set))
669  notify_change (pos);
671  return;
672  }
673  /* not found, create a new entry */
674  nlen = strlen (name) + 1;
675  pos = GNUNET_malloc (sizeof(struct StatsEntry) + nlen);
676  GNUNET_memcpy (&pos[1], name, nlen);
677  pos->name = (const char *) &pos[1];
678  pos->subsystem = se;
679  if ((0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE)) ||
680  (0 < (int64_t) GNUNET_ntohll (msg->value)))
681  {
682  pos->value = GNUNET_ntohll (msg->value);
683  pos->set = GNUNET_YES;
684  }
685  else
686  {
687  pos->set = GNUNET_NO;
688  }
689  pos->uid = uidgen++;
690  pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
693  "New statistic on `%s:%s' with value %llu created.\n",
694  service,
695  name,
696  (unsigned long long) pos->value);
697  if (NULL != ce)
699 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct StatsEntry * stat_head
Head of list of values kept for this subsystem.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:35
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
uint64_t value
Our value.
static void notify_change(struct StatsEntry *se)
Notify all clients listening about a change to a value.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_NO
Definition: gnunet_common.h:78
Message to set a statistic.
Definition: statistics.h:91
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_STATISTICS_SETFLAG_RELATIVE
The value being set is a relative change.
Definition: statistics.h:75
struct GNUNET_SERVICE_Client * client
Handle identifying the client.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
unsigned int GNUNET_STRINGS_buffer_tokenize(const char *buffer, size_t size, unsigned int count,...)
Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count ...
Definition: strings.c:135
We keep the statistics organized by subsystem for faster lookup during SET operations.
static char * value
Value of the record to add/remove.
Information about one of our clients.
int persistent
Is this value persistent?
static unsigned int size
Size of the "table".
Definition: peer.c:67
static uint32_t uidgen
Counter used to generate unique values.
Entry in the statistics list.
const char * name
Name for the value stored by this entry, allocated at the end of this struct.
int set
Is this value set? GNUNET_NO: value is n/a, GNUNET_YES: value is valid.
uint32_t flags
0 for absolute value, 1 for relative value; 2 to make persistent (see GNUNET_STATISTICS_SETFLAG_*).
Definition: statistics.h:102
struct StatsEntry * stat_tail
Tail of list of values kept for this subsystem.
const char * name
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_STATISTICS_SET.
Definition: statistics.h:96
#define GNUNET_log(kind,...)
static struct StatsEntry * find_stat_entry(struct SubsystemEntry *se, const char *name)
Find the statistics entry of the given subsystem.
#define GNUNET_YES
Definition: gnunet_common.h:77
uint32_t uid
Unique ID.
struct SubsystemEntry * subsystem
Subsystem this entry belongs to.
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2243
#define GNUNET_malloc(size)
Wrapper around malloc.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:48
#define GNUNET_STATISTICS_SETFLAG_PERSISTENT
The value being set is to be persistent (note that this bit can be combined with GNUNET_STATISTICS_SE...
Definition: statistics.h:83
static struct SubsystemEntry * find_subsystem_entry(struct ClientEntry *ce, const char *service)
Find the subsystem entry of the given name for the specified client.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_watch()

static int check_watch ( void *  cls,
const struct GNUNET_MessageHeader message 
)
static

Check integrity of WATCH-message.

Parameters
clsthe struct ClientEntry *
messagethe actual message
Returns
GNUNET_OK if message is well-formed

Definition at line 710 of file gnunet-service-statistics.c.

References GNUNET_break, GNUNET_OK, GNUNET_STRINGS_buffer_tokenize(), GNUNET_SYSERR, name, service, size, and GNUNET_MessageHeader::size.

711 {
712  size_t size;
713  const char *service;
714  const char *name;
715 
716  size = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
717  if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
718  size,
719  2,
720  &service,
721  &name))
722  {
723  GNUNET_break (0);
724  return GNUNET_SYSERR;
725  }
726  return GNUNET_OK;
727 }
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
unsigned int GNUNET_STRINGS_buffer_tokenize(const char *buffer, size_t size, unsigned int count,...)
Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count ...
Definition: strings.c:135
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:67
const char * name
Header for all communications.
Here is the call graph for this function:

◆ handle_watch()

static void handle_watch ( void *  cls,
const struct GNUNET_MessageHeader message 
)
static

Handle WATCH-message.

Parameters
clsthe struct ClientEntry *
messagethe actual message

Definition at line 737 of file gnunet-service-statistics.c.

References WatchEntry::ce, ClientEntry::client, find_stat_entry(), find_subsystem_entry(), GNUNET_assert, GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_malloc, GNUNET_memcpy, GNUNET_new, GNUNET_NO, GNUNET_SERVICE_client_continue(), GNUNET_SERVICE_client_drop(), GNUNET_SERVICE_client_mark_monitor(), GNUNET_STRINGS_buffer_tokenize(), WatchEntry::last_value_set, ClientEntry::max_wid, StatsEntry::name, name, notify_change(), service, StatsEntry::set, size, GNUNET_MessageHeader::size, SubsystemEntry::stat_head, SubsystemEntry::stat_tail, StatsEntry::subsystem, StatsEntry::uid, uidgen, StatsEntry::value, StatsEntry::we_head, StatsEntry::we_tail, and WatchEntry::wid.

738 {
739  struct ClientEntry *ce = cls;
740  const char *service;
741  const char *name;
742  uint16_t msize;
743  uint16_t size;
744  struct SubsystemEntry *se;
745  struct StatsEntry *pos;
746  struct WatchEntry *we;
747  size_t nlen;
748 
749  if (NULL == nc)
750  {
752  return;
753  }
755  msize = ntohs (message->size);
756  size = msize - sizeof(struct GNUNET_MessageHeader);
757  GNUNET_assert (size ==
758  GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
759  size,
760  2,
761  &service,
762  &name));
764  "Received request to watch statistic on `%s:%s'\n",
765  service,
766  name);
767  se = find_subsystem_entry (ce, service);
768  pos = find_stat_entry (se, name);
769  if (NULL == pos)
770  {
771  nlen = strlen (name) + 1;
772  pos = GNUNET_malloc (sizeof(struct StatsEntry) + nlen);
773  GNUNET_memcpy (&pos[1], name, nlen);
774  pos->name = (const char *) &pos[1];
775  pos->subsystem = se;
777  pos->uid = uidgen++;
778  pos->set = GNUNET_NO;
780  "New statistic on `%s:%s' with value %llu created.\n",
781  service,
782  name,
783  (unsigned long long) pos->value);
784  }
785  we = GNUNET_new (struct WatchEntry);
786  we->ce = ce;
788  we->wid = ce->max_wid++;
790  if (0 != pos->value)
791  notify_change (pos);
793 }
struct StatsEntry * stat_head
Head of list of values kept for this subsystem.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
uint64_t value
Our value.
static void notify_change(struct StatsEntry *se)
Notify all clients listening about a change to a value.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct WatchEntry * we_head
Watch context for changes to this value, or NULL for none.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
struct GNUNET_SERVICE_Client * client
Handle identifying the client.
unsigned int GNUNET_STRINGS_buffer_tokenize(const char *buffer, size_t size, unsigned int count,...)
Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count ...
Definition: strings.c:135
struct WatchEntry * we_tail
Watch context for changes to this value, or NULL for none.
We keep the statistics organized by subsystem for faster lookup during SET operations.
uint32_t wid
Unique watch number for this client and this watched value.
Information about one of our clients.
int last_value_set
Is last_value valid GNUNET_NO : last_value is n/a, GNUNET_YES: last_value is valid.
void GNUNET_SERVICE_client_mark_monitor(struct GNUNET_SERVICE_Client *c)
Set the &#39;monitor&#39; flag on this client.
Definition: service.c:2407
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2324
static unsigned int size
Size of the "table".
Definition: peer.c:67
static uint32_t uidgen
Counter used to generate unique values.
Entry in the statistics list.
struct ClientEntry * ce
For which client is this watch entry?
const char * name
Name for the value stored by this entry, allocated at the end of this struct.
int set
Is this value set? GNUNET_NO: value is n/a, GNUNET_YES: value is valid.
static struct GNUNET_NotificationContext * nc
Our notification context.
struct StatsEntry * stat_tail
Tail of list of values kept for this subsystem.
const char * name
#define GNUNET_log(kind,...)
static struct StatsEntry * find_stat_entry(struct SubsystemEntry *se, const char *name)
Find the statistics entry of the given subsystem.
Header for all communications.
uint32_t uid
Unique ID.
struct SubsystemEntry * subsystem
Subsystem this entry belongs to.
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2243
uint32_t max_wid
Maximum watch ID used by this client so far.
#define GNUNET_malloc(size)
Wrapper around malloc.
static struct SubsystemEntry * find_subsystem_entry(struct ClientEntry *ce, const char *service)
Find the subsystem entry of the given name for the specified client.
Here is the call graph for this function:

◆ handle_disconnect()

static void handle_disconnect ( void *  cls,
const struct GNUNET_MessageHeader message 
)
static

Handle DISCONNECT-message.

Sync to disk and send back a GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM message.

Parameters
clsthe struct ClientEntry *
messagethe actual message

Definition at line 805 of file gnunet-service-statistics.c.

References WatchEntry::ce, ClientEntry::client, env, GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM, GNUNET_MQ_msg, GNUNET_MQ_send(), GNUNET_SERVICE_client_continue(), ClientEntry::mq, and msg.

806 {
807  struct ClientEntry *ce = cls;
808  struct GNUNET_MQ_Envelope *env;
809  struct GNUNET_MessageHeader *msg;
810 
812  GNUNET_MQ_send (ce->mq, env);
814 }
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
struct GNUNET_SERVICE_Client * client
Handle identifying the client.
#define GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM
Service confirms disconnect and that it is done processing all requests from the client.
Information about one of our clients.
struct GNUNET_MQ_Handle * mq
Queue for transmissions to client.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
Header for all communications.
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_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2243
Here is the call graph for this function:

◆ do_shutdown()

static void do_shutdown ( )
static

Actually perform the shutdown.

Definition at line 821 of file gnunet-service-statistics.c.

References client_count, GNUNET_assert, GNUNET_break, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_notification_context_destroy(), save(), SubsystemEntry::stat_head, SubsystemEntry::stat_tail, StatsEntry::we_head, and StatsEntry::we_tail.

Referenced by client_disconnect_cb(), and shutdown_task().

822 {
823  struct WatchEntry *we;
824  struct StatsEntry *pos;
825  struct SubsystemEntry *se;
826 
827  if (NULL == nc)
828  return;
829  save ();
831  nc = NULL;
833  while (NULL != (se = sub_head))
834  {
836  while (NULL != (pos = se->stat_head))
837  {
839  while (NULL != (we = pos->we_head))
840  {
841  GNUNET_break (0);
843  GNUNET_free (we);
844  }
845  GNUNET_free (pos);
846  }
847  GNUNET_free (se);
848  }
849 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct StatsEntry * stat_head
Head of list of values kept for this subsystem.
static unsigned int client_count
Number of connected clients.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct WatchEntry * we_head
Watch context for changes to this value, or NULL for none.
static void save()
Write persistent statistics to disk.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct WatchEntry * we_tail
Watch context for changes to this value, or NULL for none.
We keep the statistics organized by subsystem for faster lookup during SET operations.
static struct SubsystemEntry * sub_tail
Tail of linked list of subsystems with active statistics.
Entry in the statistics list.
static struct SubsystemEntry * sub_head
Head of linked list of subsystems with active statistics.
static struct GNUNET_NotificationContext * nc
Our notification context.
struct StatsEntry * stat_tail
Tail of list of values kept for this subsystem.
void GNUNET_notification_context_destroy(struct GNUNET_NotificationContext *nc)
Destroy the context, force disconnect for all subscribers.
Definition: nc.c:137
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ shutdown_task()

static void shutdown_task ( void *  cls)
static

Task run during shutdown.

Parameters
clsunused

Definition at line 858 of file gnunet-service-statistics.c.

References client_count, do_shutdown(), GNUNET_YES, and in_shutdown.

Referenced by run().

859 {
861  if (0 != client_count)
862  return;
863  do_shutdown ();
864 }
static unsigned int client_count
Number of connected clients.
static int in_shutdown
Set to GNUNET_YES if we are shutting down as soon as possible.
static void do_shutdown()
Actually perform the shutdown.
#define GNUNET_YES
Definition: gnunet_common.h:77
Here is the call graph for this function:
Here is the caller graph for this function:

◆ client_disconnect_cb()

static void client_disconnect_cb ( void *  cls,
struct GNUNET_SERVICE_Client client,
void *  app_cls 
)
static

A client disconnected.

Remove all of its data structure entries.

Parameters
clsclosure, NULL
clientidentification of the client
app_clsthe struct ClientEntry *

Definition at line 875 of file gnunet-service-statistics.c.

References WatchEntry::ce, client_count, do_shutdown(), GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_YES, in_shutdown, WatchEntry::next, StatsEntry::next, SubsystemEntry::next, SubsystemEntry::stat_head, StatsEntry::we_head, and StatsEntry::we_tail.

Referenced by run().

878 {
879  struct ClientEntry *ce = app_cls;
880  struct WatchEntry *we;
881  struct WatchEntry *wen;
882  struct StatsEntry *pos;
883  struct SubsystemEntry *se;
884 
885  client_count--;
886  for (se = sub_head; NULL != se; se = se->next)
887  {
888  for (pos = se->stat_head; NULL != pos; pos = pos->next)
889  {
890  wen = pos->we_head;
891  while (NULL != (we = wen))
892  {
893  wen = we->next;
894  if (we->ce != ce)
895  continue;
897  GNUNET_free (we);
898  }
899  }
900  }
901  GNUNET_free (ce);
902  if ((0 == client_count) && (GNUNET_YES == in_shutdown))
903  do_shutdown ();
904 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct StatsEntry * stat_head
Head of list of values kept for this subsystem.
static unsigned int client_count
Number of connected clients.
struct StatsEntry * next
This is a linked list.
struct WatchEntry * we_head
Watch context for changes to this value, or NULL for none.
static int in_shutdown
Set to GNUNET_YES if we are shutting down as soon as possible.
struct SubsystemEntry * next
Subsystems are kept in a DLL.
struct WatchEntry * we_tail
Watch context for changes to this value, or NULL for none.
We keep the statistics organized by subsystem for faster lookup during SET operations.
Information about one of our clients.
static void do_shutdown()
Actually perform the shutdown.
Entry in the statistics list.
struct ClientEntry * ce
For which client is this watch entry?
static struct SubsystemEntry * sub_head
Head of linked list of subsystems with active statistics.
#define GNUNET_YES
Definition: gnunet_common.h:77
struct WatchEntry * next
Watch entries are kept in a linked list.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ inject_message()

static int inject_message ( void *  cls,
const struct GNUNET_MessageHeader message 
)
static

We've read a struct GNUNET_STATISTICS_SetMessage * from disk.

Check that it is well-formed, and if so pass it to the handler for set messages.

Parameters
clsNULL
messagethe message found on disk
Returns
GNUNET_OK on success, GNUNET_NO to stop further processing (no error) GNUNET_SYSERR to stop further processing with error

Definition at line 919 of file gnunet-service-statistics.c.

References check_set(), GNUNET_break, GNUNET_OK, GNUNET_SYSERR, handle_set(), and GNUNET_MessageHeader::size.

Referenced by load().

920 {
921  uint16_t msize = ntohs (message->size);
922  const struct GNUNET_STATISTICS_SetMessage *sm;
923 
924  sm = (const struct GNUNET_STATISTICS_SetMessage *) message;
925  if ((sizeof(struct GNUNET_STATISTICS_SetMessage) > msize) ||
926  (GNUNET_OK != check_set (NULL, sm)))
927  {
928  GNUNET_break (0);
929  return GNUNET_SYSERR;
930  }
931  handle_set (NULL, sm);
932  return GNUNET_OK;
933 }
Message to set a statistic.
Definition: statistics.h:91
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static int check_set(void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
Check format of SET-message.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static void handle_set(void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
Handle SET-message.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ load()

static void load ( )
static

Load persistent values from disk.

Disk format is exactly the same format that we also use for setting the values over the network.

Definition at line 941 of file gnunet-service-statistics.c.

References _, buf, fn, GNUNET_BIO_read(), GNUNET_BIO_read_close(), GNUNET_BIO_read_open(), GNUNET_break, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_DISK_file_size(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_log_config_missing(), GNUNET_log_strerror_file, GNUNET_malloc, GNUNET_MST_create(), GNUNET_MST_destroy(), GNUNET_MST_from_buffer(), GNUNET_NO, GNUNET_OK, GNUNET_YES, and inject_message().

Referenced by run().

942 {
943  char *fn;
944  struct GNUNET_BIO_ReadHandle *rh;
945  uint64_t fsize;
946  char *buf;
947  struct GNUNET_MessageStreamTokenizer *mst;
948 
950  "STATISTICS",
951  "DATABASE",
952  &fn))
953  {
955  "STATISTICS",
956  "DATABASE");
957  return;
958  }
959  if ((GNUNET_OK !=
960  GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) ||
961  (0 == fsize))
962  {
963  GNUNET_free (fn);
964  return;
965  }
966  buf = GNUNET_malloc (fsize);
967  rh = GNUNET_BIO_read_open (fn);
968  if (! rh)
969  {
970  GNUNET_free (buf);
971  GNUNET_free (fn);
972  return;
973  }
974  if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
975  {
978  GNUNET_free (buf);
979  GNUNET_free (fn);
980  return;
981  }
983  _ ("Loading %llu bytes of statistics from `%s'\n"),
984  (unsigned long long) fsize,
985  fn);
986  mst = GNUNET_MST_create (&inject_message, NULL);
987  GNUNET_break (
988  GNUNET_OK ==
989  GNUNET_MST_from_buffer (mst, buf, (size_t) fsize, GNUNET_YES, GNUNET_NO));
990  GNUNET_MST_destroy (mst);
991  GNUNET_free (buf);
993  GNUNET_free (fn);
994 }
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
int GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg)
Close an open file.
Definition: bio.c:119
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
struct GNUNET_BIO_ReadHandle * GNUNET_BIO_read_open(const char *fn)
Open a file for reading.
Definition: bio.c:94
int GNUNET_BIO_read(struct GNUNET_BIO_ReadHandle *h, const char *what, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: bio.c:144
static int inject_message(void *cls, const struct GNUNET_MessageHeader *message)
We&#39;ve read a struct GNUNET_STATISTICS_SetMessage * from disk.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
void GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst)
Destroys a tokenizer.
Definition: mst.c:411
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static char * fn
Filename of the unique file.
Handle to a message stream tokenizer.
Definition: mst.c:43
static char buf[2048]
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition: mst.c:85
int GNUNET_MST_from_buffer(struct GNUNET_MessageStreamTokenizer *mst, const char *buf, size_t size, int purge, int one_shot)
Add incoming data to the receive buffer and call the callback for all complete messages.
Definition: mst.c:114
int GNUNET_DISK_file_size(const char *filename, uint64_t *size, int include_symbolic_links, int single_file_mode)
Get the size of the file (or directory) of the given file (in bytes).
Definition: disk.c:257
#define GNUNET_log(kind,...)
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
#define GNUNET_YES
Definition: gnunet_common.h:77
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
#define GNUNET_malloc(size)
Wrapper around malloc.
Handle for buffered reading.
Definition: bio.c:53
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
const struct GNUNET_CONFIGURATION_Handle c,
struct GNUNET_SERVICE_Handle service 
)
static

Process statistics requests.

Parameters
clsclosure
cconfiguration to use
servicethe initialized service

Definition at line 1005 of file gnunet-service-statistics.c.

References __attribute__, client_connect_cb(), client_disconnect_cb(), disconnect(), GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT, GNUNET_MESSAGE_TYPE_STATISTICS_GET, GNUNET_MESSAGE_TYPE_STATISTICS_SET, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, GNUNET_MQ_handler_end, GNUNET_MQ_hd_fixed_size, GNUNET_MQ_hd_var_size, GNUNET_notification_context_create(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_SERVICE_MAIN(), GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, load(), shutdown_task(), and watch.

1008 {
1009  cfg = c;
1011  load ();
1013 }
static void load()
Load persistent values from disk.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1300
static void shutdown_task(void *cls)
Task run during shutdown.
static struct GNUNET_NotificationContext * nc
Our notification context.
struct GNUNET_NotificationContext * GNUNET_notification_context_create(unsigned int queue_length)
Create a new notification context.
Definition: nc.c:121
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Here is the call graph for this function:

◆ GNUNET_SERVICE_MAIN()

Define "main" method using service macro.

Referenced by run().

Here is the caller graph for this function:

Variable Documentation

◆ cfg

const struct GNUNET_CONFIGURATION_Handle* cfg
static

Our configuration.

Definition at line 206 of file gnunet-service-statistics.c.

◆ sub_head

struct SubsystemEntry* sub_head
static

Head of linked list of subsystems with active statistics.

Definition at line 211 of file gnunet-service-statistics.c.

◆ sub_tail

struct SubsystemEntry* sub_tail
static

Tail of linked list of subsystems with active statistics.

Definition at line 216 of file gnunet-service-statistics.c.

◆ client_count

unsigned int client_count
static

Number of connected clients.

Definition at line 221 of file gnunet-service-statistics.c.

Referenced by client_connect_cb(), client_disconnect_cb(), do_shutdown(), and shutdown_task().

◆ nc

struct GNUNET_NotificationContext* nc
static

Our notification context.

Definition at line 226 of file gnunet-service-statistics.c.

◆ uidgen

uint32_t uidgen
static

Counter used to generate unique values.

Definition at line 231 of file gnunet-service-statistics.c.

Referenced by handle_set(), and handle_watch().

◆ in_shutdown

int in_shutdown
static

Set to GNUNET_YES if we are shutting down as soon as possible.

Definition at line 236 of file gnunet-service-statistics.c.

Referenced by client_disconnect_cb(), and shutdown_task().