aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/generate-underlay-topology.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2014-01-07 09:09:28 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2014-01-07 09:09:28 +0000
commit3116afe785564607b30d1ba973c7345853e327f0 (patch)
treea04d9e4f2ee5106332645524c3ed42c4ec999e45 /src/testbed/generate-underlay-topology.c
parent9e0744da3a82cc7b68a9043272044584be98db44 (diff)
downloadgnunet-3116afe785564607b30d1ba973c7345853e327f0.tar.gz
gnunet-3116afe785564607b30d1ba973c7345853e327f0.zip
- populate underlay db
Diffstat (limited to 'src/testbed/generate-underlay-topology.c')
-rw-r--r--src/testbed/generate-underlay-topology.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/testbed/generate-underlay-topology.c b/src/testbed/generate-underlay-topology.c
index 1a432ec03..4299795a5 100644
--- a/src/testbed/generate-underlay-topology.c
+++ b/src/testbed/generate-underlay-topology.c
@@ -28,6 +28,7 @@
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29#include "gnunet_testbed_service.h" 29#include "gnunet_testbed_service.h"
30#include "testbed_api_topology.h" 30#include "testbed_api_topology.h"
31#include "sqlite3.h"
31 32
32#define LOG(type, ...) \ 33#define LOG(type, ...) \
33 GNUNET_log (type, __VA_ARGS__) 34 GNUNET_log (type, __VA_ARGS__)
@@ -36,6 +37,30 @@
36#define LOG_ERROR(...) \ 37#define LOG_ERROR(...) \
37 LOG (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__) 38 LOG (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__)
38 39
40/**
41 * Log an error message at log-level 'level' that indicates
42 * a failure of the command 'cmd' on file 'filename'
43 * with the message given by strerror(errno).
44 */
45#define LOG_SQLITE(db, msg, level, cmd) \
46 do { \
47 GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \
48 cmd, __FILE__,__LINE__, sqlite3_errmsg(db)); \
49 if (msg != NULL) \
50 GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, \
51 __FILE__, __LINE__, sqlite3_errmsg(db)); \
52 } while(0)
53
54
55/**
56 * Handle to the sqlite3 database
57 */
58static struct sqlite3 *db;
59
60/**
61 * Prepared statement for inserting link values into db
62 */
63struct sqlite3_stmt *stmt_insert;
39 64
40/** 65/**
41 * The topology to generate 66 * The topology to generate
@@ -72,12 +97,93 @@ link_processor (void *cls,
72 unsigned int latency, 97 unsigned int latency,
73 unsigned int loss) 98 unsigned int loss)
74{ 99{
100 if ( (SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, A)) ||
101 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, B)) ||
102 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 3, bandwidth)) ||
103 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 4, latency)) ||
104 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 5, loss)) )
105 {
106 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
107 return GNUNET_SYSERR;
108 }
109 if (SQLITE_DONE != sqlite3_step (stmt_insert))
110 {
111 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
112 return GNUNET_SYSERR;
113 }
75 FPRINTF (stdout, "%u -> %u\n", A, B); 114 FPRINTF (stdout, "%u -> %u\n", A, B);
115 GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert));
116 //GNUNET_break (SQLITE_OK == sqlite3_clear_bindings (stmt_insert));
76 return GNUNET_OK; 117 return GNUNET_OK;
77} 118}
78 119
79 120
80/** 121/**
122 * Open the database file, creating a new database if not existing and setup the
123 * whitelist table
124 *
125 * @param dbfile the database filename
126 * @return GNUNET_OK upon success; GNUNET_SYSERR upon failure (error message has
127 * to be printed)
128 */
129static int
130setup_db (const char *dbfile)
131{
132 const char *query_create =
133 "CREATE TABLE whitelist ("
134 "id INTEGER,"
135 "oid INTEGER,"
136 "bandwidth INTEGER DEFAULT NULL,"
137 "latency INTEGER DEFAULT NULL,"
138 "loss INTEGER DEFAULT NULL);";
139 const char *query_insert =
140 "INSERT INTO whitelist("
141 " id,"
142 " oid,"
143 " bandwidth,"
144 " latency,"
145 " loss"
146 ") VALUES ("
147 " ?1,"
148 " ?2,"
149 " ?3,"
150 " ?4,"
151 " ?5);";
152 struct sqlite3_stmt *stmt_create;
153 int ret;
154
155 stmt_create = NULL;
156 if (SQLITE_OK != (ret = sqlite3_open (dbfile, &db)))
157 {
158 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_open");
159 goto err_ret;
160 }
161 if (SQLITE_OK != (ret = sqlite3_prepare_v2 (db, query_create, -1,
162 &stmt_create, NULL)))
163 {
164 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
165 goto err_ret;
166 }
167 if (SQLITE_DONE != sqlite3_step (stmt_create))
168 {
169 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
170 goto err_ret;
171 }
172 if (SQLITE_OK != (ret = sqlite3_prepare_v2 (db, query_insert, -1,
173 &stmt_insert, NULL)))
174 {
175 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
176 goto err_ret;
177 }
178
179 err_ret:
180 if (NULL != stmt_create)
181 sqlite3_finalize (stmt_create);
182 return (SQLITE_OK != ret) ? GNUNET_SYSERR : GNUNET_OK;
183}
184
185
186/**
81 * Main run function. 187 * Main run function.
82 * 188 *
83 * @param cls NULL 189 * @param cls NULL
@@ -108,6 +214,8 @@ run (void *cls, char *const *args, const char *cfgfile,
108 LOG_ERROR (_("Database filename missing\n")); 214 LOG_ERROR (_("Database filename missing\n"));
109 return; 215 return;
110 } 216 }
217 if (GNUNET_OK != setup_db (dbfile))
218 return;
111 if (NULL == (topology_string = args[argc++])) 219 if (NULL == (topology_string = args[argc++]))
112 { 220 {
113 LOG_ERROR (_("Topology string missing\n")); 221 LOG_ERROR (_("Topology string missing\n"));
@@ -238,6 +346,10 @@ main (int argc, char *const argv[])
238 "\t filename: the path of the file which contains topology information\n" 346 "\t filename: the path of the file which contains topology information\n"
239 "NOTE: the format of the above file is descibed here: https://www.gnunet.org/content/topology-file-format\n"), 347 "NOTE: the format of the above file is descibed here: https://www.gnunet.org/content/topology-file-format\n"),
240 option, &run, NULL); 348 option, &run, NULL);
349 if (NULL != stmt_insert)
350 sqlite3_finalize (stmt_insert);
351 if (NULL != db)
352 sqlite3_close (db);
241 if ((GNUNET_OK != ret) || (GNUNET_OK != exit_result)) 353 if ((GNUNET_OK != ret) || (GNUNET_OK != exit_result))
242 return 1; 354 return 1;
243 return 0; 355 return 0;