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