aboutsummaryrefslogtreecommitdiff
path: root/src/arm/test_exponential_backoff.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-05-11 13:16:00 +0000
committerChristian Grothoff <christian@grothoff.org>2010-05-11 13:16:00 +0000
commitf6bad19e765e73886a1e5b6d32f0a7551e5757ba (patch)
treea8ab99a78b91a0756f4f6cfc210a2f0083956b35 /src/arm/test_exponential_backoff.c
parentbd54a7aac7b1c0ae885568992fb25b816269ce89 (diff)
downloadgnunet-f6bad19e765e73886a1e5b6d32f0a7551e5757ba.tar.gz
gnunet-f6bad19e765e73886a1e5b6d32f0a7551e5757ba.zip
moving code where it belongs:
Diffstat (limited to 'src/arm/test_exponential_backoff.c')
-rw-r--r--src/arm/test_exponential_backoff.c211
1 files changed, 207 insertions, 4 deletions
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 */
56struct 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 */
105static void
106service_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 */
166void 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 */
186static size_t
187write_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 */
230static void
231arm_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
52static void 253static void
53arm_notify_stop (void *cls, int success) 254arm_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
144static void 347static void
145kill_task (void *cbData, 348kill_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