2 This file is part of GNUnet
3 Copyright (C) 2008--2014 GNUnet e.V.
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
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file testbed/gnunet-daemon-latency-logger.c
23 * @brief log latency values from neighbour connections into an SQLite database
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_util_lib.h"
29 #include "gnunet_ats_service.h"
36 #define LOG(type,...) \
37 GNUNET_log (type, __VA_ARGS__)
40 * Debug logging shorthand
43 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
46 * Log an error message at log-level 'level' that indicates
47 * a failure of the command 'cmd' on file 'filename'
48 * with the message given by strerror(errno).
50 #define LOG_SQLITE(db, msg, level, cmd) \
52 GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \
53 cmd, __FILE__,__LINE__, sqlite3_errmsg(db)); \
55 GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, \
56 __FILE__, __LINE__, sqlite3_errmsg(db)); \
61 * Entry type to be used in the map to store old latency values
68 struct GNUNET_PeerIdentity id;
71 * The last known value for latency.
80 * Handle to the map used to store old latency values for peers
82 static struct GNUNET_CONTAINER_MultiPeerMap *map;
85 * The SQLite database handle
87 static struct sqlite3 *db;
90 * Handle to the ATS performance subsystem
92 struct GNUNET_ATS_PerformanceHandle *ats;
95 * Prepared statement for inserting values into the database table
97 struct sqlite3_stmt *stmt_insert;
100 * Shutdown task identifier
102 struct GNUNET_SCHEDULER_Task * shutdown_task;
107 * Iterator over hash map entries.
110 * @param key current public key
111 * @param value value in the hash map
112 * @return #GNUNET_YES if we should continue to
117 free_iterator (void *cls,
118 const struct GNUNET_PeerIdentity *key,
121 struct Entry *e = cls;
123 GNUNET_assert (GNUNET_YES ==
124 GNUNET_CONTAINER_multipeermap_remove (map, key, e));
134 * @param tc task context from scheduler
138 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
140 shutdown_task = NULL;
141 GNUNET_ATS_performance_done (ats);
143 if (NULL != stmt_insert)
145 sqlite3_finalize (stmt_insert);
148 GNUNET_break (SQLITE_OK == sqlite3_close (db));
152 GNUNET_assert (GNUNET_SYSERR !=
153 GNUNET_CONTAINER_multipeermap_iterate (map, free_iterator, NULL));
154 GNUNET_CONTAINER_multipeermap_destroy (map);
160 * Signature of a function that is called with QoS information about an address.
163 * @param address the address
164 * @param address_active #GNUNET_YES if this address is actively used
165 * to maintain a connection to a peer;
166 * #GNUNET_NO if the address is not actively used;
167 * #GNUNET_SYSERR if this address is no longer available for ATS
168 * @param bandwidth_out assigned outbound bandwidth for the connection
169 * @param bandwidth_in assigned inbound bandwidth for the connection
170 * @param prop performance data for the address (as far as known)
173 addr_info_cb (void *cls,
174 const struct GNUNET_HELLO_Address *address,
176 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
177 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
178 const struct GNUNET_ATS_Properties *prop)
180 static const char *query_insert =
181 "INSERT INTO ats_info("
191 int latency; /* FIXME: type!? */
195 /* ATS service temporarily disconnected */
199 GNUNET_assert (NULL != db);
200 if (GNUNET_YES != address_active)
202 latency = (int) prop->delay.rel_value_us;
204 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (map,
207 entry = GNUNET_CONTAINER_multipeermap_get (map, &address->peer);
208 GNUNET_assert (NULL != entry);
209 if (latency == entry->latency)
212 if (NULL == stmt_insert)
214 if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1, &stmt_insert,
217 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
221 if ( (SQLITE_OK != sqlite3_bind_text (stmt_insert, 1,
222 GNUNET_i2s (&address->peer), -1,
224 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, latency)) )
226 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_text");
229 if (SQLITE_DONE != sqlite3_step (stmt_insert))
231 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
234 if (SQLITE_OK != sqlite3_reset (stmt_insert))
236 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_insert");
241 entry = GNUNET_new (struct Entry);
242 entry->id = address->peer;
243 GNUNET_CONTAINER_multipeermap_put (map,
245 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
247 entry->latency = latency;
251 GNUNET_SCHEDULER_shutdown ();
256 * Main function that will be run.
259 * @param args remaining command-line arguments
260 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
261 * @param c configuration
264 run (void *cls, char *const *args, const char *cfgfile,
265 const struct GNUNET_CONFIGURATION_Handle *c)
267 const char *query_create =
268 "CREATE TABLE ats_info ("
275 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "LATENCY-LOGGER",
282 if (SQLITE_OK != sqlite3_open (dbfile, &db))
286 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2");
287 GNUNET_break (SQLITE_OK == sqlite3_close (db));
290 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile);
291 GNUNET_free (dbfile);
294 if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL))
295 DEBUG ("SQLite Error: %d. Perhaps the database `%s' already exits.\n",
296 sqlite3_errcode (db), dbfile);
297 DEBUG ("Opened database %s\n", dbfile);
298 GNUNET_free (dbfile);
300 ats = GNUNET_ATS_performance_init (c, &addr_info_cb, NULL);
301 map = GNUNET_CONTAINER_multipeermap_create (30, GNUNET_YES);
302 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
308 * Execution entry point
311 main (int argc, char * const *argv)
313 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
314 GNUNET_GETOPT_OPTION_END
318 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
322 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-latency-logger",
323 _("Daemon to log latency values of connections to neighbours"),
324 options, &run, NULL)) ? 0 : 1;
325 GNUNET_free ((void*) argv);