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"
32 * Handler to handle requests from a client.
34 struct GNUNET_RPS_Handle
37 * The handle to the client configuration.
39 const struct GNUNET_CONFIGURATION_Handle *cfg;
42 * The connection to the client.
44 struct GNUNET_CLIENT_Connection *conn;
47 * The message queue to the client.
49 struct GNUNET_MQ_Handle *mq;
53 * Handler to single requests from the client.
55 struct GNUNET_RPS_Request_Handle
58 * The client issuing the request.
60 struct GNUNET_RPS_Handle *h;
63 * The nuber of the request.
68 * The callback to be called when we receive an answer.
70 GNUNET_RPS_NotifyReadyCB ready_cb;
73 * The closure for the callback.
79 * Array of Request_Handles.
81 struct GNUNET_RPS_Request_Handle *req_handlers = NULL;
84 * Current length of req_handlers.
86 unsigned int req_handlers_size = 0;
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.
95 * Callback provided by the client
97 GNUNET_RPS_NotifyReadyCB cb;
100 * Closure provided by the client
105 * Handle to the service connection
107 struct GNUNET_CLIENT_Connection *service_conn;
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.
118 * @param cls the closure
119 * @param message the message
122 handle_reply (void *cls,
123 const struct GNUNET_MessageHeader *message)
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;
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);
140 //GNUNET_CLIENT_disconnect(pack->service_conn);
146 mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
152 * Connect to the rps service
154 * @param cfg configuration to use
155 * @return a handle to the service
157 struct GNUNET_RPS_Handle *
158 GNUNET_RPS_connect( const struct GNUNET_CONFIGURATION_Handle *cfg )
160 struct GNUNET_RPS_Handle *h;
161 //struct GNUNET_RPS_Request_Handle *rh;
162 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
163 {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
164 GNUNET_MQ_HANDLERS_END
167 h = GNUNET_new(struct GNUNET_RPS_Handle);
168 //h->cfg = GNUNET_new(struct GNUNET_CONFIGURATION_Handle);
170 h->cfg = cfg; // FIXME |^
171 h->conn = GNUNET_CLIENT_connect("rps", cfg);
172 h->mq = GNUNET_MQ_queue_for_connection_client(h->conn,
174 mq_error_handler, // TODO implement
182 * Request n random peers.
184 * @param h handle to the rps service
185 * @param n number of peers we want to receive
186 * @param ready_cb the callback called when the peers are available
187 * @param cls closure given to the callback
188 * @return a handle to cancel this request
190 struct GNUNET_RPS_Request_Handle *
191 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint64_t n,
192 GNUNET_RPS_NotifyReadyCB ready_cb,
195 struct GNUNET_RPS_Request_Handle *rh;
196 struct GNUNET_MQ_Envelope *ev;
197 struct GNUNET_RPS_CS_RequestMessage *msg;
199 // assert func != NULL
200 rh = GNUNET_new(struct GNUNET_RPS_Request_Handle);
202 rh->n = req_handlers_size; // TODO ntoh
203 rh->ready_cb = ready_cb;
204 rh->ready_cb_cls = cls;
206 GNUNET_array_append(req_handlers, req_handlers_size, *rh);
207 //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_Handle));
209 ev = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
210 msg->num_peers = GNUNET_htonll(n);
212 GNUNET_MQ_send(h->mq, ev);
217 * Seed rps service with peerIDs.
219 * @param h handle to the rps service
220 * @param n number of peers to seed
221 * @param ids the ids of the peers seeded
224 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, uint64_t n,
225 struct GNUNET_PeerIdentity * ids)
230 * Cancle an issued request.
232 * @param rh request handle of request to cancle
235 GNUNET_RPS_request_cancel ( struct GNUNET_RPS_Request_Handle *rh )
241 * Disconnect from the rps service
243 * @param h the handle to the rps service
246 GNUNET_RPS_disconnect ( struct GNUNET_RPS_Handle *h )
248 if ( NULL != h->conn ) {
249 GNUNET_CLIENT_disconnect(h->conn);
254 /* end of rps_api.c */