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/arm/arm_api.c | |
parent | bd54a7aac7b1c0ae885568992fb25b816269ce89 (diff) | |
download | gnunet-f6bad19e765e73886a1e5b6d32f0a7551e5757ba.tar.gz gnunet-f6bad19e765e73886a1e5b6d32f0a7551e5757ba.zip |
moving code where it belongs:
Diffstat (limited to 'src/arm/arm_api.c')
-rw-r--r-- | src/arm/arm_api.c | 206 |
1 files changed, 205 insertions, 1 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 */ |