aboutsummaryrefslogtreecommitdiff
path: root/src/arm/arm_api.c
blob: c5bb15fd3c01d4f5eea6f640885a2360a4a35306 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
     This file is part of GNUnet.
     (C) 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file arm/arm_api.c
 * @brief API for accessing the ARM service
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_arm_service.h"
#include "gnunet_client_lib.h"
#include "gnunet_getopt_lib.h"
#include "gnunet_os_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_server_lib.h"
#include "arm.h"


struct ArmContext
{
  GNUNET_ARM_Callback callback;
  void *cls;
  char *service_name;
  struct GNUNET_CLIENT_Connection *client;
  struct GNUNET_CONFIGURATION_Handle *cfg;
  struct GNUNET_TIME_Absolute timeout;
  uint16_t type;
};


static void
arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct ArmContext *pos = cls;
  pid_t pid;
  char *binary;
  char *config;

  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
    {
      if (pos->callback != NULL)
        pos->callback (pos->cls, GNUNET_YES);
      GNUNET_free (pos);
      return;
    }
  binary = NULL;
  config = NULL;
  /* start service */
  if ((GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_filename (pos->cfg,
                                                "arm",
                                                "BINARY",
                                                &binary)) ||
      (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_filename (pos->cfg,
                                                "arm", "CONFIG", &config)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Configuration file or binary for ARM not known!\n"));
      if (pos->callback != NULL)
        pos->callback (pos->cls, GNUNET_SYSERR);
      GNUNET_free_non_null (binary);
      GNUNET_free (pos);
      return;
    }
  pid = GNUNET_OS_start_process (binary, binary, "-d", "-c", config,
#if DEBUG_ARM
                                 "-L", "DEBUG",
#endif
                                 NULL);
  GNUNET_free (binary);
  GNUNET_free (config);
  if (pid == -1)
    {
      if (pos->callback != NULL)
        pos->callback (pos->cls, GNUNET_SYSERR);
      GNUNET_free (pos);
      return;
    }
  /* FIXME: consider checking again to see if it worked!? */
  if (pos->callback != NULL)
    pos->callback (pos->cls, GNUNET_YES);
  GNUNET_free (pos);
}


static void
handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
{
  struct ArmContext *sc = cls;
  int ret;

  if (msg == NULL)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Error receiving response from ARM service\n"));
      GNUNET_CLIENT_disconnect (sc->client);
      if (sc->callback != NULL)
        sc->callback (sc->cls, GNUNET_SYSERR);
      GNUNET_free (sc);
      return;
    }
#if DEBUG_ARM
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              _("Received response from ARM service\n"));
#endif
  switch (ntohs (msg->type))
    {
    case GNUNET_MESSAGE_TYPE_ARM_IS_UP:
      ret = GNUNET_YES;
      break;
    case GNUNET_MESSAGE_TYPE_ARM_IS_DOWN:
      ret = GNUNET_NO;
      break;
    default:
      GNUNET_break (0);
      ret = GNUNET_SYSERR;
    }
  GNUNET_CLIENT_disconnect (sc->client);
  if (sc->callback != NULL)
    sc->callback (sc->cls, ret);
  GNUNET_free (sc);
}


static size_t
send_service_msg (void *cls, size_t size, void *buf)
{
  struct ArmContext *sctx = cls;
  struct GNUNET_MessageHeader *msg;
  size_t slen;

  if (buf == NULL)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Error while trying to transmit to ARM service\n"));
      GNUNET_CLIENT_disconnect (sctx->client);
      if (sctx->callback != NULL)
        sctx->callback (sctx->cls, GNUNET_SYSERR);
      GNUNET_free (sctx->service_name);
      GNUNET_free (sctx);
      return 0;
    }
#if DEBUG_ARM
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              _("Transmitting service request to ARM.\n"));
#endif
  slen = strlen (sctx->service_name) + 1;
  GNUNET_assert (size >= slen);
  msg = buf;
  msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
  msg->type = htons (sctx->type);
  memcpy (&msg[1], sctx->service_name, slen);
  GNUNET_free (sctx->service_name);
  sctx->service_name = NULL;
  GNUNET_CLIENT_receive (sctx->client,
                         &handle_response,
                         sctx,
                         GNUNET_TIME_absolute_get_remaining (sctx->timeout));
  return slen + sizeof (struct GNUNET_MessageHeader);
}


/**
 * Start or stop a service.
 *
 * @param service_name name of the service
 * @param cfg configuration to use (needed to contact ARM;
 *        the ARM service may internally use a different
 *        configuration to determine how to start the service).
 * @param sched scheduler to use
 * @param timeout how long to wait before failing for good
 * @param cb callback to invoke when service is ready
 * @param cb_cls closure for callback
 */
