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;
53 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
57 * Called when a conclusion was successful.
60 * @param num_peers_in_consensus
61 * @param peers_in_consensus
64 conclude_cb (void *cls,
65 unsigned int num_peers_in_consensus,
66 const struct GNUNET_PeerIdentity *peers_in_consensus)
68 printf("reached conclusion with %d peers\n", num_peers_in_consensus);
69 GNUNET_SCHEDULER_shutdown ();
74 insert_done_cb (void *cls,
77 struct GNUNET_CONSENSUS_Element *element = cls;
79 GNUNET_free (element);
80 if (GNUNET_YES != success)
82 printf ("insert failed\n");
83 GNUNET_SCHEDULER_shutdown ();
86 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == stdin_tid);
87 stdin_tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, stdin_fh,
93 * Called whenever we can read stdin non-blocking
96 * @param tc scheduler context
99 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 struct GNUNET_CONSENSUS_Element *element;
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);
113 printf ("concluding ...\n");
114 GNUNET_CONSENSUS_conclude (consensus, GNUNET_TIME_UNIT_FOREVER_REL, conclude_cb, NULL);
119 printf("read: %s", buf);
121 element = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_Element) + strlen(buf) + 1);
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);
131 * Called when a new element was received from another peer, or an error occured.
133 * May deliver duplicate values.
135 * Elements given to a consensus operation by the local peer are NOT given
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
145 struct GNUNET_CONSENSUS_Element *element)
147 printf("got element\n");
153 * Function run on shutdown to clean up.
155 * @param cls the statistics handle
156 * @param tc scheduler context
159 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "shutting down\n");
162 if (NULL != consensus)
164 GNUNET_CONSENSUS_destroy (consensus);
171 run (void *cls, char *const *args, const char *cfgfile,
172 const struct GNUNET_CONFIGURATION_Handle *cfg)
174 struct GNUNET_HashCode sid;
175 struct GNUNET_PeerIdentity *pids;
179 if (NULL == session_id_str)
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "no session id given\n");
185 for (count = 0; NULL != args[count]; count++);
189 pids = GNUNET_malloc (count * sizeof (struct GNUNET_PeerIdentity));
196 for (i = 0; i < count; i++)
199 ret = GNUNET_CRYPTO_hash_from_string (args[i], &pids[i].hashPubKey);
200 if (GNUNET_OK != ret)
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "peer identity '%s' is malformed\n", args[i]);
207 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
208 &shutdown_task, NULL);
211 GNUNET_CONSENSUS_create (cfg,
216 GNUNET_CONSENSUS_begin (consensus);
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,
226 main (int argc, char **argv)
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
234 GNUNET_PROGRAM_run (argc, argv, "gnunet-consensus",
236 options, &run, NULL);