2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2016 GNUnet e.V.
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 transport/test_transport_api_monitor_peers.c
22 * @brief base test case for transport peer monitor API
25 #include "gnunet_transport_service.h"
26 #include "transport-testing.h"
29 * How long until we give up on transmitting the message?
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
34 * How long until we give up on transmitting the message?
36 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
38 #define TEST_MESSAGE_SIZE 2600
40 #define TEST_MESSAGE_TYPE 12345
42 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
44 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p1;
46 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p2;
52 static int p1_c_notify;
54 static int p2_c_notify;
58 custom_shutdown (void *cls)
62 GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
67 GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
74 notify_receive (void *cls,
75 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
76 const struct GNUNET_PeerIdentity *sender,
77 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
79 char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
81 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
82 "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
85 ntohs (message->header.type),
86 ntohs (message->header.size),
95 /* intentionally empty */
102 if ( (GNUNET_YES == p1_c) &&
103 (GNUNET_YES == p2_c) &&
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108 "Both peers state to be connected\n");
109 ccc->global_ret = GNUNET_OK;
110 GNUNET_SCHEDULER_shutdown ();
116 notify_connect (void *cls,
117 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
118 const struct GNUNET_PeerIdentity *other)
120 GNUNET_TRANSPORT_TESTING_log_connect (cls,
123 if (0 == memcmp (other, &ccc->p[0]->id, sizeof (struct GNUNET_PeerIdentity)))
125 p1_c_notify = GNUNET_YES;
127 if (0 == memcmp (other, &ccc->p[1]->id, sizeof (struct GNUNET_PeerIdentity)))
129 p2_c_notify = GNUNET_YES;
136 monitor1_cb (void *cls,
137 const struct GNUNET_PeerIdentity *peer,
138 const struct GNUNET_HELLO_Address *address,
139 enum GNUNET_TRANSPORT_PeerState state,
140 struct GNUNET_TIME_Absolute state_timeout)
142 if ((NULL == address) || (NULL == ccc->p[0]))
145 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
146 "Monitor 1: %s %s %s\n",
147 GNUNET_i2s (&address->peer),
148 GNUNET_TRANSPORT_ps2s (state),
149 GNUNET_STRINGS_absolute_time_to_string(state_timeout));
150 if ( (0 == memcmp (&address->peer, &ccc->p[1]->id, sizeof (ccc->p[1]->id))) &&
151 (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
152 (GNUNET_NO == p1_c) )
161 monitor2_cb (void *cls,
162 const struct GNUNET_PeerIdentity *peer,
163 const struct GNUNET_HELLO_Address *address,
164 enum GNUNET_TRANSPORT_PeerState state,
165 struct GNUNET_TIME_Absolute state_timeout)
167 if ((NULL == address) || (NULL == ccc->p[1]))
170 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
171 "Monitor 2: %s %s %s\n",
172 GNUNET_i2s (&address->peer),
173 GNUNET_TRANSPORT_ps2s (state),
174 GNUNET_STRINGS_absolute_time_to_string(state_timeout));
175 if ( (0 == memcmp (&address->peer, &ccc->p[0]->id, sizeof (ccc->p[0]->id))) &&
176 (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
177 (GNUNET_NO == p2_c) )
186 start_monitors (void *cls)
188 pmc_p1 = GNUNET_TRANSPORT_monitor_peers (ccc->p[0]->cfg,
193 pmc_p2 = GNUNET_TRANSPORT_monitor_peers (ccc->p[1]->cfg,
202 main (int argc, char *argv[])
204 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
205 .pre_connect_task = &start_monitors,
206 .connect_continuation = &sendtask,
207 .config_file = "test_transport_api_data.conf",
208 .rec = ¬ify_receive,
209 .nc = ¬ify_connect,
210 .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
211 .shutdown_task = &custom_shutdown,
217 GNUNET_TRANSPORT_TESTING_main (2,
218 &GNUNET_TRANSPORT_TESTING_connect_check,
224 /* end of test_transport_api_monitor_peers.c */