2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file consensus/gnunet-consensus.c
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_consensus_service.h"
33 * Handle to the consensus service
35 static struct GNUNET_CONSENSUS_Handle *consensus;
39 static char *session_id_str;
42 * File handle to STDIN
44 static struct GNUNET_DISK_FileHandle *stdin_fh;
47 * Task for reading from stdin
49 static GNUNET_SCHEDULER_TaskIdentifier stdin_tid = GNUNET_SCHEDULER_NO_TASK;
52 * Element currently being sent to the service
54 static struct GNUNET_CONSENSUS_Element *element;
59 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
63 * Called when a conclusion was successful.
66 * @param num_peers_in_consensus
67 * @param peers_in_consensus
70 conclude_cb (void *cls,
71 unsigned int num_peers_in_consensus,
72 const struct GNUNET_PeerIdentity *peers_in_consensus)
74 printf("reached conclusion with %d peers\n", num_peers_in_consensus);
75 GNUNET_SCHEDULER_shutdown ();
81 insert_done_cb (void *cls,
84 if (GNUNET_YES != success)
86 printf ("insert failed\n");
87 GNUNET_SCHEDULER_shutdown ();
90 GNUNET_free (element);
92 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == stdin_tid);
94 stdin_tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fh,
100 * Called whenever we can read stdin non-blocking
103 * @param tc scheduler context
106 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 ret = fgets (buf, 1024, stdin);
112 stdin_tid = GNUNET_SCHEDULER_NO_TASK;
118 printf ("concluding ...\n");
119 GNUNET_CONSENSUS_conclude (consensus, GNUNET_TIME_UNIT_FOREVER_REL, conclude_cb, NULL);
123 GNUNET_SCHEDULER_shutdown ();
128 printf("read: %s", buf);
130 element = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_Element) + strlen(buf) + 1);
132 element->size = strlen(buf) + 1;
133 element->data = &element[1];
134 strcpy((char *) &element[1], buf);
136 GNUNET_CONSENSUS_insert (consensus, element, insert_done_cb, NULL);
140 * Called when a new element was received from another peer, or an error occured.
142 * May deliver duplicate values.
144 * Elements given to a consensus operation by the local peer are NOT given
148 * @param element new element, NULL on error
149 * @return GNUNET_OK if the valid is well-formed and should be added to the consensus,
150 * GNUNET_SYSERR if the element should be ignored and not be propagated
154 struct GNUNET_CONSENSUS_Element *element)
156 printf("got element\n");
161 * Function run on shutdown to clean up.
163 * @param cls the statistics handle
164 * @param tc scheduler context
167 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "shutting down\n");
171 if (NULL == consensus)
176 GNUNET_CONSENSUS_destroy (consensus);
181 run (void *cls, char *const *args, const char *cfgfile,
182 const struct GNUNET_CONFIGURATION_Handle *cfg)
184 struct GNUNET_HashCode sid;
185 struct GNUNET_PeerIdentity *pids;
189 if (NULL == session_id_str)
191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no session id given\n");
195 for (count = 0; NULL != args[count]; count++);
199 pids = GNUNET_malloc (count * sizeof (struct GNUNET_PeerIdentity));
206 for (i = 0; i < count; i++)
209 ret = GNUNET_CRYPTO_hash_from_string (args[i], &pids[i].hashPubKey);
210 if (GNUNET_OK != ret)
212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "peer identity '%s' is malformed\n", args[i]);
217 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
218 &shutdown_task, NULL);
221 GNUNET_CONSENSUS_create (cfg,
226 GNUNET_CONSENSUS_begin (consensus);
229 stdin_fh = GNUNET_DISK_get_handle_from_native (stdin);
230 stdin_tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fh,
236 main (int argc, char **argv)
238 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
239 { 's', "session-id", "ID",
240 gettext_noop ("session identifier"),
241 GNUNET_YES, &GNUNET_GETOPT_set_string, &session_id_str },
242 GNUNET_GETOPT_OPTION_END
244 GNUNET_PROGRAM_run (argc, argv, "gnunet-consensus",
246 options, &run, NULL);