diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-01-10 23:40:03 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-01-10 23:40:03 +0000 |
commit | 69710b875141c1c2cc2eacd4221356d6e457b8c6 (patch) | |
tree | 6aaad2985b8057b1eee505ebe4b511037b4a810f /src/hostlist/test_gnunet_daemon_hostlist.c | |
parent | 85c435c57b151d4949288572ba964f5c98f67989 (diff) | |
download | gnunet-69710b875141c1c2cc2eacd4221356d6e457b8c6.tar.gz gnunet-69710b875141c1c2cc2eacd4221356d6e457b8c6.zip |
working on hostlist
Diffstat (limited to 'src/hostlist/test_gnunet_daemon_hostlist.c')
-rw-r--r-- | src/hostlist/test_gnunet_daemon_hostlist.c | 161 |
1 files changed, 127 insertions, 34 deletions
diff --git a/src/hostlist/test_gnunet_daemon_hostlist.c b/src/hostlist/test_gnunet_daemon_hostlist.c index ca7a2369a..66fbf35aa 100644 --- a/src/hostlist/test_gnunet_daemon_hostlist.c +++ b/src/hostlist/test_gnunet_daemon_hostlist.c | |||
@@ -40,12 +40,15 @@ | |||
40 | static int ok; | 40 | static int ok; |
41 | 41 | ||
42 | static struct GNUNET_SCHEDULER_Handle *sched; | 42 | static struct GNUNET_SCHEDULER_Handle *sched; |
43 | |||
44 | static GNUNET_SCHEDULER_TaskIdentifier timeout_task; | ||
43 | 45 | ||
44 | struct PeerContext | 46 | struct PeerContext |
45 | { | 47 | { |
46 | struct GNUNET_CONFIGURATION_Handle *cfg; | 48 | struct GNUNET_CONFIGURATION_Handle *cfg; |
47 | struct GNUNET_TRANSPORT_Handle *th; | 49 | struct GNUNET_TRANSPORT_Handle *th; |
48 | struct GNUNET_MessageHeader *hello; | 50 | struct GNUNET_MessageHeader *hello; |
51 | struct GNUNET_ARM_Handle *arm; | ||
49 | #if START_ARM | 52 | #if START_ARM |
50 | pid_t arm_pid; | 53 | pid_t arm_pid; |
51 | #endif | 54 | #endif |
@@ -57,6 +60,52 @@ static struct PeerContext p2; | |||
57 | 60 | ||
58 | 61 | ||
59 | 62 | ||
63 | /** | ||
64 | * Timeout, give up. | ||
65 | */ | ||
66 | static void | ||
67 | timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
68 | { | ||
69 | timeout_task = GNUNET_SCHEDULER_NO_TASK; | ||
70 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
71 | "Timeout trying to connect peers, test failed.\n"); | ||
72 | GNUNET_TRANSPORT_disconnect (p1.th); | ||
73 | p1.th = NULL; | ||
74 | GNUNET_TRANSPORT_disconnect (p2.th); | ||
75 | p2.th = NULL; | ||
76 | GNUNET_SCHEDULER_shutdown (sched); | ||
77 | } | ||
78 | |||
79 | |||
80 | /** | ||
81 | * Function called to notify transport users that another | ||
82 | * peer connected to us. | ||
83 | * | ||
84 | * @param cls closure | ||
85 | * @param peer the peer that connected | ||
86 | * @param latency current latency of the connection | ||
87 | */ | ||
88 | static void | ||
89 | notify_connect (void *cls, | ||
90 | const struct GNUNET_PeerIdentity * peer, | ||
91 | struct GNUNET_TIME_Relative latency) | ||
92 | { | ||
93 | if (peer == NULL) | ||
94 | return; | ||
95 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
96 | "Peers connected, shutting down.\n"); | ||
97 | GNUNET_assert (ok == 4); | ||
98 | ok = 0; | ||
99 | |||
100 | GNUNET_SCHEDULER_cancel (sched, | ||
101 | timeout_task); | ||
102 | GNUNET_TRANSPORT_disconnect (p1.th); | ||
103 | p1.th = NULL; | ||
104 | GNUNET_TRANSPORT_disconnect (p2.th); | ||
105 | p2.th = NULL; | ||
106 | } | ||
107 | |||
108 | |||
60 | static void | 109 | static void |
61 | process_hello (void *cls, | 110 | process_hello (void *cls, |
62 | struct GNUNET_TIME_Relative latency, | 111 | struct GNUNET_TIME_Relative latency, |
@@ -65,22 +114,13 @@ process_hello (void *cls, | |||
65 | { | 114 | { |
66 | struct PeerContext *p = cls; | 115 | struct PeerContext *p = cls; |
67 | 116 | ||
68 | GNUNET_assert (peer != NULL); | 117 | if (message == NULL) |
118 | return; | ||
69 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 119 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
70 | "Received (my) `%s' from transport service of `%4s'\n", | 120 | "Received HELLO, starting hostlist service.\n"); |
71 | "HELLO", GNUNET_i2s (peer)); | 121 | GNUNET_assert ( (ok >= 2) && (ok <= 3) ); |
72 | GNUNET_assert (message != NULL); | 122 | ok++; |
73 | p->hello = GNUNET_malloc (ntohs (message->size)); | 123 | GNUNET_ARM_start_services (p->cfg, sched, "hostlist", NULL); |
74 | memcpy (p->hello, message, ntohs (message->size)); | ||
75 | if ((p == &p1) && (p2.th != NULL)) | ||
76 | GNUNET_TRANSPORT_offer_hello (p2.th, message); | ||
77 | if ((p == &p2) && (p1.th != NULL)) | ||
78 | GNUNET_TRANSPORT_offer_hello (p1.th, message); | ||
79 | |||
80 | if ((p == &p1) && (p2.hello != NULL)) | ||
81 | GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello); | ||
82 | if ((p == &p2) && (p1.hello != NULL)) | ||
83 | GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello); | ||
84 | } | 124 | } |
85 | 125 | ||
86 | 126 | ||
@@ -98,13 +138,75 @@ setup_peer (struct PeerContext *p, const char *cfgname) | |||
98 | #endif | 138 | #endif |
99 | GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); | 139 | GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); |
100 | GNUNET_ARM_start_services (p->cfg, sched, "core", NULL); | 140 | GNUNET_ARM_start_services (p->cfg, sched, "core", NULL); |
101 | p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, NULL, NULL); | 141 | p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, |
142 | ¬ify_connect, NULL); | ||
102 | GNUNET_assert (p->th != NULL); | 143 | GNUNET_assert (p->th != NULL); |
103 | GNUNET_TRANSPORT_get_hello (p->th, TIMEOUT, &process_hello, p); | 144 | GNUNET_TRANSPORT_get_hello (p->th, TIMEOUT, &process_hello, p); |
104 | } | 145 | } |
105 | 146 | ||
106 | 147 | ||
107 | static void | 148 | static void |
149 | waitpid_task (void *cls, | ||
150 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
151 | { | ||
152 | struct PeerContext *p = cls; | ||
153 | |||
154 | #if START_ARM | ||
155 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
156 | "Killing ARM process.\n"); | ||
157 | if (0 != PLIBC_KILL (p->arm_pid, SIGTERM)) | ||
158 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
159 | if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK) | ||
160 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); | ||
161 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
162 | "ARM process %u stopped\n", p->arm_pid); | ||
163 | #endif | ||
164 | GNUNET_CONFIGURATION_destroy (p->cfg); | ||
165 | } | ||
166 | |||
167 | |||
168 | static void | ||
169 | stop_cb (void *cls, | ||
170 | int success) | ||
171 | { | ||
172 | struct PeerContext *p = cls; | ||
173 | |||
174 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
175 | success | ||
176 | ? "ARM stopped core service\n" | ||
177 | : "ARM failed to stop core service\n"); | ||
178 | GNUNET_ARM_disconnect (p->arm); | ||
179 | p->arm = NULL; | ||
180 | /* make sure this runs after all other tasks are done */ | ||
181 | GNUNET_SCHEDULER_add_delayed (sched, | ||
182 | GNUNET_TIME_UNIT_SECONDS, | ||
183 | &waitpid_task, p); | ||
184 | } | ||
185 | |||
186 | |||
187 | static void | ||
188 | stop_arm (struct PeerContext *p) | ||
189 | { | ||
190 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
191 | "Asking ARM to stop core service\n"); | ||
192 | p->arm = GNUNET_ARM_connect (p->cfg, sched, NULL); | ||
193 | GNUNET_ARM_stop_service (p->arm, "core", GNUNET_TIME_UNIT_SECONDS, | ||
194 | &stop_cb, p); | ||
195 | } | ||
196 | |||
197 | |||
198 | /** | ||
199 | * Try again to connect to transport service. | ||
200 | */ | ||
201 | static void | ||
202 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
203 | { | ||
204 | stop_arm (&p1); | ||
205 | stop_arm (&p2); | ||
206 | } | ||
207 | |||
208 | |||
209 | static void | ||
108 | run (void *cls, | 210 | run (void *cls, |
109 | struct GNUNET_SCHEDULER_Handle *s, | 211 | struct GNUNET_SCHEDULER_Handle *s, |
110 | char *const *args, | 212 | char *const *args, |
@@ -114,27 +216,20 @@ run (void *cls, | |||
114 | GNUNET_assert (ok == 1); | 216 | GNUNET_assert (ok == 1); |
115 | ok++; | 217 | ok++; |
116 | sched = s; | 218 | sched = s; |
219 | timeout_task = GNUNET_SCHEDULER_add_delayed (sched, | ||
220 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, | ||
221 | 15), | ||
222 | &timeout_error, | ||
223 | NULL); | ||
224 | GNUNET_SCHEDULER_add_delayed (sched, | ||
225 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
226 | &shutdown_task, | ||
227 | NULL); | ||
117 | setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf"); | 228 | setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf"); |
118 | setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf"); | 229 | setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf"); |
119 | } | 230 | } |
120 | 231 | ||
121 | 232 | ||
122 | static void | ||
123 | stop_arm (struct PeerContext *p) | ||
124 | { | ||
125 | GNUNET_ARM_stop_services (p->cfg, sched, "core", NULL); | ||
126 | #if START_ARM | ||
127 | if (0 != PLIBC_KILL (p->arm_pid, SIGTERM)) | ||
128 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
129 | if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK) | ||
130 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); | ||
131 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
132 | "ARM process %u stopped\n", p->arm_pid); | ||
133 | #endif | ||
134 | GNUNET_CONFIGURATION_destroy (p->cfg); | ||
135 | } | ||
136 | |||
137 | |||
138 | static int | 233 | static int |
139 | check () | 234 | check () |
140 | { | 235 | { |
@@ -152,8 +247,6 @@ check () | |||
152 | GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, | 247 | GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, |
153 | argv, "test-gnunet-daemon-hostlist", | 248 | argv, "test-gnunet-daemon-hostlist", |
154 | "nohelp", options, &run, &ok); | 249 | "nohelp", options, &run, &ok); |
155 | stop_arm (&p1); | ||
156 | stop_arm (&p2); | ||
157 | return ok; | 250 | return ok; |
158 | } | 251 | } |
159 | 252 | ||