diff options
Diffstat (limited to 'src/testbed/gnunet-daemon-latency-logger.c')
-rw-r--r-- | src/testbed/gnunet-daemon-latency-logger.c | 228 |
1 files changed, 116 insertions, 112 deletions
diff --git a/src/testbed/gnunet-daemon-latency-logger.c b/src/testbed/gnunet-daemon-latency-logger.c index 5e0fffe2b..c35282048 100644 --- a/src/testbed/gnunet-daemon-latency-logger.c +++ b/src/testbed/gnunet-daemon-latency-logger.c | |||
@@ -34,13 +34,13 @@ | |||
34 | * Logging shorthand | 34 | * Logging shorthand |
35 | */ | 35 | */ |
36 | #define LOG(type, ...) \ | 36 | #define LOG(type, ...) \ |
37 | GNUNET_log(type, __VA_ARGS__) | 37 | GNUNET_log (type, __VA_ARGS__) |
38 | 38 | ||
39 | /** | 39 | /** |
40 | * Debug logging shorthand | 40 | * Debug logging shorthand |
41 | */ | 41 | */ |
42 | #define DEBUG(...) \ | 42 | #define DEBUG(...) \ |
43 | LOG(GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__) | 43 | LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__) |
44 | 44 | ||
45 | /** | 45 | /** |
46 | * Log an error message at log-level 'level' that indicates | 46 | * Log an error message at log-level 'level' that indicates |
@@ -49,18 +49,20 @@ | |||
49 | */ | 49 | */ |
50 | #define LOG_SQLITE(db, msg, level, cmd) \ | 50 | #define LOG_SQLITE(db, msg, level, cmd) \ |
51 | do { \ | 51 | do { \ |
52 | GNUNET_log_from(level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \ | 52 | GNUNET_log_from (level, "sqlite", _ ( \ |
53 | cmd, __FILE__, __LINE__, sqlite3_errmsg(db)); \ | 53 | "`%s' failed at %s:%d with error: %s\n"), \ |
54 | if (msg != NULL) \ | 54 | cmd, __FILE__, __LINE__, sqlite3_errmsg (db)); \ |
55 | GNUNET_asprintf (msg, _("`%s' failed at %s:%u with error: %s"), cmd, \ | 55 | if (msg != NULL) \ |
56 | __FILE__, __LINE__, sqlite3_errmsg(db)); \ | 56 | GNUNET_asprintf (msg, _ ("`%s' failed at %s:%u with error: %s"), cmd, \ |
57 | } while (0) | 57 | __FILE__, __LINE__, sqlite3_errmsg (db)); \ |
58 | } while (0) | ||
58 | 59 | ||
59 | 60 | ||
60 | /** | 61 | /** |
61 | * Entry type to be used in the map to store old latency values | 62 | * Entry type to be used in the map to store old latency values |
62 | */ | 63 | */ |
63 | struct Entry { | 64 | struct Entry |
65 | { | ||
64 | /** | 66 | /** |
65 | * The peer's identity | 67 | * The peer's identity |
66 | */ | 68 | */ |
@@ -107,15 +109,15 @@ static struct sqlite3_stmt *stmt_insert; | |||
107 | * #GNUNET_NO if not. | 109 | * #GNUNET_NO if not. |
108 | */ | 110 | */ |
109 | static int | 111 | static int |
110 | free_iterator(void *cls, | 112 | free_iterator (void *cls, |
111 | const struct GNUNET_PeerIdentity *key, | 113 | const struct GNUNET_PeerIdentity *key, |
112 | void *value) | 114 | void *value) |
113 | { | 115 | { |
114 | struct Entry *e = cls; | 116 | struct Entry *e = cls; |
115 | 117 | ||
116 | GNUNET_assert(GNUNET_YES == | 118 | GNUNET_assert (GNUNET_YES == |
117 | GNUNET_CONTAINER_multipeermap_remove(map, key, e)); | 119 | GNUNET_CONTAINER_multipeermap_remove (map, key, e)); |
118 | GNUNET_free(e); | 120 | GNUNET_free (e); |
119 | return GNUNET_YES; | 121 | return GNUNET_YES; |
120 | } | 122 | } |
121 | 123 | ||
@@ -127,24 +129,25 @@ free_iterator(void *cls, | |||
127 | * @return | 129 | * @return |
128 | */ | 130 | */ |
129 | static void | 131 | static void |
130 | do_shutdown(void *cls) | 132 | do_shutdown (void *cls) |
131 | { | 133 | { |
132 | GNUNET_ATS_performance_done(ats); | 134 | GNUNET_ATS_performance_done (ats); |
133 | ats = NULL; | 135 | ats = NULL; |
134 | if (NULL != stmt_insert) | 136 | if (NULL != stmt_insert) |
135 | { | 137 | { |
136 | sqlite3_finalize(stmt_insert); | 138 | sqlite3_finalize (stmt_insert); |
137 | stmt_insert = NULL; | 139 | stmt_insert = NULL; |
138 | } | 140 | } |
139 | GNUNET_break(SQLITE_OK == sqlite3_close(db)); | 141 | GNUNET_break (SQLITE_OK == sqlite3_close (db)); |
140 | db = NULL; | 142 | db = NULL; |
141 | if (NULL != map) | 143 | if (NULL != map) |
142 | { | 144 | { |
143 | GNUNET_assert(GNUNET_SYSERR != | 145 | GNUNET_assert (GNUNET_SYSERR != |
144 | GNUNET_CONTAINER_multipeermap_iterate(map, free_iterator, NULL)); | 146 | GNUNET_CONTAINER_multipeermap_iterate (map, free_iterator, |
145 | GNUNET_CONTAINER_multipeermap_destroy(map); | 147 | NULL)); |
146 | map = NULL; | 148 | GNUNET_CONTAINER_multipeermap_destroy (map); |
147 | } | 149 | map = NULL; |
150 | } | ||
148 | } | 151 | } |
149 | 152 | ||
150 | /** | 153 | /** |
@@ -161,12 +164,12 @@ do_shutdown(void *cls) | |||
161 | * @param prop performance data for the address (as far as known) | 164 | * @param prop performance data for the address (as far as known) |
162 | */ | 165 | */ |
163 | static void | 166 | static void |
164 | addr_info_cb(void *cls, | 167 | addr_info_cb (void *cls, |
165 | const struct GNUNET_HELLO_Address *address, | 168 | const struct GNUNET_HELLO_Address *address, |
166 | int address_active, | 169 | int address_active, |
167 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out, | 170 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out, |
168 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, | 171 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, |
169 | const struct GNUNET_ATS_Properties *prop) | 172 | const struct GNUNET_ATS_Properties *prop) |
170 | { | 173 | { |
171 | static const char *query_insert = | 174 | static const char *query_insert = |
172 | "INSERT INTO ats_info(" | 175 | "INSERT INTO ats_info(" |
@@ -182,64 +185,64 @@ addr_info_cb(void *cls, | |||
182 | int latency; /* FIXME: type!? */ | 185 | int latency; /* FIXME: type!? */ |
183 | 186 | ||
184 | if (NULL == address) | 187 | if (NULL == address) |
185 | { | 188 | { |
186 | /* ATS service temporarily disconnected */ | 189 | /* ATS service temporarily disconnected */ |
187 | return; | 190 | return; |
188 | } | 191 | } |
189 | 192 | ||
190 | GNUNET_assert(NULL != db); | 193 | GNUNET_assert (NULL != db); |
191 | if (GNUNET_YES != address_active) | 194 | if (GNUNET_YES != address_active) |
192 | return; | 195 | return; |
193 | latency = (int)prop->delay.rel_value_us; | 196 | latency = (int) prop->delay.rel_value_us; |
194 | entry = NULL; | 197 | entry = NULL; |
195 | if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains(map, | 198 | if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (map, |
196 | &address->peer)) | 199 | &address->peer)) |
197 | { | 200 | { |
198 | entry = GNUNET_CONTAINER_multipeermap_get(map, &address->peer); | 201 | entry = GNUNET_CONTAINER_multipeermap_get (map, &address->peer); |
199 | GNUNET_assert(NULL != entry); | 202 | GNUNET_assert (NULL != entry); |
200 | if (latency == entry->latency) | 203 | if (latency == entry->latency) |
201 | return; | 204 | return; |
202 | } | 205 | } |
203 | if (NULL == stmt_insert) | 206 | if (NULL == stmt_insert) |
207 | { | ||
208 | if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1, &stmt_insert, | ||
209 | NULL)) | ||
204 | { | 210 | { |
205 | if (SQLITE_OK != sqlite3_prepare_v2(db, query_insert, -1, &stmt_insert, | 211 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2"); |
206 | NULL)) | ||
207 | { | ||
208 | LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2"); | ||
209 | goto err_shutdown; | ||
210 | } | ||
211 | } | ||
212 | if ((SQLITE_OK != sqlite3_bind_text(stmt_insert, 1, | ||
213 | GNUNET_i2s(&address->peer), -1, | ||
214 | SQLITE_STATIC)) || | ||
215 | (SQLITE_OK != sqlite3_bind_int(stmt_insert, 2, latency))) | ||
216 | { | ||
217 | LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_text"); | ||
218 | goto err_shutdown; | ||
219 | } | ||
220 | if (SQLITE_DONE != sqlite3_step(stmt_insert)) | ||
221 | { | ||
222 | LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step"); | ||
223 | goto err_shutdown; | ||
224 | } | ||
225 | if (SQLITE_OK != sqlite3_reset(stmt_insert)) | ||
226 | { | ||
227 | LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_insert"); | ||
228 | goto err_shutdown; | 212 | goto err_shutdown; |
229 | } | 213 | } |
214 | } | ||
215 | if ((SQLITE_OK != sqlite3_bind_text (stmt_insert, 1, | ||
216 | GNUNET_i2s (&address->peer), -1, | ||
217 | SQLITE_STATIC)) || | ||
218 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, latency))) | ||
219 | { | ||
220 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_text"); | ||
221 | goto err_shutdown; | ||
222 | } | ||
223 | if (SQLITE_DONE != sqlite3_step (stmt_insert)) | ||
224 | { | ||
225 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step"); | ||
226 | goto err_shutdown; | ||
227 | } | ||
228 | if (SQLITE_OK != sqlite3_reset (stmt_insert)) | ||
229 | { | ||
230 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_insert"); | ||
231 | goto err_shutdown; | ||
232 | } | ||
230 | if (NULL == entry) | 233 | if (NULL == entry) |
231 | { | 234 | { |
232 | entry = GNUNET_new(struct Entry); | 235 | entry = GNUNET_new (struct Entry); |
233 | entry->id = address->peer; | 236 | entry->id = address->peer; |
234 | GNUNET_CONTAINER_multipeermap_put(map, | 237 | GNUNET_CONTAINER_multipeermap_put (map, |
235 | &entry->id, entry, | 238 | &entry->id, entry, |
236 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | 239 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); |
237 | } | 240 | } |
238 | entry->latency = latency; | 241 | entry->latency = latency; |
239 | return; | 242 | return; |
240 | 243 | ||
241 | err_shutdown: | 244 | err_shutdown: |
242 | GNUNET_SCHEDULER_shutdown(); | 245 | GNUNET_SCHEDULER_shutdown (); |
243 | } | 246 | } |
244 | 247 | ||
245 | 248 | ||
@@ -252,8 +255,8 @@ err_shutdown: | |||
252 | * @param c configuration | 255 | * @param c configuration |
253 | */ | 256 | */ |
254 | static void | 257 | static void |
255 | run(void *cls, char *const *args, const char *cfgfile, | 258 | run (void *cls, char *const *args, const char *cfgfile, |
256 | const struct GNUNET_CONFIGURATION_Handle *c) | 259 | const struct GNUNET_CONFIGURATION_Handle *c) |
257 | { | 260 | { |
258 | const char *query_create = | 261 | const char *query_create = |
259 | "CREATE TABLE ats_info (" | 262 | "CREATE TABLE ats_info (" |
@@ -263,34 +266,34 @@ run(void *cls, char *const *args, const char *cfgfile, | |||
263 | ");"; | 266 | ");"; |
264 | char *dbfile; | 267 | char *dbfile; |
265 | 268 | ||
266 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(c, "LATENCY-LOGGER", | 269 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "LATENCY-LOGGER", |
267 | "DBFILE", | 270 | "DBFILE", |
268 | &dbfile)) | 271 | &dbfile)) |
269 | { | 272 | { |
270 | GNUNET_break(0); | 273 | GNUNET_break (0); |
271 | return; | 274 | return; |
272 | } | 275 | } |
273 | if (SQLITE_OK != sqlite3_open(dbfile, &db)) | 276 | if (SQLITE_OK != sqlite3_open (dbfile, &db)) |
277 | { | ||
278 | if (NULL != db) | ||
274 | { | 279 | { |
275 | if (NULL != db) | 280 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2"); |
276 | { | 281 | GNUNET_break (SQLITE_OK == sqlite3_close (db)); |
277 | LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2"); | ||
278 | GNUNET_break(SQLITE_OK == sqlite3_close(db)); | ||
279 | } | ||
280 | else | ||
281 | LOG(GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile); | ||
282 | GNUNET_free(dbfile); | ||
283 | return; | ||
284 | } | 282 | } |
285 | if (0 != sqlite3_exec(db, query_create, NULL, NULL, NULL)) | 283 | else |
286 | DEBUG("SQLite Error: %d. Perhaps the database `%s' already exits.\n", | 284 | LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile); |
287 | sqlite3_errcode(db), dbfile); | 285 | GNUNET_free (dbfile); |
288 | DEBUG("Opened database %s\n", dbfile); | 286 | return; |
289 | GNUNET_free(dbfile); | 287 | } |
288 | if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL)) | ||
289 | DEBUG ("SQLite Error: %d. Perhaps the database `%s' already exits.\n", | ||
290 | sqlite3_errcode (db), dbfile); | ||
291 | DEBUG ("Opened database %s\n", dbfile); | ||
292 | GNUNET_free (dbfile); | ||
290 | dbfile = NULL; | 293 | dbfile = NULL; |
291 | ats = GNUNET_ATS_performance_init(c, &addr_info_cb, NULL); | 294 | ats = GNUNET_ATS_performance_init (c, &addr_info_cb, NULL); |
292 | map = GNUNET_CONTAINER_multipeermap_create(30, GNUNET_YES); | 295 | map = GNUNET_CONTAINER_multipeermap_create (30, GNUNET_YES); |
293 | GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); | 296 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); |
294 | } | 297 | } |
295 | 298 | ||
296 | 299 | ||
@@ -298,20 +301,21 @@ run(void *cls, char *const *args, const char *cfgfile, | |||
298 | * Execution entry point | 301 | * Execution entry point |
299 | */ | 302 | */ |
300 | int | 303 | int |
301 | main(int argc, char * const *argv) | 304 | main (int argc, char *const *argv) |
302 | { | 305 | { |
303 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | 306 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { |
304 | GNUNET_GETOPT_OPTION_END | 307 | GNUNET_GETOPT_OPTION_END |
305 | }; | 308 | }; |
306 | int ret; | 309 | int ret; |
307 | 310 | ||
308 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) | 311 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) |
309 | return 2; | 312 | return 2; |
310 | ret = | 313 | ret = |
311 | (GNUNET_OK == | 314 | (GNUNET_OK == |
312 | GNUNET_PROGRAM_run(argc, argv, "gnunet-daemon-latency-logger", | 315 | GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-latency-logger", |
313 | _("Daemon to log latency values of connections to neighbours"), | 316 | _ ( |
314 | options, &run, NULL)) ? 0 : 1; | 317 | "Daemon to log latency values of connections to neighbours"), |
315 | GNUNET_free((void*)argv); | 318 | options, &run, NULL)) ? 0 : 1; |
319 | GNUNET_free ((void*) argv); | ||
316 | return ret; | 320 | return ret; |
317 | } | 321 | } |