2 This file is part of GNUnet.
3 Copyright (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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_util_lib.h"
32 static struct GNUNET_SERVER_Handle *server;
34 static struct GNUNET_CLIENT_Connection *client;
36 static struct GNUNET_CONFIGURATION_Handle *cfg;
42 send_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
44 struct GNUNET_SERVER_Client *argclient = cls;
46 GNUNET_assert (ok == 3);
48 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
53 recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
54 const struct GNUNET_MessageHeader *message)
60 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
61 (GNUNET_TIME_UNIT_MILLISECONDS, 50),
62 &send_done, argclient);
66 GNUNET_CLIENT_disconnect (client);
67 GNUNET_SERVER_receive_done (argclient, GNUNET_OK);
77 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79 GNUNET_SERVER_destroy (server);
81 GNUNET_CONFIGURATION_destroy (cfg);
87 * Functions with this signature are called whenever a client
88 * is disconnected on the network level.
91 * @param client identification of the client
94 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
98 GNUNET_assert (ok == 5);
100 GNUNET_SCHEDULER_add_now (&clean_up, NULL);
105 notify_ready (void *cls, size_t size, void *buf)
107 struct GNUNET_MessageHeader *msg;
109 GNUNET_assert (size >= 256);
110 GNUNET_assert (1 == ok);
113 msg->type = htons (MY_TYPE);
114 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
116 msg->type = htons (MY_TYPE);
117 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
118 return 2 * sizeof (struct GNUNET_MessageHeader);
122 static struct GNUNET_SERVER_MessageHandler handlers[] = {
123 {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
129 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
131 struct sockaddr_un un;
132 const char *unixpath = "/tmp/testsock";
133 struct sockaddr *sap[2];
136 memset (&un, 0, sizeof (un));
137 un.sun_family = AF_UNIX;
138 strncpy(un.sun_path, unixpath, sizeof (un.sun_path) - 1);
139 #if HAVE_SOCKADDR_IN_SIN_LEN
140 un.sun_len = (u_char) sizeof (un);
143 sap[0] = (struct sockaddr *) &un;
144 slens[0] = sizeof (un);
148 GNUNET_SERVER_create (NULL, NULL, sap, slens,
149 GNUNET_TIME_relative_multiply
150 (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
151 GNUNET_assert (server != NULL);
152 handlers[0].callback_cls = cls;
153 GNUNET_SERVER_add_handlers (server, handlers);
154 GNUNET_SERVER_disconnect_notify (server, ¬ify_disconnect, cls);
155 cfg = GNUNET_CONFIGURATION_create ();
157 GNUNET_CONFIGURATION_set_value_string (cfg, "test", "UNIXPATH", unixpath);
158 GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
161 client = GNUNET_CLIENT_connect ("test", cfg);
162 GNUNET_assert (client != NULL);
163 GNUNET_CLIENT_notify_transmit_ready (client, 256,
164 GNUNET_TIME_relative_multiply
165 (GNUNET_TIME_UNIT_MILLISECONDS, 250),
166 GNUNET_NO, ¬ify_ready, NULL);
171 main (int argc, char *argv[])
173 GNUNET_log_setup ("test_server_with_client_unix",
177 GNUNET_SCHEDULER_run (&task, NULL);
181 /* end of test_server_with_client_unix.c */