d7e3fc4ed24554792e7cc369c8fcfa7d37935eb0
[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  * This function is called, when the service replies to our request.
113  * It calls the callback the caller gave us with the provided closure
114  * and disconnects afterwards.
115  *
116  * @param cls the closure
117  * @param message the message
118  */
119   static void
120 handle_reply (void *cls,
121               const struct GNUNET_MessageHeader *message)
122 {
123   struct GNUNET_RPS_CS_ReplyMessage *msg;
124   //struct cb_cls_pack *pack;
125   //struct GNUNET_RPS_Handle *h;
126   struct GNUNET_PeerIdentity *peers;
127   struct GNUNET_RPS_Request_Handle *rh;
128
129   /* Give the peers back */
130   msg = (struct GNUNET_RPS_CS_ReplyMessage *) message;
131   //pack = (struct cb_cls_pack *) cls;
132   //h = (struct GNUNET_RPS_Handle *) cls;
133   peers = (struct GNUNET_PeerIdentity *) &msg[1];
134   rh = &req_handlers[msg->n];
135   rh->ready_cb((rh)->ready_cb_cls, msg->num_peers, peers);
136
137   /* Disconnect */
138   //GNUNET_CLIENT_disconnect(pack->service_conn);
139 }
140
141 /**
142  * Error handler for mq.
143  *
144  * This function is called whan mq encounters an error.
145  * Until now mq doesn't provide useful error messages.
146  *
147  * @param cls the closure
148  * @param error error code without specyfied meaning
149  */
150   static void
151 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
152 {
153   //TODO LOG
154 }
155
156 /**
157  * Connect to the rps service
158  *
159  * @param cfg configuration to use
160  * @return a handle to the service
161  */
162   struct GNUNET_RPS_Handle *
163 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
164 {
165   struct GNUNET_RPS_Handle *h;
166   //struct GNUNET_RPS_Request_Handle *rh;
167   static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
168     {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
169     GNUNET_MQ_HANDLERS_END
170   };
171
172   h = GNUNET_new(struct GNUNET_RPS_Handle);
173   //h->cfg = GNUNET_new(struct GNUNET_CONFIGURATION_Handle);
174   //*h->cfg = *cfg;
175   h->cfg = cfg; // FIXME |^
176   h->conn = GNUNET_CLIENT_connect("rps", cfg);
177   h->mq = GNUNET_MQ_queue_for_connection_client(h->conn,
178                                                 mq_handlers,
179                                                 mq_error_handler, // TODO implement
180                                                 h);
181
182   return h;
183 }
184
185 /**
186  * Request n random peers.
187  *
188  * @param h handle to the rps service
189  * @param n number of peers we want to receive
190  * @param ready_cb the callback called when the peers are available
191  * @param cls closure given to the callback
192  * @return a handle to cancel this request
193  */
194   struct GNUNET_RPS_Request_Handle *
195 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint32_t n,
196                           GNUNET_RPS_NotifyReadyCB ready_cb,
197                           void *cls)
198 {
199   struct GNUNET_RPS_Request_Handle *rh;
200   struct GNUNET_MQ_Envelope *ev;
201   struct GNUNET_RPS_CS_RequestMessage *msg;
202
203   // assert func != NULL
204   rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
205   rh->h = h;
206   rh->n = req_handlers_size; // TODO ntoh
207   rh->ready_cb = ready_cb;
208   rh->ready_cb_cls = cls;
209
210   GNUNET_array_append (req_handlers, req_handlers_size, *rh);
211   //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_Handle));
212
213   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
214   msg->num_peers = htonl (n);
215   msg->n = rh->n;
216   GNUNET_MQ_send (h->mq, ev);
217   return rh;
218 }
219
220 /**
221  * Seed rps service with peerIDs.
222  *
223  * @param h handle to the rps service
224  * @param n number of peers to seed
225  * @param ids the ids of the peers seeded
226  */
227   void
228 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, uint64_t n,
229                      const struct GNUNET_PeerIdentity * ids)
230 {
231   uint32_t size_needed;
232   uint32_t tmp_num_peers;
233   struct GNUNET_MQ_Envelope *ev;
234   struct GNUNET_RPS_CS_SeedMessage *msg;
235
236   size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
237                 n * sizeof (struct GNUNET_PeerIdentity);
238
239   while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
240   {
241     tmp_num_peers = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
242         sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
243       sizeof (struct GNUNET_PeerIdentity);
244     n -= tmp_num_peers;
245     size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
246                   n * sizeof (struct GNUNET_PeerIdentity);
247
248     ev = GNUNET_MQ_msg_extra (msg, tmp_num_peers * sizeof (struct GNUNET_PeerIdentity),
249         GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
250     msg->num_peers = GNUNET_htonll (tmp_num_peers);
251     memcpy (&msg[1], ids, tmp_num_peers * sizeof (struct GNUNET_PeerIdentity));
252     GNUNET_MQ_send (h->mq, ev);
253   }
254
255   ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
256                             GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
257   msg->num_peers = GNUNET_htonll (n);
258   memcpy (&msg[1], ids, n * sizeof (struct GNUNET_PeerIdentity));
259   GNUNET_MQ_send (h->mq, ev);
260 }
261
262
263 /**
264  * Cancle an issued request.
265  *
266  * @param rh request handle of request to cancle
267  */
268   void
269 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
270 {
271   // TODO
272 }
273
274
275 /**
276  * Disconnect from the rps service
277  *
278  * @param h the handle to the rps service
279  */
280   void
281 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
282 {
283   if ( NULL != h->conn )
284     GNUNET_CLIENT_disconnect (h->conn);
285 }
286
287
288 /* end of rps_api.c */