aboutsummaryrefslogtreecommitdiff
path: root/src/hostlist/hostlist-server.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-01-11 11:10:45 +0000
committerChristian Grothoff <christian@grothoff.org>2010-01-11 11:10:45 +0000
commit1f88c0fbf4743362e0f94676298b19c6b049bbbe (patch)
treeb001af343524d8a8635480d865ebb1717d71cc1e /src/hostlist/hostlist-server.c
parent0149bd94fec6b6a9ece88b0747bd0febd02eddf5 (diff)
downloadgnunet-1f88c0fbf4743362e0f94676298b19c6b049bbbe.tar.gz
gnunet-1f88c0fbf4743362e0f94676298b19c6b049bbbe.zip
complete startup / shutdown code
Diffstat (limited to 'src/hostlist/hostlist-server.c')
-rw-r--r--src/hostlist/hostlist-server.c64
1 files changed, 54 insertions, 10 deletions
diff --git a/src/hostlist/hostlist-server.c b/src/hostlist/hostlist-server.c
index b15fdc408..f1819d4c7 100644
--- a/src/hostlist/hostlist-server.c
+++ b/src/hostlist/hostlist-server.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2008 Christian Grothoff (and other contributing authors) 3 (C) 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -56,6 +56,11 @@ static struct GNUNET_SCHEDULER_Handle *sched;
56static GNUNET_SCHEDULER_TaskIdentifier hostlist_task; 56static GNUNET_SCHEDULER_TaskIdentifier hostlist_task;
57 57
58/** 58/**
59 * Task that updates our HTTP response.
60 */
61static GNUNET_SCHEDULER_TaskIdentifier response_task;
62
63/**
59 * Our canonical response. 64 * Our canonical response.
60 */ 65 */
61static struct MHD_Response *response; 66static struct MHD_Response *response;
@@ -85,16 +90,29 @@ update_response (void *cls,
85static void 90static void
86finish_response (struct HostSet *results) 91finish_response (struct HostSet *results)
87{ 92{
93 struct GNUNET_TIME_Relative freq;
94
88 if (response != NULL) 95 if (response != NULL)
89 MHD_destroy_response (response); 96 MHD_destroy_response (response);
90 response = MHD_create_response_from_data (results->size, 97 response = MHD_create_response_from_data (results->size,
91 results->data, MHD_YES, MHD_NO); 98 results->data, MHD_YES, MHD_NO);
99 if (daemon_handle != NULL)
100 {
101 freq = RESPONSE_UPDATE_FREQUENCY;
102 if (results->size == 0)
103 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50);
104 /* schedule next update of the response */
105 response_task = GNUNET_SCHEDULER_add_delayed (sched,
106 freq,
107 &update_response,
108 NULL);
109 }
110 else
111 {
112 MHD_destroy_response (response);
113 response = NULL;
114 }
92 GNUNET_free (results); 115 GNUNET_free (results);
93 /* schedule next update of the response */
94 GNUNET_SCHEDULER_add_delayed (sched,
95 RESPONSE_UPDATE_FREQUENCY,
96 &update_response,
97 NULL);
98} 116}
99 117
100 118
@@ -137,6 +155,7 @@ update_response (void *cls,
137{ 155{
138 struct HostSet *results; 156 struct HostSet *results;
139 157
158 response_task = GNUNET_SCHEDULER_NO_TASK;
140 results = GNUNET_malloc(sizeof(struct HostSet)); 159 results = GNUNET_malloc(sizeof(struct HostSet));
141 GNUNET_PEERINFO_for_all (cfg, sched, 160 GNUNET_PEERINFO_for_all (cfg, sched,
142 NULL, 161 NULL,
@@ -157,6 +176,12 @@ static int
157accept_policy_callback (void *cls, 176accept_policy_callback (void *cls,
158 const struct sockaddr *addr, socklen_t addrlen) 177 const struct sockaddr *addr, socklen_t addrlen)
159{ 178{
179 if (NULL == response)
180 {
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182 "Received request for hostlist, but I am not yet ready; rejecting!\n");
183 return MHD_NO;
184 }
160 return MHD_YES; /* accept all */ 185 return MHD_YES; /* accept all */
161} 186}
162 187
@@ -309,6 +334,9 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
309 return GNUNET_SYSERR; 334 return GNUNET_SYSERR;
310 } 335 }
311 prepare_daemon (); 336 prepare_daemon ();
337 response_task = GNUNET_SCHEDULER_add_now (sched,
338 &update_response,
339 NULL);
312 return GNUNET_OK; 340 return GNUNET_OK;
313} 341}
314 342
@@ -318,10 +346,26 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
318void 346void
319GNUNET_HOSTLIST_server_stop () 347GNUNET_HOSTLIST_server_stop ()
320{ 348{
321 GNUNET_SCHEDULER_cancel (sched, hostlist_task); 349 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task)
322 hostlist_task = GNUNET_SCHEDULER_NO_TASK; 350 {
323 MHD_stop_daemon (daemon_handle); 351 GNUNET_SCHEDULER_cancel (sched, hostlist_task);
324 daemon_handle = NULL; 352 hostlist_task = GNUNET_SCHEDULER_NO_TASK;
353 }
354 if (GNUNET_SCHEDULER_NO_TASK != response_task)
355 {
356 GNUNET_SCHEDULER_cancel (sched, response_task);
357 response_task = GNUNET_SCHEDULER_NO_TASK;
358 }
359 if (NULL != daemon_handle)
360 {
361 MHD_stop_daemon (daemon_handle);
362 daemon_handle = NULL;
363 }
364 if (response != NULL)
365 {
366 MHD_destroy_response (response);
367 response = NULL;
368 }
325} 369}
326 370
327/* end of hostlist-server.c */ 371/* end of hostlist-server.c */