big scheduler refactoring, expect some issues
[oweals/gnunet.git] / src / core / core_api_peer_request.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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 core/core_api_peer_request.c
23  * @brief implementation of the peer_request functions 
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_core_service.h"
28 #include "core.h"
29
30
31 /**
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).
35  */
36 struct GNUNET_CORE_PeerRequestHandle
37 {
38
39   /**
40    * Our connection to the service.
41    */
42   struct GNUNET_CLIENT_Connection *client;
43
44
45   /**
46    * Function to call once done.
47    */
48   GNUNET_SCHEDULER_Task cont;
49   
50   /**
51    * Closure for 'cont'.
52    */
53   void *cont_cls;
54
55   /**
56    * When to time out.
57    */
58   struct GNUNET_TIME_Absolute timeout;
59
60   /**
61    * Identity of the peer to connect to.
62    */
63   struct GNUNET_PeerIdentity peer;
64         
65   /**
66    * Message type to use.
67    */
68   uint16_t type;
69 };
70
71
72 /**
73  * Transmit the request to the core service.
74  *
75  * @param cls our 'struct GNUNET_CORE_PeerRequestHandle'
76  * @param size number of bytes available in buf
77  * @param buf where the callee should write the message
78  * @return number of bytes written to buf
79  */ 
80 static size_t
81 send_request (void *cls,
82               size_t size,
83               void *buf)
84 {
85   struct GNUNET_CORE_PeerRequestHandle * prh = cls;
86   struct ConnectMessage msg;
87
88   if (buf == NULL)
89     {
90       if (prh->cont != NULL)
91         GNUNET_SCHEDULER_add_continuation (prh->cont,
92                                            prh->cont_cls,
93                                            GNUNET_SCHEDULER_REASON_TIMEOUT);
94       GNUNET_CLIENT_disconnect (prh->client, GNUNET_NO);
95       GNUNET_free (prh);
96       return 0;
97     }
98   GNUNET_assert (size >= sizeof (struct ConnectMessage));
99   msg.header.type = htons (prh->type);
100   msg.header.size = htons (sizeof (struct ConnectMessage));
101   msg.reserved = htonl (0);
102   msg.timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (prh->timeout));
103   msg.peer = prh->peer;
104   memcpy (buf, &msg, sizeof (msg));
105   if (prh->cont != NULL)
106     {
107       GNUNET_SCHEDULER_add_continuation (prh->cont,
108                                          prh->cont_cls,
109                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
110     }
111   GNUNET_CLIENT_disconnect (prh->client, GNUNET_YES);
112   GNUNET_free (prh);
113   return sizeof (msg);
114 }
115
116
117 /**
118  * Request that the core should try to connect to a particular peer.
119  * Once the request has been transmitted to the core, the continuation
120  * function will be called.  Note that this does NOT mean that a
121  * connection was successfully established -- it only means that the
122  * core will now try.  Successful establishment of the connection
123  * will be signalled to the 'connects' callback argument of
124  * 'GNUNET_CORE_connect' only.  If the core service does not respond
125  * to our connection attempt within the given time frame, 'cont' will
126  * be called with the TIMEOUT reason code.
127  *
128  * @param cfg configuration to use
129  * @param timeout how long to try to talk to core
130  * @param peer who should we connect to
131  * @param cont function to call once the request has been completed (or timed out)
132  * @param cont_cls closure for cont
133  * @return NULL on error (cont will not be called), otherwise handle for cancellation
134  */
135 struct GNUNET_CORE_PeerRequestHandle *
136 GNUNET_CORE_peer_request_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
137                                   struct GNUNET_TIME_Relative timeout,
138                                   const struct GNUNET_PeerIdentity * peer,
139                                   GNUNET_SCHEDULER_Task cont,
140                                   void *cont_cls)
141 {
142   struct GNUNET_CORE_PeerRequestHandle *ret;
143   struct GNUNET_CLIENT_Connection *client;
144   
145   client = GNUNET_CLIENT_connect ("core", cfg);
146   if (client == NULL)
147     return NULL;
148   ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
149   ret->client = client;
150   ret->cont = cont;
151   ret->cont_cls = cont_cls;
152   ret->peer = *peer;
153   ret->type = GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT;
154   ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
155   GNUNET_CLIENT_notify_transmit_ready (client,
156                                        sizeof (struct ConnectMessage),
157                                        timeout,
158                                        GNUNET_YES,
159                                        &send_request,
160                                        ret);
161   return ret;
162 }
163
164
165 /**
166  * Cancel a pending request to connect to a particular peer.  Must not
167  * be called after the 'cont' function was invoked.
168  *
169  * @param req request handle that was returned for the original request
170  */
171 void
172 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
173 {
174   GNUNET_CLIENT_disconnect (req->client, GNUNET_NO);
175   GNUNET_free (req);
176 }
177
178
179 /* end of core_api_peer_request.c */