GNUnet  0.10.x
arm_monitor_api.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009, 2010, 2012, 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 
27 #include "platform.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_protocols.h"
31 #include "arm.h"
32 
33 #define INIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
34 
35 #define LOG(kind, ...) GNUNET_log_from (kind, "arm-monitor-api", __VA_ARGS__)
36 
41 {
42 
47 
52 
57 
62 
67 
72 };
73 
74 
81 static int
83 
84 
90 static void
92 {
93  struct GNUNET_ARM_MonitorHandle *h = cls;
94 
95  h->reconnect_task = NULL;
97  "Connecting to ARM service for monitoring after delay\n");
99 }
100 
101 
108 static void
110 {
111  if (NULL != h->mq)
112  {
113  GNUNET_MQ_destroy (h->mq);
114  h->mq = NULL;
115  }
116  GNUNET_assert (NULL == h->reconnect_task);
119  h);
121 }
122 
123 
131 static int
133 {
134  size_t sl =
135  ntohs (msg->header.size) - sizeof (struct GNUNET_ARM_StatusMessage);
136  const char *name = (const char *) &msg[1];
137 
138  (void) cls;
139  if ((0 == sl) || ('\0' != name[sl - 1]))
140  {
141  GNUNET_break (0);
142  return GNUNET_SYSERR;
143  }
144  return GNUNET_OK;
145 }
146 
147 
154 static void
156 {
157  struct GNUNET_ARM_MonitorHandle *h = cls;
159 
160  status = (enum GNUNET_ARM_ServiceStatus) ntohl (res->status);
162  "Received notification from ARM for service `%s' with status %d\n",
163  (const char *) &res[1],
164  (int) status);
165  if (NULL != h->service_status)
166  h->service_status (h->service_status_cls, (const char *) &res[1], status);
167 }
168 
169 
178 static void
179 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
180 {
181  struct GNUNET_ARM_MonitorHandle *h = cls;
182 
183  (void) error;
185 }
186 
187 
194 static int
196 {
197  struct GNUNET_MQ_MessageHandler handlers[] =
198  {GNUNET_MQ_hd_var_size (monitor_notify,
201  h),
203  struct GNUNET_MessageHeader *msg;
204  struct GNUNET_MQ_Envelope *env;
205 
206  GNUNET_assert (NULL == h->mq);
207  h->mq = GNUNET_CLIENT_connect (h->cfg, "arm", handlers, &mq_error_handler, h);
208  if (NULL == h->mq)
209  {
210  if (NULL != h->service_status)
212  NULL,
214  return GNUNET_SYSERR;
215  }
217  GNUNET_MQ_send (h->mq, env);
218  return GNUNET_OK;
219 }
220 
221 
236  void *cont_cls)
237 {
238  struct GNUNET_ARM_MonitorHandle *h;
239 
241  h->cfg = cfg;
242  h->service_status = cont;
243  h->service_status_cls = cont_cls;
244  if (GNUNET_OK != reconnect_arm_monitor (h))
245  {
246  GNUNET_free (h);
247  return NULL;
248  }
249  return h;
250 }
251 
252 
258 void
260 {
261  if (NULL != h->mq)
262  {
263  GNUNET_MQ_destroy (h->mq);
264  h->mq = NULL;
265  }
266  if (NULL != h->reconnect_task)
267  {
269  h->reconnect_task = NULL;
270  }
271  GNUNET_free (h);
272 }
273 
274 
275 /* end of arm_api.c */
void(* GNUNET_ARM_ServiceStatusCallback)(void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
Function called in when a status update arrives.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
#define GNUNET_MESSAGE_TYPE_ARM_STATUS
Status update from ARM.
void * service_status_cls
Closure for service_status.
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_TIME_Relative retry_backoff
Current delay we use for re-trying to connect to core.
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration that we are using.
Status update from ARM to client.
Definition: arm.h:41
GNUNET_MQ_Error
Error codes for the queue.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
static void reconnect_arm_monitor_later(struct GNUNET_ARM_MonitorHandle *h)
Close down any existing connection to the ARM service and try re-establishing it later.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
struct GNUNET_SCHEDULER_Task * reconnect_task
ID of the reconnect task (if any).
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Handle for interacting with ARM.
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 struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
struct GNUNET_MessageHeader header
Reply to client, of type is GNUNET_MESSAGE_TYPE_ARM_STATUS.
Definition: arm.h:47
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1246
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
uint16_t status
See PRISM_STATUS_*-constants.
Service was stopped.
Message handler for a specific message type.
static int res
static int check_monitor_notify(void *cls, const struct GNUNET_ARM_StatusMessage *msg)
Check notification messages received from ARM is well-formed.
#define LOG(kind,...)
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
const char * name
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
GNUNET_ARM_ServiceStatusCallback service_status
Callback to invoke on status updates.
void GNUNET_ARM_monitor_stop(struct GNUNET_ARM_MonitorHandle *h)
Disconnect from the ARM service (if connected) and destroy the context.
Handle to a message queue.
Definition: mq.c:85
uint32_t status
Status from the &#39;enum GNUNET_ARM_ServiceStatus&#39;.
Definition: arm.h:52
GNUNET_ARM_ServiceStatus
Statuses of services.
#define GNUNET_MESSAGE_TYPE_ARM_MONITOR
Request to ARM to notify client of service status changes.
configuration data
Definition: configuration.c:85
Entry in list of pending tasks.
Definition: scheduler.c:134
struct GNUNET_MQ_Handle * mq
Our control connection to the ARM service.
static void reconnect_arm_monitor_task(void *cls)
Task scheduled to try to re-connect to arm.
static void handle_monitor_notify(void *cls, const struct GNUNET_ARM_StatusMessage *res)
Handler for notification messages received from ARM.
Header for all communications.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
struct GNUNET_ARM_MonitorHandle * GNUNET_ARM_monitor_start(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_ARM_ServiceStatusCallback cont, void *cont_cls)
Setup a context for monitoring ARM, then start connecting to the ARM service for monitoring using tha...
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
static int reconnect_arm_monitor(struct GNUNET_ARM_MonitorHandle *h)
Connect to the ARM service for monitoring.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965