-options to play with
[oweals/gnunet.git] / src / consensus / gnunet-consensus.c
1 /*
2       This file is part of GNUnet
3       (C) 2012 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 2, 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 consensus/gnunet-consensus.c
23  * @brief 
24  * @author Florian Dold
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_consensus_service.h"
29
30
31
32 /**
33  * Handle to the consensus service
34  */
35 static struct GNUNET_CONSENSUS_Handle *consensus;
36 /**
37  * Session id
38  */
39 static char *session_id_str;
40
41 /**
42  * File handle to STDIN
43  */
44 static struct GNUNET_DISK_FileHandle *stdin_fh;
45
46 /**
47  * Task for reading from stdin
48  */
49 static GNUNET_SCHEDULER_TaskIdentifier stdin_tid = GNUNET_SCHEDULER_NO_TASK;
50
51
52 static void
53 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
54
55
56 /**
57  * Called when a conclusion was successful.
58  *
59  * @param cls
60  * @param num_peers_in_consensus
61  * @param peers_in_consensus
62  */
63 static void
64 conclude_cb (void *cls, 
65              unsigned int num_peers_in_consensus,
66              const struct GNUNET_PeerIdentity *peers_in_consensus)
67 {
68   printf("reached conclusion with %d peers\n", num_peers_in_consensus);
69   GNUNET_SCHEDULER_shutdown ();
70 }
71
72
73 static void
74 insert_done_cb (void *cls,
75                 int success)
76 {
77   struct GNUNET_CONSENSUS_Element *element = cls;
78
79   GNUNET_free (element);
80   if (GNUNET_YES != success)
81   {
82     printf ("insert failed\n");
83     GNUNET_SCHEDULER_shutdown ();
84     return;
85   }
86   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == stdin_tid);
87   stdin_tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fh,
88                                               &stdin_cb, NULL);    
89 }
90
91
92 /**
93  * Called whenever we can read stdin non-blocking 
94  *
95  * @param cls unused
96  * @param tc scheduler context 
97  */
98 static void
99 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
100 {
101   char buf[1024];
102   char *ret;
103   struct GNUNET_CONSENSUS_Element *element;
104
105   stdin_tid = GNUNET_SCHEDULER_NO_TASK;
106   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
107     return; /* we're done here */
108   ret = fgets (buf, 1024, stdin);
109   if (NULL == ret)
110   {
111     if (feof (stdin))
112     {
113       printf ("concluding ...\n");
114       GNUNET_CONSENSUS_conclude (consensus, GNUNET_TIME_UNIT_FOREVER_REL, conclude_cb, NULL);
115     }
116     return;
117   }
118
119   printf("read: %s", buf);
120
121   element = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_Element) + strlen(buf) + 1);
122   element->type = 0;
123   element->size = strlen(buf) + 1;
124   element->data = &element[1];
125   strcpy ((char *) &element[1], buf);
126   GNUNET_CONSENSUS_insert (consensus, element, &insert_done_cb, element); 
127 }
128
129
130 /**
131  * Called when a new element was received from another peer, or an error occured.
132  *
133  * May deliver duplicate values.
134  *
135  * Elements given to a consensus operation by the local peer are NOT given
136  * to this callback.
137  *
138  * @param cls closure
139  * @param element new element, NULL on error
140  * @return GNUNET_OK if the valid is well-formed and should be added to the consensus,
141  *         GNUNET_SYSERR if the element should be ignored and not be propagated
142  */
143 static int
144 cb (void *cls,
145     struct GNUNET_CONSENSUS_Element *element)
146 {
147   printf("got element\n");
148   return GNUNET_YES;
149 }
150
151
152 /**
153  * Function run on shutdown to clean up.
154  *
155  * @param cls the statistics handle
156  * @param tc scheduler context
157  */
158 static void
159 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 {
161   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "shutting down\n");
162   if (NULL != consensus)
163   {
164     GNUNET_CONSENSUS_destroy (consensus);
165     consensus = NULL;
166   }
167 }
168
169
170 static void
171 run (void *cls, char *const *args, const char *cfgfile,
172      const struct GNUNET_CONFIGURATION_Handle *cfg)
173 {
174   struct GNUNET_HashCode sid;
175   struct GNUNET_PeerIdentity *pids;
176   int count;
177   int i;
178
179   if (NULL == session_id_str)
180   {
181     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no session id given\n");
182     return;
183   }
184
185   for (count = 0; NULL != args[count]; count++);
186  
187   if (0 != count)
188   { 
189     pids = GNUNET_malloc (count * sizeof (struct GNUNET_PeerIdentity));
190   }
191   else
192   {
193     pids = NULL;
194   }
195
196   for (i = 0; i < count; i++)
197   {
198     int ret;
199     ret = GNUNET_CRYPTO_hash_from_string (args[i], &pids[i].hashPubKey);
200     if (GNUNET_OK != ret)
201     {
202       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "peer identity '%s' is malformed\n", args[i]);
203       return;
204     }
205   }
206
207   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
208                                 &shutdown_task, NULL);
209   
210   consensus = 
211       GNUNET_CONSENSUS_create (cfg,
212                                count, pids,
213                                &sid,
214                                &cb, NULL);
215
216   GNUNET_CONSENSUS_begin (consensus);
217
218
219   stdin_fh = GNUNET_DISK_get_handle_from_native (stdin);
220   stdin_tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fh,
221                                         &stdin_cb, NULL);
222 }
223
224
225 int
226 main (int argc, char **argv)
227 {
228    static const struct GNUNET_GETOPT_CommandLineOption options[] = {
229       { 's', "session-id", "ID",
230         gettext_noop ("session identifier"),
231         GNUNET_YES, &GNUNET_GETOPT_set_string, &session_id_str },
232         GNUNET_GETOPT_OPTION_END
233    };
234   GNUNET_PROGRAM_run (argc, argv, "gnunet-consensus",
235                       "help",
236                       options, &run, NULL);
237   return 0;
238 }