diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-07-23 13:23:40 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-07-23 13:23:40 +0000 |
commit | f53e991a59f4befa47b0ce90d8b68318dcd23864 (patch) | |
tree | 89dae5aaf3c8bef1fa1412133fe9d22d4f4e232d /src/nse/nse_api.c | |
parent | c1ed03a28d6de09d7d0ac2025af9f8067e6bb0fd (diff) | |
download | gnunet-f53e991a59f4befa47b0ce90d8b68318dcd23864.tar.gz gnunet-f53e991a59f4befa47b0ce90d8b68318dcd23864.zip |
nse hacking
Diffstat (limited to 'src/nse/nse_api.c')
-rw-r--r-- | src/nse/nse_api.c | 94 |
1 files changed, 50 insertions, 44 deletions
diff --git a/src/nse/nse_api.c b/src/nse/nse_api.c index 4c441608f..a50cd5a41 100644 --- a/src/nse/nse_api.c +++ b/src/nse/nse_api.c | |||
@@ -22,8 +22,6 @@ | |||
22 | * @file nse/nse_api.c | 22 | * @file nse/nse_api.c |
23 | * @brief api to get information from the network size estimation service | 23 | * @brief api to get information from the network size estimation service |
24 | * @author Nathan Evans | 24 | * @author Nathan Evans |
25 | * | ||
26 | * TODO: | ||
27 | */ | 25 | */ |
28 | #include "platform.h" | 26 | #include "platform.h" |
29 | #include "gnunet_client_lib.h" | 27 | #include "gnunet_client_lib.h" |
@@ -69,17 +67,6 @@ struct GNUNET_NSE_Handle | |||
69 | struct GNUNET_TIME_Relative reconnect_delay; | 67 | struct GNUNET_TIME_Relative reconnect_delay; |
70 | 68 | ||
71 | /** | 69 | /** |
72 | * Should this handle auto-destruct once all actions have | ||
73 | * been processed? | ||
74 | */ | ||
75 | int do_destroy; | ||
76 | |||
77 | /** | ||
78 | * Are we currently receiving from the service? | ||
79 | */ | ||
80 | int receiving; | ||
81 | |||
82 | /** | ||
83 | * Callback function to call when message is received. | 70 | * Callback function to call when message is received. |
84 | */ | 71 | */ |
85 | GNUNET_NSE_Callback recv_cb; | 72 | GNUNET_NSE_Callback recv_cb; |
@@ -93,45 +80,57 @@ struct GNUNET_NSE_Handle | |||
93 | 80 | ||
94 | 81 | ||
95 | /** | 82 | /** |
83 | * Try again to connect to network size estimation service. | ||
84 | * | ||
85 | * @param cls the handle to the transport service | ||
86 | * @param tc scheduler context | ||
87 | */ | ||
88 | static void | ||
89 | reconnect (void *cls, | ||
90 | const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
91 | |||
92 | |||
93 | /** | ||
96 | * Type of a function to call when we receive a message | 94 | * Type of a function to call when we receive a message |
97 | * from the service. | 95 | * from the service. |
98 | * | 96 | * |
99 | * @param cls closure | 97 | * @param cls closure |
100 | * @param msg message received, NULL on timeout or fatal error | 98 | * @param msg message received, NULL on timeout or fatal error |
101 | */ | 99 | */ |
102 | void message_handler (void *cls, | 100 | static void |
103 | const struct GNUNET_MessageHeader * msg) | 101 | message_handler (void *cls, |
102 | const struct GNUNET_MessageHeader * msg) | ||
104 | { | 103 | { |
105 | struct GNUNET_NSE_Handle *h = cls; | 104 | struct GNUNET_NSE_Handle *h = cls; |
106 | struct GNUNET_NSE_ClientMessage *client_msg; | 105 | const struct GNUNET_NSE_ClientMessage *client_msg; |
107 | |||
108 | if (msg == NULL) /* Error, timeout, death */ | ||
109 | return; | ||
110 | 106 | ||
111 | if ((ntohs (msg->size) < sizeof(struct GNUNET_NSE_ClientMessage)) | 107 | if (msg == NULL) |
112 | || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE)) | ||
113 | { | 108 | { |
114 | #if DEBUG_NSE | 109 | /* Error, timeout, death */ |
115 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 110 | GNUNET_CLIENT_disconnect (h->client, GNUNET_NO); |
116 | "%s: received incorrect message (size %d < %d) from service!", | 111 | h->client = NULL; |
117 | "NSE API", ntohs (msg->size), | 112 | h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, |
118 | sizeof(struct GNUNET_NSE_ClientMessage)); | 113 | &reconnect, |
119 | #endif | 114 | h); |
120 | return; | 115 | return; |
121 | } | 116 | } |
122 | 117 | if ( (ntohs (msg->size) != sizeof(struct GNUNET_NSE_ClientMessage)) || | |
123 | client_msg = (struct GNUNET_NSE_ClientMessage *)msg; | 118 | (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_NSE_ESTIMATE) ) |
124 | 119 | { | |
125 | h->recv_cb (h->recv_cb_cls, client_msg->size_estimate, | 120 | GNUNET_break (0); |
121 | return; | ||
122 | } | ||
123 | client_msg = (const struct GNUNET_NSE_ClientMessage *)msg; | ||
124 | h->recv_cb (h->recv_cb_cls, | ||
125 | client_msg->size_estimate, | ||
126 | client_msg->std_deviation); | 126 | client_msg->std_deviation); |
127 | |||
128 | GNUNET_CLIENT_receive (h->client, | 127 | GNUNET_CLIENT_receive (h->client, |
129 | &message_handler, h, GNUNET_TIME_UNIT_FOREVER_REL); | 128 | &message_handler, |
129 | h, | ||
130 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
130 | } | 131 | } |
131 | 132 | ||
132 | static void | 133 | |
133 | reconnect (void *cls, | ||
134 | const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
135 | 134 | ||
136 | /** | 135 | /** |
137 | * Reschedule a connect attempt to the service. | 136 | * Reschedule a connect attempt to the service. |
@@ -174,6 +173,7 @@ reschedule_connect (struct GNUNET_NSE_Handle *h) | |||
174 | } | 173 | } |
175 | } | 174 | } |
176 | 175 | ||
176 | |||
177 | /** | 177 | /** |
178 | * Transmit START message to service. | 178 | * Transmit START message to service. |
179 | * | 179 | * |
@@ -214,6 +214,7 @@ send_start (void *cls, size_t size, void *buf) | |||
214 | return sizeof (struct GNUNET_MessageHeader); | 214 | return sizeof (struct GNUNET_MessageHeader); |
215 | } | 215 | } |
216 | 216 | ||
217 | |||
217 | /** | 218 | /** |
218 | * Try again to connect to network size estimation service. | 219 | * Try again to connect to network size estimation service. |
219 | * | 220 | * |
@@ -250,6 +251,7 @@ reconnect (void *cls, | |||
250 | GNUNET_assert(h->th != NULL); | 251 | GNUNET_assert(h->th != NULL); |
251 | } | 252 | } |
252 | 253 | ||
254 | |||
253 | /** | 255 | /** |
254 | * Connect to the network size estimation service. | 256 | * Connect to the network size estimation service. |
255 | * | 257 | * |
@@ -265,11 +267,8 @@ GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
265 | { | 267 | { |
266 | struct GNUNET_NSE_Handle *ret; | 268 | struct GNUNET_NSE_Handle *ret; |
267 | 269 | ||
270 | GNUNET_assert (func != NULL); | ||
268 | ret = GNUNET_malloc (sizeof (struct GNUNET_NSE_Handle)); | 271 | ret = GNUNET_malloc (sizeof (struct GNUNET_NSE_Handle)); |
269 | |||
270 | if (func == NULL) | ||
271 | return NULL; | ||
272 | |||
273 | ret->cfg = cfg; | 272 | ret->cfg = cfg; |
274 | ret->recv_cb = func; | 273 | ret->recv_cb = func; |
275 | ret->recv_cb_cls = func_cls; | 274 | ret->recv_cb_cls = func_cls; |
@@ -278,11 +277,11 @@ GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
278 | return ret; | 277 | return ret; |
279 | } | 278 | } |
280 | 279 | ||
280 | |||
281 | /** | 281 | /** |
282 | * Disconnect from network size estimation service | 282 | * Disconnect from network size estimation service |
283 | * | 283 | * |
284 | * @param h handle to destroy | 284 | * @param h handle to destroy |
285 | * | ||
286 | */ | 285 | */ |
287 | void | 286 | void |
288 | GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h) | 287 | GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h) |
@@ -294,9 +293,16 @@ GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h) | |||
294 | h->reconnect_task = GNUNET_SCHEDULER_NO_TASK; | 293 | h->reconnect_task = GNUNET_SCHEDULER_NO_TASK; |
295 | } | 294 | } |
296 | if (h->th != NULL) | 295 | if (h->th != NULL) |
297 | GNUNET_CLIENT_notify_transmit_ready_cancel(h->th); | 296 | { |
297 | GNUNET_CLIENT_notify_transmit_ready_cancel(h->th); | ||
298 | h->th = NULL; | ||
299 | } | ||
298 | if (h->client != NULL) | 300 | if (h->client != NULL) |
299 | GNUNET_CLIENT_disconnect(h->client, GNUNET_NO); | 301 | { |
300 | 302 | GNUNET_CLIENT_disconnect(h->client, GNUNET_NO); | |
303 | h->client = NULL; | ||
304 | } | ||
301 | GNUNET_free(h); | 305 | GNUNET_free(h); |
302 | } | 306 | } |
307 | |||
308 | /* end of nse_api.c */ | ||