Merge remote-tracking branch 'git.irl/master'
diff --git a/client/client.cc b/client/client.cc
index 8ab8013..df827ba 100644
--- a/client/client.cc
+++ b/client/client.cc
@@ -93,19 +93,13 @@
               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_atime, fileStats.st_mtime, fileStats.st_ctime,
                                       fileStats.st_mode);
                 }
               else
diff --git a/daemon/daemon.cc b/daemon/daemon.cc
index 97fee4f..5b953d3 100644
--- a/daemon/daemon.cc
+++ b/daemon/daemon.cc
@@ -25,6 +25,7 @@
 #include <Ice/Identity.h>
 
 #include "notify-i.h"
+#include <boost/make_shared.hpp>
 
 using namespace std;
 using namespace boost;
@@ -34,6 +35,12 @@
 
 class MyService : public Ice::Service
 {
+public:
+  MyService (ActionLogPtr actionLog)
+  : m_actionLog (actionLog)
+  {
+  }
+  
 protected:
   virtual bool start (int, char*[], int&)
   {
@@ -41,7 +48,7 @@
 
     Ice::Identity identity;
     identity.name="NotifyDaemon";
-    NotifyPtr servant=new NotifyI;
+    NotifyPtr servant=new NotifyI (m_actionLog);
     
     _adapter->add (servant, identity);
     
@@ -51,7 +58,8 @@
   }
   
 private:
-    Ice::ObjectAdapterPtr _adapter;  
+  Ice::ObjectAdapterPtr _adapter;
+  ActionLogPtr m_actionLog;
 };
 
 int
@@ -62,9 +70,9 @@
   try
     {
       // DbHelper db ("./", "/ndn/ucla.edu/alex");
-      ActionLog bla ("./", "/ndn/ucla.edu/alex");
+      ActionLogPtr actionLog = make_shared<ActionLog> ("./", "/ndn/ucla.edu/alex");
 
-      MyService svc;
+      MyService svc (actionLog);
       status = svc.main (argc, argv);
 
       // HashPtr hash = db.RememberStateInStateLog ();
diff --git a/daemon/notify-i.cc b/daemon/notify-i.cc
index 0f8b004..0994aa3 100644
--- a/daemon/notify-i.cc
+++ b/daemon/notify-i.cc
@@ -23,26 +23,35 @@
 #include <hash-helper.h>
 
 using namespace std;
+using namespace boost;
 
-// NotifyI::NotifyI (DbHelperPtr &db)
-//   : m_db (db)
-// {
-// }
+NotifyI::NotifyI (ActionLogPtr &actionLog)
+  : m_actionLog (actionLog)
+{
+}
 
 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::Long atime,
+                     ::Ice::Long mtime,
+                     ::Ice::Long 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;
+  cout << "updateFile " << filename << " with hash " << hash << endl;
+  try
+    {
+      m_actionLog->AddActionUpdate (filename, hash, atime, mtime, ctime, mode);
+
+      m_actionLog->RememberStateInStateLog ();
+    }
+  catch (const boost::exception &e)
+    {
+      cout << "ERRORR: " << *get_error_info<errmsg_info_str> (e) << endl;
+    }
 }
 
 void
@@ -50,16 +59,16 @@
                    const ::std::string &newFilename,
                    const ::Ice::Current&)
 {
-  // cout << "moveFile from " << oldFilename << " to " << newFilename << endl;
-  // m_db->AddActionMove (filename, oldFilename);
+  cout << "moveFile from " << oldFilename << " to " << newFilename << endl;
+  // m_actionLog->AddActionMove (oldFilename, newFilename);
 }
 
 void
 NotifyI::deleteFile (const ::std::string &filename,
                      const ::Ice::Current&)
 {
-  // m_db->AddActionDelete (filename, oldFilename);
-  
-  // cout << "deleteFile " << filename << endl;
+  cout << "deleteFile " << filename << endl;
+  m_actionLog->AddActionDelete (filename);  
+  m_actionLog->RememberStateInStateLog ();
 }
 
diff --git a/daemon/notify-i.h b/daemon/notify-i.h
index a2cea97..9fd7044 100644
--- a/daemon/notify-i.h
+++ b/daemon/notify-i.h
@@ -22,19 +22,20 @@
 #ifndef NOTIFY_I_H
 #define NOTIFY_I_H
 
