2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
22 * @file core/core_api_peer_request.c
23 * @brief implementation of the peer_request functions
24 * @author Christian Grothoff
27 #include "gnunet_core_service.h"
32 * Handle for a request to the core to connect to
33 * a particular peer. Can be used to cancel the request
34 * (before the 'cont'inuation is called).
36 struct GNUNET_CORE_PeerRequestHandle
40 * Our connection to the service.
42 struct GNUNET_CLIENT_Connection *client;
47 struct GNUNET_SCHEDULER_Handle *sched;
50 * Function to call once done.
52 GNUNET_SCHEDULER_Task cont;
60 * Identity of the peer to connect to.
62 struct GNUNET_PeerIdentity peer;
65 * Message type to use.
72 * Transmit the request to the core service.
74 * @param cls our 'struct GNUNET_CORE_PeerRequestHandle'
75 * @param size number of bytes available in buf
76 * @param buf where the callee should write the message
77 * @return number of bytes written to buf
80 send_request (void *cls,
84 struct GNUNET_CORE_PeerRequestHandle * prh = cls;
85 struct ConnectMessage msg;
89 GNUNET_SCHEDULER_add_continuation (prh->sched,
92 GNUNET_SCHEDULER_REASON_TIMEOUT);
93 GNUNET_CLIENT_disconnect (prh->client);
97 GNUNET_assert (size >= sizeof (struct ConnectMessage));
98 msg.header.type = htons (prh->type);
99 msg.header.size = htons (sizeof (struct ConnectMessage));
100 msg.reserved = htonl (0);
101 msg.peer = prh->peer;
102 memcpy (buf, &msg, sizeof (msg));
103 GNUNET_SCHEDULER_add_continuation (prh->sched,
106 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
107 GNUNET_CLIENT_disconnect (prh->client);
114 * Request that the core should try to connect to a particular peer.
115 * Once the request has been transmitted to the core, the continuation
116 * function will be called. Note that this does NOT mean that a
117 * connection was successfully established -- it only means that the
118 * core will now try. Successful establishment of the connection
119 * will be signalled to the 'connects' callback argument of
120 * 'GNUNET_CORE_connect' only. If the core service does not respond
121 * to our connection attempt within the given time frame, 'cont' will
122 * be called with the TIMEOUT reason code.
124 * @param sched scheduler to use
125 * @param cfg configuration to use
126 * @param timeout how long to try to talk to core
127 * @param cont function to call once the request has been completed (or timed out)
128 * @param cont_cls closure for cont
129 * @return NULL on error (cont will not be called), otherwise handle for cancellation
131 struct GNUNET_CORE_PeerRequestHandle *
132 GNUNET_CORE_peer_request_connect (struct GNUNET_SCHEDULER_Handle *sched,
133 const struct GNUNET_CONFIGURATION_Handle *cfg,
134 struct GNUNET_TIME_Relative timeout,
135 const struct GNUNET_PeerIdentity * peer,
136 GNUNET_SCHEDULER_Task cont,
139 struct GNUNET_CORE_PeerRequestHandle *ret;
140 struct GNUNET_CLIENT_Connection *client;
142 client = GNUNET_CLIENT_connect (sched, "core", cfg);
145 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
146 ret->client = client;
149 ret->cont_cls = cont_cls;
151 ret->type = GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT;
152 GNUNET_CLIENT_notify_transmit_ready (client,
153 sizeof (struct ConnectMessage),
163 * Cancel a pending request to connect to a particular peer. Must not
164 * be called after the 'cont' function was invoked.
166 * @param req request handle that was returned for the original request
169 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
171 GNUNET_CLIENT_disconnect (req->client);
176 /* end of core_api_peer_request.c */