diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/sensor/gnunet-sensor-profiler.c | 190 |
1 files changed, 182 insertions, 8 deletions
diff --git a/src/sensor/gnunet-sensor-profiler.c b/src/sensor/gnunet-sensor-profiler.c index daab123bc..9f4682b70 100644 --- a/src/sensor/gnunet-sensor-profiler.c +++ b/src/sensor/gnunet-sensor-profiler.c | |||
@@ -55,10 +55,21 @@ struct PeerInfo | |||
55 | 55 | ||
56 | }; | 56 | }; |
57 | 57 | ||
58 | |||
59 | /** | ||
60 | * Name of the configuration file used | ||
61 | */ | ||
62 | static const char *cfg_filename = "gnunet-sensor-profiler.conf"; | ||
63 | |||
58 | /** | 64 | /** |
59 | * Number of peers to run | 65 | * Directory to read sensor definitions from |
60 | */ | 66 | */ |
61 | static unsigned int num_peers; | 67 | static const char *sensor_src_dir = "sensors"; |
68 | |||
69 | /** | ||
70 | * Directory to write new sensor definitions to | ||
71 | */ | ||
72 | static const char *sensor_dst_dir = "/tmp/gnunet-sensor-profiler"; | ||
62 | 73 | ||
63 | /** | 74 | /** |
64 | * Return value of the program | 75 | * Return value of the program |
@@ -66,6 +77,16 @@ static unsigned int num_peers; | |||
66 | static int ok = 1; | 77 | static int ok = 1; |
67 | 78 | ||
68 | /** | 79 | /** |
80 | * Number of peers to run (Option -p) | ||
81 | */ | ||
82 | static unsigned int num_peers = 0; | ||
83 | |||
84 | /** | ||
85 | * Set sensors running interval to this value (Option -i) | ||
86 | */ | ||
87 | static unsigned int sensors_interval = 0; | ||
88 | |||
89 | /** | ||
69 | * Array of peer info for all peers | 90 | * Array of peer info for all peers |
70 | */ | 91 | */ |
71 | static struct PeerInfo *all_peers_info; | 92 | static struct PeerInfo *all_peers_info; |
@@ -75,10 +96,16 @@ static struct PeerInfo *all_peers_info; | |||
75 | */ | 96 | */ |
76 | static int peers_known = 0; | 97 | static int peers_known = 0; |
77 | 98 | ||
99 | |||
78 | /** | 100 | /** |
79 | * Name of the configuration file used | 101 | * Copy directory recursively |
102 | * | ||
103 | * @param src Path to source directory | ||
104 | * @param dst Destination directory, will be created if it does not exist | ||
105 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | ||
80 | */ | 106 | */ |
81 | static char *cfg_filename = "gnunet-sensor-profiler.conf"; | 107 | static int |
108 | copy_dir (const char *src, const char *dst); | ||
82 | 109 | ||
83 | 110 | ||
84 | /** | 111 | /** |
@@ -87,6 +114,7 @@ static char *cfg_filename = "gnunet-sensor-profiler.conf"; | |||
87 | static void | 114 | static void |
88 | do_shutdown () // TODO: schedule timeout shutdown | 115 | do_shutdown () // TODO: schedule timeout shutdown |
89 | { | 116 | { |
117 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down.\n"); | ||
90 | if (NULL != all_peers_info) | 118 | if (NULL != all_peers_info) |
91 | { | 119 | { |
92 | GNUNET_free (all_peers_info); | 120 | GNUNET_free (all_peers_info); |
@@ -97,6 +125,143 @@ do_shutdown () // TODO: schedule timeout shutdown | |||
97 | 125 | ||
98 | 126 | ||
99 | /** | 127 | /** |
128 | * Function called with each file/folder inside a directory that is being copied. | ||
129 | * | ||
130 | * @param cls closure, destination directory | ||
131 | * @param filename complete filename (absolute path) | ||
132 | * @return #GNUNET_OK to continue to iterate. | ||
133 | * #GNUNET_SYSERR to abort iteration with error | ||
134 | */ | ||
135 | static int | ||
136 | copy_dir_scanner (void *cls, const char *filename) | ||
137 | { | ||
138 | char *dst_dir = cls; | ||
139 | char *dst; | ||
140 | int copy_result; | ||
141 | |||
142 | GNUNET_asprintf (&dst, "%s%s%s", dst_dir, DIR_SEPARATOR_STR, | ||
143 | GNUNET_STRINGS_get_short_name (filename)); | ||
144 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Copying `%s' to `%s'.\n", filename, | ||
145 | dst); | ||
146 | if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_YES)) | ||
147 | copy_result = copy_dir (filename, dst); | ||
148 | else | ||
149 | { | ||
150 | if (GNUNET_YES == GNUNET_DISK_file_test (dst)) | ||
151 | GNUNET_DISK_directory_remove (dst); | ||
152 | copy_result = GNUNET_DISK_file_copy (filename, dst); | ||
153 | } | ||
154 | GNUNET_free (dst); | ||
155 | return copy_result; | ||
156 | } | ||
157 | |||
158 | |||
159 | /** | ||
160 | * Copy directory recursively | ||
161 | * | ||
162 | * @param src Path to source directory | ||
163 | * @param dst Destination directory, will be created if it does not exist | ||
164 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | ||
165 | */ | ||
166 | static int | ||
167 | copy_dir (const char *src, const char *dst) | ||
168 | { | ||
169 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Copying directory `%s' to `%s'.\n", src, | ||
170 | dst); | ||
171 | if (GNUNET_YES != GNUNET_DISK_directory_test (src, GNUNET_YES)) | ||
172 | return GNUNET_SYSERR; | ||
173 | if (GNUNET_OK != GNUNET_DISK_directory_create (dst)) | ||
174 | return GNUNET_SYSERR; | ||
175 | if (GNUNET_SYSERR == | ||
176 | GNUNET_DISK_directory_scan (src, ©_dir_scanner, (char *) dst)) | ||
177 | return GNUNET_SYSERR; | ||
178 | return GNUNET_OK; | ||
179 | } | ||
180 | |||
181 | |||
182 | /** | ||
183 | * Function called with each file/folder inside source sensor directory. | ||
184 | * | ||
185 | * @param cls closure (unused) | ||
186 | * @param filename complete filename (absolute path) | ||
187 | * @return #GNUNET_OK to continue to iterate. | ||
188 | */ | ||
189 | static int | ||
190 | sensor_dir_scanner (void *cls, const char *filename) | ||
191 | { | ||
192 | const char *file_basename; | ||
193 | char *dst_path; | ||
194 | struct GNUNET_CONFIGURATION_Handle *sensor_cfg; | ||
195 | |||
196 | file_basename = GNUNET_STRINGS_get_short_name (filename); | ||
197 | GNUNET_asprintf (&dst_path, "%s%s%s", sensor_dst_dir, DIR_SEPARATOR_STR, | ||
198 | file_basename); | ||
199 | if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_NO)) | ||
200 | { | ||
201 | GNUNET_assert (GNUNET_OK == copy_dir (filename, dst_path)); | ||
202 | } | ||
203 | else | ||
204 | { | ||
205 | sensor_cfg = GNUNET_CONFIGURATION_create (); | ||
206 | GNUNET_assert (GNUNET_OK == | ||
207 | GNUNET_CONFIGURATION_parse (sensor_cfg, filename)); | ||
208 | GNUNET_CONFIGURATION_set_value_string (sensor_cfg, file_basename, | ||
209 | "COLLECTION_POINT", | ||
210 | GNUNET_i2s_full (&all_peers_info[0]. | ||
211 | peer_id)); | ||
212 | if (sensors_interval > 0) | ||
213 | { | ||
214 | GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename, | ||
215 | "INTERVAL", | ||
216 | (unsigned long long int) | ||
217 | sensors_interval); | ||
218 | } | ||
219 | GNUNET_CONFIGURATION_write (sensor_cfg, dst_path); | ||
220 | GNUNET_CONFIGURATION_destroy (sensor_cfg); | ||
221 | } | ||
222 | GNUNET_free (dst_path); | ||
223 | return GNUNET_OK; | ||
224 | } | ||
225 | |||
226 | |||
227 | /** | ||
228 | * Load sensor definitions and rewrite them to tmp location. | ||
229 | * Add collection point peer id and change running interval if needed. | ||
230 | */ | ||
231 | static void | ||
232 | rewrite_sensors () | ||
233 | { | ||
234 | GNUNET_assert (GNUNET_YES == | ||
235 | GNUNET_DISK_directory_test (sensor_src_dir, GNUNET_YES)); | ||
236 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (sensor_dst_dir)); | ||
237 | GNUNET_DISK_directory_scan (sensor_src_dir, &sensor_dir_scanner, NULL); | ||
238 | } | ||
239 | |||
240 | |||
241 | /** | ||
242 | * Callback to be called when dashboard service is started | ||
243 | * | ||
244 | * @param cls the callback closure from functions generating an operation | ||
245 | * @param op the operation that has been finished | ||
246 | * @param emsg error message in case the operation has failed; will be NULL if | ||
247 | * operation has executed successfully. | ||
248 | */ | ||
249 | static void | ||
250 | dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op, | ||
251 | const char *emsg) | ||
252 | { | ||
253 | if (NULL != emsg) | ||
254 | { | ||
255 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg); | ||
256 | GNUNET_assert (0); | ||
257 | } | ||
258 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Dashboard service started.\n"); | ||
259 | //TODO: | ||
260 | GNUNET_TESTBED_operation_done (op); | ||
261 | } | ||
262 | |||
263 | |||
264 | /** | ||
100 | * Callback to be called when the requested peer information is available | 265 | * Callback to be called when the requested peer information is available |
101 | * | 266 | * |
102 | * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information() | 267 | * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information() |
@@ -115,11 +280,16 @@ peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op, | |||
115 | 280 | ||
116 | peer->testbed_peer = testbed_peer; | 281 | peer->testbed_peer = testbed_peer; |
117 | GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id); | 282 | GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id); |
118 | peers_known++; | 283 | if (0 == peers_known) /* First peer is collection point */ |
119 | if (peers_known == num_peers) //TODO: remove | ||
120 | { | 284 | { |
121 | do_shutdown (); | 285 | /* Rewrite sensors */ |
286 | rewrite_sensors (); | ||
287 | /* Start dashboard */ | ||
288 | GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensordashboard", | ||
289 | &dashboard_started, NULL, 1); | ||
122 | } | 290 | } |
291 | peers_known++; | ||
292 | GNUNET_TESTBED_operation_done (op); | ||
123 | } | 293 | } |
124 | 294 | ||
125 | 295 | ||
@@ -201,12 +371,13 @@ run (void *cls, char *const *args, const char *cf, | |||
201 | return; | 371 | return; |
202 | } | 372 | } |
203 | cfg = GNUNET_CONFIGURATION_create (); | 373 | cfg = GNUNET_CONFIGURATION_create (); |
204 | GNUNET_CONFIGURATION_load (cfg, cfg_filename); | 374 | GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_filename)); |
205 | links = log (num_peers) * log (num_peers) * num_peers / 2; | 375 | links = log (num_peers) * log (num_peers) * num_peers / 2; |
206 | GNUNET_CONFIGURATION_set_value_number ((struct GNUNET_CONFIGURATION_Handle *) | 376 | GNUNET_CONFIGURATION_set_value_number ((struct GNUNET_CONFIGURATION_Handle *) |
207 | cfg, "TESTBED", "OVERLAY_RANDOM_LINKS", | 377 | cfg, "TESTBED", "OVERLAY_RANDOM_LINKS", |
208 | (unsigned long long int) links); | 378 | (unsigned long long int) links); |
209 | GNUNET_TESTBED_run (NULL, cfg, num_peers, 0, NULL, NULL, &test_master, NULL); | 379 | GNUNET_TESTBED_run (NULL, cfg, num_peers, 0, NULL, NULL, &test_master, NULL); |
380 | GNUNET_CONFIGURATION_destroy (cfg); | ||
210 | } | 381 | } |
211 | 382 | ||
212 | 383 | ||
@@ -221,6 +392,9 @@ main (int argc, char *const *argv) | |||
221 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | 392 | static struct GNUNET_GETOPT_CommandLineOption options[] = { |
222 | {'p', "peers", "COUNT", gettext_noop ("Number of peers to run"), GNUNET_YES, | 393 | {'p', "peers", "COUNT", gettext_noop ("Number of peers to run"), GNUNET_YES, |
223 | &GNUNET_GETOPT_set_uint, &num_peers}, | 394 | &GNUNET_GETOPT_set_uint, &num_peers}, |
395 | {'i', "sensors-interval", "INTERVAL", | ||
396 | gettext_noop ("Change the interval or running sensors to given value"), | ||
397 | GNUNET_YES, &GNUNET_GETOPT_set_uint, &sensors_interval}, | ||
224 | GNUNET_GETOPT_OPTION_END | 398 | GNUNET_GETOPT_OPTION_END |
225 | }; | 399 | }; |
226 | 400 | ||