aboutsummaryrefslogtreecommitdiff
path: root/src/core/gnunet-core.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2014-04-11 11:49:51 +0000
committerChristian Grothoff <christian@grothoff.org>2014-04-11 11:49:51 +0000
commit24712c94e185b72c30da25ea1b3a1784bde0defa (patch)
tree1ca399a023e0cdf48dbf9b411bb4b245081a1100 /src/core/gnunet-core.c
parenta61a4e0ffd7d563f3ae4d758f06a894edee71f58 (diff)
downloadgnunet-24712c94e185b72c30da25ea1b3a1784bde0defa.tar.gz
gnunet-24712c94e185b72c30da25ea1b3a1784bde0defa.zip
towards fixing #3363: replacing old iteration API with new monitoring API for core (needs testing, gnunet-core incomplete)
Diffstat (limited to 'src/core/gnunet-core.c')
-rw-r--r--src/core/gnunet-core.c186
1 files changed, 60 insertions, 126 deletions
diff --git a/src/core/gnunet-core.c b/src/core/gnunet-core.c
index 6309b39e0..36dc5fc6f 100644
--- a/src/core/gnunet-core.c
+++ b/src/core/gnunet-core.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors) 3 (C) 2011, 2012, 2014 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -24,13 +24,9 @@
24 * @author Nathan Evans 24 * @author Nathan Evans
25 */ 25 */
26#include "platform.h" 26#include "platform.h"
27#include "gnunet_crypto_lib.h" 27#include "gnunet_util_lib.h"
28#include "gnunet_configuration_lib.h"
29#include "gnunet_getopt_lib.h"
30#include "gnunet_peerinfo_service.h"
31#include "gnunet_transport_service.h"
32#include "gnunet_core_service.h" 28#include "gnunet_core_service.h"
33#include "gnunet_program_lib.h" 29
34 30
35/** 31/**
36 * Option -m. 32 * Option -m.
@@ -40,11 +36,13 @@ static int monitor_connections;
40/** 36/**
41 * Current number of connections in monitor mode 37 * Current number of connections in monitor mode
42 */ 38 */
43static int monitor_connections_counter; 39// static unsigned int monitor_connections_counter;
44 40
45static struct GNUNET_CORE_Handle *ch; 41/**
42 * Handle to the CORE monitor.
43 */
44static struct GNUNET_CORE_MonitorHandle *mh;
46 45
47static struct GNUNET_PeerIdentity my_id;
48 46
49/** 47/**
50 * Task run in monitor mode when the user presses CTRL-C to abort. 48 * Task run in monitor mode when the user presses CTRL-C to abort.
@@ -57,143 +55,81 @@ static void
57shutdown_task (void *cls, 55shutdown_task (void *cls,
58 const struct GNUNET_SCHEDULER_TaskContext *tc) 56 const struct GNUNET_SCHEDULER_TaskContext *tc)
59{ 57{
60 if (NULL != ch) 58 if (NULL != mh)
61 { 59 {
62 GNUNET_CORE_disconnect (ch); 60 GNUNET_CORE_monitor_stop (mh);
63 ch = NULL; 61 mh = NULL;
64 } 62 }
65} 63}
66 64
67 65
68/** 66/**
69 * Callback for retrieving a list of connected peers.
70 *
71 * @param cls closure (unused)
72 * @param peer peer identity this notification is about
73 */
74static void
75connected_peer_callback (void *cls,
76 const struct GNUNET_PeerIdentity *peer)
77{
78 if (NULL == peer)
79 return;
80 printf (_("Peer `%s'\n"),
81 GNUNET_i2s_full (peer));
82}
83
84
85static void
86monitor_notify_startup (void *cls,
87 const struct GNUNET_PeerIdentity *my_identity)
88{
89 my_id = (*my_identity);
90}
91
92
93/**
94 * Function called to notify core users that another 67 * Function called to notify core users that another
95 * peer connected to us. 68 * peer changed its state with us.
96 * 69 *
97 * @param cls closure 70 * @param cls closure
98 * @param peer the peer that connected 71 * @param peer the peer that changed state
72 * @param state new state of the peer
73 * @param timeout timeout for the new state
99 */ 74 */
100static void 75static void
101monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer) 76monitor_cb (void *cls,
77 const struct GNUNET_PeerIdentity *peer,
78 enum GNUNET_CORE_KxState state,
79 struct GNUNET_TIME_Absolute timeout)
102{ 80{
103 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get(); 81 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
104 const char *now_str; 82 const char *now_str;
105 83
106 if (0 != memcmp (&my_id, peer, sizeof (my_id))) 84 if ( (NULL == peer) &&
85 (GNUNET_NO == monitor_connections) )
107 { 86 {
108 monitor_connections_counter ++; 87 GNUNET_SCHEDULER_shutdown ();
109 now_str = GNUNET_STRINGS_absolute_time_to_string (now); 88 return;
110 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
111 now_str,
112 _("Connected to"),
113 GNUNET_i2s (peer),
114 monitor_connections_counter);
115 } 89 }
90 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
91 FPRINTF (stdout,
92 _("%24s: %-17s %d %4s\n"),
93 now_str,
94 "FIXME",
95 state,
96 GNUNET_i2s (peer));
116} 97}
117 98
118 99
119/** 100/**
120 * Function called to notify core users that another 101 * Function called with the result of the check if the CORE
121 * peer disconnected from us.
122 *
123 * @param cls closure
124 * @param peer the peer that disconnected
125 */
126static void
127monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
128{
129 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
130 const char *now_str;
131
132 if (0 != memcmp (&my_id, peer, sizeof (my_id)))
133 {
134 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
135
136 GNUNET_assert (monitor_connections_counter > 0);
137 monitor_connections_counter--;
138 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
139 now_str,
140 _("Disconnected from"),
141 GNUNET_i2s (peer),
142 monitor_connections_counter);
143 }
144}
145
146/**
147 * Function called with the result of the check if the 'transport'
148 * service is running. 102 * service is running.
149 * 103 *
150 * @param cls closure with our configuration 104 * @param cls closure with our configuration
151 * @param result #GNUNET_YES if transport is running 105 * @param result #GNUNET_YES if CORE is running
152 */ 106 */
153static void 107static void
154testservice_task (void *cls, int result) 108testservice_task (void *cls,
109 int result)
155{ 110{
156 const struct GNUNET_CONFIGURATION_Handle *cfg = cls; 111 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
157 static const struct GNUNET_CORE_MessageHandler handlers[] = {
158 {NULL, 0, 0}
159 };
160
161 if (result != GNUNET_OK)
162 {
163 FPRINTF (stderr, _("Service `%s' is not running\n"), "core");
164 return;
165 }
166 112
167 if (GNUNET_NO == monitor_connections) 113 if (GNUNET_OK != result)
168 { 114 {
169 if (GNUNET_OK != GNUNET_CORE_iterate_peers (cfg, &connected_peer_callback, NULL)) 115 FPRINTF (stderr, _("Service `%s' is not running\n"), "core");
170 { 116 return;
171 fprintf (stderr, ("Failed to connect to CORE service to iterate peers!\n"));
172 return;
173 }
174 } 117 }
175 else 118
119 mh = GNUNET_CORE_monitor_start (cfg,
120 &monitor_cb,
121 NULL);
122 if (NULL == mh)
176 { 123 {
177 memset(&my_id, '\0', sizeof (my_id)); 124 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
178 ch = GNUNET_CORE_connect (cfg, NULL, 125 fprintf (stderr, ("Failed to connect to CORE service!\n"));
179 monitor_notify_startup, 126 return;
180 monitor_notify_connect,
181 monitor_notify_disconnect,
182 NULL, GNUNET_NO,
183 NULL, GNUNET_NO,
184 handlers);
185
186 if (NULL == ch)
187 {
188 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
189 fprintf (stderr, ("Failed to connect to CORE service!\n"));
190 return;
191 }
192 else
193 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, shutdown_task, NULL);
194 } 127 }
128 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
129 &shutdown_task, NULL);
195} 130}
196 131
132
197/** 133/**
198 * Main function that will be run by the scheduler. 134 * Main function that will be run by the scheduler.
199 * 135 *
@@ -206,19 +142,21 @@ static void
206run (void *cls, char *const *args, const char *cfgfile, 142run (void *cls, char *const *args, const char *cfgfile,
207 const struct GNUNET_CONFIGURATION_Handle *cfg) 143 const struct GNUNET_CONFIGURATION_Handle *cfg)
208{ 144{
209 if (args[0] != NULL) 145 if (NULL != args[0])
210 { 146 {
211 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]); 147 FPRINTF (stderr,
148 _("Invalid command line argument `%s'\n"),
149 args[0]);
212 return; 150 return;
213 } 151 }
214 152 GNUNET_CLIENT_service_test ("core", cfg,
215 GNUNET_CLIENT_service_test ("core", cfg, GNUNET_TIME_UNIT_SECONDS, 153 GNUNET_TIME_UNIT_SECONDS,
216 &testservice_task, (void *) cfg); 154 &testservice_task, (void *) cfg);
217} 155}
218 156
219 157
220/** 158/**
221 * The main function to obtain peer information. 159 * The main function to obtain peer information from CORE.
222 * 160 *
223 * @param argc number of arguments from the command line 161 * @param argc number of arguments from the command line
224 * @param argv command line arguments 162 * @param argv command line arguments
@@ -237,19 +175,15 @@ main (int argc, char *const *argv)
237 175
238 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 176 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
239 return 2; 177 return 2;
240
241
242 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core", 178 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
243 gettext_noop 179 gettext_noop
244 ("Print information about connected peers."), 180 ("Print information about connected peers."),
245 options, &run, NULL); 181 options, &run, NULL);
246 182
247 GNUNET_free ((void *) argv); 183 GNUNET_free ((void *) argv);
248
249 if (GNUNET_OK == res) 184 if (GNUNET_OK == res)
250 return 0; 185 return 0;
251 else 186 return 1;
252 return 1;
253} 187}
254 188
255/* end of gnunet-core.c */ 189/* end of gnunet-core.c */