-indentation
[oweals/gnunet.git] / src / testbed / generate-underlay-topology.c
index 427c8e66ab38e75bd81f6c7f41cd4a83dbe8e025..e6541a3547128b6c3f4ff1f482e357cde4e238d3 100644 (file)
@@ -28,6 +28,7 @@
 #include "gnunet_util_lib.h"
 #include "gnunet_testbed_service.h"
 #include "testbed_api_topology.h"
+#include "sqlite3.h"
 
 #define LOG(type, ...)                          \
   GNUNET_log (type, __VA_ARGS__)
 #define LOG_ERROR(...)                          \
   LOG (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__)
 
+/**
+ * Log an error message at log-level 'level' that indicates
+ * a failure of the command 'cmd' on file 'filename'
+ * with the message given by strerror(errno).
+ */
+#define LOG_SQLITE(db, msg, level, cmd)                                 \
+  do {                                                                  \
+    GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \
+                     cmd, __FILE__,__LINE__, sqlite3_errmsg(db));  \
+    if (msg != NULL)                                                    \
+      GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, \
+                      __FILE__, __LINE__, sqlite3_errmsg(db));     \
+  } while(0)
+
+
+/**
+ * Handle to the sqlite3 database
+ */
+static struct sqlite3 *db;
+
+/**
+ * Prepared statement for inserting link values into db
+ */
+struct sqlite3_stmt *stmt_insert;
 
 /**
  * The topology to generate
@@ -72,11 +97,106 @@ link_processor (void *cls,
                 unsigned int latency,
                 unsigned int loss)
 {
-  GNUNET_break (0);
+  if ( (SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, A)) ||
+       (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, B)) ||
+       (SQLITE_OK != sqlite3_bind_int (stmt_insert, 3, bandwidth)) ||
+       (SQLITE_OK != sqlite3_bind_int (stmt_insert, 4, latency)) ||
+       (SQLITE_OK != sqlite3_bind_int (stmt_insert, 5, loss)) )
+  {
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
+    return GNUNET_SYSERR;
+  }
+  if (SQLITE_DONE != sqlite3_step (stmt_insert))
+  {
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
+    return GNUNET_SYSERR;
+  }
+  FPRINTF (stdout, "%u -> %u\n", A, B);
+  GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert));
+  //GNUNET_break (SQLITE_OK == sqlite3_clear_bindings (stmt_insert));
+  if ( (SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, B)) ||
+       (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, A)) )
+  {
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
+    return GNUNET_SYSERR;
+  }
+  if (SQLITE_DONE != sqlite3_step (stmt_insert))
+  {
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
+    return GNUNET_SYSERR;
+  }
+  FPRINTF (stdout, "%u -> %u\n", B, A);
+  GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert));
   return GNUNET_OK;
 }
 
 
+/**
+ * Open the database file, creating a new database if not existing and setup the
+ * whitelist table
+ *
+ * @param dbfile the database filename
+ * @return GNUNET_OK upon success; GNUNET_SYSERR upon failure (error message has
+ * to be printed)
+ */
+static int
+setup_db (const char *dbfile)
+{
+  const char *query_create =
+      "CREATE TABLE whitelist ("
+      "id INTEGER,"
+      "oid INTEGER,"
+      "bandwidth INTEGER DEFAULT NULL,"
+      "latency INTEGER DEFAULT NULL,"
+      "loss INTEGER DEFAULT NULL,"
+      " UNIQUE ("
+      "  id,"
+      "  oid"
+      " ) ON CONFLICT IGNORE"
+      ");";
+  const char *query_insert =
+      "INSERT INTO whitelist("
+      " id,"
+      " oid,"
+      " bandwidth,"
+      " latency,"
+      " loss"
+      ") VALUES ("
+      " ?1,"
+      " ?2,"
+      " ?3,"
+      " ?4," 
+      " ?5);";
+  int ret;
+  
+  ret = GNUNET_SYSERR;
+  if (SQLITE_OK != sqlite3_open (dbfile, &db))
+  {
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_open");
+    goto err_ret;
+  }
+  if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL))
+  {
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
+    FPRINTF (stderr, "Error: %d.  Perhaps the database `%s' already exits.\n",
+             sqlite3_errcode (db),
+             dbfile);
+    goto err_ret;
+  }
+  GNUNET_break (0 == sqlite3_exec (db, "PRAGMA synchronous = 0;", NULL, NULL, NULL));
+  if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1,
+                                       &stmt_insert, NULL))
+  {
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
+    goto err_ret;
+  }
+  ret = GNUNET_OK;
+
+ err_ret:
+  return ret;
+}
+
+
 /**
  * Main run function.
  *
@@ -108,6 +228,8 @@ run (void *cls, char *const *args, const char *cfgfile,
     LOG_ERROR (_("Database filename missing\n"));
     return;
   }
+  if (GNUNET_OK != setup_db (dbfile))
+    return;
   if (NULL == (topology_string = args[argc++]))
   {
     LOG_ERROR (_("Topology string missing\n"));
@@ -118,6 +240,7 @@ run (void *cls, char *const *args, const char *cfgfile,
     LOG_ERROR (_("Invalid topology: %s\n"), topology_string);
     return;
   }
+  arg_str1 = NULL;
   /* parse for first TOPOOPT.  This can either be arg_uint1 or arg_str1 */
   switch (topology)
   {
@@ -147,7 +270,7 @@ run (void *cls, char *const *args, const char *cfgfile,
     }
     break;
   default:
-    GNUNET_assert (0);
+    break;
   }
   /* parse for second TOPOOPT.  Only required for SCALE_FREE topology */
   switch (topology)
@@ -166,7 +289,7 @@ run (void *cls, char *const *args, const char *cfgfile,
     }
     break;
   default:
-    GNUNET_assert (0);
+    break;
   }
   /* contruct topologies */
   switch (topology)
@@ -209,6 +332,9 @@ int
 main (int argc, char *const argv[])
 {
   struct GNUNET_GETOPT_CommandLineOption option[] = {
+    {'p', "num-peers", "COUNT",
+     gettext_noop ("create COUNT number of peers"),
+     GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers},
     GNUNET_GETOPT_OPTION_END
   };
   int ret;
@@ -235,6 +361,10 @@ main (int argc, char *const argv[])
                             "\t filename: the path of the file which contains topology information\n"
                             "NOTE: the format of the above file is descibed here: https://www.gnunet.org/content/topology-file-format\n"),
                           option, &run, NULL);
+  if (NULL != stmt_insert)
+    sqlite3_finalize (stmt_insert);
+  if (NULL != db)
+    sqlite3_close (db);
   if ((GNUNET_OK != ret) || (GNUNET_OK != exit_result))
     return 1;
   return 0;