2 This file is part of GNUnet.
3 (C) 2011 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/test_mesh_local.c
23 * @brief test mesh local: test of mesh channels with just one peer
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_mesh_service_enc.h"
34 #define DATA_SIZE 35000
36 struct GNUNET_TESTING_Peer *me;
38 static struct GNUNET_MESH_Handle *mesh;
40 static struct GNUNET_MESH_Channel *ch1;
42 static struct GNUNET_MESH_Channel *ch2;
44 static int result = GNUNET_OK;
46 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
48 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
50 static unsigned int repetition;
53 /* forward declaration */
55 do_send (void *cls, size_t size, void *buf);
62 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
64 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
65 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
67 GNUNET_SCHEDULER_cancel (abort_task);
71 GNUNET_MESH_channel_destroy (ch1);
73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 1\n");
74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 2\n");
77 GNUNET_MESH_disconnect (mesh);
83 * Something went wrong and timed out. Kill everything and set error flag
86 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ABORT\n");
89 result = GNUNET_SYSERR;
90 abort_task = GNUNET_SCHEDULER_NO_TASK;
91 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
93 GNUNET_SCHEDULER_cancel (shutdown_task);
94 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
96 do_shutdown (cls, tc);
101 * Function is called whenever a message is received.
103 * @param cls closure (set from GNUNET_MESH_connect)
104 * @param channel connection to the other end
105 * @param channel_ctx place to store local state associated with the channel
106 * @param message the actual message
108 * @return GNUNET_OK to keep the connection open,
109 * GNUNET_SYSERR to close it (signal serious error)
112 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
114 const struct GNUNET_MessageHeader *message)
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117 "Data callback! Repetition %u/%u\n",
118 repetition, REPETITIONS);
119 repetition = repetition + 1;
120 if (repetition < REPETITIONS)
122 struct GNUNET_MESH_Channel *my_channel;
123 if (repetition % 2 == 0)
127 GNUNET_MESH_notify_transmit_ready (my_channel, GNUNET_NO,
128 GNUNET_TIME_UNIT_FOREVER_REL,
129 sizeof (struct GNUNET_MessageHeader) + DATA_SIZE,
131 GNUNET_MESH_receive_done (channel);
134 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
135 GNUNET_SCHEDULER_cancel (shutdown_task);
137 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown,
139 GNUNET_MESH_receive_done (channel);
145 * Method called whenever another peer has added us to a channel
146 * the other peer initiated.
149 * @param channel new handle to the channel
150 * @param initiator peer that started the channel
151 * @param port port number
152 * @return initial channel context for the channel (can be NULL -- that's not an error)
155 inbound_channel (void *cls, struct GNUNET_MESH_Channel *channel,
156 const struct GNUNET_PeerIdentity *initiator,
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "received incoming channel on port %u\n",
168 * Function called whenever an inbound channel is destroyed. Should clean up
169 * any associated state.
171 * @param cls closure (set from GNUNET_MESH_connect)
172 * @param channel connection to the other end (henceforth invalid)
173 * @param channel_ctx place where local state associated
174 * with the channel is stored
177 inbound_end (void *cls, const struct GNUNET_MESH_Channel *channel,
180 long id = (long) cls;
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "incoming channel closed at peer %ld\n",
189 * Handler array for traffic received on peer1
191 static struct GNUNET_MESH_MessageHandler handlers1[] = {
192 {&data_callback, 1, 0},
198 * Data send callback: fillbuffer with test packet.
200 * @param cls Closure (unused).
201 * @param size Buffer size.
202 * @param buf Buffer to fill.
204 * @return size of test packet.
207 do_send (void *cls, size_t size, void *buf)
209 struct GNUNET_MessageHeader *m = buf;
214 result = GNUNET_SYSERR;
217 m->size = htons (sizeof (struct GNUNET_MessageHeader));
219 memset (&m[1], 0, DATA_SIZE);
220 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader) + DATA_SIZE);
221 return sizeof (struct GNUNET_MessageHeader) + DATA_SIZE;
225 * Connect to other client and send data
227 * @param cls Closue (unused).
228 * @param tc TaskContext.
231 do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
233 struct GNUNET_PeerIdentity id;
235 GNUNET_TESTING_peer_get_identity (me, &id);
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECT BY PORT\n");
237 ch1 = GNUNET_MESH_channel_create (mesh, NULL, &id, 1, GNUNET_YES, GNUNET_NO);
238 GNUNET_MESH_notify_transmit_ready (ch1, GNUNET_NO,
239 GNUNET_TIME_UNIT_FOREVER_REL,
240 sizeof (struct GNUNET_MessageHeader) + DATA_SIZE,
246 * Initialize framework and start test
248 * @param cls Closure (unused).
249 * @param cfg Configuration handle.
250 * @param peer Testing peer handle.
254 const struct GNUNET_CONFIGURATION_Handle *cfg,
255 struct GNUNET_TESTING_Peer *peer)
257 static uint32_t ports[] = {1, 0};
261 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
262 (GNUNET_TIME_UNIT_SECONDS, 15), &do_abort,
264 mesh = GNUNET_MESH_connect (cfg, /* configuration */
265 (void *) 1L, /* cls */
266 &inbound_channel, /* inbound new hndlr */
267 &inbound_end, /* inbound end hndlr */
268 handlers1, /* traffic handlers */
269 ports); /* ports offered */
273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh :(\n");
274 result = GNUNET_SYSERR;
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO MESH :D\n");
281 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_connect, NULL);
289 main (int argc, char *argv[])
291 if (0 != GNUNET_TESTING_peer_run ("test-mesh-local",
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
299 return (result == GNUNET_OK) ? 0 : 1;
302 /* end of test_mesh_local_1.c */