- fix peer fwd ack gathering
[oweals/gnunet.git] / src / mesh / test_mesh_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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 mesh/test_mesh_api.c
23  * @brief test mesh api: dummy test of callbacks
24  * @author Bartlomiej Polot
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib-new.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_mesh_service.h"
31
32 static struct GNUNET_MESH_Handle *mesh;
33
34 static struct GNUNET_MESH_Tunnel *t;
35
36 static int result;
37
38 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
39
40
41 /**
42  * Function is called whenever a message is received.
43  *
44  * @param cls closure (set from GNUNET_MESH_connect)
45  * @param tunnel connection to the other end
46  * @param tunnel_ctx place to store local state associated with the tunnel
47  * @param sender who sent the message
48  * @param message the actual message
49  * @param atsi performance data for the connection
50  * @return GNUNET_OK to keep the connection open,
51  *         GNUNET_SYSERR to close it (signal serious error)
52  */
53 static int
54 callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
55           const struct GNUNET_PeerIdentity *sender,
56           const struct GNUNET_MessageHeader *message,
57           const struct GNUNET_ATS_Information *atsi)
58 {
59   return GNUNET_OK;
60 }
61
62
63 static struct GNUNET_MESH_MessageHandler handlers[] = { 
64   { &callback, 1, 0 },
65   { NULL, 0, 0 }
66 };
67
68
69 static void
70 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 {
72   if (NULL != t)
73   {
74     GNUNET_MESH_tunnel_destroy (t);
75   }
76   if (0 != abort_task)
77   {
78     GNUNET_SCHEDULER_cancel (abort_task);
79   }
80   if (NULL != mesh)
81   {
82     GNUNET_MESH_disconnect (mesh);
83   }
84 }
85
86
87 static void
88 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
89 {
90   result = GNUNET_SYSERR;
91   abort_task = 0;
92   do_shutdown (cls, tc);
93 }
94
95
96 static void
97 run (void *cls, 
98      const struct GNUNET_CONFIGURATION_Handle *cfg,
99      struct GNUNET_TESTING_Peer *peer)
100 {
101   static const GNUNET_MESH_ApplicationType app[] =
102     { 1, 2, 3, 4, 5, 6, 7, 8, 0 };
103
104   mesh = GNUNET_MESH_connect (cfg, NULL, NULL, NULL, handlers, app);
105   if (NULL == mesh)
106   {
107     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
108     result = GNUNET_SYSERR;
109     return;
110   }
111   else
112   {
113     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
114   }
115   t = GNUNET_MESH_tunnel_create (mesh, NULL, NULL, NULL, NULL);
116   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
117                                 (GNUNET_TIME_UNIT_SECONDS, 5), &do_shutdown,
118                                 NULL);
119   abort_task =
120       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
121                                     (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
122                                     NULL);
123 }
124
125
126 int
127 main (int argc, char *argv[])
128 {
129   result = GNUNET_OK;
130   if (0 != GNUNET_TESTING_peer_run ("test-mesh-api",
131                                     "test_mesh.conf",
132                                     &run, NULL))
133     return 1;
134   return (result == GNUNET_OK) ? 0 : 1;
135 }
136
137 /* end of test_mesh_api.c */
138