implemented the modified consensus api, started implementing p2p protocol for consensus
[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 consensus_group_count,
66              const struct GNUNET_CONSENSUS_Group *groups)
67 {
68   printf("reached conclusion\n");
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, 0, 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   if (NULL == element)
148   {
149     printf("error receiving from consensus\n");
150     GNUNET_SCHEDULER_shutdown ();
151     return GNUNET_NO;
152   }
153   printf("got element\n");
154   return GNUNET_YES;
155 }
156
157
158 /**
159  * Function run on shutdown to clean up.
160  *
161  * @param cls the statistics handle
162  * @param tc scheduler context
163  */
164 static void
165 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
166 {
167   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "shutting down\n");
168   if (NULL != consensus)
169   {
170     GNUNET_CONSENSUS_destroy (consensus);
171     consensus = NULL;
172   }
173 }
174
175
176 static void
177 run (void *cls, char *const *args, const char *cfgfile,
178      const struct GNUNET_CONFIGURATION_Handle *cfg)
179 {
180   struct GNUNET_HashCode sid;
181   struct GNUNET_PeerIdentity *pids;
182   int count;
183   int i;
184
185   if (NULL == session_id_str)
186   {
187     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no session id given (missing -s/--session-id)\n");
188     return;
189   }
190
191   GNUNET_CRYPTO_hash (session_id_str, strlen (session_id_str), &sid);
192
193   for (count = 0; NULL != args[count]; count++);
194  
195   if (0 != count)
196   { 
197     pids = GNUNET_malloc (count * sizeof (struct GNUNET_PeerIdentity));
198   }
199   else
200   {
201     pids = NULL;
202   }
203
204   for (i = 0; i < count; i++)
205   {
206     int ret;
207     ret = GNUNET_CRYPTO_hash_from_string (args[i], &pids[i].hashPubKey);
208     if (GNUNET_OK != ret)
209     {
210       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "peer identity '%s' is malformed\n", args[i]);
211       return;
212     }
213   }
214
215   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
216                                 &shutdown_task, NULL);
217   
218   consensus = 
219       GNUNET_CONSENSUS_create (cfg,
220                                count, pids,
221                                &sid,
222                                &cb, NULL);
223
224   stdin_fh = GNUNET_DISK_get_handle_from_native (stdin);
225   stdin_tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fh,
226                                         &stdin_cb, NULL);
227 }
228
229
230 int
231 main (int argc, char **argv)
232 {
233    static const struct GNUNET_GETOPT_CommandLineOption options[] = {
234       { 's', "session-id", "ID",
235         gettext_noop ("session identifier"),
236         GNUNET_YES, &GNUNET_GETOPT_set_string, &session_id_str },
237         GNUNET_GETOPT_OPTION_END
238    };
239   GNUNET_PROGRAM_run (argc, argv, "gnunet-consensus",
240                       "help",
241                       options, &run, NULL);
242   return 0;
243 }