+#include <action-log.h>
 #include <chronoshare-client.ice.h>
 
 class NotifyI : public ChronoshareClient::Notify
 {
 public:
-  // NotifyI (DbHelperPtr &db);
+  NotifyI (ActionLogPtr &actionLog);
   
   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::Long atime,
+              ::Ice::Long mtime,
+              ::Ice::Long ctime,
               ::Ice::Int mode,
               const ::Ice::Current& = ::Ice::Current());
 
@@ -48,7 +49,7 @@
               const ::Ice::Current& = ::Ice::Current());
 
 private:
-  // DbHelperPtr m_db;
+  ActionLogPtr m_actionLog;
 };
 
 #endif // NOTIFY_I_H
diff --git a/include/ccnx-wrapper.h b/include/ccnx-wrapper.h
index 90089e7..4801bc2 100644
--- a/include/ccnx-wrapper.h
+++ b/include/ccnx-wrapper.h
@@ -51,9 +51,8 @@
   static string
   extractName(const unsigned char *data, const ccn_indexbuf *comps);
 
-protected:
   Bytes
-  createContentObject(const string &name, const unsigned char *buf, size_t len, int freshness);
+  createContentObject(const string &name, const unsigned char *buf, size_t len, int freshness = 2147/* max value for ccnx*/);
 
   int
   putToCcnd (const Bytes &contentObject);
diff --git a/src/action-item.proto b/src/action-item.proto
new file mode 100644
index 0000000..6d50065
--- /dev/null
+++ b/src/action-item.proto
@@ -0,0 +1,22 @@
+message ActionItem
+{
+  enum ActionType
+  {
+    UPDATE = 0;
+    DELETE = 1;
+  }
+  required ActionType action = 3;
+
+  required string filename = 4;
+  required uint64 version = 5;
+  required uint32 timestamp = 6;
+
+  optional bytes  file_hash = 7;
+  optional uint32 atime = 8;
+  optional uint32 mtime = 9;
+  optional uint32 ctime = 10;
+  optional uint32 mode  = 11;
+
+  optional string parent_device_name = 12;
+  optional uint64 parent_seq_no = 13;
+}
diff --git a/src/action-log.cc b/src/action-log.cc
index cf5156a..41166e7 100644
--- a/src/action-log.cc
+++ b/src/action-log.cc
@@ -20,59 +20,213 @@
  */
 
 #include "action-log.h"
+#include <action-item.pb.h>
 
 using namespace boost;
 using namespace std;
 
 ActionLog::ActionLog (const std::string &path, const std::string &localName)
-  : SyncLog (path)
-  , m_localName (localName)
+  : SyncLog (path, localName)
 {
 }
 
+tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, string>
+ActionLog::GetExistingRecord (const std::string &filename)
+{
+  // check if something already exists
+  sqlite3_stmt *stmt;
+  int res = sqlite3_prepare_v2 (m_db, "SELECT a.version,a.device_id,a.seq_no,a.action,s.device_name "
+                                "FROM ActionLog a JOIN SyncNodes s ON s.device_id = a.device_id "
+                                "WHERE filename=? ORDER BY a.version DESC,a.device_id DESC LIMIT 1", -1, &stmt, 0);
+
+  if (res != SQLITE_OK)
+    {
+      BOOST_THROW_EXCEPTION (Error::Db ()
+                             << errmsg_info_str ("Some error with GetExistingRecord"));
+    }
+
+  // with parent with version number + 1
+  sqlite3_int64 version = 0;
+  sqlite3_int64 parent_device_id = -1;
+  sqlite3_int64 parent_seq_no = -1;
+  string parent_device_name;
+  
+  sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
+  if (sqlite3_step (stmt) == SQLITE_ROW) 
+    {
+      version = sqlite3_column_int64 (stmt, 0) + 1;
+
+      if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
+        {
+          parent_device_id = sqlite3_column_int64 (stmt, 1);
+          parent_seq_no = sqlite3_column_int64 (stmt, 2);
+          parent_device_name = string(reinterpret_cast<const char*> (sqlite3_column_text (stmt, 4)));
+        }
+    }
+  
+  sqlite3_finalize (stmt);
+  return make_tuple (version, parent_device_id, parent_seq_no, parent_device_name);
+}
+
 // 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,
