Extending ccnx-name and fixing bug with toString
diff --git a/daemon/daemon.cc b/daemon/daemon.cc
index 5b953d3..ae41418 100644
--- a/daemon/daemon.cc
+++ b/daemon/daemon.cc
@@ -23,6 +23,7 @@
 #include <iostream>
 #include <Ice/Service.h>
 #include <Ice/Identity.h>
+#include <ccnx-wrapper.h>
 
 #include "notify-i.h"
 #include <boost/make_shared.hpp>
@@ -30,6 +31,7 @@
 using namespace std;
 using namespace boost;
 using namespace ChronoshareClient;
+using namespace Ccnx;
 
 typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; 
 
@@ -70,7 +72,8 @@
   try
     {
       // DbHelper db ("./", "/ndn/ucla.edu/alex");
-      ActionLogPtr actionLog = make_shared<ActionLog> ("./", "/ndn/ucla.edu/alex");
+      CcnxWrapperPtr ccnx = make_shared<CcnxWrapper> ();
+      ActionLogPtr actionLog = make_shared<ActionLog> (ccnx, "./", "/ndn/ucla.edu/alex", "shared");
 
       MyService svc (actionLog);
       status = svc.main (argc, argv);
diff --git a/include/ccnx-name.h b/include/ccnx-name.h
index 5f5a5c0..a6b6228 100644
--- a/include/ccnx-name.h
+++ b/include/ccnx-name.h
@@ -85,6 +85,22 @@
   Name &
   appendComp(const string &compStr);
 
+  Name &
+  appendComp(const Name &name);
+
+  Name &
+  appendComp(const void *buf, size_t size); 
+
+  /**
+   * Append int component
+   *
+   * Difference between this and appendComp call is that data is appended in network order
+   *
+   * Also, this function honors naming convention (%00 prefix is added)
+   */
+  Name &
+  appendComp(uint64_t number);
+  
   int
   size() const {return m_comps.size();}
 
@@ -97,6 +113,9 @@
   string
   getCompAsString(int index) const;
 
+  uint64_t
+  getCompAsInt (int index) const;
+
   Name
   getPartialName(int start, int n = -1) const;
 
diff --git a/src/action-log.cc b/src/action-log.cc
index c63424d..ff8e257 100644
--- a/src/action-log.cc
+++ b/src/action-log.cc
@@ -24,9 +24,12 @@
 
 using namespace boost;
 using namespace std;
+using namespace Ccnx;
 
-ActionLog::ActionLog (const std::string &path, const std::string &localName)
+ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const std::string &path, const std::string &localName, const std::string &sharedFolder)
   : SyncLog (path, localName)
+  , m_ccnx (ccnx)
+  , m_sharedFolderName (sharedFolder)
 {
   int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
                                  ActionLog::apply_action_xFun,
@@ -163,8 +166,8 @@
 
   if (parent_device_id > 0 && parent_seq_no > 0)
     {
-      cout << Ccnx::Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()),
-                          parent_device_name.size ()) << endl;
+      cout << Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()),
+                    parent_device_name.size ()) << endl;
       
       item.set_parent_device_name (parent_device_name);
       item.set_parent_seq_no (parent_seq_no);
@@ -222,10 +225,7 @@
 
   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);
@@ -235,8 +235,25 @@
   item.set_parent_device_name (parent_device_name);
   item.set_parent_seq_no (parent_seq_no);
 
-  cout << Ccnx::Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()),
-                      parent_device_name.size ()) << endl;
+  string item_msg;
+  item.SerializeToString (&item_msg);
+  Name actionName (m_localName);
+  actionName
+    .appendComp ("action")
+    .appendComp (m_sharedFolderName)
+    .appendComp (reinterpret_cast<void*> (seq_no), sizeof (seq_no));
+  
+  // Bytes actionData = m_ccnx->createContentObject (?name, item_msg.c_str (), item_msg.size ());
+
+  
+  // sqlite3_bind_blob (stmt, 10, item_msg.c_str (), item_msg.size (), SQLITE_TRANSIENT);
+  
+  sqlite3_step (stmt);
+
+
+  
+  // cout << Ccnx::Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()),
+  //                     parent_device_name.size ()) << endl;
   
   // assign name to the action, serialize action, and create content object
   
