consensus now implemented with primitive conclusion group selection
[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 static void
109 test_master (void *cls,
110              unsigned int num_peers,
111              struct GNUNET_TESTBED_Peer **started_peers)
112 {
113   int i;
114
115   printf("started %d peers\n", num_peers);
116   peers = started_peers;
117
118   for (i = 0; i < num_peers; i++)
119   {
120     GNUNET_TESTBED_peer_get_information (peers[i],
121                                          GNUNET_TESTBED_PIT_IDENTITY,
122                                          peer_info_cb,
123                                          &peers[i]);
124     GNUNET_TESTBED_peer_get_information (peers[i],
125                                          GNUNET_TESTBED_PIT_CONFIGURATION,
126                                          peer_info_cb,
127                                          &peers[i]);
128   }
129 }
130
131
132 static void
133 run (void *cls, char *const *args, const char *cfgfile,
134      const struct GNUNET_CONFIGURATION_Handle *config)
135 {
136   if (NULL == config_template_file)
137   {
138     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no template file specified\n");
139     return;
140   }
141
142   (void) GNUNET_TESTBED_test_run ("gnunet-consensus-start-peers",
143                                   config_template_file,
144                                   num_peers_requested,
145                                   0,
146                                   controller_cb,
147                                   NULL,
148                                   test_master,
149                                   NULL);
150 }
151
152
153 int
154 main (int argc, char **argv)
155 {
156   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
157       { 't', "config-template", "TEMPLATE",
158         gettext_noop ("start peers with the given template configuration"),
159         GNUNET_YES, &GNUNET_GETOPT_set_string, &config_template_file },
160       { 'n', "num-peers", "NUM",
161         gettext_noop ("number of peers to start"),
162         GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers_requested },
163       GNUNET_GETOPT_OPTION_END
164    };
165
166   /* run without scheduler, as test_run already does this */
167   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-start-peers",
168                       "help",
169                       options, &run, NULL, GNUNET_YES);
170   return 0;
171 }
172