2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 GNUnet e.V.
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.
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.
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.
22 * @author Christian Grothoff
25 * Functions related to accessing services
27 * @defgroup client Client library
28 * Generic client-side communication with services
30 * @see [Documentation](https://gnunet.org/ipc)
35 #ifndef GNUNET_CLIENT_LIB_H
36 #define GNUNET_CLIENT_LIB_H
41 #if 0 /* keep Emacsens' auto-indent happy */
46 #include "gnunet_mq_lib.h"
49 * Opaque handle for a connection to a service.
51 struct GNUNET_CLIENT_Connection;
55 * Create a message queue to connect to a GNUnet service.
56 * If handlers are specfied, receive messages from the connection.
58 * @param connection the client connection
59 * @param handlers handlers for receiving messages, can be NULL
60 * @param error_handler error handler
61 * @param error_handler_cls closure for the @a error_handler
62 * @return the message queue, NULL on error
64 struct GNUNET_MQ_Handle *
65 GNUNET_CLIENT_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg,
66 const char *service_name,
67 const struct GNUNET_MQ_MessageHandler *handlers,
68 GNUNET_MQ_ErrorHandler error_handler,
69 void *error_handler_cls);
73 * Create a message queue for a GNUNET_CLIENT_Connection.
74 * If handlers are specfied, receive messages from the connection.
76 * @param connection the client connection, taken over and freed by the MQ
77 * @param handlers handlers for receiving messages
78 * @param error_handler error handler
79 * @param error_handler_cls closure for the @a error_handler
80 * @return the message queue
81 * @deprecated use #GNUNET_CLIENT_connecT
83 struct GNUNET_MQ_Handle *
84 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
85 const struct GNUNET_MQ_MessageHandler *handlers,
86 GNUNET_MQ_ErrorHandler error_handler,
87 void *error_handler_cls);
91 * Get a connection with a service.
93 * @param service_name name of the service
94 * @param cfg configuration to use
95 * @return NULL on error (service unknown to configuration)
96 * @deprecated use #GNUNET_CLIENT_connect2
98 struct GNUNET_CLIENT_Connection *
99 GNUNET_CLIENT_connect (const char *service_name,
100 const struct GNUNET_CONFIGURATION_Handle *cfg);
104 * Destroy connection with the service. This will automatically
105 * cancel any pending "receive" request (however, the handler will
106 * *NOT* be called, not even with a NULL message). Any pending
107 * transmission request will also be cancelled UNLESS the callback for
108 * the transmission request has already been called, in which case the
109 * transmission 'finish_pending_write' argument determines whether or
110 * not the write is guaranteed to complete before the socket is fully
111 * destroyed (unless, of course, there is an error with the server in
112 * which case the message may still be lost).
114 * @param client handle to the service connection
118 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client);
122 * Type of a function to call when we receive a message
126 * @param msg message received, NULL on timeout or fatal error
129 (*GNUNET_CLIENT_MessageHandler) (void *cls,
130 const struct GNUNET_MessageHeader *msg);
134 * Read from the service.
136 * @param client connection to the service
137 * @param handler function to call with the message
138 * @param handler_cls closure for @a handler
139 * @param timeout how long to wait until timing out
143 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
144 GNUNET_CLIENT_MessageHandler handler,
146 struct GNUNET_TIME_Relative timeout);
150 * Transmit handle for client connections.
152 struct GNUNET_CLIENT_TransmitHandle;
156 * Ask the client to call us once the specified number of bytes
157 * are free in the transmission buffer. Will never call the @a notify
158 * callback in this task, but always first go into the scheduler.
160 * @param client connection to the service
161 * @param size number of bytes to send
162 * @param timeout after how long should we give up (and call
163 * @a notify with buf NULL and size 0)?
164 * @param auto_retry if the connection to the service dies, should we
165 * automatically re-connect and retry (within the timeout period)
166 * or should we immediately fail in this case? Pass #GNUNET_YES
167 * if the caller does not care about temporary connection errors,
168 * for example because the protocol is stateless
169 * @param notify function to call
170 * @param notify_cls closure for @a notify
171 * @return NULL if someone else is already waiting to be notified
172 * non-NULL if the notify callback was queued (can be used to cancel
173 * using #GNUNET_CONNECTION_notify_transmit_ready_cancel)
176 struct GNUNET_CLIENT_TransmitHandle *
177 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
179 struct GNUNET_TIME_Relative timeout,
181 GNUNET_CONNECTION_TransmitReadyNotify notify,
186 * Cancel a request for notification.
188 * @param th handle from the original request.
192 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle *th);
196 * Convenience API that combines sending a request
197 * to the service and waiting for a response.
198 * If either operation times out, the callback
199 * will be called with a "NULL" response (in which
200 * case the connection should probably be destroyed).
202 * @param client connection to use
203 * @param hdr message to transmit
204 * @param timeout when to give up (for both transmission
205 * and for waiting for a response)
206 * @param auto_retry if the connection to the service dies, should we
207 * automatically re-connect and retry (within the timeout period)
208 * or should we immediately fail in this case? Pass #GNUNET_YES
209 * if the caller does not care about temporary connection errors,
210 * for example because the protocol is stateless
211 * @param rn function to call with the response
212 * @param rn_cls closure for @a rn
213 * @return #GNUNET_OK on success, #GNUNET_SYSERR if a request
218 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
219 const struct GNUNET_MessageHeader *hdr,
220 struct GNUNET_TIME_Relative timeout,
222 GNUNET_CLIENT_MessageHandler rn,
227 * Handle for a test to check if a service is running.
229 struct GNUNET_CLIENT_TestHandle;
232 * Function called with the result on the service test.
235 * @param result #GNUNET_YES if the service is running,
236 * #GNUNET_NO if the service is not running
237 * #GNUNET_SYSERR if the configuration is invalid
240 (*GNUNET_CLIENT_TestResultCallback)(void *cls,
245 * Test if the service is running. If we are given a UNIXPATH or a
246 * local address, we do this NOT by trying to connect to the service,
247 * but by trying to BIND to the same port. If the BIND fails, we know
248 * the service is running.
250 * @param service name of the service to wait for
251 * @param cfg configuration to use
252 * @param timeout how long to wait at most
253 * @param cb function to call with the result
254 * @param cb_cls closure for @a cb
255 * @return handle to cancel the test
258 struct GNUNET_CLIENT_TestHandle *
259 GNUNET_CLIENT_service_test (const char *service,
260 const struct GNUNET_CONFIGURATION_Handle *cfg,
261 struct GNUNET_TIME_Relative timeout,
262 GNUNET_CLIENT_TestResultCallback cb, void *cb_cls);
266 * Abort testing for service.
268 * @param th test handle
272 GNUNET_CLIENT_service_test_cancel (struct GNUNET_CLIENT_TestHandle *th);
275 #if 0 /* keep Emacsens' auto-indent happy */
282 /* ifndef GNUNET_CLIENT_LIB_H */
285 /** @} */ /* end of group client */
287 /* end of gnunet_client_lib.h */