aboutsummaryrefslogtreecommitdiff
path: root/src/core/gnunet-core.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2012-06-12 11:24:39 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2012-06-12 11:24:39 +0000
commit1de2afe6f09ed227322c1abe20f2a928e8b6dc56 (patch)
tree2f201691cae0a0f9f9e8852b79c9c1b7338d38b8 /src/core/gnunet-core.c
parent384c28a7b0eb7ade7bd93663e35375f5fd16a0c3 (diff)
downloadgnunet-1de2afe6f09ed227322c1abe20f2a928e8b6dc56.tar.gz
gnunet-1de2afe6f09ed227322c1abe20f2a928e8b6dc56.zip
core monitor mode
Diffstat (limited to 'src/core/gnunet-core.c')
-rw-r--r--src/core/gnunet-core.c124
1 files changed, 118 insertions, 6 deletions
diff --git a/src/core/gnunet-core.c b/src/core/gnunet-core.c
index 78bc14154..4f49f8130 100644
--- a/src/core/gnunet-core.c
+++ b/src/core/gnunet-core.c
@@ -32,6 +32,36 @@
32#include "gnunet_core_service.h" 32#include "gnunet_core_service.h"
33#include "gnunet_program_lib.h" 33#include "gnunet_program_lib.h"
34 34
35/**
36 * Option -m.
37 */
38static int monitor_connections;
39
40/**
41 * Current number of connections in monitor mode
42 */
43static int monitor_connections_counter;
44
45static struct GNUNET_CORE_Handle *ch;
46
47/**
48 * Task run in monitor mode when the user presses CTRL-C to abort.
49 * Stops monitoring activity.
50 *
51 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
52 * @param tc scheduler context
53 */
54static void
55shutdown_task (void *cls,
56 const struct GNUNET_SCHEDULER_TaskContext *tc)
57{
58 if (NULL != ch)
59 {
60 GNUNET_CORE_disconnect (ch);
61 ch = NULL;
62 }
63}
64
35 65
36/** 66/**
37 * Callback for retrieving a list of connected peers. 67 * Callback for retrieving a list of connected peers.
@@ -56,6 +86,58 @@ connected_peer_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
56 86
57 87
58/** 88/**
89 * Function called to notify core users that another
90 * peer connected to us.
91 *
92 * @param cls closure
93 * @param peer the peer that connected
94 * @param ats performance data
95 * @param ats_count number of entries in ats (excluding 0-termination)
96 */
97static void
98monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
99 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
100{
101 monitor_connections_counter ++;
102 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
103 char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
104 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
105 now_str,
106 _("Connected to"),
107 GNUNET_i2s (peer),
108 monitor_connections_counter);
109
110 GNUNET_free (now_str);
111}
112
113
114/**
115 * Function called to notify core users that another
116 * peer disconnected from us.
117 *
118 * @param cls closure
119 * @param peer the peer that disconnected
120 */
121static void
122monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
123{
124 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
125 char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
126
127 GNUNET_assert (monitor_connections_counter > 0);
128 monitor_connections_counter --;
129
130 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
131 now_str,
132 _("Disconnected from"),
133 GNUNET_i2s (peer),
134 monitor_connections_counter);
135 GNUNET_free (now_str);
136}
137
138
139
140/**
59 * Main function that will be run by the scheduler. 141 * Main function that will be run by the scheduler.
60 * 142 *
61 * @param cls closure 143 * @param cls closure
@@ -72,7 +154,26 @@ run (void *cls, char *const *args, const char *cfgfile,
72 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]); 154 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
73 return; 155 return;
74 } 156 }
75 GNUNET_CORE_iterate_peers (cfg, &connected_peer_callback, NULL); 157 if (GNUNET_NO == monitor_connections)
158 GNUNET_CORE_iterate_peers (cfg, &connected_peer_callback, NULL);
159 else
160 {
161 const static struct GNUNET_CORE_MessageHandler handlers[] = {
162 {NULL, 0, 0}
163 };
164
165 ch = GNUNET_CORE_connect (cfg, NULL, NULL,
166 monitor_notify_connect,
167 monitor_notify_disconnect,
168 NULL, GNUNET_NO,
169 NULL, GNUNET_NO,
170 handlers);
171
172 if (NULL == ch)
173 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
174 else
175 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, shutdown_task, NULL);
176 }
76} 177}
77 178
78 179
@@ -86,18 +187,29 @@ run (void *cls, char *const *args, const char *cfgfile,
86int 187int
87main (int argc, char *const *argv) 188main (int argc, char *const *argv)
88{ 189{
190 int res;
89 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 191 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
192 {'m', "monitor", NULL,
193 gettext_noop ("provide information about all current connections (continuously)"),
194 0, &GNUNET_GETOPT_set_one, &monitor_connections},
90 GNUNET_GETOPT_OPTION_END 195 GNUNET_GETOPT_OPTION_END
91 }; 196 };
92 197
93 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 198 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
94 return 2; 199 return 2;
95 200
96 return (GNUNET_OK == 201
97 GNUNET_PROGRAM_run (argc, argv, "gnunet-core", 202 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
98 gettext_noop 203 gettext_noop
99 ("Print information about connected peers."), 204 ("Print information about connected peers."),
100 options, &run, NULL)) ? 0 : 1; 205 options, &run, NULL);
206
207 GNUNET_free ((void *) argv);
208
209 if (GNUNET_OK == res)
210 return 0;
211 else
212 return 1;
101} 213}
102 214
103/* end of gnunet-core.c */ 215/* end of gnunet-core.c */