aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_server_disconnect.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-06-22 16:30:43 +0000
committerChristian Grothoff <christian@grothoff.org>2010-06-22 16:30:43 +0000
commitd5b01c568df1eec766605aa5051e8bee254ae9ae (patch)
tree567e0c189d8640653a03a7229665a1ca1b61f3c8 /src/util/test_server_disconnect.c
parent1a363a9aaf1635a57c35b8ae615d53841d9445f7 (diff)
downloadgnunet-d5b01c568df1eec766605aa5051e8bee254ae9ae.tar.gz
gnunet-d5b01c568df1eec766605aa5051e8bee254ae9ae.zip
cleaning up server.c code and fixing mst bugs
Diffstat (limited to 'src/util/test_server_disconnect.c')
-rw-r--r--src/util/test_server_disconnect.c208
1 files changed, 76 insertions, 132 deletions
diff --git a/src/util/test_server_disconnect.c b/src/util/test_server_disconnect.c
index faaca968d..29134d38d 100644
--- a/src/util/test_server_disconnect.c
+++ b/src/util/test_server_disconnect.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors) 3 (C) 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
@@ -19,150 +19,84 @@
19*/ 19*/
20/** 20/**
21 * @file util/test_server_disconnect.c 21 * @file util/test_server_disconnect.c
22 * @brief tests for server.c and client.c, 22 * @brief tests for server.c, specifically GNUNET_SERVER_client_disconnect
23 * specifically client_disconnect
24 */ 23 */
25#include "platform.h" 24#include "platform.h"
26#include "gnunet_common.h" 25#include "gnunet_common.h"
27#include "gnunet_scheduler_lib.h"
28#include "gnunet_client_lib.h" 26#include "gnunet_client_lib.h"
27#include "gnunet_scheduler_lib.h"
29#include "gnunet_server_lib.h" 28#include "gnunet_server_lib.h"
30#include "gnunet_time_lib.h" 29#include "gnunet_time_lib.h"
31 30
32#define VERBOSE GNUNET_NO 31#define VERBOSE GNUNET_NO
33 32
34#define PORT 22335 33#define PORT 12435
35 34
36#define MY_TYPE 128 35#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
37 36
37#define MY_TYPE 128
38 38
39static struct GNUNET_SERVER_Handle *server; 39static struct GNUNET_SERVER_Handle *server;
40 40
41static struct GNUNET_CLIENT_Connection *client; 41static struct GNUNET_CLIENT_Connection *cc;
42
43static struct GNUNET_SCHEDULER_Handle *sched;
44 42
45static struct GNUNET_CONFIGURATION_Handle *cfg; 43static struct GNUNET_CONFIGURATION_Handle *cfg;
46 44
45static struct GNUNET_SCHEDULER_Handle *sched;
46
47static int ok; 47static int ok;
48 48
49static void
50send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
51{
52 struct GNUNET_SERVER_Client *argclient = cls;
53 GNUNET_assert (ok == 3);
54 ok++;
55 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
56}
57 49
58static void 50static void
59server_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 51finish_up (void *cls,
52 const struct GNUNET_SCHEDULER_TaskContext *tc)
60{ 53{
61 struct GNUNET_SERVER_Client *argclient = cls;
62 GNUNET_assert (ok == 5); 54 GNUNET_assert (ok == 5);
63 ok++;
64 GNUNET_SERVER_client_disconnect (argclient);
65}
66
67
68static void
69recv_cb (void *cls,
70 struct GNUNET_SERVER_Client *argclient,
71 const struct GNUNET_MessageHeader *message)
72{
73 void *addr;
74 size_t addrlen;
75 struct sockaddr_in sa;
76 struct sockaddr_in *have;
77
78 GNUNET_assert (GNUNET_OK ==
79 GNUNET_SERVER_client_get_address (argclient,
80 &addr, &addrlen));
81
82 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
83 have = addr;
84 memset (&sa, 0, sizeof (sa));
85#if HAVE_SOCKADDR_IN_SIN_LEN
86 sa.sin_len = sizeof (sa);
87#endif
88 sa.sin_family = AF_INET;
89 sa.sin_port = have->sin_port;
90 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
91 GNUNET_assert (0 == memcmp (&sa, addr, addrlen));
92 GNUNET_free (addr);
93 switch (ok)
94 {
95 case 2:
96 ok++;
97 GNUNET_SCHEDULER_add_delayed (sched,
98 GNUNET_TIME_relative_multiply
99 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
100 &send_done, argclient);
101 break;
102 case 4:
103 ok++;
104 GNUNET_SCHEDULER_add_delayed (sched,
105 GNUNET_TIME_relative_multiply
106 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
107 &server_disconnect, argclient);
108 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
109 break;
110 default:
111 GNUNET_assert (0);
112 }
113
114}
115
116static void
117disconnect_notify (void *cls, const struct GNUNET_MessageHeader *msg)
118{
119 GNUNET_assert (msg == NULL);
120 GNUNET_assert (ok == 7);
121 ok = 0; 55 ok = 0;
122 GNUNET_CLIENT_disconnect (client, GNUNET_YES);
123 client = NULL;
124 GNUNET_SERVER_destroy (server); 56 GNUNET_SERVER_destroy (server);
125 server = NULL; 57 GNUNET_CLIENT_disconnect (cc, GNUNET_NO);
126 GNUNET_CONFIGURATION_destroy (cfg); 58 GNUNET_CONFIGURATION_destroy (cfg);
127 cfg = NULL;
128} 59}
129 60
130 61
131/**
132 * Functions with this signature are called whenever a client
133 * is disconnected on the network level.
134 *
135 * @param cls closure
136 * @param client identification of the client
137 */
138static void 62static void
139notify_disconnect (void *cls, struct GNUNET_SERVER_Client *clientarg) 63notify_disconnect (void *cls, struct GNUNET_SERVER_Client *clientarg)
140{ 64{
141 if (clientarg == NULL) 65 if (clientarg == NULL)
142 return; 66 return;
143 GNUNET_assert (ok == 6); 67 GNUNET_assert (ok == 4);
144 ok++; 68 ok = 5;
145 GNUNET_CLIENT_receive (client, 69 GNUNET_SCHEDULER_add_now (sched,
146 &disconnect_notify, 70 &finish_up,
147 NULL, GNUNET_TIME_UNIT_FOREVER_REL); 71 NULL);
148} 72}
149 73
150 74
151static size_t 75static void
152notify_ready (void *cls, size_t size, void *buf) 76server_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
77{
78 struct GNUNET_SERVER_Client *argclient = cls;
79 GNUNET_assert (ok == 3);
80 ok = 4;
81 GNUNET_SERVER_client_disconnect (argclient);
82 GNUNET_SERVER_client_drop (argclient);
83}
84
85
86static void
87recv_cb (void *cls,
88 struct GNUNET_SERVER_Client *client,
89 const struct GNUNET_MessageHeader *message)
153{ 90{
154 struct GNUNET_MessageHeader *msg; 91 GNUNET_assert (ok == 2);
155 92 ok = 3;
156 GNUNET_assert (size >= 256); 93 GNUNET_SERVER_client_keep (client);
157 GNUNET_assert (1 == ok); 94 GNUNET_SCHEDULER_add_now (sched,
158 ok++; 95 &server_disconnect, client);
159 msg = buf; 96 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
160 msg->type = htons (MY_TYPE); 97 ntohs (message->size));
161 msg->size = htons (sizeof (struct GNUNET_MessageHeader)); 98 GNUNET_assert (MY_TYPE == ntohs (message->type));
162 msg++; 99 GNUNET_SERVER_receive_done (client, GNUNET_OK);
163 msg->type = htons (MY_TYPE);
164 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
165 return 2 * sizeof (struct GNUNET_MessageHeader);
166} 100}
167 101
168 102
@@ -172,6 +106,23 @@ static struct GNUNET_SERVER_MessageHandler handlers[] = {
172}; 106};
173 107
174 108
109static size_t
110transmit_initial_message (void *cls,
111 size_t size,
112 void *buf)
113{
114 struct GNUNET_MessageHeader msg;
115
116 GNUNET_assert (ok == 1);
117 ok = 2;
118 GNUNET_assert (size > sizeof (struct GNUNET_MessageHeader));
119 msg.type = htons (MY_TYPE);
120 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
121 memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
122 return sizeof (struct GNUNET_MessageHeader);
123}
124
125
175static void 126static void
176task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 127task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
177{ 128{
@@ -196,26 +147,26 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 sap, 147 sap,
197 slens, 148 slens,
198 1024, 149 1024,
199 GNUNET_TIME_relative_multiply 150 TIMEOUT,
200 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
201 GNUNET_NO); 151 GNUNET_NO);
202 GNUNET_assert (server != NULL); 152 GNUNET_assert (server != NULL);
203 handlers[0].callback_cls = cls;
204 GNUNET_SERVER_add_handlers (server, handlers); 153 GNUNET_SERVER_add_handlers (server, handlers);
205 GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls); 154 GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, NULL);
206 cfg = GNUNET_CONFIGURATION_create (); 155 cfg = GNUNET_CONFIGURATION_create ();
207 GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT); 156 GNUNET_CONFIGURATION_set_value_number (cfg, "test-server", "PORT", PORT);
208 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", 157 GNUNET_CONFIGURATION_set_value_string (cfg, "test-server", "HOSTNAME", "localhost");
209 "localhost"); 158 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", "localhost");
210 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME", 159 cc = GNUNET_CLIENT_connect (tc->sched,
211 "localhost"); 160 "test-server",
212 client = GNUNET_CLIENT_connect (tc->sched, "test", cfg); 161 cfg);
213 GNUNET_assert (client != NULL); 162 GNUNET_assert (cc != NULL);
214 GNUNET_CLIENT_notify_transmit_ready (client, 163 GNUNET_assert (NULL !=
215 256, 164 GNUNET_CLIENT_notify_transmit_ready (cc,
216 GNUNET_TIME_relative_multiply 165 sizeof (struct GNUNET_MessageHeader),
217 (GNUNET_TIME_UNIT_MILLISECONDS, 250), 166 TIMEOUT,
218 GNUNET_NO, &notify_ready, NULL); 167 GNUNET_YES,
168 &transmit_initial_message,
169 NULL));
219} 170}
220 171
221 172
@@ -226,9 +177,8 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
226static int 177static int
227check () 178check ()
228{ 179{
229
230 ok = 1; 180 ok = 1;
231 GNUNET_SCHEDULER_run (&task, NULL); 181 GNUNET_SCHEDULER_run (&task, &ok);
232 return ok; 182 return ok;
233} 183}
234 184
@@ -238,13 +188,7 @@ main (int argc, char *argv[])
238{ 188{
239 int ret = 0; 189 int ret = 0;
240 190
241 GNUNET_log_setup ("test_server_disconnect", 191 GNUNET_log_setup ("test_server_disconnect", "WARNING", NULL);
242#if VERBOSE
243 "DEBUG",
244#else
245 "WARNING",
246#endif
247 NULL);
248 ret += check (); 192 ret += check ();
249 193
250 return ret; 194 return ret;