+                            time_t atime, time_t mtime, time_t ctime,
                             int mode)
 {
-  int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
+  sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
+  
+  sqlite3_int64 seq_no = GetNextLocalSeqNo ();
+  sqlite3_int64 version = 0;
+  sqlite3_int64 parent_device_id = -1;
+  string        parent_device_name;
+  sqlite3_int64 parent_seq_no = -1;
 
-  // 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);
+  sqlite3_int64 action_time = time (0);
+  
+  tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
+  
+  sqlite3_stmt *stmt;
+  int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
+                                "(device_id, seq_no, action, filename, version, action_timestamp, "
+                                "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
+                                "parent_device_id, parent_seq_no) "
+                                "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
+                                "        ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
+                                "        ?, ?);", -1, &stmt, 0);
 
-  // if (res != SQLITE_OK)
-  //   {
-  //     BOOST_THROW_EXCEPTION (Error::Db ()
-  //                            << errmsg_info_str ("Some error with AddActionUpdate"));
-  //   }
+  // cout << "INSERT INTO ActionLog "
+  //                               "(device_id, seq_no, action, filename, version, action_timestamp, "
+  //                               "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
+  //                               "parent_device_id, parent_seq_no) "
+  //                               "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
+  //                               "        ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
+  //   "        ?, ?)" << endl;
+  
+  if (res != SQLITE_OK)
+    {
+      BOOST_THROW_EXCEPTION (Error::Db ()
+                             << errmsg_info_str (sqlite3_errmsg (m_db))
+                             );
+                             // << errmsg_info_str ("Some error with prepare 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);
+  
+  sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
+  sqlite3_bind_int64 (stmt, 2, seq_no);
+  sqlite3_bind_int   (stmt, 3, 0);
+  sqlite3_bind_text  (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
+  sqlite3_bind_int64 (stmt, 5, version);
+  sqlite3_bind_int64 (stmt, 6, action_time);
+  
+  sqlite3_bind_blob  (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
+  
+  sqlite3_bind_int64 (stmt, 8, atime);
+  sqlite3_bind_int64 (stmt, 9, mtime);
+  sqlite3_bind_int64 (stmt, 10, ctime);
+  sqlite3_bind_int   (stmt, 11, mode);
 
-      
-  //   }
-  // else
-  //   {
-  //     // without parent
-      
-  //   }
+  if (parent_device_id > 0 && parent_seq_no > 0)
+    {
+      sqlite3_bind_int64 (stmt, 12, parent_device_id);
+      sqlite3_bind_int64 (stmt, 13, parent_seq_no);
+    }
+  else
+    {
+      sqlite3_bind_null (stmt, 12);
+      sqlite3_bind_null (stmt, 13);
+    }
+  
+  sqlite3_step (stmt);
 
-  res += sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
+  // missing part: creating ContentObject for the action !!!
+
+  ActionItem item;
+  item.set_action (ActionItem::UPDATE);
+  item.set_filename (filename);
+  item.set_version (version);
+  item.set_timestamp (action_time);
+  item.set_file_hash (hash.GetHash (), hash.GetHashBytes ());
+  item.set_atime (atime);
+  item.set_mtime (mtime);
+  item.set_ctime (ctime);
+  item.set_mode (mode);
+
+  if (parent_device_id > 0 && parent_seq_no > 0)
+    {
+      item.set_parent_device_name (parent_device_name);
+      item.set_parent_seq_no (parent_seq_no);
+    }
+
+  // assign name to the action, serialize action, and create content object
+  
+  sqlite3_finalize (stmt); 
+                          
+  sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
 }
 
 void
 ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
 {
+  // not supported yet
+  BOOST_THROW_EXCEPTION (Error::Db ()
+                         << errmsg_info_str ("Move operation is not yet supported"));
 }
 
 void
 ActionLog::AddActionDelete (const std::string &filename)
 {
+  sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
+  
+  sqlite3_int64 version = 0;
+  sqlite3_int64 parent_device_id = -1;
+  string        parent_device_name;
+  sqlite3_int64 parent_seq_no = -1;
+
+  sqlite3_int64 action_time = time (0);
+  
+  tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
+  if (parent_device_id < 0) // no records exist or file was already deleted
+    {
+      sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);  
+      return;
+    }
+
+  sqlite3_int64 seq_no = GetNextLocalSeqNo ();
+
+  sqlite3_stmt *stmt;
+  sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
+                      "(device_id, seq_no, action, filename, version, action_timestamp, "
+                      "parent_device_id, parent_seq_no) "
+                      "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
+                      "        ?, ?)", -1, &stmt, 0);
+
+  sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
+  sqlite3_bind_int64 (stmt, 2, seq_no);
+  sqlite3_bind_int   (stmt, 3, 1);
+  sqlite3_bind_text  (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
+  sqlite3_bind_int64 (stmt, 5, version);
+  sqlite3_bind_int64 (stmt, 6, action_time);
+
+  sqlite3_bind_int64 (stmt, 7, parent_device_id);
+  sqlite3_bind_int64 (stmt, 8, parent_seq_no);
+  
+  sqlite3_step (stmt);
+
+  // missing part: creating ContentObject for the action !!!
+
+  ActionItem item;
+  item.set_action (ActionItem::UPDATE);
+  item.set_filename (filename);
+  item.set_version (version);
+  item.set_timestamp (action_time);
+  item.set_parent_device_name (parent_device_name);
+  item.set_parent_seq_no (parent_seq_no);
+
+  // assign name to the action, serialize action, and create content object
+  
+  sqlite3_finalize (stmt); 
+  
+  sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);    
 }
