aboutsummaryrefslogtreecommitdiff
path: root/src/nse/nse_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nse/nse_api.c')
-rw-r--r--src/nse/nse_api.c156
1 files changed, 72 insertions, 84 deletions
diff --git a/src/nse/nse_api.c b/src/nse/nse_api.c
index 3be140c01..62c58ca77 100644
--- a/src/nse/nse_api.c
+++ b/src/nse/nse_api.c
@@ -86,8 +86,7 @@ struct GNUNET_NSE_Handle
86 * @param tc scheduler context 86 * @param tc scheduler context
87 */ 87 */
88static void 88static void
89reconnect (void *cls, 89reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
90 const struct GNUNET_SCHEDULER_TaskContext *tc);
91 90
92 91
93/** 92/**
@@ -97,38 +96,33 @@ reconnect (void *cls,
97 * @param cls closure 96 * @param cls closure
98 * @param msg message received, NULL on timeout or fatal error 97 * @param msg message received, NULL on timeout or fatal error
99 */ 98 */
100static void 99static void
101message_handler (void *cls, 100message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
102 const struct GNUNET_MessageHeader * msg)
103{ 101{
104 struct GNUNET_NSE_Handle *h = cls; 102 struct GNUNET_NSE_Handle *h = cls;
105 const struct GNUNET_NSE_ClientMessage *client_msg; 103 const struct GNUNET_NSE_ClientMessage *client_msg;
106 104
107 if (msg == NULL) 105 if (msg == NULL)
108 { 106 {
109 /* Error, timeout, death */ 107 /* Error, timeout, death */
110 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO); 108 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
111 h->client = NULL; 109 h->client = NULL;
112 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, 110 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
113 &reconnect, 111 &reconnect, h);
114 h); 112 return;
115 return; 113 }
116 } 114 if ((ntohs (msg->size) != sizeof (struct GNUNET_NSE_ClientMessage)) ||
117 if ( (ntohs (msg->size) != sizeof(struct GNUNET_NSE_ClientMessage)) || 115 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE))
118 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE) ) 116 {
119 { 117 GNUNET_break (0);
120 GNUNET_break (0); 118 return;
121 return; 119 }
122 } 120 client_msg = (const struct GNUNET_NSE_ClientMessage *) msg;
123 client_msg = (const struct GNUNET_NSE_ClientMessage *)msg; 121 h->recv_cb (h->recv_cb_cls,
124 h->recv_cb (h->recv_cb_cls, 122 GNUNET_TIME_absolute_ntoh (client_msg->timestamp),
125 GNUNET_TIME_absolute_ntoh(client_msg->timestamp), 123 client_msg->size_estimate, client_msg->std_deviation);
126 client_msg->size_estimate,
127 client_msg->std_deviation);
128 GNUNET_CLIENT_receive (h->client, 124 GNUNET_CLIENT_receive (h->client,
129 &message_handler, 125 &message_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
130 h,
131 GNUNET_TIME_UNIT_FOREVER_REL);
132} 126}
133 127
134 128
@@ -144,15 +138,15 @@ reschedule_connect (struct GNUNET_NSE_Handle *h)
144 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK); 138 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
145 139
146 if (NULL != h->th) 140 if (NULL != h->th)
147 { 141 {
148 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th); 142 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
149 h->th = NULL; 143 h->th = NULL;
150 } 144 }
151 if (NULL != h->client) 145 if (NULL != h->client)
152 { 146 {
153 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO); 147 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
154 h->client = NULL; 148 h->client = NULL;
155 } 149 }
156 150
157#if DEBUG_NSE 151#if DEBUG_NSE
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -160,18 +154,17 @@ reschedule_connect (struct GNUNET_NSE_Handle *h)
160 h->reconnect_delay.rel_value); 154 h->reconnect_delay.rel_value);
161#endif 155#endif
162 h->reconnect_task 156 h->reconnect_task
163 = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, 157 = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
164 &reconnect, h);
165 if (h->reconnect_delay.rel_value == 0) 158 if (h->reconnect_delay.rel_value == 0)
166 { 159 {
167 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS; 160 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
168 } 161 }
169 else 162 else
170 { 163 {
171 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2); 164 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
172 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, 165 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
173 h->reconnect_delay); 166 h->reconnect_delay);
174 } 167 }
175} 168}
176 169
177 170
@@ -191,23 +184,21 @@ send_start (void *cls, size_t size, void *buf)
191 184
192 h->th = NULL; 185 h->th = NULL;
193 if (buf == NULL) 186 if (buf == NULL)
194 { 187 {
195 /* Connect error... */ 188 /* Connect error... */
196#if DEBUG_NSE 189#if DEBUG_NSE
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Shutdown while trying to transmit `%s' request.\n", 191 "Shutdown while trying to transmit `%s' request.\n", "START");
199 "START");
200#endif 192#endif
201 reschedule_connect(h); 193 reschedule_connect (h);
202 return 0; 194 return 0;
203 } 195 }
204#if DEBUG_NSE 196#if DEBUG_NSE
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
206 "Transmitting `%s' request.\n", "START");
207#endif 198#endif
208 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); 199 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
209 200
210 msg = (struct GNUNET_MessageHeader *)buf; 201 msg = (struct GNUNET_MessageHeader *) buf;
211 msg->size = htons (sizeof (struct GNUNET_MessageHeader)); 202 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
212 msg->type = htons (GNUNET_MESSAGE_TYPE_NSE_START); 203 msg->type = htons (GNUNET_MESSAGE_TYPE_NSE_START);
213 GNUNET_CLIENT_receive (h->client, 204 GNUNET_CLIENT_receive (h->client,
@@ -223,17 +214,16 @@ send_start (void *cls, size_t size, void *buf)
223 * @param tc scheduler context 214 * @param tc scheduler context
224 */ 215 */
225static void 216static void
226reconnect (void *cls, 217reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
227 const struct GNUNET_SCHEDULER_TaskContext *tc)
228{ 218{
229 struct GNUNET_NSE_Handle *h = cls; 219 struct GNUNET_NSE_Handle *h = cls;
230 220
231 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 221 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
232 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 222 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
233 { 223 {
234 /* shutdown, just give up */ 224 /* shutdown, just give up */
235 return; 225 return;
236 } 226 }
237#if DEBUG_NSE 227#if DEBUG_NSE
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "Connecting to network size estimation service.\n"); 229 "Connecting to network size estimation service.\n");
@@ -243,13 +233,11 @@ reconnect (void *cls,
243 GNUNET_assert (h->client != NULL); 233 GNUNET_assert (h->client != NULL);
244 234
245 h->th = 235 h->th =
246 GNUNET_CLIENT_notify_transmit_ready (h->client, 236 GNUNET_CLIENT_notify_transmit_ready (h->client,
247 sizeof(struct GNUNET_MessageHeader), 237 sizeof (struct GNUNET_MessageHeader),
248 GNUNET_TIME_UNIT_FOREVER_REL, 238 GNUNET_TIME_UNIT_FOREVER_REL,
249 GNUNET_NO, 239 GNUNET_NO, &send_start, h);
250 &send_start, 240 GNUNET_assert (h->th != NULL);
251 h);
252 GNUNET_assert(h->th != NULL);
253} 241}
254 242
255 243
@@ -287,23 +275,23 @@ GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
287void 275void
288GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h) 276GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
289{ 277{
290 GNUNET_assert(h != NULL); 278 GNUNET_assert (h != NULL);
291 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK) 279 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
292 { 280 {
293 GNUNET_SCHEDULER_cancel(h->reconnect_task); 281 GNUNET_SCHEDULER_cancel (h->reconnect_task);
294 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 282 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
295 } 283 }
296 if (h->th != NULL) 284 if (h->th != NULL)
297 { 285 {
298 GNUNET_CLIENT_notify_transmit_ready_cancel(h->th); 286 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
299 h->th = NULL; 287 h->th = NULL;
300 } 288 }
301 if (h->client != NULL) 289 if (h->client != NULL)
302 { 290 {
303 GNUNET_CLIENT_disconnect(h->client, GNUNET_NO); 291 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
304 h->client = NULL; 292 h->client = NULL;
305 } 293 }
306 GNUNET_free(h); 294 GNUNET_free (h);
307} 295}
308 296
309/* end of nse_api.c */ 297/* end of nse_api.c */