diff options
Diffstat (limited to 'src/testbed/gnunet-daemon-latency-logger.c')
-rw-r--r-- | src/testbed/gnunet-daemon-latency-logger.c | 264 |
1 files changed, 131 insertions, 133 deletions
diff --git a/src/testbed/gnunet-daemon-latency-logger.c b/src/testbed/gnunet-daemon-latency-logger.c index a1685d849..5e0fffe2b 100644 --- a/src/testbed/gnunet-daemon-latency-logger.c +++ b/src/testbed/gnunet-daemon-latency-logger.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file testbed/gnunet-daemon-latency-logger.c | 22 | * @file testbed/gnunet-daemon-latency-logger.c |
@@ -33,14 +33,14 @@ | |||
33 | /** | 33 | /** |
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,19 +49,18 @@ | |||
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", _("`%s' failed at %s:%d with error: %s\n"), \ |
53 | cmd, __FILE__,__LINE__, sqlite3_errmsg(db)); \ | 53 | cmd, __FILE__, __LINE__, sqlite3_errmsg(db)); \ |
54 | if (msg != NULL) \ | 54 | if (msg != NULL) \ |
55 | GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, \ | 55 | GNUNET_asprintf (msg, _("`%s' failed at %s:%u with error: %s"), cmd, \ |
56 | __FILE__, __LINE__, sqlite3_errmsg(db)); \ | 56 | __FILE__, __LINE__, sqlite3_errmsg(db)); \ |
57 | } while(0) | 57 | } while (0) |
58 | 58 | ||
59 | 59 | ||
60 | /** | 60 | /** |
61 | * Entry type to be used in the map to store old latency values | 61 | * Entry type to be used in the map to store old latency values |
62 | */ | 62 | */ |
63 | struct Entry | 63 | struct Entry { |
64 | { | ||
65 | /** | 64 | /** |
66 | * The peer's identity | 65 | * The peer's identity |
67 | */ | 66 | */ |
@@ -72,7 +71,6 @@ struct Entry | |||
72 | * FIXME: type! | 71 | * FIXME: type! |
73 | */ | 72 | */ |
74 | unsigned int latency; | 73 | unsigned int latency; |
75 | |||
76 | }; | 74 | }; |
77 | 75 | ||
78 | 76 | ||
@@ -109,15 +107,15 @@ static struct sqlite3_stmt *stmt_insert; | |||
109 | * #GNUNET_NO if not. | 107 | * #GNUNET_NO if not. |
110 | */ | 108 | */ |
111 | static int | 109 | static int |
112 | free_iterator (void *cls, | 110 | free_iterator(void *cls, |
113 | const struct GNUNET_PeerIdentity *key, | 111 | const struct GNUNET_PeerIdentity *key, |
114 | void *value) | 112 | void *value) |
115 | { | 113 | { |
116 | struct Entry *e = cls; | 114 | struct Entry *e = cls; |
117 | 115 | ||
118 | GNUNET_assert (GNUNET_YES == | 116 | GNUNET_assert(GNUNET_YES == |
119 | GNUNET_CONTAINER_multipeermap_remove (map, key, e)); | 117 | GNUNET_CONTAINER_multipeermap_remove(map, key, e)); |
120 | GNUNET_free (e); | 118 | GNUNET_free(e); |
121 | return GNUNET_YES; | 119 | return GNUNET_YES; |
122 | } | 120 | } |
123 | 121 | ||
@@ -129,24 +127,24 @@ free_iterator (void *cls, | |||
129 | * @return | 127 | * @return |
130 | */ | 128 | */ |
131 | static void | 129 | static void |
132 | do_shutdown (void *cls) | 130 | do_shutdown(void *cls) |
133 | { | 131 | { |
134 | GNUNET_ATS_performance_done (ats); | 132 | GNUNET_ATS_performance_done(ats); |
135 | ats = NULL; | 133 | ats = NULL; |
136 | if (NULL != stmt_insert) | 134 | if (NULL != stmt_insert) |
137 | { | 135 | { |
138 | sqlite3_finalize (stmt_insert); | 136 | sqlite3_finalize(stmt_insert); |
139 | stmt_insert = NULL; | 137 | stmt_insert = NULL; |
140 | } | 138 | } |
141 | GNUNET_break (SQLITE_OK == sqlite3_close (db)); | 139 | GNUNET_break(SQLITE_OK == sqlite3_close(db)); |
142 | db = NULL; | 140 | db = NULL; |
143 | if (NULL != map) | 141 | if (NULL != map) |
144 | { | 142 | { |
145 | GNUNET_assert (GNUNET_SYSERR != | 143 | GNUNET_assert(GNUNET_SYSERR != |
146 | GNUNET_CONTAINER_multipeermap_iterate (map, free_iterator, NULL)); | 144 | GNUNET_CONTAINER_multipeermap_iterate(map, free_iterator, NULL)); |
147 | GNUNET_CONTAINER_multipeermap_destroy (map); | 145 | GNUNET_CONTAINER_multipeermap_destroy(map); |
148 | map = NULL; | 146 | map = NULL; |
149 | } | 147 | } |
150 | } | 148 | } |
151 | 149 | ||
152 | /** | 150 | /** |
@@ -163,85 +161,85 @@ do_shutdown (void *cls) | |||
163 | * @param prop performance data for the address (as far as known) | 161 | * @param prop performance data for the address (as far as known) |
164 | */ | 162 | */ |
165 | static void | 163 | static void |
166 | addr_info_cb (void *cls, | 164 | addr_info_cb(void *cls, |
167 | const struct GNUNET_HELLO_Address *address, | 165 | const struct GNUNET_HELLO_Address *address, |
168 | int address_active, | 166 | int address_active, |
169 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out, | 167 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out, |
170 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, | 168 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, |
171 | const struct GNUNET_ATS_Properties *prop) | 169 | const struct GNUNET_ATS_Properties *prop) |
172 | { | 170 | { |
173 | static const char *query_insert = | 171 | static const char *query_insert = |
174 | "INSERT INTO ats_info(" | 172 | "INSERT INTO ats_info(" |
175 | " id," | 173 | " id," |
176 | " val," | 174 | " val," |
177 | " timestamp" | 175 | " timestamp" |
178 | ") VALUES (" | 176 | ") VALUES (" |
179 | " ?1," | 177 | " ?1," |
180 | " ?2," | 178 | " ?2," |
181 | " datetime('now')" | 179 | " datetime('now')" |
182 | ");"; | 180 | ");"; |
183 | struct Entry *entry; | 181 | struct Entry *entry; |
184 | int latency; /* FIXME: type!? */ | 182 | int latency; /* FIXME: type!? */ |
185 | 183 | ||
186 | if (NULL == address) | 184 | if (NULL == address) |
187 | { | 185 | { |
188 | /* ATS service temporarily disconnected */ | 186 | /* ATS service temporarily disconnected */ |
189 | return; | 187 | return; |
190 | } | 188 | } |
191 | 189 | ||
192 | GNUNET_assert (NULL != db); | 190 | GNUNET_assert(NULL != db); |
193 | if (GNUNET_YES != address_active) | 191 | if (GNUNET_YES != address_active) |
194 | return; | 192 | return; |
195 | latency = (int) prop->delay.rel_value_us; | 193 | latency = (int)prop->delay.rel_value_us; |
196 | entry = NULL; | 194 | entry = NULL; |
197 | if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (map, | 195 | if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains(map, |
198 | &address->peer)) | 196 | &address->peer)) |
199 | { | 197 | { |
200 | entry = GNUNET_CONTAINER_multipeermap_get (map, &address->peer); | 198 | entry = GNUNET_CONTAINER_multipeermap_get(map, &address->peer); |
201 | GNUNET_assert (NULL != entry); | 199 | GNUNET_assert(NULL != entry); |
202 | if (latency == entry->latency) | 200 | if (latency == entry->latency) |
203 | return; | 201 | return; |
204 | } | 202 | } |
205 | if (NULL == stmt_insert) | 203 | if (NULL == stmt_insert) |
206 | { | ||
207 | if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1, &stmt_insert, | ||
208 | NULL)) | ||
209 | { | 204 | { |
210 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2"); | 205 | if (SQLITE_OK != sqlite3_prepare_v2(db, query_insert, -1, &stmt_insert, |
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"); | ||
211 | goto err_shutdown; | 228 | goto err_shutdown; |
212 | } | 229 | } |
213 | } | ||
214 | if ( (SQLITE_OK != sqlite3_bind_text (stmt_insert, 1, | ||
215 | GNUNET_i2s (&address->peer), -1, | ||
216 | SQLITE_STATIC)) || | ||
217 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, latency)) ) | ||
218 | { | ||
219 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_text"); | ||
220 | goto err_shutdown; | ||
221 | } | ||
222 | if (SQLITE_DONE != sqlite3_step (stmt_insert)) | ||
223 | { | ||
224 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step"); | ||
225 | goto err_shutdown; | ||
226 | } | ||
227 | if (SQLITE_OK != sqlite3_reset (stmt_insert)) | ||
228 | { | ||
229 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_insert"); | ||
230 | goto err_shutdown; | ||
231 | } | ||
232 | if (NULL == entry) | 230 | if (NULL == entry) |
233 | { | 231 | { |
234 | entry = GNUNET_new (struct Entry); | 232 | entry = GNUNET_new(struct Entry); |
235 | entry->id = address->peer; | 233 | entry->id = address->peer; |
236 | GNUNET_CONTAINER_multipeermap_put (map, | 234 | GNUNET_CONTAINER_multipeermap_put(map, |
237 | &entry->id, entry, | 235 | &entry->id, entry, |
238 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | 236 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); |
239 | } | 237 | } |
240 | entry->latency = latency; | 238 | entry->latency = latency; |
241 | return; | 239 | return; |
242 | 240 | ||
243 | err_shutdown: | 241 | err_shutdown: |
244 | GNUNET_SCHEDULER_shutdown (); | 242 | GNUNET_SCHEDULER_shutdown(); |
245 | } | 243 | } |
246 | 244 | ||
247 | 245 | ||
@@ -254,45 +252,45 @@ addr_info_cb (void *cls, | |||
254 | * @param c configuration | 252 | * @param c configuration |
255 | */ | 253 | */ |
256 | static void | 254 | static void |
257 | run (void *cls, char *const *args, const char *cfgfile, | 255 | run(void *cls, char *const *args, const char *cfgfile, |
258 | const struct GNUNET_CONFIGURATION_Handle *c) | 256 | const struct GNUNET_CONFIGURATION_Handle *c) |
259 | { | 257 | { |
260 | const char *query_create = | 258 | const char *query_create = |
261 | "CREATE TABLE ats_info (" | 259 | "CREATE TABLE ats_info (" |
262 | "id TEXT," | 260 | "id TEXT," |
263 | "val INTEGER," | 261 | "val INTEGER," |
264 | "timestamp NUMERIC" | 262 | "timestamp NUMERIC" |
265 | ");"; | 263 | ");"; |
266 | char *dbfile; | 264 | char *dbfile; |
267 | 265 | ||
268 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "LATENCY-LOGGER", | 266 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(c, "LATENCY-LOGGER", |
269 | "DBFILE", | 267 | "DBFILE", |
270 | &dbfile)) | 268 | &dbfile)) |
271 | { | ||
272 | GNUNET_break (0); | ||
273 | return; | ||
274 | } | ||
275 | if (SQLITE_OK != sqlite3_open (dbfile, &db)) | ||
276 | { | ||
277 | if (NULL != db) | ||
278 | { | 269 | { |
279 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2"); | 270 | GNUNET_break(0); |
280 | GNUNET_break (SQLITE_OK == sqlite3_close (db)); | 271 | return; |
281 | } | 272 | } |
282 | else | 273 | if (SQLITE_OK != sqlite3_open(dbfile, &db)) |
283 | LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile); | 274 | { |
284 | GNUNET_free (dbfile); | 275 | if (NULL != db) |
285 | return; | 276 | { |
286 | } | 277 | LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2"); |
287 | if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL)) | 278 | GNUNET_break(SQLITE_OK == sqlite3_close(db)); |
288 | DEBUG ("SQLite Error: %d. Perhaps the database `%s' already exits.\n", | 279 | } |
289 | sqlite3_errcode (db), dbfile); | 280 | else |
290 | DEBUG ("Opened database %s\n", dbfile); | 281 | LOG(GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile); |
291 | GNUNET_free (dbfile); | 282 | GNUNET_free(dbfile); |
283 | return; | ||
284 | } | ||
285 | if (0 != sqlite3_exec(db, query_create, NULL, NULL, NULL)) | ||
286 | DEBUG("SQLite Error: %d. Perhaps the database `%s' already exits.\n", | ||
287 | sqlite3_errcode(db), dbfile); | ||
288 | DEBUG("Opened database %s\n", dbfile); | ||
289 | GNUNET_free(dbfile); | ||
292 | dbfile = NULL; | 290 | dbfile = NULL; |
293 | ats = GNUNET_ATS_performance_init (c, &addr_info_cb, NULL); | 291 | ats = GNUNET_ATS_performance_init(c, &addr_info_cb, NULL); |
294 | map = GNUNET_CONTAINER_multipeermap_create (30, GNUNET_YES); | 292 | map = GNUNET_CONTAINER_multipeermap_create(30, GNUNET_YES); |
295 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); | 293 | GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); |
296 | } | 294 | } |
297 | 295 | ||
298 | 296 | ||
@@ -300,20 +298,20 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
300 | * Execution entry point | 298 | * Execution entry point |
301 | */ | 299 | */ |
302 | int | 300 | int |
303 | main (int argc, char * const *argv) | 301 | main(int argc, char * const *argv) |
304 | { | 302 | { |
305 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | 303 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { |
306 | GNUNET_GETOPT_OPTION_END | 304 | GNUNET_GETOPT_OPTION_END |
307 | }; | 305 | }; |
308 | int ret; | 306 | int ret; |
309 | 307 | ||
310 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | 308 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) |
311 | return 2; | 309 | return 2; |
312 | ret = | 310 | ret = |
313 | (GNUNET_OK == | 311 | (GNUNET_OK == |
314 | GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-latency-logger", | 312 | GNUNET_PROGRAM_run(argc, argv, "gnunet-daemon-latency-logger", |
315 | _("Daemon to log latency values of connections to neighbours"), | 313 | _("Daemon to log latency values of connections to neighbours"), |
316 | options, &run, NULL)) ? 0 : 1; | 314 | options, &run, NULL)) ? 0 : 1; |
317 | GNUNET_free ((void*) argv); | 315 | GNUNET_free((void*)argv); |
318 | return ret; | 316 | return ret; |
319 | } | 317 | } |