configuration serialization
[oweals/gnunet.git] / src / include / gnunet_client_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 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 2, 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_client_lib.h
23  * @brief functions related to accessing services
24  * @author Christian Grothoff
25  */
26
27 #ifndef GNUNET_CLIENT_LIB_H
28 #define GNUNET_CLIENT_LIB_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_common.h"
39 #include "gnunet_configuration_lib.h"
40 #include "gnunet_connection_lib.h"
41 #include "gnunet_scheduler_lib.h"
42 #include "gnunet_time_lib.h"
43
44 /**
45  * Opaque handle for a connection to a service.
46  */
47 struct GNUNET_CLIENT_Connection;
48
49 /**
50  * Get a connection with a service.
51  *
52  * @param service_name name of the service
53  * @param cfg configuration to use
54  * @return NULL on error (service unknown to configuration)
55  */
56 struct GNUNET_CLIENT_Connection *
57 GNUNET_CLIENT_connect (const char *service_name,
58                        const struct GNUNET_CONFIGURATION_Handle *cfg);
59
60
61 /**
62  * Destroy connection with the service.  This will automatically
63  * cancel any pending "receive" request (however, the handler will
64  * *NOT* be called, not even with a NULL message).  Any pending
65  * transmission request will also be cancelled UNLESS the callback for
66  * the transmission request has already been called, in which case the
67  * transmission 'finish_pending_write' argument determines whether or
68  * not the write is guaranteed to complete before the socket is fully
69  * destroyed (unless, of course, there is an error with the server in
70  * which case the message may still be lost).
71  *
72  * @param client handle to the service connection
73  */
74 void
75 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client);
76
77
78 /**
79  * Type of a function to call when we receive a message
80  * from the service.
81  *
82  * @param cls closure
83  * @param msg message received, NULL on timeout or fatal error
84  */
85 typedef void (*GNUNET_CLIENT_MessageHandler) (void *cls,
86                                               const struct GNUNET_MessageHeader
87                                               * msg);
88
89
90 /**
91  * Type of a function to call when we have finished shutting
92  * down a service, or failed.
93  *
94  * @param cls closure
95  * @param reason what is the result of the shutdown
96  *        GNUNET_NO on shutdown (not running)
97  *        GNUNET_YES on running
98  *        GNUNET_SYSERR on failure to transmit message
99  */
100 typedef void (*GNUNET_CLIENT_ShutdownTask) (void *cls, int reason);
101
102
103 /**
104  * Read from the service.
105  *
106  * @param client connection to the service
107  * @param handler function to call with the message
108  * @param handler_cls closure for handler
109  * @param timeout how long to wait until timing out
110  */
111 void
112 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
113                        GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
114                        struct GNUNET_TIME_Relative timeout);
115
116
117 /**
118  * Transmit handle for client connections.
119  */
120 struct GNUNET_CLIENT_TransmitHandle;
121
122
123 /**
124  * Ask the client to call us once the specified number of bytes
125  * are free in the transmission buffer.  May call the notify
126  * method immediately if enough space is available.
127  *
128  * @param client connection to the service
129  * @param size number of bytes to send
130  * @param timeout after how long should we give up (and call
131  *        notify with buf NULL and size 0)?
132  * @param auto_retry if the connection to the service dies, should we
133  *        automatically re-connect and retry (within the timeout period)
134  *        or should we immediately fail in this case?  Pass GNUNET_YES
135  *        if the caller does not care about temporary connection errors,
136  *        for example because the protocol is stateless
137  * @param notify function to call
138  * @param notify_cls closure for notify
139  * @return NULL if someone else is already waiting to be notified
140  *         non-NULL if the notify callback was queued (can be used to cancel
141  *         using GNUNET_CONNECTION_notify_transmit_ready_cancel)
142  */
143 struct GNUNET_CLIENT_TransmitHandle *
144 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
145                                      size_t size,
146                                      struct GNUNET_TIME_Relative timeout,
147                                      int auto_retry,
148                                      GNUNET_CONNECTION_TransmitReadyNotify
149                                      notify, void *notify_cls);
150
151
152 /**
153  * Cancel a request for notification.
154  *
155  * @param th handle from the original request.
156  */
157 void
158 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
159                                             *th);
160
161
162 /**
163  * Convenience API that combines sending a request
164  * to the service and waiting for a response.
165  * If either operation times out, the callback
166  * will be called with a "NULL" response (in which
167  * case the connection should probably be destroyed).
168  *
169  * @param client connection to use
170  * @param hdr message to transmit
171  * @param timeout when to give up (for both transmission
172  *         and for waiting for a response)
173  * @param auto_retry if the connection to the service dies, should we
174  *        automatically re-connect and retry (within the timeout period)
175  *        or should we immediately fail in this case?  Pass GNUNET_YES
176  *        if the caller does not care about temporary connection errors,
177  *        for example because the protocol is stateless
178  * @param rn function to call with the response
179  * @param rn_cls closure for rn
180  * @return GNUNET_OK on success, GNUNET_SYSERR if a request
181  *         is already pending
182  */
183 int
184 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
185                                          const struct GNUNET_MessageHeader *hdr,
186                                          struct GNUNET_TIME_Relative timeout,
187                                          int auto_retry,
188                                          GNUNET_CLIENT_MessageHandler rn,
189                                          void *rn_cls);
190
191
192 /**
193  * Wait until the service is running.
194  *
195  * @param service name of the service to wait for
196  * @param cfg configuration to use
197  * @param timeout how long to wait at most in ms
198  * @param task task to run if service is running
199  *        (reason will be "PREREQ_DONE" (service running)
200  *         or "TIMEOUT" (service not known to be running))
201  * @param task_cls closure for task
202  */
203 void
204 GNUNET_CLIENT_service_test (const char *service,
205                             const struct GNUNET_CONFIGURATION_Handle *cfg,
206                             struct GNUNET_TIME_Relative timeout,
207                             GNUNET_SCHEDULER_Task task, void *task_cls);
208
209
210 #if 0                           /* keep Emacsens' auto-indent happy */
211 {
212 #endif
213 #ifdef __cplusplus
214 }
215 #endif
216
217 /* ifndef GNUNET_CLIENT_LIB_H */
218 #endif
219 /* end of gnunet_client_lib.h */