ca615d37e433ae02076fe21ba3b252a224e22275
[oweals/gnunet.git] / src / set / test_mq_client.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file set/test_mq.c
23  * @brief tests for mq with connection client
24  */
25 /**
26  * @file util/test_server_with_client.c
27  * @brief tests for server.c and client.c,
28  *       specifically disconnect_notify,
29  *       client_get_address and receive_done (resume processing)
30  */
31 #include "platform.h"
32 #include "gnunet_common.h"
33 #include "gnunet_scheduler_lib.h"
34 #include "gnunet_client_lib.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_time_lib.h"
37 #include "mq.h"
38
39 #define PORT 23336
40
41 #define MY_TYPE 128
42
43
44 static struct GNUNET_SERVER_Handle *server;
45
46 static struct GNUNET_CLIENT_Connection *client;
47
48 static struct GNUNET_CONFIGURATION_Handle *cfg;
49
50 static int ok;
51
52 static int notify = GNUNET_NO;
53
54 static int received = 0;
55
56
57 static void
58 recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
59          const struct GNUNET_MessageHeader *message)
60 {
61   received++;
62
63   printf ("received\n");
64
65
66   if ((received == 2) && (GNUNET_YES == notify))
67   {
68     printf ("done\n");
69     GNUNET_SERVER_receive_done (argclient, GNUNET_NO);
70     return;
71   }
72
73   GNUNET_SERVER_receive_done (argclient, GNUNET_YES);
74 }
75
76
77 static void
78 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79 {
80   GNUNET_SERVER_destroy (server);
81   server = NULL;
82   GNUNET_CONFIGURATION_destroy (cfg);
83   cfg = NULL;
84 }
85
86
87 /**
88  * Functions with this signature are called whenever a client
89  * is disconnected on the network level.
90  *
91  * @param cls closure
92  * @param client identification of the client
93  */
94 static void
95 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
96 {
97   if (client == NULL)
98     return;
99   ok = 0;
100   GNUNET_SCHEDULER_add_now (&clean_up, NULL);
101 }
102
103
104 static struct GNUNET_SERVER_MessageHandler handlers[] = {
105   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
106   {NULL, NULL, 0, 0}
107 };
108
109 void send_cb (void *cls)
110 {
111   printf ("notify sent\n");
112   notify = GNUNET_YES;
113 }
114
115 void test_mq (struct GNUNET_CLIENT_Connection *client)
116 {
117   struct GNUNET_MQ_MessageQueue *mq;
118   struct GNUNET_MQ_Message *mqm;
119
120   /* FIXME: test handling responses */
121   mq = GNUNET_MQ_queue_for_connection_client (client, NULL, NULL);
122
123   mqm = GNUNET_MQ_msg_header (MY_TYPE);
124   GNUNET_MQ_send (mq, mqm);
125
126   mqm = GNUNET_MQ_msg_header (MY_TYPE);
127   GNUNET_MQ_notify_sent (mqm, send_cb, NULL);
128   GNUNET_MQ_send (mq, mqm);
129
130   /* FIXME: add a message that will be canceled */
131 }
132
133
134 static void
135 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 {
137   struct sockaddr_in sa;
138   struct sockaddr *sap[2];
139   socklen_t slens[2];
140
141   sap[0] = (struct sockaddr *) &sa;
142   slens[0] = sizeof (sa);
143   sap[1] = NULL;
144   slens[1] = 0;
145   memset (&sa, 0, sizeof (sa));
146 #if HAVE_SOCKADDR_IN_SIN_LEN
147   sa.sin_len = sizeof (sa);
148 #endif
149   sa.sin_family = AF_INET;
150   sa.sin_port = htons (PORT);
151   server =
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, &notify_disconnect, cls);
159   cfg = GNUNET_CONFIGURATION_create ();
160   GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
161   GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost");
162   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
163                                          "localhost");
164   client = GNUNET_CLIENT_connect ("test", cfg);
165   GNUNET_assert (client != NULL);
166
167   test_mq (client);
168 }
169
170
171 int
172 main (int argc, char *argv[])
173 {
174   GNUNET_log_setup ("test-mq-client",
175                     "INFO",
176                     NULL);
177   ok = 1;
178   GNUNET_SCHEDULER_run (&task, NULL);
179   return ok;
180 }
181