documentation
[oweals/gnunet.git] / src / consensus / gnunet-consensus-start-peers.c
1
2 /*
3       This file is part of GNUnet
4       (C) 2012 Christian Grothoff (and other contributing authors)
5
6       GNUnet is free software; you can redistribute it and/or modify
7       it under the terms of the GNU General Public License as published
8       by the Free Software Foundation; either version 2, or (at your
9       option) any later version.
10
11       GNUnet is distributed in the hope that it will be useful, but
12       WITHOUT ANY WARRANTY; without even the implied warranty of
13       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14       General Public License for more details.
15
16       You should have received a copy of the GNU General Public License
17       along with GNUnet; see the file COPYING.  If not, write to the
18       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19       Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * @file consensus/gnunet-consensus-start-peers.c
24  * @brief Starts peers with testebed on localhost,
25  *        prints their configuration files and waits for ^C.
26  * @author Florian Dold
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31
32
33 static char *config_template_file;
34 static unsigned int num_peers_requested = 2;
35 static struct GNUNET_TESTBED_Peer **peers;
36
37
38 /**
39  * Callback to be called when the requested peer information is available
40  *
41  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
42  * @param op the operation this callback corresponds to
43  * @param pinfo the result; will be NULL if the operation has failed
44  * @param emsg error message if the operation has failed; will be NULL if the
45  *          operation is successfull
46  */
47 static void
48 peer_info_cb (void *cb_cls,
49               struct GNUNET_TESTBED_Operation
50               *op,
51               const struct
52               GNUNET_TESTBED_PeerInformation
53               *pinfo,
54               const char *emsg)
55 {
56   GNUNET_assert (NULL == emsg);
57   if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
58   {
59     struct GNUNET_CRYPTO_HashAsciiEncoded enc;
60     GNUNET_CRYPTO_hash_to_enc (&pinfo->result.id->hashPubKey, &enc);
61     printf("peer %td identity:\n", ((struct GNUNET_TESTBED_Peer **) cb_cls) - &peers[0]);
62     printf("%s\n", (char *)&enc);
63   }
64   else if (pinfo->pit == GNUNET_TESTBED_PIT_CONFIGURATION)
65   {
66     char *tmpfilename;
67     if (NULL == (tmpfilename = GNUNET_DISK_mktemp ("gnunet-consensus")))
68     {
69       GNUNET_break (0);
70       GNUNET_SCHEDULER_shutdown ();
71       return;
72     }
73     if (GNUNET_SYSERR == 
74         GNUNET_CONFIGURATION_write (pinfo->result.cfg,
75                                     tmpfilename))
76     {
77       GNUNET_break (0);
78       return;
79     }
80     printf("peer %td config file:\n", ((struct GNUNET_TESTBED_Peer **) cb_cls) - &peers[0]);
81     printf("%s\n", tmpfilename);
82   }
83   else
84   {
85     GNUNET_assert (0);
86   }
87 }
88
89
90
91 /**
92  * Signature of the event handler function called by the
93  * respective event controller.
94  *
95  * @param cls closure
96  * @param event information about the event
97  */
98 static void
99 controller_cb(void *cls,
100               const struct GNUNET_TESTBED_EventInformation *event)
101 {
102   GNUNET_assert (0);
103 }
104
105
106
107
108 /**
109  * Signature of a main function for a testcase.
110  *
111  * @param cls closure
112  * @param num_peers number of peers in 'peers'
113  * @param started_peers handle to peers being run in the testbed.  NULL upon
114  *          timeout (see GNUNET_TESTBED_test_run()).
115  * @param links_succeeded the number of overlay link connection attempts that
116  *          succeeded
117  * @param links_failed the number of overlay link connection attempts that
118  *          failed
119  */
120 static void
121 test_master (void *cls,
122              unsigned int num_peers,
123              struct GNUNET_TESTBED_Peer **started_peers,
124              unsigned int links_succeeded,
125              unsigned int links_failed)
126 {
127   int i;
128
129   printf("started %d peers\n", num_peers);
130   peers = started_peers;
131
132   for (i = 0; i < num_peers; i++)
133   {
134     GNUNET_TESTBED_peer_get_information (peers[i],
135                                          GNUNET_TESTBED_PIT_IDENTITY,
136                                          peer_info_cb,
137                                          &peers[i]);
138     GNUNET_TESTBED_peer_get_information (peers[i],
139                                          GNUNET_TESTBED_PIT_CONFIGURATION,
140                                          peer_info_cb,
141                                          &peers[i]);
142   }
143 }
144
145
146 static void
147 run (void *cls, char *const *args, const char *cfgfile,
148      const struct GNUNET_CONFIGURATION_Handle *config)
149 {
150   if (NULL == config_template_file)
151   {
152     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no template file specified\n");
153     return;
154   }
155
156   (void) GNUNET_TESTBED_test_run ("gnunet-consensus-start-peers",
157                                   config_template_file,
158                                   num_peers_requested,
159                                   0,
160                                   controller_cb,
161                                   NULL,
162                                   test_master,
163                                   NULL);
164 }
165
166
167 int
168 main (int argc, char **argv)
169 {
170   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
171       { 't', "config-template", "TEMPLATE",
172         gettext_noop ("start peers with the given template configuration"),
173         GNUNET_YES, &GNUNET_GETOPT_set_string, &config_template_file },
174       { 'n', "num-peers", "NUM",
175         gettext_noop ("number of peers to start"),
176         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers_requested },
177       GNUNET_GETOPT_OPTION_END
178    };
179
180   /* run without scheduler, as test_run already does this */
181   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-start-peers",
182                       "help",
183                       options, &run, NULL, GNUNET_YES);
184   return 0;
185 }
186