- add user feedback
[oweals/gnunet.git] / src / util / 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 util/test_mq_client.c
23  * @brief tests for mq with connection client
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27
28 #define PORT 23336
29
30 #define MY_TYPE 128
31
32
33 static struct GNUNET_SERVER_Handle *server;
34
35 static struct GNUNET_CLIENT_Connection *client;
36
37 static struct GNUNET_CONFIGURATION_Handle *cfg;
38
39 static int ok;
40
41 static int notify = GNUNET_NO;
42
43 static int received = 0;
44
45
46 static void
47 recv_cb (void *cls, struct GNUNET_SERVER_Client *argclient,
48          const struct GNUNET_MessageHeader *message)
49 {
50   received++;
51   if (received == 2)
52   {
53     GNUNET_SERVER_receive_done (argclient, GNUNET_NO);
54     return;
55   }
56
57   /* can happen if notify does not work */
58   GNUNET_assert (received < 2);
59
60   GNUNET_SERVER_receive_done (argclient, GNUNET_YES);
61 }
62
63
64 static void
65 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
66 {
67   GNUNET_SERVER_destroy (server);
68   server = NULL;
69   GNUNET_CONFIGURATION_destroy (cfg);
70   cfg = NULL;
71 }
72
73
74 /**
75  * Functions with this signature are called whenever a client
76  * is disconnected on the network level.
77  *
78  * @param cls closure
79  * @param client identification of the client
80  */
81 static void
82 notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
83 {
84   if (client == NULL)
85     return;
86   ok = 0;
87   GNUNET_SCHEDULER_add_now (&clean_up, NULL);
88 }
89
90
91 static struct GNUNET_SERVER_MessageHandler handlers[] = {
92   {&recv_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
93   {NULL, NULL, 0, 0}
94 };
95
96
97 static void
98 send_cb (void *cls)
99 {
100   /* the notify should only be called once */
101   GNUNET_assert (GNUNET_NO == notify);
102   notify = GNUNET_YES;
103 }
104
105
106 static void
107 test_mq (struct GNUNET_CLIENT_Connection *client)
108 {
109   struct GNUNET_MQ_Handle *mq;
110   struct GNUNET_MQ_Envelope *mqm;
111
112   /* FIXME: test handling responses */
113   mq = GNUNET_MQ_queue_for_connection_client (client, NULL, NULL, NULL);
114
115   mqm = GNUNET_MQ_msg_header (MY_TYPE);
116   GNUNET_MQ_send (mq, mqm);
117
118   mqm = GNUNET_MQ_msg_header (MY_TYPE);
119   GNUNET_MQ_notify_sent (mqm, send_cb, NULL);
120   GNUNET_MQ_send (mq, mqm);
121
122   /* FIXME: add a message that will be canceled */
123 }
124
125
126 static void
127 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
128 {
129   struct sockaddr_in sa;
130   struct sockaddr *sap[2];
131   socklen_t slens[2];
132
133   sap[0] = (struct sockaddr *) &sa;
134   slens[0] = sizeof (sa);
135   sap[1] = NULL;
136   slens[1] = 0;
137   memset (&sa, 0, sizeof (sa));
138 #if HAVE_SOCKADDR_IN_SIN_LEN
139   sa.sin_len = sizeof (sa);
140 #endif
141   sa.sin_family = AF_INET;
142   sa.sin_port = htons (PORT);
143   server =
144       GNUNET_SERVER_create (NULL, NULL, sap, slens,
145                             GNUNET_TIME_relative_multiply
146                             (GNUNET_TIME_UNIT_MILLISECONDS, 250), GNUNET_NO);
147   GNUNET_assert (server != NULL);
148   handlers[0].callback_cls = cls;
149   GNUNET_SERVER_add_handlers (server, handlers);
150   GNUNET_SERVER_disconnect_notify (server, &notify_disconnect, cls);
151   cfg = GNUNET_CONFIGURATION_create ();
152   GNUNET_CONFIGURATION_set_value_number (cfg, "test", "PORT", PORT);
153   GNUNET_CONFIGURATION_set_value_string (cfg, "test", "HOSTNAME", "localhost");
154   GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
155                                          "localhost");
156   client = GNUNET_CLIENT_connect ("test", cfg);
157   GNUNET_assert (client != NULL);
158
159   test_mq (client);
160 }
161
162
163 int
164 main (int argc, char *argv[])
165 {
166   GNUNET_log_setup ("test-mq-client",
167                     "INFO",
168                     NULL);
169   ok = 1;
170   GNUNET_SCHEDULER_run (&task, NULL);
171   GNUNET_assert (GNUNET_YES == notify);
172   return ok;
173 }
174