37f50c4fcacb66f4412f91db872f6b2ece4b4116
[oweals/gnunet.git] / src / transport / test_transport_api_monitor_peers.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file transport/test_transport_api_monitor_peers.c
22  * @brief base test case for transport peer monitor API
23  */
24 #include "platform.h"
25 #include "gnunet_transport_service.h"
26 #include "transport-testing.h"
27
28 /**
29  * How long until we give up on transmitting the message?
30  */
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
32
33 /**
34  * How long until we give up on transmitting the message?
35  */
36 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37
38 #define TEST_MESSAGE_SIZE 2600
39
40 #define TEST_MESSAGE_TYPE 12345
41
42 static struct GNUNET_TRANSPORT_TransmitHandle *th;
43
44 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
45
46 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p1;
47
48 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p2;
49
50 static int p1_c;
51
52 static int p2_c;
53
54 static int p1_c_notify;
55
56 static int p2_c_notify;
57
58
59 static void
60 custom_shutdown (void *cls)
61 {
62   if (th != NULL)
63   {
64     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
65     th = NULL;
66   }
67   if (NULL != pmc_p1)
68   {
69     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
70     pmc_p1 = NULL;
71   }
72   if (NULL != pmc_p2)
73   {
74     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
75     pmc_p2 = NULL;
76   }
77 }
78
79
80 static void
81 notify_receive (void *cls,
82                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
83                 const struct GNUNET_PeerIdentity *sender,
84                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
85 {
86   char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
87
88   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
89               "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
90               receiver->no,
91               ps,
92               ntohs (message->header.type),
93               ntohs (message->header.size),
94               GNUNET_i2s (sender));
95   GNUNET_free (ps);
96 }
97
98
99 static void
100 sendtask (void *cls)
101 {
102   /* intentionally empty */
103 }
104
105
106 static void
107 check_done ()
108 {
109   if ( (GNUNET_YES == p1_c) &&
110        (GNUNET_YES == p2_c) &&
111        p1_c_notify &&
112        p2_c_notify)
113   {
114     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115                 "Both peers state to be connected\n");
116     ccc->global_ret = GNUNET_OK;
117     GNUNET_SCHEDULER_shutdown ();
118   }
119 }
120
121
122 static void
123 notify_connect (void *cls,
124                 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
125                 const struct GNUNET_PeerIdentity *other)
126 {
127   GNUNET_TRANSPORT_TESTING_log_connect (cls,
128                                         me,
129                                         other);
130   if (0 == memcmp (other, &ccc->p[0]->id, sizeof (struct GNUNET_PeerIdentity)))
131   {
132     p1_c_notify = GNUNET_YES;
133   }
134   if (0 == memcmp (other, &ccc->p[1]->id, sizeof (struct GNUNET_PeerIdentity)))
135   {
136     p2_c_notify = GNUNET_YES;
137   }
138   check_done ();
139 }
140
141
142 static void
143 notify_disconnect (void *cls,
144                    struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
145                    const struct GNUNET_PeerIdentity *other)
146 {
147   GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
148                                            me,
149                                            other);
150   if (NULL != th)
151   {
152     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
153     th = NULL;
154   }
155 }
156
157
158 static void
159 monitor1_cb (void *cls,
160              const struct GNUNET_PeerIdentity *peer,
161              const struct GNUNET_HELLO_Address *address,
162              enum GNUNET_TRANSPORT_PeerState state,
163              struct GNUNET_TIME_Absolute state_timeout)
164 {
165   if ((NULL == address) || (NULL == ccc->p[0]))
166     return;
167
168   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
169               "Monitor 1: %s %s %s\n",
170               GNUNET_i2s (&address->peer),
171               GNUNET_TRANSPORT_ps2s (state),
172               GNUNET_STRINGS_absolute_time_to_string(state_timeout));
173   if ( (0 == memcmp (&address->peer, &ccc->p[1]->id, sizeof (ccc->p[1]->id))) &&
174        (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
175        (GNUNET_NO == p1_c) )
176   {
177     p1_c = GNUNET_YES;
178     check_done ();
179   }
180 }
181
182
183 static void
184 monitor2_cb (void *cls,
185              const struct GNUNET_PeerIdentity *peer,
186              const struct GNUNET_HELLO_Address *address,
187              enum GNUNET_TRANSPORT_PeerState state,
188              struct GNUNET_TIME_Absolute state_timeout)
189 {
190   if ((NULL == address) || (NULL == ccc->p[1]))
191     return;
192
193   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
194               "Monitor 2: %s %s %s\n",
195               GNUNET_i2s (&address->peer),
196               GNUNET_TRANSPORT_ps2s (state),
197               GNUNET_STRINGS_absolute_time_to_string(state_timeout));
198   if ( (0 == memcmp (&address->peer, &ccc->p[0]->id, sizeof (ccc->p[0]->id))) &&
199        (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
200        (GNUNET_NO == p2_c) )
201   {
202     p2_c = GNUNET_YES;
203     check_done ();
204   }
205 }
206
207
208 static void
209 start_monitors (void *cls)
210 {
211   pmc_p1 = GNUNET_TRANSPORT_monitor_peers (ccc->p[0]->cfg,
212                                            NULL,
213                                            GNUNET_NO,
214                                            &monitor1_cb,
215                                            NULL);
216   pmc_p2 = GNUNET_TRANSPORT_monitor_peers (ccc->p[1]->cfg,
217                                            NULL,
218                                            GNUNET_NO,
219                                            &monitor2_cb,
220                                            NULL);
221 }
222
223
224 int
225 main (int argc, char *argv[])
226 {
227   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
228     .pre_connect_task = &start_monitors,
229     .connect_continuation = &sendtask,
230     .config_file = "test_transport_api_data.conf",
231     .rec = &notify_receive,
232     .nc = &notify_connect,
233     .nd = &notify_disconnect,
234     .shutdown_task = &custom_shutdown,
235     .timeout = TIMEOUT
236   };
237
238   ccc = &my_ccc;
239   if (GNUNET_OK !=
240       GNUNET_TRANSPORT_TESTING_main (2,
241                                      &GNUNET_TRANSPORT_TESTING_connect_check,
242                                      ccc))
243     return 1;
244   return 0;
245 }
246
247 /* end of test_transport_api_monitor_peers.c */