fixed doxygen
[oweals/gnunet.git] / src / rps / rps_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 
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 rps/rps_api.c
23  * @brief API for rps
24  * @author Julius Bünger
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "rps.h"
29 #include "gnunet_rps_service.h"
30
31 /**
32  * Handler to handle requests from a client.
33  */
34 struct GNUNET_RPS_Handle
35 {
36   /**
37    * The handle to the client configuration.
38    */
39   const struct GNUNET_CONFIGURATION_Handle *cfg;
40
41   /**
42    * The connection to the client.
43    */
44   struct GNUNET_CLIENT_Connection *conn;
45
46   /**
47    * The message queue to the client.
48    */
49   struct GNUNET_MQ_Handle *mq;
50 };
51
52 /**
53  * Handler to single requests from the client.
54  */
55 struct GNUNET_RPS_Request_Handle
56 {
57   /**
58    * The client issuing the request.
59    */
60   struct GNUNET_RPS_Handle *h;
61
62   /**
63    * The nuber of the request.
64    */
65   uint64_t n;
66
67   /**
68    * The callback to be called when we receive an answer.
69    */
70   GNUNET_RPS_NotifyReadyCB ready_cb;
71
72   /**
73    * The closure for the callback.
74    */
75   void *ready_cb_cls;
76 };
77
78 /**
79  * Array of Request_Handles.
80  */
81 struct GNUNET_RPS_Request_Handle *req_handlers = NULL;
82
83 /**
84  * Current length of req_handlers.
85  */
86 unsigned int req_handlers_size = 0;
87
88 /**
89  * Struct used to pack the callback, its closure (provided by the caller)
90  * and the connection handler to the service to pass it to a callback function.
91  */
92 struct cb_cls_pack
93 {
94   /**
95    * Callback provided by the client
96    */
97   GNUNET_RPS_NotifyReadyCB cb;
98
99   /**
100    * Closure provided by the client
101    */
102   void *cls;
103
104   /**
105    * Handle to the service connection
106    */
107  struct GNUNET_CLIENT_Connection *service_conn;
108 };
109
110
111
112
113 /**
114  * This function is called, when the service replies to our request.
115  * It calls the callback the caller gave us with the provided closure
116  * and disconnects afterwards.
117  *
118  * @param cls the closure
119  * @param message the message
120  */
121   static void
122 handle_reply (void *cls,
123               const struct GNUNET_MessageHeader *message)
124 {
125   struct GNUNET_RPS_CS_ReplyMessage *msg;
126   //struct cb_cls_pack *pack;
127   //struct GNUNET_RPS_Handle *h;
128   struct GNUNET_PeerIdentity *peers;
129   struct GNUNET_RPS_Request_Handle *rh;
130
131   /* Give the peers back */
132   msg = (struct GNUNET_RPS_CS_ReplyMessage *) message;
133   //pack = (struct cb_cls_pack *) cls;
134   //h = (struct GNUNET_RPS_Handle *) cls;
135   peers = (struct GNUNET_PeerIdentity *) &msg[1];
136   rh = &req_handlers[msg->n];
137   rh->ready_cb((rh)->ready_cb_cls, msg->num_peers, peers);
138
139   /* Disconnect */
140   //GNUNET_CLIENT_disconnect(pack->service_conn);
141 }
142
143 /**
144  */
145   static void
146 mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
147 {
148   //TODO LOG
149 }
150
151 /**
152  * Request n random peers.
153  *
154  * @param cfg the configuration to use.
155  * @param n number of peers requesting.
156  * @param cb a callback function called when the peers are ready
157  * @param cls a closure given to the callback function
158  */
159   struct GNUNET_RPS_Request_Handle *
160 GNUNET_RPS_request_peers_single_call (const struct GNUNET_CONFIGURATION_Handle *cfg,
161                           uint64_t n,
162                           GNUNET_RPS_NotifyReadyCB ready_cb,
163                           void *cls)
164 {
165   //struct GNUNET_CLIENT_Connection *service_conn;
166   //static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
167   //  {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
168   //  GNUNET_MQ_HANDLERS_END
169   //};
170   //struct cb_cls_pack *pack;
171   //struct GNUNET_MQ_Handle *mq;
172   //struct GNUNET_MQ_Envelope *ev;
173   //struct GNUNET_RPS_CS_RequestMessage *msg;
174   struct GNUNET_RPS_Handle *h;
175   struct GNUNET_RPS_Request_Handle *rh;
176
177   /* Connect to the service */
178   h = GNUNET_RPS_connect(cfg);
179   //h = GNUNET_new(struct GNUNET_RPS_Handle);
180   //h->conn = GNUNET_CLIENT_connect("rps", cfg);
181   //rh = GNUNET_new(struct GNUNET_RPS_Request_Handle);
182   ////pack = GNUNET_malloc(sizeof(struct cb_cls_pack));
183   ////pack->cb = ready_cb;
184   ////pack->cls = cls;
185   ////pack->service_conn = service_conn;
186   //mq = GNUNET_MQ_queue_for_connection_client(service_conn,
187   //                                           mq_handlers,
188   //                                           mq_error_handler, // TODO implement
189   //                                           h);
190
191   /* Send the request to the service */
192   rh = GNUNET_RPS_request_peers(h, n, ready_cb, cls);
193   //ev = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
194   //msg->num_peers = GNUNET_htonll(n);
195   //GNUNET_MQ_send(mq, ev);
196   //GNUNET_CLIENT_disconnect(service_conn);
197   //rh = GNUNET_new(struct GNUNET_RPS_Request_Handle);
198   GNUNET_RPS_disconnect(h);
199   return rh;
200 }
201
202 /**
203  * Connect to the rps service
204  *
205  * @param cfg configuration to use
206  * @return a handle to the service
207  */
208   struct GNUNET_RPS_Handle *
209 GNUNET_RPS_connect( const struct GNUNET_CONFIGURATION_Handle *cfg )
210 {
211   struct GNUNET_RPS_Handle *h;
212   //struct GNUNET_RPS_Request_Handle *rh;
213   static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
214     {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
215     GNUNET_MQ_HANDLERS_END
216   };
217
218   h = GNUNET_new(struct GNUNET_RPS_Handle);
219   //h->cfg = GNUNET_new(struct GNUNET_CONFIGURATION_Handle);
220   //*h->cfg = *cfg;
221   h->cfg = cfg; // FIXME |^
222   h->conn = GNUNET_CLIENT_connect("rps", cfg);
223   h->mq = GNUNET_MQ_queue_for_connection_client(h->conn,
224                                                 mq_handlers,
225                                                 mq_error_handler, // TODO implement
226                                                 h);
227
228
229   return h;
230 }
231
232 /**
233  * Request n random peers.
234  *
235  * @param h handle to the rps service
236  * @param n number of peers we want to receive
237  * @param ready_cb the callback called when the peers are available
238  * @param cls closure given to the callback
239  * @return a handle to cancel this request
240  */
241   struct GNUNET_RPS_Request_Handle *
242 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint64_t n,
243                           GNUNET_RPS_NotifyReadyCB ready_cb,
244                           void *cls)
245 {
246   struct GNUNET_RPS_Request_Handle *rh;
247   struct GNUNET_MQ_Envelope *ev;
248   struct GNUNET_RPS_CS_RequestMessage *msg;
249
250   // assert func != NULL
251   rh = GNUNET_new(struct GNUNET_RPS_Request_Handle);
252   rh->h = h;
253   rh->n = req_handlers_size; // TODO ntoh
254   rh->ready_cb = ready_cb;
255   rh->ready_cb_cls = cls;
256
257   GNUNET_array_append(req_handlers, req_handlers_size, *rh);
258   //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_Handle));
259
260   ev = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
261   msg->num_peers = GNUNET_htonll(n);
262   msg->n = rh->n;
263   GNUNET_MQ_send(h->mq, ev);
264   return rh;
265 }
266
267 /**
268  * Seed rps service with peerIDs.
269  *
270  * @param h handle to the rps service
271  * @param n number of peers to seed
272  * @param ids the ids of the peers seeded
273  */
274   void
275 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, uint64_t n,
276                      struct GNUNET_PeerIdentity * ids)
277 {
278 }
279
280 /**
281  * Cancle an issued request.
282  *
283  * @param rh request handle of request to cancle
284  */
285   void
286 GNUNET_RPS_request_cancel ( struct GNUNET_RPS_Request_Handle *rh )
287 {
288   // TODO
289 }
290
291 /**
292  * Disconnect from the rps service
293  *
294  * @param h the handle to the rps service
295  */
296   void
297 GNUNET_RPS_disconnect ( struct GNUNET_RPS_Handle *h )
298 {
299   if ( NULL != h->conn ) {
300     GNUNET_CLIENT_disconnect(h->conn);
301   }
302 }
303
304
305 /* end of rps_api.c */