diff --git a/src/action-log.h b/src/action-log.h
index 6827cba..1d63fb8 100644
--- a/src/action-log.h
+++ b/src/action-log.h
@@ -23,29 +23,33 @@
 #define ACTION_LOG_H
 
 #include "sync-log.h"
+#include <boost/tuple/tuple.hpp>
+
+class ActionLog;
+typedef boost::shared_ptr<ActionLog> ActionLogPtr;
 
 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);
+                   const Hash &hash,
+                   time_t atime, time_t mtime, time_t ctime,
+                   int mode);
 
   void
   AddActionMove (const std::string &oldFile, const std::string &newFile);
-
+  
   void
   AddActionDelete (const std::string &filename);
+
+private:
+  boost::tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, std::string>
+  GetExistingRecord (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
index be40c3e..e3bf8e2 100644
--- a/src/chronoshare-client.ice
+++ b/src/chronoshare-client.ice
@@ -25,7 +25,7 @@
   
   interface Notify 
   {
-    void updateFile (string filename, ["cpp:array"] HashBytes fileHash, string atime, string mtime, string ctime, int mode);
+    void updateFile (string filename, ["cpp:array"] HashBytes fileHash, long atime, long mtime, long ctime, int mode);
 
     void moveFile (string origFilename, string newFilename);
 
diff --git a/src/db-helper.cc b/src/db-helper.cc
index 9bd2d61..f0e78d0 100644
--- a/src/db-helper.cc
+++ b/src/db-helper.cc
@@ -106,13 +106,13 @@
     parent_device_id INTEGER,                                           \n\
     parent_seq_no    INTEGER,                                           \n\
                                                                         \n\
-    action_name	     TEXT NOT NULL,                                     \n\
-    action_content_object BLOB NOT NULL,                                \n\
+    action_name	     TEXT,                                              \n\
+    action_content_object BLOB,                                         \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\
+	REFERENCES ActionLog (device_id, seq_no)                        \n\
 	ON UPDATE RESTRICT                                              \n\
 	ON DELETE SET NULL                                              \n\
 );                                                                      \n\
diff --git a/src/sync-log.cc b/src/sync-log.cc
index 3e49c69..677904a 100644
--- a/src/sync-log.cc
+++ b/src/sync-log.cc
@@ -26,6 +26,53 @@
 using namespace boost;
 using namespace std;
 
+SyncLog::SyncLog (const std::string &path, const std::string &localName)
+  : DbHelper (path)
+  , m_localName (localName)
+{
+  SyncLog::RememberStateInStateLog ();
+
+  UpdateDeviceSeqno (localName, 0);
+  
+  sqlite3_stmt *stmt;
+  int res = sqlite3_prepare_v2 (m_db, "SELECT device_id, seq_no FROM SyncNodes WHERE device_name=?", -1, &stmt, 0);
+  sqlite3_bind_text (stmt, 1, m_localName.c_str (), m_localName.size (), SQLITE_STATIC);
+
+  if (sqlite3_step (stmt) == SQLITE_ROW)
+    {
+      m_localDeviceId = sqlite3_column_int64 (stmt, 0);
+    }
+  else
+    {
+      BOOST_THROW_EXCEPTION (Error::Db ()
+                             << errmsg_info_str ("Impossible thing in SyncLog::SyncLog"));
+    }
+  sqlite3_finalize (stmt);
+}
+
+
+sqlite3_int64
+SyncLog::GetNextLocalSeqNo ()
+{
+  sqlite3_stmt *stmt_seq;
+  sqlite3_prepare_v2 (m_db, "SELECT seq_no FROM SyncNodes WHERE device_id = ?", -1, &stmt_seq, 0);
+  sqlite3_bind_int64 (stmt_seq, 1, m_localDeviceId);
+
+  if (sqlite3_step (stmt_seq) != SQLITE_ROW)
+    {
+      BOOST_THROW_EXCEPTION (Error::Db ()
+                             << errmsg_info_str ("Impossible thing in ActionLog::AddActionUpdate"));
+    }
+  
+  sqlite3_int64 seq_no = sqlite3_column_int64 (stmt_seq, 0) + 1; 
+  sqlite3_finalize (stmt_seq);
+
+  UpdateDeviceSeqNo (m_localDeviceId, seq_no);
+  
+  return seq_no;
+}
+
+
 HashPtr
 SyncLog::RememberStateInStateLog ()
 {
@@ -130,7 +177,7 @@
 }
 
 void
-SyncLog::UpdateDeviceSeqno (const std::string &name, uint64_t seqNo)
+SyncLog::UpdateDeviceSeqno (const std::string &name, sqlite3_int64 seqNo)
 {
   sqlite3_stmt *stmt;
   // update is performed using trigger
@@ -144,11 +191,32 @@
   if (res != SQLITE_OK)
     {
       BOOST_THROW_EXCEPTION (Error::Db ()
-                             << errmsg_info_str ("Some error with UpdateDeviceSeqno"));
+                             << errmsg_info_str ("Some error with UpdateDeviceSeqno (name)"));
     }
   sqlite3_finalize (stmt);
 }
 
