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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
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/>.
20 * @file testbed/gnunet-daemon-latency-logger.c
21 * @brief log latency values from neighbour connections into an SQLite database
22 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
26 #include "gnunet_util_lib.h"
27 #include "gnunet_ats_service.h"
34 #define LOG(type,...) \
35 GNUNET_log (type, __VA_ARGS__)
38 * Debug logging shorthand
41 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
44 * Log an error message at log-level 'level' that indicates
45 * a failure of the command 'cmd' on file 'filename'
46 * with the message given by strerror(errno).
48 #define LOG_SQLITE(db, msg, level, cmd) \
50 GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \
51 cmd, __FILE__,__LINE__, sqlite3_errmsg(db)); \
53 GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, \
54 __FILE__, __LINE__, sqlite3_errmsg(db)); \
59 * Entry type to be used in the map to store old latency values
66 struct GNUNET_PeerIdentity id;
69 * The last known value for latency.
78 * Handle to the map used to store old latency values for peers
80 static struct GNUNET_CONTAINER_MultiPeerMap *map;
83 * The SQLite database handle
85 static struct sqlite3 *db;
88 * Handle to the ATS performance subsystem
90 static struct GNUNET_ATS_PerformanceHandle *ats;
93 * Prepared statement for inserting values into the database table
95 static struct sqlite3_stmt *stmt_insert;
100 * Iterator over hash map entries.
103 * @param key current public key
104 * @param value value in the hash map
105 * @return #GNUNET_YES if we should continue to
110 free_iterator (void *cls,
111 const struct GNUNET_PeerIdentity *key,
114 struct Entry *e = cls;
116 GNUNET_assert (GNUNET_YES ==
117 GNUNET_CONTAINER_multipeermap_remove (map, key, e));
130 do_shutdown (void *cls)
132 GNUNET_ATS_performance_done (ats);
134 if (NULL != stmt_insert)
136 sqlite3_finalize (stmt_insert);
139 GNUNET_break (SQLITE_OK == sqlite3_close (db));
143 GNUNET_assert (GNUNET_SYSERR !=
144 GNUNET_CONTAINER_multipeermap_iterate (map, free_iterator, NULL));
145 GNUNET_CONTAINER_multipeermap_destroy (map);
151 * Signature of a function that is called with QoS information about an address.
154 * @param address the address
155 * @param address_active #GNUNET_YES if this address is actively used
156 * to maintain a connection to a peer;
157 * #GNUNET_NO if the address is not actively used;
158 * #GNUNET_SYSERR if this address is no longer available for ATS
159 * @param bandwidth_out assigned outbound bandwidth for the connection
160 * @param bandwidth_in assigned inbound bandwidth for the connection
161 * @param prop performance data for the address (as far as known)
164 addr_info_cb (void *cls,
165 const struct GNUNET_HELLO_Address *address,
167 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
168 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
169 const struct GNUNET_ATS_Properties *prop)
171 static const char *query_insert =
172 "INSERT INTO ats_info("
182 int latency; /* FIXME: type!? */
186 /* ATS service temporarily disconnected */
190 GNUNET_assert (NULL != db);
191 if (GNUNET_YES != address_active)
193 latency = (int) prop->delay.rel_value_us;
195 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (map,
198 entry = GNUNET_CONTAINER_multipeermap_get (map, &address->peer);
199 GNUNET_assert (NULL != entry);
200 if (latency == entry->latency)
203 if (NULL == stmt_insert)
205 if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1, &stmt_insert,
208 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
212 if ( (SQLITE_OK != sqlite3_bind_text (stmt_insert, 1,
213 GNUNET_i2s (&address->peer), -1,
215 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, latency)) )
217 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_text");
220 if (SQLITE_DONE != sqlite3_step (stmt_insert))
222 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
225 if (SQLITE_OK != sqlite3_reset (stmt_insert))
227 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_insert");
232 entry = GNUNET_new (struct Entry);
233 entry->id = address->peer;
234 GNUNET_CONTAINER_multipeermap_put (map,
236 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
238 entry->latency = latency;
242 GNUNET_SCHEDULER_shutdown ();
247 * Main function that will be run.
250 * @param args remaining command-line arguments
251 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
252 * @param c configuration
255 run (void *cls, char *const *args, const char *cfgfile,
256 const struct GNUNET_CONFIGURATION_Handle *c)
258 const char *query_create =
259 "CREATE TABLE ats_info ("
266 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "LATENCY-LOGGER",
273 if (SQLITE_OK != sqlite3_open (dbfile, &db))
277 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2");
278 GNUNET_break (SQLITE_OK == sqlite3_close (db));
281 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile);
282 GNUNET_free (dbfile);
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);
291 ats = GNUNET_ATS_performance_init (c, &addr_info_cb, NULL);
292 map = GNUNET_CONTAINER_multipeermap_create (30, GNUNET_YES);
293 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
298 * Execution entry point
301 main (int argc, char * const *argv)
303 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
304 GNUNET_GETOPT_OPTION_END
308 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
312 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-latency-logger",
313 _("Daemon to log latency values of connections to neighbours"),
314 options, &run, NULL)) ? 0 : 1;
315 GNUNET_free ((void*) argv);