Merge remote-tracking branch 'origin/master'
diff --git a/client/client.cc b/client/client.cc
new file mode 100644
index 0000000..8ab8013
--- /dev/null
+++ b/client/client.cc
@@ -0,0 +1,152 @@
+/* -*- 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 <iostream>
+#include <boost/algorithm/string.hpp>
+#include <config.h>
+#include <Ice/Ice.h>
+#include <chronoshare-client.ice.h>
+#include <sys/stat.h>
+#include <hash-helper.h>
+
+
+using namespace std;
+using namespace boost;
+using namespace ChronoshareClient;
+
+void
+usage ()
+{
+ cerr << "Usage: chronoshare <cmd> [<options>]\n"
+ << "\n"
+ << " <cmd> is one of:\n"
+ << " version\n"
+ << " update <filename>\n"
+ << " delete <filename>\n"
+ << " move <filename> <filename>\n";
+ exit (1);
+}
+
+
+int
+main (int argc, char **argv)
+{
+ if (argc < 2)
+ {
+ usage ();
+ }
+
+ string cmd = argv[1];
+ algorithm::to_lower (cmd);
+
+ if (cmd == "version")
+ {
+ cout << "chronoshare version " << CHRONOSHARE_VERSION << endl;
+ exit (0);
+ }
+
+ int status = 0;
+ Ice::CommunicatorPtr ic;
+ try
+ {
+ // Create a communicator
+ //
+ ic = Ice::initialize (argc, argv);
+
+ Ice::ObjectPrx base = ic->stringToProxy("NotifyDaemon:default -p 55436");
+ if (!base)
+ {
+ throw "Could not create proxy";
+ }
+
+ NotifyPrx notify = NotifyPrx::checkedCast (base);
+ if (notify)
+ {
+
+ if (cmd == "update")
+ {
+ if (argc != 3)
+ {
+ usage ();
+ }
+
+ struct stat fileStats;
+ int ok = stat (argv[2], &fileStats);
+ if (ok == 0)
+ {
+ // Alex: the following code is platform specific :(
+
+ char atimespec[26], mtimespec[26], ctimespec[26];
+ ctime_r (&fileStats.st_atime, atimespec);
+ ctime_r (&fileStats.st_mtime, mtimespec);
+ ctime_r (&fileStats.st_ctime, ctimespec);
+
+ HashPtr fileHash = Hash::FromFileContent (argv[2]);
+
+ notify->updateFile (argv[2],
+ make_pair(reinterpret_cast<const ::Ice::Byte*> (fileHash->GetHash ()),
+ reinterpret_cast<const ::Ice::Byte*> (fileHash->GetHash ()) +
+ fileHash->GetHashBytes ()),
+ atimespec, mtimespec, ctimespec,
+ fileStats.st_mode);
+ }
+ else
+ {
+ cerr << "File [" << argv[2] << "] doesn't exist or is not accessible" << endl;
+ return 1;
+ }
+ }
+ else if (cmd == "delete")
+ {
+ if (argc != 3)
+ {
+ usage ();
+ }
+
+ notify->deleteFile (argv[2]);
+ }
+ else if (cmd == "move")
+ {
+ if (argc != 4)
+ {
+ usage ();
+ }
+
+
+ notify->moveFile (argv[2], argv[3]);
+ }
+ }
+ else
+ {
+ cerr << "Cannot connect to the daemon\n";
+ status = 1;
+ }
+ }
+ catch (const Ice::Exception& ex)
+ {
+ cerr << ex << endl;
+ status = 1;
+ }
+
+ if (ic)
+ ic->destroy();
+ return status;
+}
diff --git a/daemon/daemon.cc b/daemon/daemon.cc
new file mode 100644
index 0000000..97fee4f
--- /dev/null
+++ b/daemon/daemon.cc
@@ -0,0 +1,118 @@
+/* -*- 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 "action-log.h"
+#include <iostream>
+#include <Ice/Service.h>
+#include <Ice/Identity.h>
+
+#include "notify-i.h"
+
+using namespace std;
+using namespace boost;
+using namespace ChronoshareClient;
+
+typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
+
+class MyService : public Ice::Service
+{
+protected:
+ virtual bool start (int, char*[], int&)
+ {
+ _adapter = communicator ()->createObjectAdapterWithEndpoints ("ChronoShare", "default -p 55436");
+
+ Ice::Identity identity;
+ identity.name="NotifyDaemon";
+ NotifyPtr servant=new NotifyI;
+
+ _adapter->add (servant, identity);
+
+ _adapter->activate();
+ // status = EXIT_SUCCESS;
+ return true;
+ }
+
+private:
+ Ice::ObjectAdapterPtr _adapter;
+};
+
+int
+main (int argc, char **argv)
+{
+ int status = 0;
+
+ try
+ {
+ // DbHelper db ("./", "/ndn/ucla.edu/alex");
+ ActionLog bla ("./", "/ndn/ucla.edu/alex");
+
+ MyService svc;
+ status = svc.main (argc, argv);
+
+ // HashPtr hash = db.RememberStateInStateLog ();
+ // // should be empty
+ // cout << "Hash: [" << *hash << "]" << endl;
+
+ // //
+ // db.UpdateDeviceSeqno ("Alex", 1);
+ // hash = db.RememberStateInStateLog ();
+ // cout << "Hash: [" << *hash << "]" << endl;
+
+ // db.UpdateDeviceSeqno ("Alex", 2);
+ // hash = db.RememberStateInStateLog ();
+ // cout << "Hash: [" << *hash << "]" << endl;
+
+ // db.UpdateDeviceSeqno ("Alex", 2);
+ // hash = db.RememberStateInStateLog ();
+ // cout << "Hash: [" << *hash << "]" << endl;
+
+ // db.UpdateDeviceSeqno ("Alex", 1);
+ // hash = db.RememberStateInStateLog ();
+ // cout << "Hash: [" << *hash << "]" << endl;
+
+ // db.FindStateDifferences ("00", "ec0a9941fa726e1fb8f34ecdbd8e3faa75dc9dba22e6a2ea1d8482aae5fdfb52");
+ // db.FindStateDifferences ("ec0a9941fa726e1fb8f34ecdbd8e3faa75dc9dba22e6a2ea1d8482aae5fdfb52", "00");
+ // db.FindStateDifferences ("869c38c6dffe8911ced320aecc6d9244904d13d3e8cd21081311f2129b4557ce",
+ // "ec0a9941fa726e1fb8f34ecdbd8e3faa75dc9dba22e6a2ea1d8482aae5fdfb52");
+ // db.FindStateDifferences ("ec0a9941fa726e1fb8f34ecdbd8e3faa75dc9dba22e6a2ea1d8482aae5fdfb52",
+ // "869c38c6dffe8911ced320aecc6d9244904d13d3e8cd21081311f2129b4557ce");
+
+ // db.UpdateDeviceSeqno ("Bob", 1);
+ // hash = db.RememberStateInStateLog ();
+ // cout << "Hash: [" << *hash << "]" << endl;
+
+ // db.FindStateDifferences ("00", "48f4d95b503b9a79c2d5939fa67722b13fc01db861fc501d09efd0a38dbafab8");
+ // db.FindStateDifferences ("ec0a9941fa726e1fb8f34ecdbd8e3faa75dc9dba22e6a2ea1d8482aae5fdfb52",
+ // "48f4d95b503b9a79c2d5939fa67722b13fc01db861fc501d09efd0a38dbafab8");
+ }
+ catch (const Ice::Exception& e)
+ {
+ cerr << e << endl;
+ status = 1;
+ }
+ catch (const boost::exception &e)
+ {
+ cout << "ERRORR: " << *get_error_info<errmsg_info_str> (e) << endl;
+ status = 1;
+ }
+
+ return status;
+}
diff --git a/daemon/notify-i.cc b/daemon/notify-i.cc
new file mode 100644
index 0000000..0f8b004
--- /dev/null
+++ b/daemon/notify-i.cc
@@ -0,0 +1,65 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 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 "notify-i.h"
+#include <hash-helper.h>
+
+using namespace std;
+
+// NotifyI::NotifyI (DbHelperPtr &db)
+// : m_db (db)
+// {
+// }
+
+void
+NotifyI::updateFile (const ::std::string &filename,
+ const ::std::pair<const Ice::Byte*, const Ice::Byte*> &hashRaw,
+ const ::std::string &atime,
+ const ::std::string &mtime,
+ const ::std::string &ctime,
+ ::Ice::Int mode,
+ const ::Ice::Current&)
+{
+ Hash hash (hashRaw.first, hashRaw.second-hashRaw.first);
+
+ // m_db->AddActionUpdate (filename, hash, atime, mtime, ctime, mode);
+
+ // cout << "updateFile " << filename << " with hash " << hash << endl;
+}
+
+void
+NotifyI::moveFile (const ::std::string &oldFilename,
+ const ::std::string &newFilename,
+ const ::Ice::Current&)
+{
+ // cout << "moveFile from " << oldFilename << " to " << newFilename << endl;
+ // m_db->AddActionMove (filename, oldFilename);
+}
+
+void
+NotifyI::deleteFile (const ::std::string &filename,
+ const ::Ice::Current&)
+{
+ // m_db->AddActionDelete (filename, oldFilename);
+
+ // cout << "deleteFile " << filename << endl;
+}
+
diff --git a/daemon/notify-i.h b/daemon/notify-i.h
new file mode 100644
index 0000000..a2cea97
--- /dev/null
+++ b/daemon/notify-i.h
@@ -0,0 +1,54 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 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 NOTIFY_I_H
+#define NOTIFY_I_H
+
+#include <chronoshare-client.ice.h>
+
+class NotifyI : public ChronoshareClient::Notify
+{
+public:
+ // NotifyI (DbHelperPtr &db);
+
+ virtual void
+ updateFile (const ::std::string &filename,
+ const ::std::pair<const Ice::Byte*, const Ice::Byte*> &hash,
+ const ::std::string &atime,
+ const ::std::string &mtime,
+ const ::std::string &ctime,
+ ::Ice::Int mode,
+ const ::Ice::Current& = ::Ice::Current());
+
+ virtual void
+ moveFile (const ::std::string &oldFilename,
+ const ::std::string &newFilename,
+ const ::Ice::Current& = ::Ice::Current());
+
+ virtual void
+ deleteFile (const ::std::string &filename,
+ const ::Ice::Current& = ::Ice::Current());
+
+private:
+ // DbHelperPtr m_db;
+};
+
+#endif // NOTIFY_I_H
diff --git a/src/action-log.cc b/src/action-log.cc
new file mode 100644
index 0000000..cf5156a
--- /dev/null
+++ b/src/action-log.cc
@@ -0,0 +1,78 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2013 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 "action-log.h"
+
+using namespace boost;
+using namespace std;
+
+ActionLog::ActionLog (const std::string &path, const std::string &localName)
+ : SyncLog (path)
+ , m_localName (localName)
+{
+}
+
+// local add action. remote action is extracted from content object
+void
+ActionLog::AddActionUpdate (const std::string &filename,
+ const Hash &hash,
+ const std::string &atime, const std::string &mtime, const std::string &ctime,
+ int mode)
+{
+ int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
+
+ // sqlite3_stmt *stmt;
+ // res += sqlite3_prepare_v2 (m_db, "SELECT version,device_id,seq_no FROM ActionLog WHERE filename=? ORDER BY version DESC,device_id DESC LIMIT 1", -1, &stmt, 0);
+
+ // if (res != SQLITE_OK)
+ // {
+ // BOOST_THROW_EXCEPTION (Error::Db ()
+ // << errmsg_info_str ("Some error with AddActionUpdate"));
+ // }
+
+ // sqlite3_bind_text (stmt, 1, filename.c_str (), -1);
+ // if (sqlite3_step (stmt) == SQLITE_ROW)
+ // {
+ // // with parent
+ // sqlite3_int64 version = sqlite3_value_int64 (stmt, 0) + 1;
+ // sqlite3_int64 device_id = sqlite3_value_int64 (stmt, 1);
+ // sqlite3_int64 seq_no = sqlite3_value_int64 (stmt, 0);
+
+
+ // }
+ // else
+ // {
+ // // without parent
+
+ // }
+
+ res += sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
+}
+
+void
+ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
+{
+}
+
+void
+ActionLog::AddActionDelete (const std::string &filename)
+{
+}
diff --git a/src/action-log.h b/src/action-log.h
new file mode 100644
index 0000000..6827cba
--- /dev/null
+++ b/src/action-log.h
@@ -0,0 +1,51 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2013 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 ACTION_LOG_H
+#define ACTION_LOG_H
+
+#include "sync-log.h"
+
+class ActionLog : public SyncLog
+{
+public:
+ ActionLog (const std::string &path, const std::string &localName);
+
+
+
+
+ void
+ AddActionUpdate (const std::string &filename,
+ const Hash &hash,
+ const std::string &atime, const std::string &mtime, const std::string &ctime,
+ int mode);
+
+ void
+ AddActionMove (const std::string &oldFile, const std::string &newFile);
+
+ void
+ AddActionDelete (const std::string &filename);
+
+protected:
+ std::string m_localName;
+};
+
+#endif // ACTION_LOG_H
diff --git a/src/chronoshare-client.ice b/src/chronoshare-client.ice
new file mode 100644
index 0000000..be40c3e
--- /dev/null
+++ b/src/chronoshare-client.ice
@@ -0,0 +1,34 @@
+/* -*- 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>
+ */
+
+module ChronoshareClient
+{
+ sequence<byte> HashBytes;
+
+ interface Notify
+ {
+ void updateFile (string filename, ["cpp:array"] HashBytes fileHash, string atime, string mtime, string ctime, int mode);
+
+ void moveFile (string origFilename, string newFilename);
+
+ void deleteFile (string filename);
+ };
+};
diff --git a/src/main.cc b/src/database-test.cc
similarity index 94%
rename from src/main.cc
rename to src/database-test.cc
index 82b5c7d..ba1fecc 100644
--- a/src/main.cc
+++ b/src/database-test.cc
@@ -15,12 +15,11 @@
* 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>
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Zhenkai Zhu <zhenkai@cs.ucla.edu>
*/
-#include "sqlite-helper.h"
+#include "db-helper.h"
#include <iostream>
using namespace std;
diff --git a/src/db-helper.cc b/src/db-helper.cc
new file mode 100644
index 0000000..9bd2d61
--- /dev/null
+++ b/src/db-helper.cc
@@ -0,0 +1,233 @@
+/* -*- 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 "db-helper.h"
+// #include "sync-log.h"
+#include <boost/make_shared.hpp>
+#include <boost/ref.hpp>
+#include <boost/throw_exception.hpp>
+
+using namespace boost;
+
+const std::string INIT_DATABASE = "\
+PRAGMA foreign_keys = ON; \n\
+ \n\
+CREATE TABLE \n\
+ SyncNodes( \n\
+ device_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
+ device_name TEXT NOT NULL, \n\
+ description TEXT, \n\
+ seq_no INTEGER NOT NULL, \n\
+ last_known_tdi TEXT, \n\
+ last_update TIMESTAMP \n\
+ ); \n\
+ \n\
+CREATE TRIGGER SyncNodesUpdater_trigger \n\
+ BEFORE INSERT ON SyncNodes \n\
+ FOR EACH ROW \n\
+ WHEN (SELECT device_id \n\
+ FROM SyncNodes \n\
+ WHERE device_name=NEW.device_name) \n\
+ IS NOT NULL \n\
+ BEGIN \n\
+ UPDATE SyncNodes \n\
+ SET seq_no=max(seq_no,NEW.seq_no) \n\
+ WHERE device_name=NEW.device_name; \n\
+ SELECT RAISE(IGNORE); \n\
+ END; \n\
+ \n\
+CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name); \n\
+ \n\
+CREATE TABLE SyncLog( \n\
+ state_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
+ state_hash BLOB NOT NULL UNIQUE, \n\
+ last_update TIMESTAMP NOT NULL \n\
+ ); \n\
+ \n\
+CREATE TABLE \n\
+ SyncStateNodes( \n\
+ id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
+ state_id INTEGER NOT NULL \n\
+ REFERENCES SyncLog (state_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
+ device_id INTEGER NOT NULL \n\
+ REFERENCES SyncNodes (device_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
+ seq_no INTEGER NOT NULL \n\
+ ); \n\
+ \n\
+CREATE INDEX SyncStateNodes_device_id ON SyncStateNodes (device_id); \n\
+CREATE INDEX SyncStateNodes_state_id ON SyncStateNodes (state_id); \n\
+CREATE INDEX SyncStateNodes_seq_no ON SyncStateNodes (seq_no); \n\
+ \n\
+CREATE TRIGGER SyncLogGuard_trigger \n\
+ BEFORE INSERT ON SyncLog \n\
+ FOR EACH ROW \n\
+ WHEN (SELECT state_hash \n\
+ FROM SyncLog \n\
+ WHERE state_hash=NEW.state_hash) \n\
+ IS NOT NULL \n\
+ BEGIN \n\
+ DELETE FROM SyncLog WHERE state_hash=NEW.state_hash; \n\
+ END; \n\
+ \n\
+CREATE TABLE ActionLog ( \n\
+ device_id INTEGER NOT NULL, \n\
+ seq_no INTEGER NOT NULL, \n\
+ \n\
+ action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\
+ filename TEXT NOT NULL, \n\
+ \n\
+ version INTEGER NOT NULL, \n\
+ action_timestamp TIMESTAMP NOT NULL, \n\
+ \n\
+ file_hash BLOB, /* NULL if action is \"delete\" */ \n\
+ file_atime TIMESTAMP, \n\
+ file_mtime TIMESTAMP, \n\
+ file_ctime TIMESTAMP, \n\
+ file_chmod INTEGER, \n\
+ \n\
+ parent_device_id INTEGER, \n\
+ parent_seq_no INTEGER, \n\
+ \n\
+ action_name TEXT NOT NULL, \n\
+ action_content_object BLOB NOT NULL, \n\
+ \n\
+ PRIMARY KEY (device_id, seq_no), \n\
+ \n\
+ FOREIGN KEY (parent_device_id, parent_seq_no) \n\
+ REFERENCES ActionLog (device_id, parent_seq_no) \n\
+ ON UPDATE RESTRICT \n\
+ ON DELETE SET NULL \n\
+); \n\
+ \n\
+CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
+CREATE INDEX ActionLog_parent ON ActionLog (parent_device_id, parent_seq_no); \n\
+CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
+";
+
+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);
+ }
+}
+
+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
+ {
+ char charNullResult = 0;
+ sqlite3_result_blob (context, &charNullResult, 1, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
+ 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/db-helper.h b/src/db-helper.h
new file mode 100644
index 0000000..51970dd
--- /dev/null
+++ b/src/db-helper.h
@@ -0,0 +1,58 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2013 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 DB_HELPER_H
+#define DB_HELPER_H
+
+#include <stdint.h>
+#include <sqlite3.h>
+#include <openssl/evp.h>
+#include <boost/exception/all.hpp>
+#include <string>
+#include "hash-helper.h"
+
+typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
+
+class DbHelper
+{
+public:
+ DbHelper (const std::string &path);
+ virtual ~DbHelper ();
+
+private:
+ static void
+ hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv);
+
+ static void
+ hash_xFinal (sqlite3_context *context);
+
+protected:
+ sqlite3 *m_db;
+};
+
+namespace Error {
+struct Db : virtual boost::exception, virtual std::exception { };
+}
+
+typedef boost::shared_ptr<DbHelper> DbHelperPtr;
+
+
+#endif // DB_HELPER_H
diff --git a/src/hash-string-converter.cc b/src/hash-helper.cc
similarity index 78%
rename from src/hash-string-converter.cc
rename to src/hash-helper.cc
index 2961069..04b52b2 100644
--- a/src/hash-string-converter.cc
+++ b/src/hash-helper.cc
@@ -19,12 +19,13 @@
* Zhenkai Zhu <zhenkai@cs.ucla.edu>
*/
-#include "hash-string-converter.h"
+#include "hash-helper.h"
#include <boost/assert.hpp>
#include <boost/throw_exception.hpp>
#include <boost/make_shared.hpp>
#include <openssl/evp.h>
+#include <fstream>
typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
@@ -101,29 +102,56 @@
return os;
}
-Hash::Hash (const std::string &hashInTextEncoding)
+HashPtr
+Hash::FromString (const std::string &hashInTextEncoding)
{
+ HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
+
if (hashInTextEncoding.size () == 0)
{
- m_buf = 0;
- m_length = 0;
- return;
+ return retval;
}
if (hashInTextEncoding.size () > EVP_MAX_MD_SIZE * 2)
{
cerr << "Input hash is too long. Returning an empty hash" << endl;
- m_buf = 0;
- m_length = 0;
- return;
+ return retval;
}
- m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
unsigned char *end = copy (string_to_binary (hashInTextEncoding.begin ()),
string_to_binary (hashInTextEncoding.end ()),
- m_buf);
+ retval->m_buf);
- m_length = end-m_buf;
+ retval->m_length = end - retval->m_buf;
+
+ return retval;
}
+HashPtr
+Hash::FromFileContent (const std::string &filename)
+{
+ HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
+ retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+ EVP_MD_CTX *hash_context = EVP_MD_CTX_create ();
+ EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0);
+
+ ifstream iff (filename.c_str (), std::ios::in | std::ios::binary);
+ while (iff.good ())
+ {
+ char buf[1024];
+ iff.read (buf, 1024);
+ EVP_DigestUpdate (hash_context, buf, iff.gcount ());
+ }
+
+ retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+ EVP_DigestFinal_ex (hash_context,
+ retval->m_buf, &retval->m_length);
+
+ EVP_MD_CTX_destroy (hash_context);
+
+ return retval;
+}
diff --git a/src/hash-string-converter.h b/src/hash-helper.h
similarity index 80%
rename from src/hash-string-converter.h
rename to src/hash-helper.h
index b696426..ea12f30 100644
--- a/src/hash-string-converter.h
+++ b/src/hash-helper.h
@@ -1,6 +1,6 @@
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
- * Copyright (c) 2012 University of California, Los Angeles
+ * Copyright (c) 2012-2013 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
@@ -19,18 +19,24 @@
* Zhenkai Zhu <zhenkai@cs.ucla.edu>
*/
-#ifndef HASH_STRING_CONVERTER_H
-#define HASH_STRING_CONVERTER_H
+#ifndef HASH_HELPER_H
+#define HASH_HELPER_H
#include <string.h>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/exception/all.hpp>
+// 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
+
+class Hash;
+typedef boost::shared_ptr<Hash> HashPtr;
+
class Hash
{
public:
- Hash (const void *buf, size_t length)
+ Hash (const void *buf, unsigned int length)
: m_length (length)
{
if (m_length != 0)
@@ -40,8 +46,12 @@
}
}
- Hash (const std::string &hashInTextEncoding);
+ static HashPtr
+ FromString (const std::string &hashInTextEncoding);
+ static HashPtr
+ FromFileContent (const std::string &hashInTextEncoding);
+
~Hash ()
{
if (m_length != 0)
@@ -70,7 +80,7 @@
return m_buf;
}
- size_t
+ unsigned int
GetHashBytes () const
{
return m_length;
@@ -78,7 +88,7 @@
private:
unsigned char *m_buf;
- size_t m_length;
+ unsigned int m_length;
Hash (const Hash &) { }
Hash & operator = (const Hash &) { return *this; }
@@ -91,8 +101,6 @@
struct HashConversion : virtual boost::exception, virtual std::exception { };
}
-typedef boost::shared_ptr<Hash> HashPtr;
-
std::ostream &
operator << (std::ostream &os, const Hash &digest);
diff --git a/src/sqlite-helper.cc b/src/sqlite-helper.cc
deleted file mode 100644
index 1a3ed1c..0000000
--- a/src/sqlite-helper.cc
+++ /dev/null
@@ -1,453 +0,0 @@
-/* -*- 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"
-#include <boost/make_shared.hpp>
-#include <boost/ref.hpp>
-
-// 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;
-
-using namespace boost;
-
-const std::string INIT_DATABASE = "\
-PRAGMA foreign_keys = ON; \
- \
-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 TRIGGER SyncNodesUpdater_trigger \
- BEFORE INSERT ON SyncNodes \
- FOR EACH ROW \
- WHEN (SELECT device_id \
- FROM SyncNodes \
- WHERE device_name=NEW.device_name) \
- IS NOT NULL \
- BEGIN \
- UPDATE SyncNodes \
- SET seq_no=max(seq_no,NEW.seq_no) \
- WHERE device_name=NEW.device_name; \
- SELECT RAISE(IGNORE); \
- END; \
- \
-CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name); \
- \
-CREATE TABLE SyncLog( \
- state_id INTEGER PRIMARY KEY AUTOINCREMENT, \
- state_hash BLOB NOT NULL UNIQUE, \
- last_update TIMESTAMP NOT NULL \
- ); \
- \
-CREATE TABLE \
- SyncStateNodes( \
- id INTEGER PRIMARY KEY AUTOINCREMENT, \
- state_id INTEGER NOT NULL \
- REFERENCES SyncLog (state_id) 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_id ON SyncStateNodes (state_id); \
-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''"));
- }
-
- res = sqlite3_create_function (m_db, "hash2str", 2, SQLITE_ANY, 0,
- DbHelper::hash2str_Func,
- 0, 0);
- if (res != SQLITE_OK)
- {
- BOOST_THROW_EXCEPTION (Error::Db ()
- << errmsg_info_str ("Cannot create function ``hash''"));
- }
-
- res = sqlite3_create_function (m_db, "str2hash", 2, SQLITE_ANY, 0,
- DbHelper::str2hash_Func,
- 0, 0);
- 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);
- }
-}
-
-DbHelper::~DbHelper ()
-{
- int res = sqlite3_close (m_db);
- if (res != SQLITE_OK)
- {
- // complain
- }
-}
-
-HashPtr
-DbHelper::RememberStateInStateLog ()
-{
- int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
-
- 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; \
-", 0,0,0);
-
- if (res != SQLITE_OK)
- {
- BOOST_THROW_EXCEPTION (Error::Db ()
- << errmsg_info_str ("1"));
- }
-
- sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db);
-
- sqlite3_stmt *insertStmt;
- res += sqlite3_prepare (m_db, "\
-INSERT INTO SyncStateNodes \
- (state_id, device_id, seq_no) \
- SELECT ?, device_id, seq_no \
- FROM SyncNodes; \
-", -1, &insertStmt, 0);
-
- res += sqlite3_bind_int64 (insertStmt, 1, rowId);
- sqlite3_step (insertStmt);
-
- if (res != SQLITE_OK)
- {
- BOOST_THROW_EXCEPTION (Error::Db ()
- << errmsg_info_str ("4"));
- }
- sqlite3_finalize (insertStmt);
-
- sqlite3_stmt *getHashStmt;
- res += sqlite3_prepare (m_db, "\
-SELECT state_hash FROM SyncLog WHERE state_id = ?\
-", -1, &getHashStmt, 0);
- res += sqlite3_bind_int64 (getHashStmt, 1, rowId);
-
- HashPtr retval;
- int stepRes = sqlite3_step (getHashStmt);
- if (stepRes == SQLITE_ROW)
- {
- retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0),
- sqlite3_column_bytes (getHashStmt, 0));
- }
- sqlite3_finalize (getHashStmt);
- res += sqlite3_exec (m_db, "COMMIT;", 0,0,0);
-
- if (res != SQLITE_OK)
- {
- BOOST_THROW_EXCEPTION (Error::Db ()
- << errmsg_info_str ("Some error with rememberStateInStateLog"));
- }
-
- return retval;
-}
-
-
-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
- {
- char charNullResult = 0;
- sqlite3_result_blob (context, &charNullResult, 1, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
- 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);
-}
-
-void
-DbHelper::hash2str_Func (sqlite3_context *context, int argc, sqlite3_value **argv)
-{
- if (argc != 1 || sqlite3_value_type (argv[0]) != SQLITE_BLOB)
- {
- sqlite3_result_error (context, "Wrong arguments are supplied for ``hash2str'' function", -1);
- return;
- }
-
- int hashBytes = sqlite3_value_bytes (argv[0]);
- const void *hash = sqlite3_value_blob (argv[0]);
-
- std::ostringstream os;
- Hash tmpHash (hash, hashBytes);
- os << tmpHash;
- sqlite3_result_text (context, os.str ().c_str (), -1, SQLITE_TRANSIENT);
-}
-
-void
-DbHelper::str2hash_Func (sqlite3_context *context, int argc, sqlite3_value **argv)
-{
- if (argc != 1 || sqlite3_value_type (argv[0]) != SQLITE_TEXT)
- {
- sqlite3_result_error (context, "Wrong arguments are supplied for ``str2hash'' function", -1);
- return;
- }
-
- size_t hashTextBytes = sqlite3_value_bytes (argv[0]);
- const unsigned char *hashText = sqlite3_value_text (argv[0]);
-
- Hash hash (std::string (reinterpret_cast<const char*> (hashText), hashTextBytes));
- sqlite3_result_blob (context, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
-}
-
-
-sqlite3_int64
-DbHelper::LookupSyncLog (const std::string &stateHash)
-{
- Hash tmpHash (stateHash);
- return LookupSyncLog (stateHash);
-}
-
-sqlite3_int64
-DbHelper::LookupSyncLog (const Hash &stateHash)
-{
- sqlite3_stmt *stmt;
- int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?",
- -1, &stmt, 0);
-
- if (res != SQLITE_OK)
- {
- BOOST_THROW_EXCEPTION (Error::Db ()
- << errmsg_info_str ("Cannot prepare statement"));
- }
-
- res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC);
- if (res != SQLITE_OK)
- {
- BOOST_THROW_EXCEPTION (Error::Db ()
- << errmsg_info_str ("Cannot bind"));
- }
-
- sqlite3_int64 row = 0; // something bad
-
- if (sqlite3_step (stmt) == SQLITE_ROW)
- {
- row = sqlite3_column_int64 (stmt, 0);
- }
-
- sqlite3_finalize (stmt);
-
- return row;
-}
-
-void
-DbHelper::UpdateDeviceSeqno (const std::string &name, uint64_t seqNo)
-{
- sqlite3_stmt *stmt;
- // update is performed using trigger
- int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);",
- -1, &stmt, 0);
-
- res += sqlite3_bind_text (stmt, 1, name.c_str (), name.size (), SQLITE_STATIC);
- res += sqlite3_bind_int64 (stmt, 2, seqNo);
- sqlite3_step (stmt);
-
- if (res != SQLITE_OK)
- {
- BOOST_THROW_EXCEPTION (Error::Db ()
- << errmsg_info_str ("Some error with UpdateDeviceSeqno"));
- }
- sqlite3_finalize (stmt);
-}
-
-void
-DbHelper::FindStateDifferences (const std::string &oldHash, const std::string &newHash)
-{
- Hash tmpOldHash (oldHash);
- Hash tmpNewHash (newHash);
-
- FindStateDifferences (tmpOldHash, tmpNewHash);
-}
-
-void
-DbHelper::FindStateDifferences (const Hash &oldHash, const Hash &newHash)
-{
- sqlite3_stmt *stmt;
-
- int res = sqlite3_prepare_v2 (m_db, "\
-SELECT sn.device_name, s_old.seq_no, s_new.seq_no \
- FROM (SELECT * \
- FROM SyncStateNodes \
- WHERE state_id=(SELECT state_id \
- FROM SyncLog \
- WHERE state_hash=:old_hash)) s_old \
- LEFT JOIN (SELECT * \
- FROM SyncStateNodes \
- WHERE state_id=(SELECT state_id \
- FROM SyncLog \
- WHERE state_hash=:new_hash)) s_new \
- \
- ON s_old.device_id = s_new.device_id \
- JOIN SyncNodes sn ON sn.device_id = s_old.device_id \
- \
- WHERE s_new.seq_no IS NULL OR \
- s_old.seq_no != s_new.seq_no \
- \
-UNION ALL \
- \
-SELECT sn.device_name, s_old.seq_no, s_new.seq_no \
- FROM (SELECT * \
- FROM SyncStateNodes \
- WHERE state_id=(SELECT state_id \
- FROM SyncLog \
- WHERE state_hash=:new_hash )) s_new \
- LEFT JOIN (SELECT * \
- FROM SyncStateNodes \
- WHERE state_id=(SELECT state_id \
- FROM SyncLog \
- WHERE state_hash=:old_hash)) s_old \
- \
- ON s_old.device_id = s_new.device_id \
- JOIN SyncNodes sn ON sn.device_id = s_new.device_id \
- \
- WHERE s_old.seq_no IS NULL \
-", -1, &stmt, 0);
-
- if (res != SQLITE_OK)
- {
- BOOST_THROW_EXCEPTION (Error::Db ()
- << errmsg_info_str ("Some error with FindStateDifferences"));
- }
-
- res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC);
- res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC);
-
- while (sqlite3_step (stmt) == SQLITE_ROW)
- {
- std::cout << sqlite3_column_text (stmt, 0) <<
- ": from " << sqlite3_column_int64 (stmt, 1) <<
- " to " << sqlite3_column_int64 (stmt, 2) <<
- std::endl;
- }
- sqlite3_finalize (stmt);
-
-}
diff --git a/src/sqlite-helper.h b/src/sqlite-helper.h
deleted file mode 100644
index bf98fe8..0000000
--- a/src/sqlite-helper.h
+++ /dev/null
@@ -1,94 +0,0 @@
-/* -*- 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 <stdint.h>
-#include <sqlite3.h>
-#include <openssl/evp.h>
-#include <boost/exception/all.hpp>
-#include <string>
-#include "hash-string-converter.h"
-
-// temporarily ignoring all potential database errors
-
-class DbHelper
-{
-public:
- DbHelper (const std::string &path);
- ~DbHelper ();
-
- // done
- void
- UpdateDeviceSeqno (const std::string &name, uint64_t seqNo);
-
- // std::string
- // LookupForwardingAlias (const std::string &deviceName);
-
- // void
- // UpdateForwardingAlias ();
-
- // done
- /**
- * Create an entry in SyncLog and SyncStateNodes corresponding to the current state of SyncNodes
- */
- HashPtr
- RememberStateInStateLog ();
-
- // done
- sqlite3_int64
- LookupSyncLog (const std::string &stateHash);
-
- // done
- sqlite3_int64
- LookupSyncLog (const Hash &stateHash);
-
- // How difference is exposed will be determined later by the actual protocol
- void
- FindStateDifferences (const std::string &oldHash, const std::string &newHash);
-
- void
- FindStateDifferences (const Hash &oldHash, const Hash &newHash);
-
-private:
- static void
- hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv);
-
- static void
- hash_xFinal (sqlite3_context *context);
-
- static void
- hash2str_Func (sqlite3_context *context, int argc, sqlite3_value **argv);
-
- static void
- str2hash_Func (sqlite3_context *context, int argc, sqlite3_value **argv);
-
-private:
- sqlite3 *m_db;
-};
-
-namespace Error {
-struct Db : virtual boost::exception, virtual std::exception { };
-}
-
-
-#endif // SQLITE_HELPER_H
diff --git a/src/sync-log.cc b/src/sync-log.cc
new file mode 100644
index 0000000..3e49c69
--- /dev/null
+++ b/src/sync-log.cc
@@ -0,0 +1,236 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 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 "sync-log.h"
+
+#include <boost/make_shared.hpp>
+
+using namespace boost;
+using namespace std;
+
+HashPtr
+SyncLog::RememberStateInStateLog ()
+{
+ int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
+
+ 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; \
+", 0,0,0);
+
+ if (res != SQLITE_OK)
+ {
+ BOOST_THROW_EXCEPTION (Error::Db ()
+ << errmsg_info_str ("1"));
+ }
+
+ sqlite3_int64 rowId = sqlite3_last_insert_rowid (m_db);
+
+ sqlite3_stmt *insertStmt;
+ res += sqlite3_prepare (m_db, "\
+INSERT INTO SyncStateNodes \
+ (state_id, device_id, seq_no) \
+ SELECT ?, device_id, seq_no \
+ FROM SyncNodes; \
+", -1, &insertStmt, 0);
+
+ res += sqlite3_bind_int64 (insertStmt, 1, rowId);
+ sqlite3_step (insertStmt);
+
+ if (res != SQLITE_OK)
+ {
+ BOOST_THROW_EXCEPTION (Error::Db ()
+ << errmsg_info_str ("4"));
+ }
+ sqlite3_finalize (insertStmt);
+
+ sqlite3_stmt *getHashStmt;
+ res += sqlite3_prepare (m_db, "\
+SELECT state_hash FROM SyncLog WHERE state_id = ?\
+", -1, &getHashStmt, 0);
+ res += sqlite3_bind_int64 (getHashStmt, 1, rowId);
+
+ HashPtr retval;
+ int stepRes = sqlite3_step (getHashStmt);
+ if (stepRes == SQLITE_ROW)
+ {
+ retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0),
+ sqlite3_column_bytes (getHashStmt, 0));
+ }
+ sqlite3_finalize (getHashStmt);
+ res += sqlite3_exec (m_db, "COMMIT;", 0,0,0);
+
+ if (res != SQLITE_OK)
+ {
+ BOOST_THROW_EXCEPTION (Error::Db ()
+ << errmsg_info_str ("Some error with rememberStateInStateLog"));
+ }
+
+ return retval;
+}
+
+sqlite3_int64
+SyncLog::LookupSyncLog (const std::string &stateHash)
+{
+ return LookupSyncLog (*Hash::FromString (stateHash));
+}
+
+sqlite3_int64
+SyncLog::LookupSyncLog (const Hash &stateHash)
+{
+ sqlite3_stmt *stmt;
+ int res = sqlite3_prepare (m_db, "SELECT state_id FROM SyncLog WHERE state_hash = ?",
+ -1, &stmt, 0);
+
+ if (res != SQLITE_OK)
+ {
+ BOOST_THROW_EXCEPTION (Error::Db ()
+ << errmsg_info_str ("Cannot prepare statement"));
+ }
+
+ res = sqlite3_bind_blob (stmt, 1, stateHash.GetHash (), stateHash.GetHashBytes (), SQLITE_STATIC);
+ if (res != SQLITE_OK)
+ {
+ BOOST_THROW_EXCEPTION (Error::Db ()
+ << errmsg_info_str ("Cannot bind"));
+ }
+
+ sqlite3_int64 row = 0; // something bad
+
+ if (sqlite3_step (stmt) == SQLITE_ROW)
+ {
+ row = sqlite3_column_int64 (stmt, 0);
+ }
+
+ sqlite3_finalize (stmt);
+
+ return row;
+}
+
+void
+SyncLog::UpdateDeviceSeqno (const std::string &name, uint64_t seqNo)
+{
+ sqlite3_stmt *stmt;
+ // update is performed using trigger
+ int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_name, seq_no) VALUES (?,?);",
+ -1, &stmt, 0);
+
+ res += sqlite3_bind_text (stmt, 1, name.c_str (), name.size (), SQLITE_STATIC);
+ res += sqlite3_bind_int64 (stmt, 2, seqNo);
+ sqlite3_step (stmt);
+
+ if (res != SQLITE_OK)
+ {
+ BOOST_THROW_EXCEPTION (Error::Db ()
+ << errmsg_info_str ("Some error with UpdateDeviceSeqno"));
+ }
+ sqlite3_finalize (stmt);
+}
+
+SyncStateMsgPtr
+SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash)
+{
+ return FindStateDifferences (*Hash::FromString (oldHash), *Hash::FromString (newHash));
+}
+
+SyncStateMsgPtr
+SyncLog::FindStateDifferences (const Hash &oldHash, const Hash &newHash)
+{
+ sqlite3_stmt *stmt;
+
+ int res = sqlite3_prepare_v2 (m_db, "\
+SELECT sn.device_name, s_old.seq_no, s_new.seq_no \
+ FROM (SELECT * \
+ FROM SyncStateNodes \
+ WHERE state_id=(SELECT state_id \
+ FROM SyncLog \
+ WHERE state_hash=:old_hash)) s_old \
+ LEFT JOIN (SELECT * \
+ FROM SyncStateNodes \
+ WHERE state_id=(SELECT state_id \
+ FROM SyncLog \
+ WHERE state_hash=:new_hash)) s_new \
+ \
+ ON s_old.device_id = s_new.device_id \
+ JOIN SyncNodes sn ON sn.device_id = s_old.device_id \
+ \
+ WHERE s_new.seq_no IS NULL OR \
+ s_old.seq_no != s_new.seq_no \
+ \
+UNION ALL \
+ \
+SELECT sn.device_name, s_old.seq_no, s_new.seq_no \
+ FROM (SELECT * \
+ FROM SyncStateNodes \
+ WHERE state_id=(SELECT state_id \
+ FROM SyncLog \
+ WHERE state_hash=:new_hash )) s_new \
+ LEFT JOIN (SELECT * \
+ FROM SyncStateNodes \
+ WHERE state_id=(SELECT state_id \
+ FROM SyncLog \
+ WHERE state_hash=:old_hash)) s_old \
+ \
+ ON s_old.device_id = s_new.device_id \
+ JOIN SyncNodes sn ON sn.device_id = s_new.device_id \
+ \
+ WHERE s_old.seq_no IS NULL \
+", -1, &stmt, 0);
+
+ if (res != SQLITE_OK)
+ {
+ BOOST_THROW_EXCEPTION (Error::Db ()
+ << errmsg_info_str ("Some error with FindStateDifferences"));
+ }
+
+ res += sqlite3_bind_blob (stmt, 1, oldHash.GetHash (), oldHash.GetHashBytes (), SQLITE_STATIC);
+ res += sqlite3_bind_blob (stmt, 2, newHash.GetHash (), newHash.GetHashBytes (), SQLITE_STATIC);
+
+ SyncStateMsgPtr msg = make_shared<SyncStateMsg> ();
+
+ while (sqlite3_step (stmt) == SQLITE_ROW)
+ {
+ SyncState *state = msg->add_state ();
+
+ state->set_name (reinterpret_cast<const char*> (sqlite3_column_text (stmt, 0)));
+
+ sqlite3_int64 newSeqNo = sqlite3_column_int64 (stmt, 2);
+ if (newSeqNo > 0)
+ {
+ state->set_type (SyncState::UPDATE);
+ state->set_seq (newSeqNo);
+ }
+ else
+ state->set_type (SyncState::DELETE);
+
+ // std::cout << sqlite3_column_text (stmt, 0) <<
+ // ": from " << sqlite3_column_int64 (stmt, 1) <<
+ // " to " << sqlite3_column_int64 (stmt, 2) <<
+ // std::endl;
+ }
+ sqlite3_finalize (stmt);
+
+ return msg;
+}
diff --git a/src/sync-log.h b/src/sync-log.h
index 267a08d..5705e45 100644
--- a/src/sync-log.h
+++ b/src/sync-log.h
@@ -1,6 +1,6 @@
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
- * Copyright (c) 2012 University of California, Los Angeles
+ * Copyright (c) 2013 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
@@ -22,27 +22,51 @@
#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)
+#include "db-helper.h"
+#include <sync-state.pb.h>
-#ifdef _DEBUG
+typedef boost::shared_ptr<SyncStateMsg> SyncStateMsgPtr;
-#include <boost/thread/thread.hpp>
-#include <boost/date_time/posix_time/posix_time.hpp>
-#include <iostream>
-#include <string>
+class SyncLog : public DbHelper
+{
+public:
+ SyncLog (const std::string &path)
+ : DbHelper (path)
+ {
+ }
-#define _LOG_DEBUG(x) \
- std::clog << boost::get_system_time () << " " << boost::this_thread::get_id () << " " << x << std::endl;
+ // done
+ void
+ UpdateDeviceSeqno (const std::string &name, uint64_t seqNo);
-#else
-#define _LOG_DEBUG(x)
-#endif
+ // std::string
+ // LookupForwardingAlias (const std::string &deviceName);
-#define _LOG_ERROR(x) \
- std::clog << boost::get_system_time () << " " << boost::this_thread::get_id () << " " << x << std::endl;
+ // void
+ // UpdateForwardingAlias ();
+
+ // done
+ /**
+ * Create an entry in SyncLog and SyncStateNodes corresponding to the current state of SyncNodes
+ */
+ HashPtr
+ RememberStateInStateLog ();
+
+ // done
+ sqlite3_int64
+ LookupSyncLog (const std::string &stateHash);
+
+ // done
+ sqlite3_int64
+ LookupSyncLog (const Hash &stateHash);
+
+ // How difference is exposed will be determined later by the actual protocol
+ SyncStateMsgPtr
+ FindStateDifferences (const std::string &oldHash, const std::string &newHash);
+
+ SyncStateMsgPtr
+ FindStateDifferences (const Hash &oldHash, const Hash &newHash);
+};
+
#endif // SYNC_LOG_H
diff --git a/src/sync-state.proto b/src/sync-state.proto
new file mode 100644
index 0000000..9ef5e33
--- /dev/null
+++ b/src/sync-state.proto
@@ -0,0 +1,24 @@
+// message Name
+// {
+// repeated bytes comp = 1;
+// }
+
+message SyncState
+{
+ // required Name name = 1;
+ required string name = 1;
+
+ enum ActionType
+ {
+ UPDATE = 0;
+ DELETE = 1;
+ }
+ required ActionType type = 2;
+
+ optional uint64 seq = 3;
+}
+
+message SyncStateMsg
+{
+ repeated SyncState state = 1;
+}
diff --git a/waf b/waf
index 69fb819..a6859ac 100755
--- a/waf
+++ b/waf
Binary files differ
diff --git a/wscript b/wscript
index e35bc4c..cf5f8bf 100644
--- a/wscript
+++ b/wscript
@@ -11,11 +11,13 @@
opt.add_option('--test', action='store_true',default=False,dest='_test',help='''build unit tests''')
opt.add_option('--yes',action='store_true',default=False) # for autoconf/automake/make compatibility
- opt.load('compiler_cxx boost ccnx protoc')
+ opt.load('compiler_cxx boost ccnx protoc ice_cxx')
def configure(conf):
conf.load("compiler_cxx")
+ conf.define ("CHRONOSHARE_VERSION", VERSION)
+
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):
@@ -50,6 +52,7 @@
conf.define('_TEST', 1)
conf.load('protoc')
+ conf.load('ice_cxx')
conf.write_config_header('src/config.h')
@@ -79,13 +82,37 @@
includes = ['include', ],
)
- chronoshare = bld (
- target="tmp",
+ common = bld.objects (
+ target = "common",
+ features = ["cxx"],
+ source = ['src/hash-helper.cc',
+ 'src/chronoshare-client.ice',
+ ],
+ use = 'BOOST',
+ includes = ['include', 'src'],
+ )
+
+
+ client = bld (
+ target="cs-client",
+ features=['cxx', 'cxxprogram'],
+ source = ['client/client.cc',
+ ],
+ use = "BOOST CCNX SSL ICE common",
+ includes = ['include', 'src'],
+ )
+
+ daemon = bld (
+ target="cs-daemon",
features=['cxx', 'cxxprogram'],
# source = bld.path.ant_glob(['src/**/*.cc']),
- source = ['src/main.cc',
- 'src/sqlite-helper.cc',
- 'src/hash-string-converter.cc'],
- use = 'BOOST BOOST_IOSTREAMS BOOST_REGEX CCNX SSL SQLITE3',
+ source = ['daemon/daemon.cc',
+ 'daemon/notify-i.cc',
+ 'src/db-helper.cc',
+ 'src/sync-log.cc',
+ 'src/action-log.cc',
+ 'src/sync-state.proto',
+ ],
+ use = "BOOST CCNX SSL SQLITE3 ICE common",
includes = ['include', 'src'],
)