doxygen: group/module definitions (part 1)
[oweals/gnunet.git] / src / include / gnunet_client_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2013 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * Functions related to accessing services
26
27  * @defgroup client  Client library
28  * Generic client-side communication with services
29  * @{
30  */
31
32 #ifndef GNUNET_CLIENT_LIB_H
33 #define GNUNET_CLIENT_LIB_H
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #if 0                           /* keep Emacsens' auto-indent happy */
39 }
40 #endif
41 #endif
42
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 *msg);
87
88
89 /**
90  * Read from the service.
91  *
92  * @param client connection to the service
93  * @param handler function to call with the message
94  * @param handler_cls closure for @a handler
95  * @param timeout how long to wait until timing out
96  */
97 void
98 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
99                        GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
100                        struct GNUNET_TIME_Relative timeout);
101
102
103 /**
104  * Transmit handle for client connections.
105  */
106 struct GNUNET_CLIENT_TransmitHandle;
107
108
109 /**
110  * Ask the client to call us once the specified number of bytes
111  * are free in the transmission buffer.  Will never call the @a notify
112  * callback in this task, but always first go into the scheduler.
113  *
114  * @param client connection to the service
115  * @param size number of bytes to send
116  * @param timeout after how long should we give up (and call
117  *        @a notify with buf NULL and size 0)?
118  * @param auto_retry if the connection to the service dies, should we
119  *        automatically re-connect and retry (within the timeout period)
120  *        or should we immediately fail in this case?  Pass #GNUNET_YES
121  *        if the caller does not care about temporary connection errors,
122  *        for example because the protocol is stateless
123  * @param notify function to call
124  * @param notify_cls closure for @a notify
125  * @return NULL if someone else is already waiting to be notified
126  *         non-NULL if the notify callback was queued (can be used to cancel
127  *         using #GNUNET_CONNECTION_notify_transmit_ready_cancel)
128  */
129 struct GNUNET_CLIENT_TransmitHandle *
130 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
131                                      size_t size,
132                                      struct GNUNET_TIME_Relative timeout,
133                                      int auto_retry,
134                                      GNUNET_CONNECTION_TransmitReadyNotify notify,
135                                      void *notify_cls);
136
137
138 /**
139  * Cancel a request for notification.
140  *
141  * @param th handle from the original request.
142  */
143 void
144 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
145                                             *th);
146
147
148 /**
149  * Convenience API that combines sending a request
150  * to the service and waiting for a response.
151  * If either operation times out, the callback
152  * will be called with a "NULL" response (in which
153  * case the connection should probably be destroyed).
154  *
155  * @param client connection to use
156  * @param hdr message to transmit
157  * @param timeout when to give up (for both transmission
158  *         and for waiting for a response)
159  * @param auto_retry if the connection to the service dies, should we
160  *        automatically re-connect and retry (within the timeout period)
161  *        or should we immediately fail in this case?  Pass #GNUNET_YES
162  *        if the caller does not care about temporary connection errors,
163  *        for example because the protocol is stateless
164  * @param rn function to call with the response
165  * @param rn_cls closure for @a rn
166  * @return #GNUNET_OK on success, #GNUNET_SYSERR if a request
167  *         is already pending
168  */
169 int
170 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
171                                          const struct GNUNET_MessageHeader *hdr,
172                                          struct GNUNET_TIME_Relative timeout,
173                                          int auto_retry,
174                                          GNUNET_CLIENT_MessageHandler rn,
175                                          void *rn_cls);
176
177
178 /**
179  * Handle for a test to check if a service is running.
180  */
181 struct GNUNET_CLIENT_TestHandle;
182
183 /**
184  * Function called with the result on the service test.
185  *
186  * @param cls closure
187  * @param result #GNUNET_YES if the service is running,
188  *               #GNUNET_NO if the service is not running
189  *               #GNUNET_SYSERR if the configuration is invalid
190  */
191 typedef void (*GNUNET_CLIENT_TestResultCallback)(void *cls,
192                                                  int result);
193
194
195 /**
196  * Test if the service is running.  If we are given a UNIXPATH or a
197  * local address, we do this NOT by trying to connect to the service,
198  * but by trying to BIND to the same port.  If the BIND fails, we know
199  * the service is running.
200  *
201  * @param service name of the service to wait for
202  * @param cfg configuration to use
203  * @param timeout how long to wait at most
204  * @param cb function to call with the result
205  * @param cb_cls closure for @a cb
206  * @return handle to cancel the test
207  */
208 struct GNUNET_CLIENT_TestHandle *
209 GNUNET_CLIENT_service_test (const char *service,
210                             const struct GNUNET_CONFIGURATION_Handle *cfg,
211                             struct GNUNET_TIME_Relative timeout,
212                             GNUNET_CLIENT_TestResultCallback cb, void *cb_cls);
213
214
215 /**
216  * Abort testing for service.
217  *
218  * @param th test handle
219  */
220 void
221 GNUNET_CLIENT_service_test_cancel (struct GNUNET_CLIENT_TestHandle *th);
222
223
224 #if 0                           /* keep Emacsens' auto-indent happy */
225 {
226 #endif
227 #ifdef __cplusplus
228 }
229 #endif
230
231 /** @} */ /* end of group client */
232
233 /* ifndef GNUNET_CLIENT_LIB_H */
234 #endif
235 /* end of gnunet_client_lib.h */