2 This file is part of GNUnet.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
29 #include "gnunet_rps_service.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "rps-api",__VA_ARGS__)
36 * Handler to handle requests from a client.
38 struct GNUNET_RPS_Handle
41 * The handle to the client configuration.
43 const struct GNUNET_CONFIGURATION_Handle *cfg;
46 * The connection to the client.
48 struct GNUNET_CLIENT_Connection *conn;
51 * The message queue to the client.
53 struct GNUNET_MQ_Handle *mq;
58 * Handler to single requests from the client.
60 struct GNUNET_RPS_Request_Handle
63 * The client issuing the request.
65 struct GNUNET_RPS_Handle *h;
68 * The nuber of the request.
73 * The callback to be called when we receive an answer.
75 GNUNET_RPS_NotifyReadyCB ready_cb;
78 * The closure for the callback.
85 * Array of Request_Handles.
87 struct GNUNET_RPS_Request_Handle *req_handlers = NULL;
90 * Current length of req_handlers.
92 unsigned int req_handlers_size = 0;
96 * Struct used to pack the callback, its closure (provided by the caller)
97 * and the connection handler to the service to pass it to a callback function.
102 * Callback provided by the client
104 GNUNET_RPS_NotifyReadyCB cb;
107 * Closure provided by the client
112 * Handle to the service connection
114 struct GNUNET_CLIENT_Connection *service_conn;
119 * This function is called, when the service replies to our request.
120 * It calls the callback the caller gave us with the provided closure
121 * and disconnects afterwards.
123 * @param cls the closure
124 * @param message the message
127 handle_reply (void *cls,
128 const struct GNUNET_MessageHeader *message)
130 struct GNUNET_RPS_CS_ReplyMessage *msg;
131 //struct cb_cls_pack *pack;
132 //struct GNUNET_RPS_Handle *h;
133 struct GNUNET_PeerIdentity *peers;
134 struct GNUNET_RPS_Request_Handle *rh;
136 /* Give the peers back */
137 msg = (struct GNUNET_RPS_CS_ReplyMessage *) message;
138 //pack = (struct cb_cls_pack *) cls;
139 //h = (struct GNUNET_RPS_Handle *) cls;
140 peers = (struct GNUNET_PeerIdentity *) &msg[1];
141 rh = &req_handlers[msg->n];
142 rh->ready_cb((rh)->ready_cb_cls, msg->num_peers, peers);
145 //GNUNET_CLIENT_disconnect(pack->service_conn);
150 * Error handler for mq.
152 * This function is called whan mq encounters an error.
153 * Until now mq doesn't provide useful error messages.
155 * @param cls the closure
156 * @param error error code without specyfied meaning
159 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
165 * Connect to the rps service
167 * @param cfg configuration to use
168 * @return a handle to the service
170 struct GNUNET_RPS_Handle *
171 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
173 struct GNUNET_RPS_Handle *h;
174 //struct GNUNET_RPS_Request_Handle *rh;
175 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
176 {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
177 GNUNET_MQ_HANDLERS_END
180 h = GNUNET_new(struct GNUNET_RPS_Handle);
181 //h->cfg = GNUNET_new(struct GNUNET_CONFIGURATION_Handle);
183 h->cfg = cfg; // FIXME |^
184 h->conn = GNUNET_CLIENT_connect("rps", cfg);
185 h->mq = GNUNET_MQ_queue_for_connection_client(h->conn,
187 mq_error_handler, // TODO implement
195 * Request n random peers.
197 * @param h handle to the rps service
198 * @param n number of peers we want to receive
199 * @param ready_cb the callback called when the peers are available
200 * @param cls closure given to the callback
201 * @return a handle to cancel this request
203 struct GNUNET_RPS_Request_Handle *
204 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint32_t n,
205 GNUNET_RPS_NotifyReadyCB ready_cb,
208 struct GNUNET_RPS_Request_Handle *rh;
209 struct GNUNET_MQ_Envelope *ev;
210 struct GNUNET_RPS_CS_RequestMessage *msg;
212 // assert func != NULL
213 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
215 rh->n = req_handlers_size; // TODO ntoh
216 rh->ready_cb = ready_cb;
217 rh->ready_cb_cls = cls;
219 GNUNET_array_append (req_handlers, req_handlers_size, *rh);
220 //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_Handle));
222 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
223 msg->num_peers = htonl (n);
225 GNUNET_MQ_send (h->mq, ev);
231 * Seed rps service with peerIDs.
233 * @param h handle to the rps service
234 * @param n number of peers to seed
235 * @param ids the ids of the peers seeded
238 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, uint32_t n,
239 const struct GNUNET_PeerIdentity *ids)
241 uint32_t size_needed;
242 uint32_t num_peers_max;
243 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
244 struct GNUNET_MQ_Envelope *ev;
245 struct GNUNET_RPS_CS_SeedMessage *msg;
249 LOG (GNUNET_ERROR_TYPE_DEBUG,
250 "Client wants to seed %" PRIX32 " peers:\n",
252 for (i = 0 ; i < n ; i++)
253 LOG (GNUNET_ERROR_TYPE_DEBUG,
256 GNUNET_i2s (&ids[i]));
258 /* The actual size the message occupies */
259 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
260 n * sizeof (struct GNUNET_PeerIdentity);
261 /* The number of peers that fits in one message together with
262 * the respective header */
263 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
264 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
265 sizeof (struct GNUNET_PeerIdentity);
266 tmp_peer_pointer = ids;
268 while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
270 ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
271 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
272 msg->num_peers = ntohl (num_peers_max);
273 memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
274 GNUNET_MQ_send (h->mq, ev);
277 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
278 n * sizeof (struct GNUNET_PeerIdentity);
279 /* Set pointer to beginning of next block of num_peers_max peers */
280 tmp_peer_pointer = &ids[num_peers_max];
283 ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
284 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
285 msg->num_peers = htonl (n);
286 memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
288 GNUNET_MQ_send (h->mq, ev);
293 * Cancle an issued request.
295 * @param rh request handle of request to cancle
298 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
305 * Disconnect from the rps service
307 * @param h the handle to the rps service
310 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
312 if ( NULL != h->conn )
313 GNUNET_CLIENT_disconnect (h->conn);
317 /* end of rps_api.c */