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