1a432ec03b623ddb43e64fd76169aaf4c61858ef
[oweals/gnunet.git] / src / testbed / generate-underlay-topology.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2014 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 testbed/generate-underlay-topology.c
23  * @brief Program to generate a database file containing given underlay topology
24  * @author Sree Harsha Totakura <sreeharsha@totakura.in> 
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "testbed_api_topology.h"
31
32 #define LOG(type, ...)                          \
33   GNUNET_log (type, __VA_ARGS__)
34
35
36 #define LOG_ERROR(...)                          \
37   LOG (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__)
38
39
40 /**
41  * The topology to generate
42  */
43 enum GNUNET_TESTBED_TopologyOption topology;
44
45 /**
46  * The number of peers to include in the topology
47  */
48 static int num_peers;
49
50 /**
51  * program result
52  */
53 static int exit_result;
54
55
56 /**
57  * Functions of this type are called to process underlay link
58  *
59  * @param cls closure
60  * @param A offset of first peer
61  * @param B offset of second peer
62  * @param bandwidth the bandwidth of the link in bytes per second
63  * @param latency the latency of link in milliseconds
64  * @param loss the percentage of messages dropped on the link
65  * @return GNUNET_OK to continue processing; GNUNET_SYSERR to abort
66  */
67 static int
68 link_processor (void *cls,
69                 unsigned int A,
70                 unsigned int B,
71                 unsigned int bandwidth,
72                 unsigned int latency,
73                 unsigned int loss)
74 {
75   FPRINTF (stdout, "%u -> %u\n", A, B);
76   return GNUNET_OK;
77 }
78
79
80 /**
81  * Main run function.
82  *
83  * @param cls NULL
84  * @param args arguments passed to GNUNET_PROGRAM_run
85  * @param cfgfile the path to configuration file
86  * @param cfg the configuration file handle
87  */
88 static void
89 run (void *cls, char *const *args, const char *cfgfile,
90      const struct GNUNET_CONFIGURATION_Handle *config)
91 {
92   const char *dbfile;
93   const char *topology_string;
94   unsigned int arg_uint1;
95   unsigned int arg_uint2;
96   const char *arg_str1;
97   const char *value;
98   unsigned int argc;
99
100   argc = 0;
101   if (NULL == args)
102   {
103     LOG_ERROR (_("Need atleast 2 arguments\n"));
104     return;
105   }
106   if (NULL == (dbfile = args[argc++]))
107   {
108     LOG_ERROR (_("Database filename missing\n"));
109     return;
110   }
111   if (NULL == (topology_string = args[argc++]))
112   {
113     LOG_ERROR (_("Topology string missing\n"));
114     return;
115   }
116   if (GNUNET_YES != GNUNET_TESTBED_topology_get_ (&topology, topology_string))
117   {
118     LOG_ERROR (_("Invalid topology: %s\n"), topology_string);
119     return;
120   }
121   /* parse for first TOPOOPT.  This can either be arg_uint1 or arg_str1 */
122   switch (topology)
123   {
124   case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
125   case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
126   case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
127   case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
128     if (NULL == (value = args[argc++]))
129     {
130       LOG_ERROR (_("An argument is missing for given topology `%s'\n"),
131                  topology_string);
132       return;
133     }
134     if (-1 == SSCANF (value, "%u", &arg_uint1))
135     {
136       LOG_ERROR (_("Invalid argument `%s' given as topology argument\n"),
137                  value);
138       return;
139     }
140     break;
141   case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
142     if (NULL == (arg_str1 = args[argc++]))
143     {
144       LOG_ERROR (_("Filename argument missing for topology `%s'\n"),
145                  topology_string);
146       return;
147     }
148     break;
149   default:
150     GNUNET_assert (0);
151   }
152   /* parse for second TOPOOPT.  Only required for SCALE_FREE topology */
153   switch (topology)
154   {
155   case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
156     if (NULL == (value = args[argc++]))
157     {
158       LOG_ERROR (_("Second argument for topology `%s' is missing\n"),
159                  topology_string);
160       return;
161     }
162     if (-1 == SSCANF (value, "%u", &arg_uint2))
163     {
164       LOG_ERROR (_("Invalid argument `%s'; expecting unsigned int\n"), value);
165       return;
166     }
167     break;
168   default:
169     break;
170   }
171   /* contruct topologies */
172   switch (topology)
173   {
174   case GNUNET_TESTBED_TOPOLOGY_LINE:
175   case GNUNET_TESTBED_TOPOLOGY_RING:
176   case GNUNET_TESTBED_TOPOLOGY_CLIQUE:
177   case GNUNET_TESTBED_TOPOLOGY_2D_TORUS:
178     GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
179                                         topology);
180     break;
181   case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
182   case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
183   case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
184     GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
185                                         topology,
186                                         arg_uint1);
187     break;
188   case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
189     GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
190                                         topology,
191                                         arg_str1);
192     break;
193   case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
194     GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
195                                         topology,
196                                         arg_uint1,
197                                         arg_uint2);
198     break;
199   default:
200     GNUNET_assert (0);
201   }
202 }
203
204
205 /**
206  * Main
207  */
208 int
209 main (int argc, char *const argv[])
210 {
211   struct GNUNET_GETOPT_CommandLineOption option[] = {
212     {'p', "num-peers", "COUNT",
213      gettext_noop ("create COUNT number of peers"),
214      GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers},
215     GNUNET_GETOPT_OPTION_END
216   };
217   int ret;
218   
219   exit_result = GNUNET_SYSERR;
220   ret =
221       GNUNET_PROGRAM_run (argc, argv, "gnunet-underlay-topology",
222                           _("Generates SQLite3 database representing a given underlay topology.\n"
223                             "Usage: gnunet-underlay-topology [OPTIONS] db-filename TOPO [TOPOOPTS]\n"
224                             "The following options are available for TOPO followed by TOPOOPTS if applicable:\n"
225                             "\t LINE\n"
226                             "\t RING\n"
227                             "\t RANDOM <num_rnd_links>\n"
228                             "\t SMALL_WORLD <num_rnd_links>\n"
229                             "\t SMALL_WORLD_RING <num_rnd_links>\n"
230                             "\t CLIQUE\n"
231                             "\t 2D_TORUS\n"
232                             "\t SCALE_FREE <cap> <m>\n"
233                             "\t FROM_FILE <filename>\n"
234                             "TOPOOPTS:\n"
235                             "\t num_rnd_links: The number of random links\n"
236                             "\t cap: the maximum number of links a node can have\n"
237                             "\t m: the number of links a node should have while joining the network\n"
238                             "\t filename: the path of the file which contains topology information\n"
239                             "NOTE: the format of the above file is descibed here: https://www.gnunet.org/content/topology-file-format\n"),
240                           option, &run, NULL);
241   if ((GNUNET_OK != ret) || (GNUNET_OK != exit_result))
242     return 1;
243   return 0;
244 }