static void
change_service (const char *service_name,
                struct GNUNET_CONFIGURATION_Handle *cfg,
                struct GNUNET_SCHEDULER_Handle *sched,
                struct GNUNET_TIME_Relative timeout,
                GNUNET_ARM_Callback cb, void *cb_cls, uint16_t type)
{
  struct GNUNET_CLIENT_Connection *client;
  struct ArmContext *sctx;
  size_t slen;

  slen = strlen (service_name) + 1;
  if (slen + sizeof (struct GNUNET_MessageHeader) >
      GNUNET_SERVER_MAX_MESSAGE_SIZE)
    {
      GNUNET_break (0);
      if (cb != NULL)
        cb (cb_cls, GNUNET_NO);
      return;
    }
  client = GNUNET_CLIENT_connect (sched, "arm", cfg);
  if (client == NULL)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Failed to connect to ARM service\n"));
      if (cb != NULL)
        cb (cb_cls, GNUNET_SYSERR);
      return;
    }
#if DEBUG_ARM
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              _("ARM requests starting of service `%s'.\n"), service_name);
#endif
  sctx = GNUNET_malloc (sizeof (struct ArmContext));
  sctx->callback = cb;
  sctx->cls = cb_cls;
  sctx->client = client;
  sctx->service_name = GNUNET_strdup (service_name);
  sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  sctx->type = type;
  if (NULL ==
      GNUNET_CLIENT_notify_transmit_ready (client,
                                           slen +
                                           sizeof (struct
                                                   GNUNET_MessageHeader),
                                           timeout, &send_service_msg, sctx))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Failed to transmit request to ARM service\n"));
      GNUNET_free (sctx->service_name);
      GNUNET_free (sctx);
      if (cb != NULL)
        cb (cb_cls, GNUNET_SYSERR);
      GNUNET_CLIENT_disconnect (client);
      return;
    }
}


/**
 * Start a service.
 *
 * @param service_name name of the service
 * @param cfg configuration to use (needed to contact ARM;
 *        the ARM service may internally use a different
 *        configuration to determine how to start the service).
 * @param sched scheduler to use
 * @param timeout how long to wait before failing for good
 * @param cb callback to invoke when service is ready
 * @param cb_cls closure for callback
 */
void
GNUNET_ARM_start_service (const char *service_name,
                          struct GNUNET_CONFIGURATION_Handle *cfg,
                          struct GNUNET_SCHEDULER_Handle *sched,
                          struct GNUNET_TIME_Relative timeout,
                          GNUNET_ARM_Callback cb, void *cb_cls)
{
  struct ArmContext *sctx;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              _("Starting service `%s'\n"), service_name);
  if (0 == strcmp ("arm", service_name))
    {
      sctx = GNUNET_malloc (sizeof (struct ArmContext));
      sctx->callback = cb;
      sctx->cls = cb_cls;
      sctx->cfg = cfg;
      GNUNET_CLIENT_service_test (sched,
                                  "arm",
                                  cfg, timeout, &arm_service_report, sctx);
      return;
    }
  change_service (service_name,
                  cfg,
                  sched, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_START);
}




/**
 * Stop a service.
 *
 * @param service_name name of the service
 * @param cfg configuration to use (needed to contact ARM;
 *        the ARM service may internally use a different
 *        configuration to determine how to start the service).
 * @param sched scheduler to use
 * @param timeout how long to wait before failing for good
 * @param cb callback to invoke when service is ready
 * @param cb_cls closure for callback
 */
void
GNUNET_ARM_stop_service (const char *service_name,
                         struct GNUNET_CONFIGURATION_Handle *cfg,
                         struct GNUNET_SCHEDULER_Handle *sched,
                         struct GNUNET_TIME_Relative timeout,
                         GNUNET_ARM_Callback cb, void *cb_cls)
{
  struct GNUNET_CLIENT_Connection *client;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              _("Stopping service `%s'\n"), service_name);
  if (0 == strcmp ("arm", service_name))
    {
      client = GNUNET_CLIENT_connect (sched, "arm", cfg);
      if (client == NULL)
        {
          if (cb != NULL)
            cb (cb_cls, GNUNET_SYSERR);
          return;
        }
      GNUNET_CLIENT_service_shutdown (client);
      GNUNET_CLIENT_disconnect (client);
      if (cb != NULL)
        cb (cb_cls, GNUNET_NO);
      return;
    }
  change_service (service_name,
                  cfg,
                  sched, timeout, cb, cb_cls, GNUNET_MESSAGE_TYPE_ARM_STOP);
}

/* end of arm_api.c */