diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-05-11 13:16:00 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-05-11 13:16:00 +0000 |
commit | f6bad19e765e73886a1e5b6d32f0a7551e5757ba (patch) | |
tree | a8ab99a78b91a0756f4f6cfc210a2f0083956b35 /src | |
parent | bd54a7aac7b1c0ae885568992fb25b816269ce89 (diff) | |
download | gnunet-f6bad19e765e73886a1e5b6d32f0a7551e5757ba.tar.gz gnunet-f6bad19e765e73886a1e5b6d32f0a7551e5757ba.zip |
moving code where it belongs:
Diffstat (limited to 'src')
-rw-r--r-- | src/arm/arm_api.c | 206 | ||||
-rw-r--r-- | src/arm/test_exponential_backoff.c | 211 | ||||
-rw-r--r-- | src/include/gnunet_client_lib.h | 23 | ||||
-rw-r--r-- | src/util/client.c | 202 | ||||
-rw-r--r-- | src/util/test_service.c | 18 |
5 files changed, 418 insertions, 242 deletions
diff --git a/src/arm/arm_api.c b/src/arm/arm_api.c index ee53fd87b..f15c9ab53 100644 --- a/src/arm/arm_api.c +++ b/src/arm/arm_api.c | |||
@@ -65,6 +65,206 @@ struct GNUNET_ARM_Handle | |||
65 | 65 | ||
66 | 66 | ||
67 | /** | 67 | /** |
68 | * Context for handling the shutdown of a service. | ||
69 | */ | ||
70 | struct ShutdownContext | ||
71 | { | ||
72 | /** | ||
73 | * Scheduler to be used to call continuation | ||
74 | */ | ||
75 | struct GNUNET_SCHEDULER_Handle *sched; | ||
76 | /** | ||
77 | * Connection to the service that is being shutdown. | ||
78 | */ | ||
79 | struct GNUNET_CLIENT_Connection *sock; | ||
80 | |||
81 | /** | ||
82 | * Time allowed for shutdown to happen. | ||
83 | */ | ||
84 | struct GNUNET_TIME_Absolute timeout; | ||
85 | |||
86 | /** | ||
87 | * Task set up to cancel the shutdown request on timeout. | ||
88 | */ | ||
89 | GNUNET_SCHEDULER_TaskIdentifier cancel_task; | ||
90 | |||
91 | /** | ||
92 | * Task to call once shutdown complete | ||
93 | */ | ||
94 | GNUNET_CLIENT_ShutdownTask cont; | ||
95 | |||
96 | /** | ||
97 | * Closure for shutdown continuation | ||
98 | */ | ||
99 | void *cont_cls; | ||
100 | |||
101 | /** | ||
102 | * We received a confirmation that the service will shut down. | ||
103 | */ | ||
104 | int confirmed; | ||
105 | |||
106 | }; | ||
107 | |||
108 | /** | ||
109 | * Handler receiving response to service shutdown requests. | ||
110 | * First call with NULL: service misbehaving, or something. | ||
111 | * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK: | ||
112 | * - service will shutdown | ||
113 | * Second call with NULL: | ||
114 | * - service has now really shut down. | ||
115 | * | ||
116 | * @param cls closure | ||
117 | * @param msg NULL, indicating socket closure. | ||
118 | */ | ||
119 | static void | ||
120 | service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg) | ||
121 | { | ||
122 | struct ShutdownContext *shutdown_ctx = cls; | ||
123 | |||
124 | if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES)) | ||
125 | { | ||
126 | /* Means the other side closed the connection and never confirmed a shutdown */ | ||
127 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
128 | "Service handle shutdown before ACK!\n"); | ||
129 | if (shutdown_ctx->cont != NULL) | ||
130 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); | ||
131 | GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task); | ||
132 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
133 | GNUNET_free(shutdown_ctx); | ||
134 | } | ||
135 | else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES)) | ||
136 | { | ||
137 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
138 | "Service shutdown complete.\n"); | ||
139 | if (shutdown_ctx->cont != NULL) | ||
140 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO); | ||
141 | |||
142 | GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task); | ||
143 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
144 | GNUNET_free(shutdown_ctx); | ||
145 | } | ||
146 | else | ||
147 | { | ||
148 | GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader)); | ||
149 | switch (ntohs(msg->type)) | ||
150 | { | ||
151 | case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK: | ||
152 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
153 | "Received confirmation for service shutdown.\n"); | ||
154 | shutdown_ctx->confirmed = GNUNET_YES; | ||
155 | GNUNET_CLIENT_receive (shutdown_ctx->sock, | ||
156 | &service_shutdown_handler, | ||
157 | shutdown_ctx, | ||
158 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
159 | break; | ||
160 | default: /* Fall through */ | ||
161 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
162 | "Service shutdown refused!\n"); | ||
163 | if (shutdown_ctx->cont != NULL) | ||
164 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES); | ||
165 | |||
166 | GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task); | ||
167 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
168 | GNUNET_free(shutdown_ctx); | ||
169 | break; | ||
170 | } | ||
171 | } | ||
172 | } | ||
173 | |||
174 | /** | ||
175 | * Shutting down took too long, cancel receive and return error. | ||
176 | * | ||
177 | * @param cls closure | ||
178 | * @param tc context information (why was this task triggered now) | ||
179 | */ | ||
180 | void service_shutdown_cancel (void *cls, | ||
181 | const struct GNUNET_SCHEDULER_TaskContext * tc) | ||
182 | { | ||
183 | struct ShutdownContext *shutdown_ctx = cls; | ||
184 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n"); | ||
185 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); | ||
186 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
187 | GNUNET_free(shutdown_ctx); | ||
188 | } | ||
189 | |||
190 | |||
191 | /** | ||
192 | * If possible, write a shutdown message to the target | ||
193 | * buffer and destroy the client connection. | ||
194 | * | ||
195 | * @param cls the "struct GNUNET_CLIENT_Connection" to destroy | ||
196 | * @param size number of bytes available in buf | ||
197 | * @param buf NULL on error, otherwise target buffer | ||
198 | * @return number of bytes written to buf | ||
199 | */ | ||
200 | static size_t | ||
201 | write_shutdown (void *cls, size_t size, void *buf) | ||
202 | { | ||
203 | struct GNUNET_MessageHeader *msg; | ||
204 | struct ShutdownContext *shutdown_ctx = cls; | ||
205 | |||
206 | if (size < sizeof (struct GNUNET_MessageHeader)) | ||
207 | { | ||
208 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
209 | _("Failed to transmit shutdown request to client.\n")); | ||
210 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); | ||
211 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
212 | GNUNET_free(shutdown_ctx); | ||
213 | return 0; /* client disconnected */ | ||
214 | } | ||
215 | |||
216 | GNUNET_CLIENT_receive (shutdown_ctx->sock, | ||
217 | &service_shutdown_handler, shutdown_ctx, | ||
218 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
219 | shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (shutdown_ctx->sched, | ||
220 | GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout), | ||
221 | &service_shutdown_cancel, | ||
222 | shutdown_ctx); | ||
223 | msg = (struct GNUNET_MessageHeader *) buf; | ||
224 | msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN); | ||
225 | msg->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
226 | return sizeof (struct GNUNET_MessageHeader); | ||
227 | } | ||
228 | |||
229 | |||
230 | /** | ||
231 | * Request that the service should shutdown. | ||
232 | * Afterwards, the connection will automatically be | ||
233 | * disconnected. Hence the "sock" should not | ||
234 | * be used by the caller after this call | ||
235 | * (calling this function frees "sock" after a while). | ||
236 | * | ||
237 | * @param sched the scheduler to use for calling shutdown continuation | ||
238 | * @param sock the socket connected to the service | ||
239 | * @param timeout how long to wait before giving up on transmission | ||
240 | * @param cont continuation to call once the service is really down | ||
241 | * @param cont_cls closure for continuation | ||
242 | * | ||
243 | */ | ||
244 | static void | ||
245 | arm_service_shutdown (struct GNUNET_SCHEDULER_Handle *sched, | ||
246 | struct GNUNET_CLIENT_Connection *sock, | ||
247 | struct GNUNET_TIME_Relative timeout, | ||
248 | GNUNET_CLIENT_ShutdownTask cont, | ||
249 | void *cont_cls) | ||
250 | { | ||
251 | struct ShutdownContext *shutdown_ctx; | ||
252 | shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext)); | ||
253 | shutdown_ctx->sched = sched; | ||
254 | shutdown_ctx->cont = cont; | ||
255 | shutdown_ctx->cont_cls = cont_cls; | ||
256 | shutdown_ctx->sock = sock; | ||
257 | shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout); | ||
258 | GNUNET_CLIENT_notify_transmit_ready (sock, | ||
259 | sizeof (struct | ||
260 | GNUNET_MessageHeader), | ||
261 | timeout, | ||
262 | GNUNET_NO, | ||
263 | &write_shutdown, shutdown_ctx); | ||
264 | } | ||
265 | |||
266 | |||
267 | /** | ||
68 | * Setup a context for communicating with ARM. Note that this | 268 | * Setup a context for communicating with ARM. Note that this |
69 | * can be done even if the ARM service is not yet running. | 269 | * can be done even if the ARM service is not yet running. |
70 | * | 270 | * |
@@ -498,7 +698,7 @@ GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h, | |||
498 | arm_shutdown_ctx = GNUNET_malloc(sizeof(struct ARM_ShutdownContext)); | 698 | arm_shutdown_ctx = GNUNET_malloc(sizeof(struct ARM_ShutdownContext)); |
499 | arm_shutdown_ctx->cb = cb; | 699 | arm_shutdown_ctx->cb = cb; |
500 | arm_shutdown_ctx->cb_cls = cb_cls; | 700 | arm_shutdown_ctx->cb_cls = cb_cls; |
501 | GNUNET_CLIENT_service_shutdown (h->sched, h->client, timeout, &arm_shutdown_callback, arm_shutdown_ctx); | 701 | arm_service_shutdown (h->sched, h->client, timeout, &arm_shutdown_callback, arm_shutdown_ctx); |
502 | h->client = NULL; | 702 | h->client = NULL; |
503 | return; | 703 | return; |
504 | } | 704 | } |
@@ -669,4 +869,8 @@ GNUNET_ARM_stop_services (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
669 | } | 869 | } |
670 | 870 | ||
671 | 871 | ||
872 | |||
873 | |||
874 | |||
875 | |||
672 | /* end of arm_api.c */ | 876 | /* end of arm_api.c */ |
diff --git a/src/arm/test_exponential_backoff.c b/src/arm/test_exponential_backoff.c index 8657563fc..0ff0ebdad 100644 --- a/src/arm/test_exponential_backoff.c +++ b/src/arm/test_exponential_backoff.c | |||
@@ -26,6 +26,7 @@ | |||
26 | #include "gnunet_client_lib.h" | 26 | #include "gnunet_client_lib.h" |
27 | #include "gnunet_configuration_lib.h" | 27 | #include "gnunet_configuration_lib.h" |
28 | #include "gnunet_program_lib.h" | 28 | #include "gnunet_program_lib.h" |
29 | #include "gnunet_protocols.h" | ||
29 | 30 | ||
30 | #define VERBOSE GNUNET_NO | 31 | #define VERBOSE GNUNET_NO |
31 | #define START_ARM GNUNET_YES | 32 | #define START_ARM GNUNET_YES |
@@ -49,6 +50,206 @@ static char *killLogFileName; | |||
49 | #endif | 50 | #endif |
50 | 51 | ||
51 | 52 | ||
53 | /** | ||
54 | * Context for handling the shutdown of a service. | ||
55 | */ | ||
56 | struct ShutdownContext | ||
57 | { | ||
58 | /** | ||
59 | * Scheduler to be used to call continuation | ||
60 | */ | ||
61 | struct GNUNET_SCHEDULER_Handle *sched; | ||
62 | /** | ||
63 | * Connection to the service that is being shutdown. | ||
64 | */ | ||
65 | struct GNUNET_CLIENT_Connection *sock; | ||
66 | |||
67 | /** | ||
68 | * Time allowed for shutdown to happen. | ||
69 | */ | ||
70 | struct GNUNET_TIME_Absolute timeout; | ||
71 | |||
72 | /** | ||
73 | * Task set up to cancel the shutdown request on timeout. | ||
74 | */ | ||
75 | GNUNET_SCHEDULER_TaskIdentifier cancel_task; | ||
76 | |||
77 | /** | ||
78 | * Task to call once shutdown complete | ||
79 | */ | ||
80 | GNUNET_CLIENT_ShutdownTask cont; | ||
81 | |||
82 | /** | ||
83 | * Closure for shutdown continuation | ||
84 | */ | ||
85 | void *cont_cls; | ||
86 | |||
87 | /** | ||
88 | * We received a confirmation that the service will shut down. | ||
89 | */ | ||
90 | int confirmed; | ||
91 | |||
92 | }; | ||
93 | |||
94 | /** | ||
95 | * Handler receiving response to service shutdown requests. | ||
96 | * First call with NULL: service misbehaving, or something. | ||
97 | * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK: | ||
98 | * - service will shutdown | ||
99 | * Second call with NULL: | ||
100 | * - service has now really shut down. | ||
101 | * | ||
102 | * @param cls closure | ||
103 | * @param msg NULL, indicating socket closure. | ||
104 | */ | ||
105 | static void | ||
106 | service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg) | ||
107 | { | ||
108 | struct ShutdownContext *shutdown_ctx = cls; | ||
109 | |||
110 | if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES)) | ||
111 | { | ||
112 | /* Means the other side closed the connection and never confirmed a shutdown */ | ||
113 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
114 | "Service handle shutdown before ACK!\n"); | ||
115 | if (shutdown_ctx->cont != NULL) | ||
116 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); | ||
117 | GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task); | ||
118 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
119 | GNUNET_free(shutdown_ctx); | ||
120 | } | ||
121 | else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES)) | ||
122 | { | ||
123 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
124 | "Service shutdown complete.\n"); | ||
125 | if (shutdown_ctx->cont != NULL) | ||
126 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO); | ||
127 | |||
128 | GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task); | ||
129 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
130 | GNUNET_free(shutdown_ctx); | ||
131 | } | ||
132 | else | ||
133 | { | ||
134 | GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader)); | ||
135 | switch (ntohs(msg->type)) | ||
136 | { | ||
137 | case GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK: | ||
138 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
139 | "Received confirmation for service shutdown.\n"); | ||
140 | shutdown_ctx->confirmed = GNUNET_YES; | ||
141 | GNUNET_CLIENT_receive (shutdown_ctx->sock, | ||
142 | &service_shutdown_handler, | ||
143 | shutdown_ctx, | ||
144 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
145 | break; | ||
146 | default: /* Fall through */ | ||
147 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
148 | "Service shutdown refused!\n"); | ||
149 | if (shutdown_ctx->cont != NULL) | ||
150 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES); | ||
151 | |||
152 | GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task); | ||
153 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
154 | GNUNET_free(shutdown_ctx); | ||
155 | break; | ||
156 | } | ||
157 | } | ||
158 | } | ||
159 | |||
160 | /** | ||
161 | * Shutting down took too long, cancel receive and return error. | ||
162 | * | ||
163 | * @param cls closure | ||
164 | * @param tc context information (why was this task triggered now) | ||
165 | */ | ||
166 | void service_shutdown_cancel (void *cls, | ||
167 | const struct GNUNET_SCHEDULER_TaskContext * tc) | ||
168 | { | ||
169 | struct ShutdownContext *shutdown_ctx = cls; | ||
170 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n"); | ||
171 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); | ||
172 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
173 | GNUNET_free(shutdown_ctx); | ||
174 | } | ||
175 | |||
176 | |||
177 | /** | ||
178 | * If possible, write a shutdown message to the target | ||
179 | * buffer and destroy the client connection. | ||
180 | * | ||
181 | * @param cls the "struct GNUNET_CLIENT_Connection" to destroy | ||
182 | * @param size number of bytes available in buf | ||
183 | * @param buf NULL on error, otherwise target buffer | ||
184 | * @return number of bytes written to buf | ||
185 | */ | ||
186 | static size_t | ||
187 | write_shutdown (void *cls, size_t size, void *buf) | ||
188 | { | ||
189 | struct GNUNET_MessageHeader *msg; | ||
190 | struct ShutdownContext *shutdown_ctx = cls; | ||
191 | |||
192 | if (size < sizeof (struct GNUNET_MessageHeader)) | ||
193 | { | ||
194 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
195 | _("Failed to transmit shutdown request to client.\n")); | ||
196 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); | ||
197 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
198 | GNUNET_free(shutdown_ctx); | ||
199 | return 0; /* client disconnected */ | ||
200 | } | ||
201 | |||
202 | GNUNET_CLIENT_receive (shutdown_ctx->sock, | ||
203 | &service_shutdown_handler, shutdown_ctx, | ||
204 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
205 | shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (shutdown_ctx->sched, | ||
206 | GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout), | ||
207 | &service_shutdown_cancel, | ||
208 | shutdown_ctx); | ||
209 | msg = (struct GNUNET_MessageHeader *) buf; | ||
210 | msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN); | ||
211 | msg->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
212 | return sizeof (struct GNUNET_MessageHeader); | ||
213 | } | ||
214 | |||
215 | |||
216 | /** | ||
217 | * Request that the service should shutdown. | ||
218 | * Afterwards, the connection will automatically be | ||
219 | * disconnected. Hence the "sock" should not | ||
220 | * be used by the caller after this call | ||
221 | * (calling this function frees "sock" after a while). | ||
222 | * | ||
223 | * @param sched the scheduler to use for calling shutdown continuation | ||
224 | * @param sock the socket connected to the service | ||
225 | * @param timeout how long to wait before giving up on transmission | ||
226 | * @param cont continuation to call once the service is really down | ||
227 | * @param cont_cls closure for continuation | ||
228 | * | ||
229 | */ | ||
230 | static void | ||
231 | arm_service_shutdown (struct GNUNET_SCHEDULER_Handle *sched, | ||
232 | struct GNUNET_CLIENT_Connection *sock, | ||
233 | struct GNUNET_TIME_Relative timeout, | ||
234 | GNUNET_CLIENT_ShutdownTask cont, | ||
235 | void *cont_cls) | ||
236 | { | ||
237 | struct ShutdownContext *shutdown_ctx; | ||
238 | shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext)); | ||
239 | shutdown_ctx->sched = sched; | ||
240 | shutdown_ctx->cont = cont; | ||
241 | shutdown_ctx->cont_cls = cont_cls; | ||
242 | shutdown_ctx->sock = sock; | ||
243 | shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout); | ||
244 | GNUNET_CLIENT_notify_transmit_ready (sock, | ||
245 | sizeof (struct | ||
246 | GNUNET_MessageHeader), | ||
247 | timeout, | ||
248 | GNUNET_NO, | ||
249 | &write_shutdown, shutdown_ctx); | ||
250 | } | ||
251 | |||
252 | |||
52 | static void | 253 | static void |
53 | arm_notify_stop (void *cls, int success) | 254 | arm_notify_stop (void *cls, int success) |
54 | { | 255 | { |
@@ -141,6 +342,8 @@ shutdown_cont (void *cls, int reason) | |||
141 | &do_test, | 342 | &do_test, |
142 | NULL); | 343 | NULL); |
143 | } | 344 | } |
345 | |||
346 | |||
144 | static void | 347 | static void |
145 | kill_task (void *cbData, | 348 | kill_task (void *cbData, |
146 | const struct GNUNET_SCHEDULER_TaskContext *tc) | 349 | const struct GNUNET_SCHEDULER_TaskContext *tc) |
@@ -178,10 +381,10 @@ kill_task (void *cbData, | |||
178 | } | 381 | } |
179 | 382 | ||
180 | /* Use the created connection to kill the doNothingTask */ | 383 | /* Use the created connection to kill the doNothingTask */ |
181 | GNUNET_CLIENT_service_shutdown(sched, | 384 | arm_service_shutdown(sched, |
182 | doNothingConnection, | 385 | doNothingConnection, |
183 | TIMEOUT, | 386 | TIMEOUT, |
184 | &shutdown_cont, NULL); | 387 | &shutdown_cont, NULL); |
185 | } | 388 | } |
186 | 389 | ||
187 | 390 | ||
diff --git a/src/include/gnunet_client_lib.h b/src/include/gnunet_client_lib.h index 56f6915a4..49ccc6c6b 100644 --- a/src/include/gnunet_client_lib.h +++ b/src/include/gnunet_client_lib.h | |||
@@ -207,29 +207,6 @@ GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock, | |||
207 | void *rn_cls); | 207 | void *rn_cls); |
208 | 208 | ||
209 | 209 | ||
210 | |||
211 | /** | ||
212 | * Request that the service should shutdown. | ||
213 | * Afterwards, the connection will automatically be | ||
214 | * disconnected. Hence the "sock" shoud not | ||
215 | * be used by the caller after this call | ||
216 | * (calling this function frees "sock" after a while). | ||
217 | * | ||
218 | * @param sched the scheduler to use for calling shutdown continuation | ||
219 | * @param sock the socket connected to the service | ||
220 | * @param timeout how long to wait before giving up on transmission | ||
221 | * @param cont continuation to call once the service is really down | ||
222 | * @param cont_cls closure for continuation | ||
223 | * | ||
224 | */ | ||
225 | void | ||
226 | GNUNET_CLIENT_service_shutdown (struct GNUNET_SCHEDULER_Handle *sched, | ||
227 | struct GNUNET_CLIENT_Connection *sock, | ||
228 | struct GNUNET_TIME_Relative timeout, | ||
229 | GNUNET_CLIENT_ShutdownTask cont, | ||
230 | void *cont_cls); | ||
231 | |||
232 | |||
233 | /** | 210 | /** |
234 | * Wait until the service is running. | 211 | * Wait until the service is running. |
235 | * | 212 | * |
diff --git a/src/util/client.c b/src/util/client.c index c87d239e6..8a93bae9b 100644 --- a/src/util/client.c +++ b/src/util/client.c | |||
@@ -137,47 +137,6 @@ struct TransmitGetResponseContext | |||
137 | }; | 137 | }; |
138 | 138 | ||
139 | /** | 139 | /** |
140 | * Context for handling the shutdown of a service. | ||
141 | */ | ||
142 | struct ShutdownContext | ||
143 | { | ||
144 | /** | ||
145 | * Scheduler to be used to call continuation | ||
146 | */ | ||
147 | struct GNUNET_SCHEDULER_Handle *sched; | ||
148 | /** | ||
149 | * Connection to the service that is being shutdown. | ||
150 | */ | ||
151 | struct GNUNET_CLIENT_Connection *sock; | ||
152 | |||
153 | /** | ||
154 | * Time allowed for shutdown to happen. | ||
155 | */ | ||
156 | struct GNUNET_TIME_Absolute timeout; | ||
157 | |||
158 | /** | ||
159 | * Task set up to cancel the shutdown request on timeout. | ||
160 | */ | ||
161 | GNUNET_SCHEDULER_TaskIdentifier cancel_task; | ||
162 | |||
163 | /** | ||
164 | * Task to call once shutdown complete | ||
165 | */ | ||
166 | GNUNET_CLIENT_ShutdownTask cont; | ||
167 | |||
168 | /** | ||
169 | * Closure for shutdown continuation | ||
170 | */ | ||
171 | void *cont_cls; | ||
172 | |||
173 | /** | ||
174 | * We received a confirmation that the service will shut down. | ||
175 | */ | ||
176 | int confirmed; | ||
177 | |||
178 | }; | ||
179 | |||
180 | /** | ||
181 | * Struct to refer to a GNUnet TCP connection. | 140 | * Struct to refer to a GNUnet TCP connection. |
182 | * This is more than just a socket because if the server | 141 | * This is more than just a socket because if the server |
183 | * drops the connection, the client automatically tries | 142 | * drops the connection, the client automatically tries |
@@ -581,167 +540,6 @@ GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock, | |||
581 | 540 | ||
582 | 541 | ||
583 | /** | 542 | /** |
584 | * Handler receiving response to service shutdown requests. | ||
585 | * First call with NULL: service misbehaving, or something. | ||
586 | * First call with GNUNET_MESSAGE_TYPE_SHUTDOWN_ACK: | ||
587 | * - service will shutdown | ||
588 | * First call with GNUNET_MESSAGE_TYPE_SHUTDOWN_REFUSE: | ||
589 | * - service will not be stopped! | ||
590 | * | ||
591 | * Second call with NULL: | ||
592 | * - service has now really shut down. | ||
593 | * | ||
594 | * @param cls closure | ||
595 | * @param msg NULL, indicating socket closure. | ||
596 | */ | ||
597 | static void | ||
598 | service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg) | ||
599 | { | ||
600 | struct ShutdownContext *shutdown_ctx = cls; | ||
601 | |||
602 | if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES)) | ||
603 | { | ||
604 | /* Means the other side closed the connection and never confirmed a shutdown */ | ||
605 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
606 | "Service handle shutdown before ACK!\n"); | ||
607 | if (shutdown_ctx->cont != NULL) | ||
608 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); | ||
609 | GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task); | ||
610 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
611 | GNUNET_free(shutdown_ctx); | ||
612 | } | ||
613 | else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES)) | ||
614 | { | ||
615 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
616 | "Service shutdown complete.\n"); | ||
617 | if (shutdown_ctx->cont != NULL) | ||
618 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO); | ||
619 | |||
620 | GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task); | ||
621 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
622 | GNUNET_free(shutdown_ctx); | ||
623 | } | ||
624 | else | ||
625 | { | ||
626 | GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader)); | ||
627 | switch (ntohs(msg->type)) | ||
628 | { | ||
629 | case GNUNET_MESSAGE_TYPE_SHUTDOWN_ACK: | ||
630 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
631 | "Received confirmation for service shutdown.\n"); | ||
632 | shutdown_ctx->confirmed = GNUNET_YES; | ||
633 | GNUNET_CLIENT_receive (shutdown_ctx->sock, | ||
634 | &service_shutdown_handler, | ||
635 | shutdown_ctx, | ||
636 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
637 | break; | ||
638 | default: /* Fall through */ | ||
639 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | ||
640 | "Service shutdown refused!\n"); | ||
641 | if (shutdown_ctx->cont != NULL) | ||
642 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES); | ||
643 | |||
644 | GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task); | ||
645 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
646 | GNUNET_free(shutdown_ctx); | ||
647 | break; | ||
648 | } | ||
649 | } | ||
650 | } | ||
651 | |||
652 | /** | ||
653 | * Shutting down took too long, cancel receive and return error. | ||
654 | * | ||
655 | * @param cls closure | ||
656 | * @param tc context information (why was this task triggered now) | ||
657 | */ | ||
658 | void service_shutdown_cancel (void *cls, | ||
659 | const struct GNUNET_SCHEDULER_TaskContext * tc) | ||
660 | { | ||
661 | struct ShutdownContext *shutdown_ctx = cls; | ||
662 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n"); | ||
663 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); | ||
664 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
665 | GNUNET_free(shutdown_ctx); | ||
666 | } | ||
667 | |||
668 | |||
669 | /** | ||
670 | * If possible, write a shutdown message to the target | ||
671 | * buffer and destroy the client connection. | ||
672 | * | ||
673 | * @param cls the "struct GNUNET_CLIENT_Connection" to destroy | ||
674 | * @param size number of bytes available in buf | ||
675 | * @param buf NULL on error, otherwise target buffer | ||
676 | * @return number of bytes written to buf | ||
677 | */ | ||
678 | static size_t | ||
679 | write_shutdown (void *cls, size_t size, void *buf) | ||
680 | { | ||
681 | struct GNUNET_MessageHeader *msg; | ||
682 | struct ShutdownContext *shutdown_ctx = cls; | ||
683 | |||
684 | if (size < sizeof (struct GNUNET_MessageHeader)) | ||
685 | { | ||
686 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
687 | _("Failed to transmit shutdown request to client.\n")); | ||
688 | shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR); | ||
689 | GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO); | ||
690 | GNUNET_free(shutdown_ctx); | ||
691 | return 0; /* client disconnected */ | ||
692 | } | ||
693 | |||
694 | GNUNET_CLIENT_receive (shutdown_ctx->sock, | ||
695 | &service_shutdown_handler, shutdown_ctx, | ||
696 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
697 | shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (shutdown_ctx->sched, | ||
698 | GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout), | ||
699 | &service_shutdown_cancel, | ||
700 | shutdown_ctx); | ||
701 | msg = (struct GNUNET_MessageHeader *) buf; | ||
702 | msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN); | ||
703 | msg->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
704 | return sizeof (struct GNUNET_MessageHeader); | ||
705 | } | ||
706 | |||
707 | |||
708 | /** | ||
709 | * Request that the service should shutdown. | ||
710 | * Afterwards, the connection will automatically be | ||
711 | * disconnected. Hence the "sock" should not | ||
712 | * be used by the caller after this call | ||
713 | * (calling this function frees "sock" after a while). | ||
714 | * | ||
715 | * @param sched the scheduler to use for calling shutdown continuation | ||
716 | * @param sock the socket connected to the service | ||
717 | * @param timeout how long to wait before giving up on transmission | ||
718 | * @param cont continuation to call once the service is really down | ||
719 | * @param cont_cls closure for continuation | ||
720 | * | ||
721 | */ | ||
722 | void | ||
723 | GNUNET_CLIENT_service_shutdown (struct GNUNET_SCHEDULER_Handle *sched, | ||
724 | struct GNUNET_CLIENT_Connection *sock, | ||
725 | struct GNUNET_TIME_Relative timeout, | ||
726 | GNUNET_CLIENT_ShutdownTask cont, | ||
727 | void *cont_cls) | ||
728 | { | ||
729 | struct ShutdownContext *shutdown_ctx; | ||
730 | shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext)); | ||
731 | shutdown_ctx->sched = sched; | ||
732 | shutdown_ctx->cont = cont; | ||
733 | shutdown_ctx->cont_cls = cont_cls; | ||
734 | shutdown_ctx->sock = sock; | ||
735 | shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout); | ||
736 | GNUNET_CONNECTION_notify_transmit_ready (sock->sock, | ||
737 | sizeof (struct | ||
738 | GNUNET_MessageHeader), | ||
739 | timeout, | ||
740 | &write_shutdown, shutdown_ctx); | ||
741 | } | ||
742 | |||
743 | |||
744 | /** | ||
745 | * Report service unavailable. | 543 | * Report service unavailable. |
746 | */ | 544 | */ |
747 | static void | 545 | static void |
diff --git a/src/util/test_service.c b/src/util/test_service.c index 544f86fba..5f3146205 100644 --- a/src/util/test_service.c +++ b/src/util/test_service.c | |||
@@ -43,24 +43,18 @@ static struct GNUNET_SERVICE_Context *sctx; | |||
43 | 43 | ||
44 | static int ok = 1; | 44 | static int ok = 1; |
45 | 45 | ||
46 | void | ||
47 | end_cont (void *cls, | ||
48 | int reason) | ||
49 | { | ||
50 | if (sctx != NULL) | ||
51 | GNUNET_SERVICE_stop (sctx); | ||
52 | else | ||
53 | GNUNET_SCHEDULER_shutdown (sched); | ||
54 | ok = 0; | ||
55 | } | ||
56 | |||
57 | static void | 46 | static void |
58 | end_it (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 47 | end_it (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
59 | { | 48 | { |
60 | struct GNUNET_CLIENT_Connection *client = cls; | 49 | struct GNUNET_CLIENT_Connection *client = cls; |
61 | 50 | ||
62 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down service\n"); | 51 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down service\n"); |
63 | GNUNET_CLIENT_service_shutdown (sched, client, GNUNET_TIME_UNIT_FOREVER_REL, &end_cont, NULL); | 52 | GNUNET_CLIENT_disconnect (client, GNUNET_NO); |
53 | if (sctx != NULL) | ||
54 | { | ||
55 | GNUNET_SERVICE_stop (sctx); | ||
56 | sctx = NULL; | ||
57 | } | ||
64 | } | 58 | } |
65 | 59 | ||
66 | 60 | ||