2 This file is part of GNUnet
3 (C) 2008--2014 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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
79 * Handle to the map used to store old latency values for peers
81 static struct GNUNET_CONTAINER_MultiPeerMap *map;
84 * The SQLite database handle
86 static struct sqlite3 *db;
89 * Handle to the ATS performance subsystem
91 struct GNUNET_ATS_PerformanceHandle *ats;
94 * Prepared statement for inserting values into the database table
96 struct sqlite3_stmt *stmt_insert;
99 * Shutdown task identifier
101 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
106 * Iterator over hash map entries.
109 * @param key current public key
110 * @param value value in the hash map
111 * @return #GNUNET_YES if we should continue to
116 free_iterator (void *cls,
117 const struct GNUNET_PeerIdentity *key,
120 struct Entry *e = cls;
122 GNUNET_assert (GNUNET_YES ==
123 GNUNET_CONTAINER_multipeermap_remove (map, key, e));
133 * @param tc task context from scheduler
137 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
139 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
140 GNUNET_ATS_performance_done (ats);
142 if (NULL != stmt_insert)
144 sqlite3_finalize (stmt_insert);
147 GNUNET_break (SQLITE_OK == sqlite3_close (db));
151 GNUNET_assert (GNUNET_SYSERR !=
152 GNUNET_CONTAINER_multipeermap_iterate (map, free_iterator, NULL));
153 GNUNET_CONTAINER_multipeermap_destroy (map);
159 * Signature of a function that is called with QoS information about an address.
162 * @param address the address
163 * @param address_active is this address actively used to maintain a connection
165 * @param bandwidth_out assigned outbound bandwidth for the connection
166 * @param bandwidth_in assigned inbound bandwidth for the connection
167 * @param ats performance data for the address (as far as known)
168 * @param ats_count number of performance records in 'ats'
171 addr_info_cb (void *cls,
172 const struct GNUNET_HELLO_Address *address,
174 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
175 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
176 const struct GNUNET_ATS_Information *ats,
179 static const char *query_insert =
180 "INSERT INTO ats_info("
193 GNUNET_assert (NULL != db);
194 if (GNUNET_NO == address_active)
196 for (cnt = 0; cnt < ats_count; cnt++)
198 if (GNUNET_ATS_QUALITY_NET_DELAY == ntohl (ats[cnt].type))
204 latency = (int) ntohl (ats[cnt].value);
206 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (map,
209 entry = GNUNET_CONTAINER_multipeermap_get (map, &address->peer);
210 GNUNET_assert (NULL != entry);
211 if (latency == entry->latency)
214 if (NULL == stmt_insert)
216 if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1, &stmt_insert,
219 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
223 if ( (SQLITE_OK != sqlite3_bind_text (stmt_insert, 1,
224 GNUNET_i2s (&address->peer), -1,
226 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, latency)) )
228 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_text");
231 if (SQLITE_DONE != sqlite3_step (stmt_insert))
233 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
236 if (SQLITE_OK != sqlite3_reset (stmt_insert))
238 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_insert");
243 entry = GNUNET_new (struct Entry);
244 entry->id = address->peer;
245 GNUNET_CONTAINER_multipeermap_put (map,
247 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
249 entry->latency = latency;
253 GNUNET_SCHEDULER_shutdown ();
258 * Main function that will be run.
261 * @param args remaining command-line arguments
262 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
263 * @param c configuration
266 run (void *cls, char *const *args, const char *cfgfile,
267 const struct GNUNET_CONFIGURATION_Handle *c)
269 const char *query_create =
270 "CREATE TABLE ats_info ("
277 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "LATENCY-LOGGER",
284 if (SQLITE_OK != sqlite3_open (dbfile, &db))
288 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2");
292 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile);
293 GNUNET_free (dbfile);
296 if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL))
297 DEBUG ("SQLite Error: %d. Perhaps the database `%s' already exits.\n",
298 sqlite3_errcode (db), dbfile);
299 DEBUG ("Opened database %s\n", dbfile);
300 GNUNET_free (dbfile);
302 ats = GNUNET_ATS_performance_init (c, addr_info_cb, NULL);
303 map = GNUNET_CONTAINER_multipeermap_create (30, GNUNET_YES);
304 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
310 * Execution entry point
313 main (int argc, char * const *argv)
315 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
316 GNUNET_GETOPT_OPTION_END
320 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
324 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-latency-logger",
325 _("Daemon to log latency values of connections to neighbours"),
326 options, &run, NULL)) ? 0 : 1;
327 GNUNET_free ((void*) argv);