diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-01-20 13:51:20 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-01-20 13:51:20 +0000 |
commit | 7d9f187d106394b2451660294df9428eb50e82d7 (patch) | |
tree | 373a78f3c10529689791b65a5ab885d3f4c761b7 /src/core/core_api_peer_request.c | |
parent | 924137ff73bab48b720d41cf73a1197df2d4b04d (diff) | |
download | gnunet-7d9f187d106394b2451660294df9428eb50e82d7.tar.gz gnunet-7d9f187d106394b2451660294df9428eb50e82d7.zip |
fixing core API issues
Diffstat (limited to 'src/core/core_api_peer_request.c')
-rw-r--r-- | src/core/core_api_peer_request.c | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/src/core/core_api_peer_request.c b/src/core/core_api_peer_request.c index e873a5a9d..58837c773 100644 --- a/src/core/core_api_peer_request.c +++ b/src/core/core_api_peer_request.c | |||
@@ -28,7 +28,201 @@ | |||
28 | #include "core.h" | 28 | #include "core.h" |
29 | 29 | ||
30 | 30 | ||
31 | /** | ||
32 | * Handle for a request to the core to connect or disconnect | ||
33 | * from 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 | * Scheduler. | ||
46 | */ | ||
47 | struct GNUNET_SCHEDULER_Handle *sched; | ||
48 | |||
49 | /** | ||
50 | * Function to call once done. | ||
51 | */ | ||
52 | GNUNET_SCHEDULER_Task cont; | ||
53 | |||
54 | /** | ||
55 | * Closure for 'cont'. | ||
56 | */ | ||
57 | void *cont_cls; | ||
58 | |||
59 | /** | ||
60 | * Identity of the peer to connect/disconnect. | ||
61 | */ | ||
62 | struct GNUNET_PeerIdentity peer; | ||
63 | |||
64 | /** | ||
65 | * Message type to use. | ||
66 | */ | ||
67 | uint16_t type; | ||
68 | }; | ||
69 | |||
70 | |||
71 | /** | ||
72 | * Transmit the request to the core service. | ||
73 | * | ||
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 | ||
78 | */ | ||
79 | static size_t | ||
80 | send_request (void *cls, | ||
81 | size_t size, | ||
82 | void *buf) | ||
83 | { | ||
84 | struct GNUNET_CORE_PeerRequestHandle * prh = cls; | ||
85 | struct ConnectMessage msg; | ||
86 | |||
87 | if (buf == NULL) | ||
88 | { | ||
89 | GNUNET_SCHEDULER_add_continuation (prh->sched, | ||
90 | prh->cont, | ||
91 | prh->cont_cls, | ||
92 | GNUNET_SCHEDULER_REASON_TIMEOUT); | ||
93 | GNUNET_CLIENT_disconnect (prh->client); | ||
94 | GNUNET_free (prh); | ||
95 | return 0; | ||
96 | } | ||
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, | ||
104 | prh->cont, | ||
105 | prh->cont_cls, | ||
106 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); | ||
107 | GNUNET_CLIENT_disconnect (prh->client); | ||
108 | GNUNET_free (prh); | ||
109 | return sizeof (msg); | ||
110 | } | ||
111 | |||
31 | 112 | ||
113 | /** | ||
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. | ||
123 | * | ||
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 | ||
130 | */ | ||
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, | ||
137 | void *cont_cls) | ||
138 | { | ||
139 | struct GNUNET_CORE_PeerRequestHandle *ret; | ||
140 | struct GNUNET_CLIENT_Connection *client; | ||
141 | |||
142 | client = GNUNET_CLIENT_connect (sched, "core", cfg); | ||
143 | if (client == NULL) | ||
144 | return NULL; | ||
145 | ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle)); | ||
146 | ret->client = client; | ||
147 | ret->sched = sched; | ||
148 | ret->cont = cont; | ||
149 | ret->cont_cls = cont_cls; | ||
150 | ret->peer = *peer; | ||
151 | ret->type = GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT; | ||
152 | GNUNET_CLIENT_notify_transmit_ready (client, | ||
153 | sizeof (struct ConnectMessage), | ||
154 | timeout, | ||
155 | GNUNET_YES, | ||
156 | &send_request, | ||
157 | ret); | ||
158 | return ret; | ||
159 | } | ||
160 | |||
161 | |||
162 | /** | ||
163 | * Request that the core should try to disconnect from a particular | ||
164 | * peer. Once the request has been transmitted to the core, the | ||
165 | * continuation function will be called. Note that this does NOT mean | ||
166 | * that a connection was successfully cut -- it only means that the | ||
167 | * core will now try. Typically this will work pretty much | ||
168 | * immediately, but it is at least in theory also possible that a | ||
169 | * reconnect is also triggered rather quickly. Successful creation | ||
170 | * and destruction of connections will be signalled to the 'connects' | ||
171 | * and 'disconnects' callback arguments of 'GNUNET_CORE_connect' only. | ||
172 | * If the core service does not respond to our connection attempt | ||
173 | * within the given time frame, 'cont' will be called with the TIMEOUT | ||
174 | * reason code. | ||
175 | * | ||
176 | * @param sched scheduler to use | ||
177 | * @param cfg configuration to use | ||
178 | * @param timeout how long to try to talk to core | ||
179 | * @param cont function to call once the request has been completed (or timed out) | ||
180 | * @param cont_cls closure for cont | ||
181 | * @return NULL on error (cont will not be called), otherwise handle for cancellation | ||
182 | */ | ||
183 | struct GNUNET_CORE_PeerRequestHandle * | ||
184 | GNUNET_CORE_peer_request_disconnect (struct GNUNET_SCHEDULER_Handle *sched, | ||
185 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
186 | struct GNUNET_TIME_Relative timeout, | ||
187 | const struct GNUNET_PeerIdentity * peer, | ||
188 | GNUNET_SCHEDULER_Task cont, | ||
189 | void *cont_cls) | ||
190 | { | ||
191 | struct GNUNET_CORE_PeerRequestHandle *ret; | ||
192 | struct GNUNET_CLIENT_Connection *client; | ||
193 | |||
194 | client = GNUNET_CLIENT_connect (sched, "core", cfg); | ||
195 | if (client == NULL) | ||
196 | return NULL; | ||
197 | ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle)); | ||
198 | ret->client = client; | ||
199 | ret->sched = sched; | ||
200 | ret->cont = cont; | ||
201 | ret->cont_cls = cont_cls; | ||
202 | ret->peer = *peer; | ||
203 | ret->type = GNUNET_MESSAGE_TYPE_CORE_REQUEST_DISCONNECT; | ||
204 | GNUNET_CLIENT_notify_transmit_ready (client, | ||
205 | sizeof (struct ConnectMessage), | ||
206 | timeout, | ||
207 | GNUNET_YES, | ||
208 | &send_request, | ||
209 | ret); | ||
210 | return ret; | ||
211 | } | ||
212 | |||
213 | |||
214 | /** | ||
215 | * Cancel a pending request to connect or disconnect from/to a particular | ||
216 | * peer. Must not be called after the 'cont' function was invoked. | ||
217 | * | ||
218 | * @param req request handle that was returned for the original request | ||
219 | */ | ||
220 | void | ||
221 | GNUNET_CORE_peer_request_cancel (struct GNUNET_CORE_PeerRequestHandle *req) | ||
222 | { | ||
223 | GNUNET_CLIENT_disconnect (req->client); | ||
224 | GNUNET_free (req); | ||
225 | } | ||
32 | 226 | ||
33 | 227 | ||
34 | /* end of core_api_peer_request.c */ | 228 | /* end of core_api_peer_request.c */ |