Adding initial stuff for the sqlite, including custom hash function
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a5b87d9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+.lock-waf_darwin_build
+.waf*
+build/
+chronoshare.db
diff --git a/src/main.cc b/src/main.cc
new file mode 100644
index 0000000..f8338c4
--- /dev/null
+++ b/src/main.cc
@@ -0,0 +1,30 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Chaoyi Bian <bcy@pku.edu.cn>
+ *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "sqlite-helper.h"
+
+int
+main (int argc, char **argv)
+{
+  DbHelper db ("./");
+  return 0;
+}
diff --git a/src/sqlite-helper.cc b/src/sqlite-helper.cc
new file mode 100644
index 0000000..c9c508b
--- /dev/null
+++ b/src/sqlite-helper.cc
@@ -0,0 +1,189 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *	   Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#include "sqlite-helper.h"
+// #include "sync-log.h"
+
+// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
+#define HASH_FUNCTION EVP_sha256
+
+#include <boost/throw_exception.hpp>
+typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; 
+// typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; 
+
+
+const std::string INIT_DATABASE = "\
+CREATE TABLE                                                    \
+    SyncNodes(                                                  \
+        device_id       INTEGER PRIMARY KEY AUTOINCREMENT,      \
+        device_name     TEXT NOT NULL,                          \
+        description     TEXT,                                   \
+        seq_no          INTEGER NOT NULL,                       \
+        last_known_tdi  TEXT,                                   \
+        last_update     TIMESTAMP                               \
+    );                                                          \
+                                                                \
+CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name);  \
+                                                                \
+CREATE TABLE SyncLog(                                           \
+        state_hash  BLOB NOT NULL PRIMARY KEY,                  \
+        last_update TIMESTAMP NOT NULL                          \
+    );                                                          \
+                                                                \
+CREATE TABLE                                                    \
+    SyncStateNodes(                                             \
+        id          INTEGER PRIMARY KEY AUTOINCREMENT,          \
+        state_hash  BLOB NOT NULL                                       \
+            REFERENCES SyncLog (state_hash) ON UPDATE CASCADE ON DELETE CASCADE, \
+        device_id   INTEGER NOT NULL                                    \
+            REFERENCES SyncNodes (device_id) ON UPDATE CASCADE ON DELETE CASCADE, \
+        seq_no      INTEGER NOT NULL                                    \
+    );                                                                  \
+                                                                        \
+CREATE INDEX SyncStateNodes_device_id ON SyncStateNodes (device_id);    \
+CREATE INDEX SyncStateNodes_state_hash ON SyncStateNodes (state_hash);  \
+CREATE INDEX SyncStateNodes_seq_no ON SyncStateNodes (seq_no);          \
+                                                                        \
+CREATE TRIGGER SyncLogGuard_trigger                                     \
+    BEFORE INSERT ON SyncLog                                            \
+    FOR EACH ROW                                                        \
+    WHEN (SELECT state_hash                                             \
+            FROM SyncLog                                                \
+            WHERE state_hash=NEW.state_hash)                            \
+        IS NOT NULL                                                     \
+    BEGIN                                                               \
+        DELETE FROM SyncLog WHERE state_hash=NEW.state_hash;            \
+    END;                                                                \
+";
+
+DbHelper::DbHelper (const std::string &path)
+{
+  int res = sqlite3_open((path+"chronoshare.db").c_str (), &m_db);
+  if (res != SQLITE_OK)
+    {
+      BOOST_THROW_EXCEPTION (Error::Db ()
+                             << errmsg_info_str ("Cannot open/create dabatabase: [" + path + "chronoshare.db" + "]"));
+    }
+  
+  res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0,
+                                     DbHelper::hash_xStep, DbHelper::hash_xFinal);
+  if (res != SQLITE_OK)
+    {
+      BOOST_THROW_EXCEPTION (Error::Db ()
+                             << errmsg_info_str ("Cannot create function ``hash''"));
+    }
+
+  // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go...
+  // for now, just attempt to create everything
+
+  char *errmsg = 0;
+  res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg);
+  // if (res != SQLITE_OK && errmsg != 0)
+  //   {
+  //     std::cerr << "DEBUG: " << errmsg << std::endl;
+  //     sqlite3_free (errmsg);
+  //   }
+
+  res = sqlite3_exec (m_db, "INSERT INTO SyncLog (state_hash, last_update) SELECT hash(device_name, seq_no), datetime('now') FROM SyncNodes ORDER BY device_name",
+                          NULL, NULL, &errmsg);
+  if (res != SQLITE_OK && errmsg != 0)
+    {
+      std::cerr << "DEBUG: " << errmsg << std::endl;
+      sqlite3_free (errmsg);
+    }
+}
+
+DbHelper::~DbHelper ()
+{
+  int res = sqlite3_close (m_db);
+  if (res != SQLITE_OK)
+    {
+      // complain
+    }
+}
+
+void
+DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv)
+{
+  if (argc != 2)
+    {
+      // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function");
+      sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1);
+      return;
+    }
+  if (sqlite3_value_type (argv[0]) != SQLITE_TEXT ||
+      sqlite3_value_type (argv[1]) != SQLITE_INTEGER)
+    {
+      // _LOG_ERROR ("Hash expects (text,integer) parameters");
+      sqlite3_result_error (context, "Hash expects (text,integer) parameters", -1);
+      return;
+    }
+  
+  EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
+
+  if (hash_context == 0)
+    {
+      sqlite3_result_error_nomem (context);
+      return;
+    }
+
+  if (*hash_context == 0)
+    {
+      *hash_context = EVP_MD_CTX_create ();
+      EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0);
+    }
+  
+  int nameBytes = sqlite3_value_bytes (argv[0]);
+  const unsigned char *name = sqlite3_value_text (argv[0]);
+  sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]);
+
+  EVP_DigestUpdate (*hash_context, name, nameBytes);
+  EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64));
+}
+
+void
+DbHelper::hash_xFinal (sqlite3_context *context)
+{
+  EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
+
+  if (hash_context == 0)
+    {
+      sqlite3_result_error_nomem (context);
+      return;
+    }
+
+  if (*hash_context == 0) // no rows
+    {
+      sqlite3_result_null (context);
+      return;
+    }
+  
+  unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE];
+  unsigned int hashLength = 0;
+
+  int ok = EVP_DigestFinal_ex (*hash_context,
+			       hash, &hashLength);
+
+  sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
+  delete [] hash;
+  
+  EVP_MD_CTX_destroy (*hash_context);
+}
diff --git a/src/sqlite-helper.h b/src/sqlite-helper.h
new file mode 100644
index 0000000..6874259
--- /dev/null
+++ b/src/sqlite-helper.h
@@ -0,0 +1,56 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *	   Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#ifndef SQLITE_HELPER_H
+#define SQLITE_HELPER_H
+
+#include <sqlite3.h>
+#include <openssl/evp.h>
+#include <boost/exception/all.hpp>
+#include <string>
+
+// temporarily ignoring all potential database errors
+
+class DbHelper
+{
+public:
+  DbHelper (const std::string &path);
+  ~DbHelper ();
+
+  
+  
+private:
+  static void
+  hash_xStep (sqlite3_context*,int,sqlite3_value**);
+
+  static void
+  hash_xFinal (sqlite3_context*);
+  
+private:
+  sqlite3 *m_db;
+};
+
+namespace Error {
+struct Db : virtual boost::exception, virtual std::exception { };
+}
+
+
+#endif // SQLITE_HELPER_H
diff --git a/src/sync-log.h b/src/sync-log.h
new file mode 100644
index 0000000..267a08d
--- /dev/null
+++ b/src/sync-log.h
@@ -0,0 +1,48 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *	   Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#ifndef SYNC_LOG_H
+#define SYNC_LOG_H
+
+#define INIT_LOGGER(name)
+#define _LOG_FUNCTION(x)
+#define _LOG_FUNCTION_NOARGS
+#define _LOG_TRACE(x)
+#define INIT_LOGGERS(x)
+
+#ifdef _DEBUG
+
+#include <boost/thread/thread.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <iostream>
+#include <string>
+
+#define _LOG_DEBUG(x) \
+  std::clog << boost::get_system_time () << " " << boost::this_thread::get_id () << " " << x << std::endl;
+
+#else
+#define _LOG_DEBUG(x)
+#endif
+
+#define _LOG_ERROR(x) \
+  std::clog << boost::get_system_time () << " " << boost::this_thread::get_id () << " " << x << std::endl;
+
+#endif // SYNC_LOG_H
diff --git a/waf b/waf
new file mode 100755
index 0000000..7195638
--- /dev/null
+++ b/waf
Binary files differ
diff --git a/wscript b/wscript
new file mode 100644
index 0000000..8300acc
--- /dev/null
+++ b/wscript
@@ -0,0 +1,55 @@
+# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
+
+VERSION='0.1'
+APPNAME='chronoshare'
+
+from waflib import Build, Logs
+
+def options(opt):
+    opt.load('compiler_cxx boost ccnx')
+    opt.add_option('--debug',action='store_true',default=False,dest='debug',help='''debugging mode''')
+    opt.add_option('--yes',action='store_true',default=False) # for autoconf/automake/make compatibility
+
+def configure(conf):
+    conf.load("compiler_cxx boost ccnx")
+
+    conf.check_cfg(package='sqlite3', args=['--cflags', '--libs'], uselib_store='SQLITE3', mandatory=True)
+
+    if not conf.check_cfg(package='openssl', args=['--cflags', '--libs'], uselib_store='SSL', mandatory=False):
+        Logs.error ("bla")
+        conf.check_cc(lib='crypto',
+                      header_name='openssl/crypto.h',
+                      define_name='HAVE_SSL',
+                      uselib_store='SSL')
+    else:
+        conf.define ("HAVE_SSL", 1)
+    
+    if not conf.get_define ("HAVE_SSL"):
+        conf.fatal ("Cannot find SSL libraries")
+
+    conf.check_boost(lib='system iostreams regex')
+
+    boost_version = conf.env.BOOST_VERSION.split('_')
+    if int(boost_version[0]) < 1 or int(boost_version[1]) < 46:
+        Logs.error ("Minumum required boost version is 1.46")
+        return
+
+    conf.check_ccnx (path=conf.options.ccnx_dir)
+
+    if conf.options.debug:
+        conf.define ('_DEBUG', 1)
+        conf.env.append_value('CXXFLAGS', ['-O0', '-g3'])
+    else:
+        conf.env.append_value('CXXFLAGS', ['-O3', '-g'])
+
+    conf.write_config_header('src/config.h')
+
+def build (bld):
+    chronoshare = bld (
+        target=APPNAME,
+        features=['cxx', 'cxxprogram'],
+        # source = bld.path.ant_glob(['src/**/*.cc']),
+        source = ['src/main.cc', 'src/sqlite-helper.cc'],
+        use = 'BOOST BOOST_IOSTREAMS BOOST_REGEX CCNX SSL SQLITE3',
+        includes = ['include', 'src'],
+        )