+void
+SyncLog::UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo)
+{
+  sqlite3_stmt *stmt;
+  // update is performed using trigger
+  int res = sqlite3_prepare (m_db, "UPDATE SyncNodes SET seq_no=MAX(seq_no,?) WHERE device_id=?;", 
+                             -1, &stmt, 0);
+
+  res += sqlite3_bind_int64 (stmt, 1, seqNo);
+  res += sqlite3_bind_int64 (stmt, 2, deviceId);
+  sqlite3_step (stmt);
+  
+  if (res != SQLITE_OK)
+    {
+      BOOST_THROW_EXCEPTION (Error::Db ()
+                             << errmsg_info_str ("Some error with UpdateDeviceSeqNo (id)"));
+    }
+  sqlite3_finalize (stmt);
+}
+
+
 SyncStateMsgPtr
 SyncLog::FindStateDifferences (const std::string &oldHash, const std::string &newHash)
 {
diff --git a/src/sync-log.h b/src/sync-log.h
index 5705e45..bef41a7 100644
--- a/src/sync-log.h
+++ b/src/sync-log.h
@@ -30,14 +30,17 @@
 class SyncLog : public DbHelper
 {
 public:
-  SyncLog (const std::string &path)
-    : DbHelper (path)
-  {
-  }
+  SyncLog (const std::string &path, const std::string &localName);
 
+  sqlite3_int64
+  GetNextLocalSeqNo (); // side effect: local seq_no will be increased
+
+  void
+  UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo);
+  
   // done
   void
-  UpdateDeviceSeqno (const std::string &name, uint64_t seqNo);
+  UpdateDeviceSeqno (const std::string &name, sqlite3_int64 seqNo);
 
   // std::string
   // LookupForwardingAlias (const std::string &deviceName);
@@ -66,6 +69,11 @@
 
   SyncStateMsgPtr
   FindStateDifferences (const Hash &oldHash, const Hash &newHash);  
+
+protected:
+  std::string m_localName;
+  sqlite3_int64 m_localDeviceId;
+  
 };
 
 
diff --git a/wscript b/wscript
index cf5f8bf..7ce0339 100644
--- a/wscript
+++ b/wscript
@@ -112,6 +112,7 @@
                   'src/sync-log.cc',
                   'src/action-log.cc',
                   'src/sync-state.proto',
+                  'src/action-item.proto',
                   ],
         use = "BOOST CCNX SSL SQLITE3 ICE common",
         includes = ['include', 'src'],