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