diff --git a/src/action-log.h b/src/action-log.h
index f6cc0a7..323f6a3 100644
--- a/src/action-log.h
+++ b/src/action-log.h
@@ -24,6 +24,7 @@
 
 #include "sync-log.h"
 #include <boost/tuple/tuple.hpp>
+#include <ccnx-wrapper.h>
 
 class ActionLog;
 typedef boost::shared_ptr<ActionLog> ActionLogPtr;
@@ -31,7 +32,7 @@
 class ActionLog : public SyncLog
 {
 public:
-  ActionLog (const std::string &path, const std::string &localName);
+  ActionLog (Ccnx::CcnxWrapperPtr ccnx, const std::string &path, const std::string &localName, const std::string &sharedFolder);
 
   void
   AddActionUpdate (const std::string &filename,
@@ -52,7 +53,9 @@
   static void
   apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv);
   
-protected:
+private:
+  Ccnx::CcnxWrapperPtr m_ccnx;
+  Ccnx::Name m_sharedFolderName;
 };
 
 #endif // ACTION_LOG_H
diff --git a/src/ccnx-name.cpp b/src/ccnx-name.cpp
index 3803987..28761dd 100644
--- a/src/ccnx-name.cpp
+++ b/src/ccnx-name.cpp
@@ -165,10 +165,59 @@
 Name::appendComp(const string &compStr)
 {
   Bytes comp(compStr.begin(), compStr.end());
-  appendComp(comp);
+  return appendComp(comp);
+}
+
+Name &
+Name::appendComp(const Name &name)
+{
+  for (vector<Bytes>::const_iterator i = name.m_comps.begin (); i != name.m_comps.end (); i++)
+    {
+      appendComp (*i);
+    }
   return *this;
 }
 
+Name &
+Name::appendComp (const void *buf, size_t size)
+{
+  Bytes comp (reinterpret_cast<const unsigned char*> (buf), reinterpret_cast<const unsigned char*> (buf) + size);
+  return appendComp(comp);
+}
+
+Name &
+Name::appendComp(uint64_t number)
+{
+  Bytes comp;
+  comp.push_back (0);
+  
+  while (number > 0)
+    {
+      comp.push_back (static_cast<unsigned char> (number & 0xFF));
+      number >>= 8;
+    }
+  return appendComp (comp);
+}
+
+uint64_t
+Name::getCompAsInt (int index) const
+{
+  Bytes comp = getComp(index);
+  if (comp.size () < 1 ||
+      comp[0] != 0)
+    {
+      boost::throw_exception(NameException()
+                             << error_info_str("Non integer component: " + getCompAsString(index)));
+    }
+  uint64_t ret = 0;
+  for (int i = comp.size () - 1; i >= 1; i--)
+    {
+      ret <<= 8;
+      ret |= comp [i];
+    }
+  return ret;
+}
+
 Bytes
 Name::getComp(int index) const
 {
@@ -194,7 +243,7 @@
     }
     else
     {
-      ss << "%" << hex << setfill('0') << setw(2) << ch;
+      ss << "%" << hex << setfill('0') << setw(2) << (unsigned int)ch;
     }
   }
 
diff --git a/test/test-ccnx-name.cc b/test/test-ccnx-name.cc
index f617397..a876ba4 100644
--- a/test/test-ccnx-name.cc
+++ b/test/test-ccnx-name.cc
@@ -30,6 +30,19 @@
   BOOST_CHECK_EQUAL(empty, name);
   BOOST_CHECK_EQUAL(name, Name("/hello") + Name("/world"));
 
+
+  name.appendComp (1);
+  name.appendComp (255);
+  name.appendComp (256);
+  name.appendComp (1234567890);
+
+  BOOST_CHECK_EQUAL (name.toString (), "/hello/world/%00%01/%00%ff/%00%00%01/%00%d2%02%96I");
+
+  BOOST_CHECK_EQUAL (name.getCompAsInt (5), 1234567890);
+  BOOST_CHECK_EQUAL (name.getCompAsInt (4), 256);
+  BOOST_CHECK_EQUAL (name.getCompAsInt (3), 255);
+  BOOST_CHECK_EQUAL (name.getCompAsInt (2), 1);
+  
   // Charbuf related stuff will be checked in other place
 }