2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file util/test_server_with_client_unix.c
22 * @brief tests for server.c and client.c,
23 * specifically disconnect_notify,
24 * client_get_address and receive_done (resume processing)
27 #include "gnunet_common.h"
28 #include "gnunet_scheduler_lib.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_server_lib.h"
31 #include "gnunet_time_lib.h"
36 static struct GNUNET_SERVER_Handle *server;
38 static struct GNUNET_CLIENT_Connection *client;
40 static struct GNUNET_CONFIGURATION_Handle *cfg;
46 send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
48 struct GNUNET_SERVER_Client *argclient = cls;
50 GNUNET_assert (ok == 3);
52 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
57 recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
58 const struct GNUNET_MessageHeader *message)
64 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
65 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
66 &send_done, argclient);
70 GNUNET_CLIENT_disconnect (client);
71 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
81 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 GNUNET_SERVER_destroy (server);
85 GNUNET_CONFIGURATION_destroy (cfg);
91 * Functions with this signature are called whenever a client
92 * is disconnected on the network level.
95 * @param client identification of the client
98 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
102 GNUNET_assert (ok == 5);
104 GNUNET_SCHEDULER_add_now (&clean_up, NULL);
109 notify_ready (void *cls, size_t size, void *buf)
111 struct GNUNET_MessageHeader *msg;
113 GNUNET_assert (size >= 256);
114 GNUNET_assert (1 == ok);
117 msg->type = htons (MY_TYPE);
118 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
120 msg->type = htons (MY_TYPE);
121 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
122 return 2 * sizeof (struct GNUNET_MessageHeader);
126 static struct GNUNET_SERVER_MessageHandler handlers[] = {
127 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
133 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 struct sockaddr_un un;
136 const char *unixpath = "/tmp/testsock";
137 struct sockaddr *sap[2];
140 memset (&un, 0, sizeof (un));
141 un.sun_family = AF_UNIX;
142 strncpy(un.sun_path, unixpath, sizeof (un.sun_path) - 1);
143 #if HAVE_SOCKADDR_IN_SIN_LEN
144 un.sun_len = (u_char) sizeof (un);
147 sap[0] = (struct sockaddr *) &un;
148 slens[0] = sizeof (un);
152 GNUNET_SERVER_create (NULL, NULL, sap, slens,
153 GNUNET_TIME_relative_multiply
154 (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
155 GNUNET_assert (server != NULL);
156 handlers[0].callback_cls = cls;
157 GNUNET_SERVER_add_handlers (server, handlers);
158 GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, cls);
159 cfg = GNUNET_CONFIGURATION_create ();
161 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "UNIXPATH", unixpath);
162 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
165 client = GNUNET_CLIENT_connect ("test", cfg);
166 GNUNET_assert (client != NULL);
167 GNUNET_CLIENT_notify_transmit_ready (client, 256,
168 GNUNET_TIME_relative_multiply
169 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
170 GNUNET_NO, ¬ify_ready, NULL);
175 main (int argc, char *argv[])
177 GNUNET_log_setup ("test_server_with_client_unix",
181 GNUNET_SCHEDULER_run (&task, NULL);
185 /* end of test_server_with_client_unix.c */