header
[oweals/gnunet.git] / src / include / gnunet_arm_service.h
1 /*
2       This file is part of GNUnet
3       (C) 2009 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       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       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file include/gnunet_arm_service.h
23  * @brief API to access gnunet-arm
24  * @author Christian Grothoff
25  */
26
27 #ifndef GNUNET_ARM_SERVICE_H
28 #define GNUNET_ARM_SERVICE_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 #include "gnunet_util_lib.h"
39
40 /**
41  * Version of the arm API.
42  */
43 #define GNUNET_ARM_VERSION 0x00000002
44
45
46 /**
47  * Statuses of the requests that client can send to ARM.
48  */
49 enum GNUNET_ARM_RequestStatus
50 {
51   /**
52    * Message was sent successfully.
53    */
54   GNUNET_ARM_REQUEST_SENT_OK = 0,
55
56   /**
57    * Misconfiguration (can't connect to the ARM service).
58    */
59   GNUNET_ARM_REQUEST_CONFIGURATION_ERROR = 1,
60
61   /**
62    * We disconnected from ARM, and request was not sent.
63    */
64   GNUNET_ARM_REQUEST_DISCONNECTED = 2,
65
66   /**
67    * ARM API is busy (probably trying to connect to ARM),
68    * and request was not sent. Try again later.
69    */
70   GNUNET_ARM_REQUEST_BUSY = 3,
71
72   /**
73    * It was discovered that the request would be too long to fit in a message,
74    * and thus it was not sent.
75    */
76   GNUNET_ARM_REQUEST_TOO_LONG = 4,
77
78   /**
79    * Request time ran out before we had a chance to send it.
80    */
81   GNUNET_ARM_REQUEST_TIMEOUT = 5
82
83 };
84
85
86 /**
87  * Statuses of services.
88  */
89 enum GNUNET_ARM_ServiceStatus
90 {
91   /**
92    * Dummy message.
93    */
94   GNUNET_ARM_SERVICE_MONITORING_STARTED = 0,
95
96   /**
97    * Service was stopped.
98    */
99   GNUNET_ARM_SERVICE_STOPPED = 1,
100
101   /**
102    * Service starting was initiated
103    */
104   GNUNET_ARM_SERVICE_STARTING = 2,
105
106   /**
107    * Service stopping was initiated
108    */
109   GNUNET_ARM_SERVICE_STOPPING = 3
110 };
111
112
113 /**
114  * Replies to ARM requests
115  */
116 enum GNUNET_ARM_Result
117 {
118   /**
119    * Service was stopped (never sent for ARM itself).
120    */
121   GNUNET_ARM_RESULT_STOPPED = 0,
122
123   /**
124    * ARM stopping was initiated (there's no "stopped" for ARM itself).
125    */
126   GNUNET_ARM_RESULT_STOPPING = 1,
127
128   /**
129    * Service starting was initiated
130    */
131   GNUNET_ARM_RESULT_STARTING = 2,
132
133   /**
134    * Asked to start it, but it's already starting.
135    */
136   GNUNET_ARM_RESULT_IS_STARTING_ALREADY = 3,
137
138   /**
139    * Asked to stop it, but it's already stopping.
140    */
141   GNUNET_ARM_RESULT_IS_STOPPING_ALREADY = 4,
142
143   /**
144    * Asked to start it, but it's already started.
145    */
146   GNUNET_ARM_RESULT_IS_STARTED_ALREADY = 5,
147
148   /**
149    * Asked to stop it, but it's already stopped.
150    */
151   GNUNET_ARM_RESULT_IS_STOPPED_ALREADY = 6,
152
153   /**
154    * Asked to start or stop a service, but it's not known.
155    */
156   GNUNET_ARM_RESULT_IS_NOT_KNOWN = 7,
157
158   /**
159    * Tried to start a service, but that failed for some reason.
160    */
161   GNUNET_ARM_RESULT_START_FAILED = 8,
162
163   /**
164    * Asked to start something, but ARM is shutting down and can't comply.
165    */
166   GNUNET_ARM_RESULT_IN_SHUTDOWN = 9
167 };
168
169
170 /**
171  * Handle for interacting with ARM.
172  */
173 struct GNUNET_ARM_Handle;
174
175
176 /**
177  * Function called whenever we connect to or disconnect from ARM.
178  *
179  * @param cls closure
180  * @param connected GNUNET_YES if connected, GNUNET_NO if disconnected,
181  *                  GNUNET_SYSERR if there was an error.
182  */
183 typedef void (*GNUNET_ARM_ConnectionStatusCallback) (void *cls,
184                                                      int connected);
185
186
187 /**
188  * Function called in response to a start/stop request.
189  * Will be called when request was not sent successfully,
190  * or when a reply comes. If the request was not sent successfully,
191  * 'rs' will indicate that, and 'service' and 'result' will be undefined.
192  *
193  * @param cls closure
194  * @param rs status of the request
195  * @param service service name
196  * @param result result of the operation
197  */
198 typedef void (*GNUNET_ARM_ResultCallback) (void *cls,
199                                            enum GNUNET_ARM_RequestStatus rs,
200                                            const char *service,
201                                            enum GNUNET_ARM_Result result);
202
203
204 /**
205  * Callback function invoked when list operation is complete.
206  * Will be called when request was not sent successfully,
207  * or when a reply comes. If the request was not sent successfully,
208  * 'rs' will indicate that, and 'count' and 'list' will be undefined.
209  *
210  * @param cls closure
211  * @param rs status of the request
212  * @param count number of strings in the list
213  * @param list list of running services
214  */
215 typedef void (*GNUNET_ARM_ServiceListCallback) (void *cls,
216                                                 enum GNUNET_ARM_RequestStatus rs,
217                                                 unsigned int count,
218                                                 const char *const*list);
219
220
221 /**
222  * Set up a context for communicating with ARM, then
223  * start connecting to the ARM service using that context.
224  *
225  * @param cfg configuration to use (needed to contact ARM;
226  *        the ARM service may internally use a different
227  *        configuration to determine how to start the service).
228  * @param conn_status will be called when connecting/disconnecting
229  * @param cls closure for conn_status
230  * @return context to use for further ARM operations, NULL on error.
231  */
232 struct GNUNET_ARM_Handle *
233 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
234                     GNUNET_ARM_ConnectionStatusCallback conn_status,
235                     void *cls);
236
237
238 /**
239  * Disconnect from the ARM service and destroy the handle.
240  *
241  * @param h the handle that was being used
242  */
243 void
244 GNUNET_ARM_disconnect_and_free (struct GNUNET_ARM_Handle *h);
245
246
247 /**
248  * Request a list of running services.
249  *
250  * @param h handle to ARM
251  * @param timeout how long to wait before failing for good
252  * @param cont callback to invoke after request is sent or is not sent
253  * @param cont_cls closure for callback
254  */
255 void
256 GNUNET_ARM_request_service_list (struct GNUNET_ARM_Handle *h,
257                                  struct GNUNET_TIME_Relative timeout,
258                                  GNUNET_ARM_ServiceListCallback cont, void *cont_cls);
259
260
261 /**
262  * Request a service to be stopped.
263  * Stopping arm itself will not invalidate its handle, and
264  * ARM API will try to restore connection to the ARM service,
265  * even if ARM connection was lost because you asked for ARM to be stopped.
266  * Call GNUNET_ARM_disconnect_and_free () to free the handle and prevent
267  * further connection attempts.
268  *
269  * @param h handle to ARM
270  * @param service_name name of the service
271  * @param timeout how long to wait before failing for good
272  * @param cont callback to invoke after request is sent or is not sent
273  * @param cont_cls closure for callback
274  */
275 void
276 GNUNET_ARM_request_service_stop (struct GNUNET_ARM_Handle *h,
277                                  const char *service_name,
278                                  struct GNUNET_TIME_Relative timeout,
279                                  GNUNET_ARM_ResultCallback cont, void *cont_cls);
280
281
282 /**
283  * Request for a service to be started.
284  *
285  * @param h handle to ARM
286  * @param service_name name of the service
287  * @param std_inheritance inheritance of std streams
288  * @param timeout how long to wait before failing for good
289  * @param cont callback to invoke after request is sent or not sent
290  * @param cont_cls closure for callback
291  */
292 void
293 GNUNET_ARM_request_service_start (struct GNUNET_ARM_Handle *h,
294                                   const char *service_name,
295                                   enum GNUNET_OS_InheritStdioFlags std_inheritance,
296                                   struct GNUNET_TIME_Relative timeout,
297                                   GNUNET_ARM_ResultCallback cont,
298                                   void *cont_cls);
299
300
301 /**
302  * Handle for monitoring ARM.
303  */
304 struct GNUNET_ARM_MonitorHandle;
305
306
307 /**
308  * Function called in when a status update arrives.
309  *
310  * @param cls closure
311  * @param arm handle to the arm connection
312  * @param service service name
313  * @param status status of the service
314  */
315 typedef void (*GNUNET_ARM_ServiceStatusCallback) (void *cls,
316                                                   const char *service,
317                                                   enum GNUNET_ARM_ServiceStatus status);
318
319
320 /**
321  * Setup a context for monitoring ARM, then
322  * start connecting to the ARM service for monitoring using that context.
323  *
324  * @param cfg configuration to use (needed to contact ARM;
325  *        the ARM service may internally use a different
326  *        configuration to determine how to start the service).
327  * @param cont callback to invoke on status updates
328  * @param cont_cls closure
329  * @return context to use for further ARM monitor operations, NULL on error.
330  */
331 struct GNUNET_ARM_MonitorHandle *
332 GNUNET_ARM_monitor (const struct GNUNET_CONFIGURATION_Handle *cfg,
333                     GNUNET_ARM_ServiceStatusCallback cont,
334                     void *cont_cls);
335
336
337 /**
338  * Disconnect from the ARM service and destroy the handle.
339  *
340  * @param h the handle that was being used
341  */
342 void
343 GNUNET_ARM_monitor_disconnect_and_free (struct GNUNET_ARM_MonitorHandle *h);
344
345
346 #if 0                           /* keep Emacsens' auto-indent happy */
347 {
348 #endif
349 #ifdef __cplusplus
350 }
351 #endif
352
353 #endif