sync-logic+sync-socket: Chaing std::string name to ndn::Name
diff --git a/src/sync-logic.cc b/src/sync-logic.cc
index 240e9df..67fb342 100644
--- a/src/sync-logic.cc
+++ b/src/sync-logic.cc
@@ -18,6 +18,7 @@
  * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
  *         Chaoyi Bian <bcy@pku.edu.cn>
  *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *         Yingdi Yu <yingdi@cs.ucla.edu>
  */
 
 #ifdef NS3_MODULE
@@ -179,21 +180,21 @@
  * Recovery name:  .../recovery/<hash>
  */
 boost::tuple<DigestConstPtr, std::string>
-SyncLogic::convertNameToDigestAndType (const std::string &name)
+SyncLogic::convertNameToDigestAndType (const Name &name)
 {
-  BOOST_ASSERT (name.find (m_syncPrefix.toUri()) == 0);
+  BOOST_ASSERT (m_syncPrefix.isPrefixOf(name));
 
-  string hash = name.substr (m_syncPrefix.toUri().size (), name.size ()-m_syncPrefix.toUri().size ());
-  if (hash[0] == '/')
-    hash = hash.substr (1, hash.size ()-1);
-  string interestType = "normal";
-
-  size_t pos = hash.find ('/');
-  if (pos != string::npos)
-    {
-      interestType = hash.substr (0, pos);
-      hash         = hash.substr (pos + 1);
-    }
+  int nameLengthDiff = name.size() - m_syncPrefix.size();
+  BOOST_ASSERT (nameLengthDiff > 0);
+  BOOST_ASSERT (nameLengthDiff < 3);
+  
+  string hash = name.get(-1).toEscapedString();
+  string interestType;
+  
+  if(nameLengthDiff == 1)
+    interestType = "normal";
+  else
+    interestType = name.get(-2).toEscapedString();
 
   _LOG_TRACE (hash << ", " << interestType);
 
@@ -210,8 +211,10 @@
                            Transport& transport, 
                            uint64_t registeredPrefixId)
 {
-  _LOG_DEBUG("respondSyncInterest: " << interest->getName().toUri());
-  string name = interest->getName().toUri();
+  Name name = interest->getName();
+
+  _LOG_DEBUG("respondSyncInterest: " << name);
+
   try
     {
       _LOG_TRACE ("<< I " << name);
@@ -287,7 +290,7 @@
 void
 SyncLogic::onSyncDataVerified(const shared_ptr<Data>& data)
 {
-  string name = data->getName().toUri();
+  Name name = data->getName();
   const char* wireData = (const char*)data->getContent().value();
   size_t len = data->getContent().value_size();
 
@@ -319,7 +322,7 @@
 }
 
 void
-SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
+SyncLogic::processSyncInterest (const Name &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
 {
   _LOG_DEBUG("processSyncInterest");
   DigestConstPtr rootDigest;
@@ -344,7 +347,7 @@
   if (*rootDigest == *digest)
     {
       _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
-      m_syncInterestTable.insert (digest, name, false);
+      m_syncInterestTable.insert (digest, name.toUri(), false);
       return;
     }
   
@@ -360,7 +363,7 @@
 
   if (!timedProcessing)
     {
-      bool exists = m_syncInterestTable.insert (digest, name, true);
+      bool exists = m_syncInterestTable.insert (digest, name.toUri(), true);
       if (exists) // somebody else replied, so restart random-game timer
         {
           _LOG_DEBUG ("Unknown digest, but somebody may have already replied, so restart our timer");
@@ -384,7 +387,7 @@
 }
 
 void
-SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const char *wireData, size_t len)
+SyncLogic::processSyncData (const Name &name, DigestConstPtr digest, const char *wireData, size_t len)
 {
   DiffStatePtr diffLog = boost::make_shared<DiffState> ();
   bool ownInterestSatisfied = false;
@@ -392,7 +395,7 @@
   try
     {
 
-      m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
+      m_syncInterestTable.remove (name.toUri()); // Remove satisfied interest from PIT
 
       ownInterestSatisfied = (name == m_outstandingInterestName);
 
@@ -504,7 +507,7 @@
 }
 
 void
-SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
+SyncLogic::processSyncRecoveryInterest (const Name &name, DigestConstPtr digest)
 {
   _LOG_DEBUG("processSyncRecoveryInterest");
   DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
@@ -577,13 +580,13 @@
 }
 
 void
-SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
+SyncLogic::addLocalNames (const Name &prefix, uint32_t session, uint32_t seq)
 {
   DiffStatePtr diff;
   {
     //cout << "Add local names" <<endl;
     boost::recursive_mutex::scoped_lock lock (m_stateMutex);
-    NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
+    NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
 
     _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
 
@@ -602,12 +605,12 @@
 }
 
 void
-SyncLogic::remove(const string &prefix) 
+SyncLogic::remove(const Name &prefix) 
 {
   DiffStatePtr diff;
   {
     boost::recursive_mutex::scoped_lock lock (m_stateMutex);
-    NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
+    NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix.toUri());
     m_state->remove(info);	
 
     // increment the sequence number for the forwarder node
@@ -636,39 +639,41 @@
 SyncLogic::sendSyncInterest ()
 {
   _LOG_DEBUG("sendSyncInterest");
-  ostringstream os;
 
   {
     boost::recursive_mutex::scoped_lock lock (m_stateMutex);
-
-    os << m_syncPrefix << "/" << *m_state->getDigest();
-    m_outstandingInterestName = os.str ();
-    _LOG_TRACE (">> I " << os.str ());
+    m_outstandingInterestName = m_syncPrefix;
+    ostringstream os;
+    os << *m_state->getDigest();
+    m_outstandingInterestName.append(os.str());
+    _LOG_TRACE (">> I " << m_outstandingInterestName);
   }
 
-  _LOG_DEBUG("sendSyncInterest: " << os.str());
+  _LOG_DEBUG("sendSyncInterest: " << m_outstandingInterestName);
 
   m_scheduler.cancel (REEXPRESSING_INTEREST);
   m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
                         func_lib::bind (&SyncLogic::sendSyncInterest, this),
                         REEXPRESSING_INTEREST);
 
-  shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(os.str());
+  ndn::Interest interest(m_outstandingInterestName);
 
   OnVerified onVerified = func_lib::bind(&SyncLogic::onSyncDataVerified, this, _1);
   OnVerifyFailed onVerifyFailed = func_lib::bind(&SyncLogic::onSyncDataVerifyFailed, this, _1);
 
-  m_face->expressInterest(*interest,
+  m_face->expressInterest(interest,
                           func_lib::bind(&SyncLogic::onSyncData, this, _1, _2, onVerified, onVerifyFailed),
-                          func_lib::bind(&SyncLogic::onSyncDataTimeout, this, _1, 1, onVerified, onVerifyFailed));
+                          func_lib::bind(&SyncLogic::onSyncDataTimeout, this, _1, 0, onVerified, onVerifyFailed));
 }
 
 void
 SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
 {
   ostringstream os;
-  os << m_syncPrefix << "/recovery/" << *digest;
-  _LOG_TRACE (">> I " << os.str ());
+  os << *digest;
+  
+  Name interestName = m_syncPrefix;
+  interestName.append("recovery").append(os.str());
 
   TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval);
   m_recoveryRetransmissionInterval <<= 1;
@@ -681,19 +686,19 @@
                             REEXPRESSING_RECOVERY_INTEREST);
     }
 
-  shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(os.str());
+  ndn::Interest interest(interestName);
 
   OnVerified onVerified = func_lib::bind(&SyncLogic::onSyncDataVerified, this, _1);
   OnVerifyFailed onVerifyFailed = func_lib::bind(&SyncLogic::onSyncDataVerifyFailed, this, _1);
 
-  m_face->expressInterest(*interest,
+  m_face->expressInterest(interest,
                           func_lib::bind(&SyncLogic::onSyncData, this, _1, _2, onVerified, onVerifyFailed),
-                          func_lib::bind(&SyncLogic::onSyncDataTimeout, this, _1, 1, onVerified, onVerifyFailed));
+                          func_lib::bind(&SyncLogic::onSyncDataTimeout, this, _1, 0, onVerified, onVerifyFailed));
 }
 
 
 void
-SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
+SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, StateConstPtr state)
 {
   SyncStateMsg msg;
   msg << (*state);
@@ -703,21 +708,19 @@
 // pass in state msg instead of state, so that there is no need to lock the state until
 // this function returns
 void
-SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, SyncStateMsg &ssm)
+SyncLogic::sendSyncData (const Name &name, DigestConstPtr digest, SyncStateMsg &ssm)
 {
   _LOG_TRACE (">> D " << name);
   int size = ssm.ByteSize();
   char *wireData = new char[size];
-  Name dataName(name);
-  Name signingIdentity = m_policy->inferSigningIdentity(dataName);
+  Name signingIdentity = m_policy->inferSigningIdentity(name);
 
-  shared_ptr<Data> syncData = make_shared<Data>(dataName);
-  syncData->setContent(reinterpret_cast<const uint8_t*>(wireData), size);
+  Data syncData(name);
+  syncData.setContent(reinterpret_cast<const uint8_t*>(wireData), size);
   
-  Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(signingIdentity);
-  m_keyChain->sign(*syncData, certificateName);
+  m_keyChain->signByIdentity(syncData, signingIdentity);
   
-  m_face->put(*syncData);
+  m_face->put(syncData);
   
   delete []wireData;
 
diff --git a/src/sync-logic.h b/src/sync-logic.h
index aef6158..d3e70c1 100644
--- a/src/sync-logic.h
+++ b/src/sync-logic.h
@@ -18,6 +18,7 @@
  * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
  *         Chaoyi Bian <bcy@pku.edu.cn>
  *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *         Yingdi Yu <yingdi@cs.ucla.edu>
  */
 
 #ifndef SYNC_LOGIC_H
@@ -102,7 +103,7 @@
   /**
    * a wrapper for the same func in SyncApp
    */
-  void addLocalNames (const std::string &prefix, uint32_t session, uint32_t seq);
+  void addLocalNames (const ndn::Name &prefix, uint32_t session, uint32_t seq);
 
   /**
    * @brief respond to the Sync Interest; a lot of logic needs to go in here
@@ -121,7 +122,7 @@
    * @brief remove a participant's subtree from the sync tree
    * @param prefix the name prefix for the participant
    */
-  void remove (const std::string &prefix);
+  void remove (const ndn::Name &prefix);
 
   std::string
   getRootDigest();
@@ -187,15 +188,15 @@
   onSyncDataVerified(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
 
   void
-  processSyncInterest (const std::string &name,
+  processSyncInterest (const ndn::Name &name,
                        DigestConstPtr digest, bool timedProcessing=false);
 
   void
-  processSyncData (const std::string &name,
+  processSyncData (const ndn::Name &name,
                    DigestConstPtr digest, const char *wireData, size_t len);
   
   void
-  processSyncRecoveryInterest (const std::string &name,
+  processSyncRecoveryInterest (const ndn::Name &name,
                                DigestConstPtr digest);
   
   void 
@@ -205,7 +206,7 @@
   satisfyPendingSyncInterests (DiffStateConstPtr diff);
 
   boost::tuple<DigestConstPtr, std::string>
-  convertNameToDigestAndType (const std::string &name);
+  convertNameToDigestAndType (const ndn::Name &name);
 
   void
   sendSyncInterest ();
@@ -214,11 +215,11 @@
   sendSyncRecoveryInterests (DigestConstPtr digest);
 
   void
-  sendSyncData (const std::string &name,
+  sendSyncData (const ndn::Name &name,
                 DigestConstPtr digest, StateConstPtr state);
 
   void
-  sendSyncData (const std::string &name,
+  sendSyncData (const ndn::Name &name,
                 DigestConstPtr digest, SyncStateMsg &msg);
 
   size_t
@@ -229,7 +230,7 @@
   DiffStateContainer m_log;
   mutable boost::recursive_mutex m_stateMutex;
 
-  std::string m_outstandingInterestName;
+  ndn::Name m_outstandingInterestName;
   SyncInterestTable m_syncInterestTable;
 
   ndn::Name m_syncPrefix;
diff --git a/src/sync-socket.cc b/src/sync-socket.cc
index cb00ba4..02fb85d 100644
--- a/src/sync-socket.cc
+++ b/src/sync-socket.cc
@@ -53,22 +53,25 @@
 }
 
 bool 
-SyncSocket::publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness)
+SyncSocket::publishData(const Name &prefix, uint32_t session, const char *buf, size_t len, int freshness)
 {
   uint32_t sequence = getNextSeq(prefix, session);
-  ostringstream contentNameWithSeqno;
-  contentNameWithSeqno << prefix << "/" << session << "/" << sequence;
+  ostringstream sessionStream;
+  ostringstream seqStream;
+  sessionStream <<  session;
+  seqStream << sequence;
   
-  Name dataName(contentNameWithSeqno.str ());
+  Name dataName = prefix;
+  dataName.append(sessionStream.str()).append(seqStream.str());
+  
   Name signingIdentity = m_policy->inferSigningIdentity(dataName);
 
-  shared_ptr<Data> data = make_shared<Data>(dataName);
-  data->setContent(reinterpret_cast<const uint8_t*>(buf), len);
+  Data data(dataName);
+  data.setContent(reinterpret_cast<const uint8_t*>(buf), len);
 
-  Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(signingIdentity);
-  m_keyChain->sign(*data, certificateName);
+  m_keyChain->signByIdentity(data, signingIdentity);
   
-  m_face->put(*data);
+  m_face->put(data);
   
   SeqNo s(session, sequence + 1);
   m_sequenceLog[prefix] = s;
@@ -77,47 +80,51 @@
 }
 
 void 
-SyncSocket::fetchData(const string &prefix, const SeqNo &seq, const OnVerified& onVerified, int retry)
+SyncSocket::fetchData(const Name &prefix, const SeqNo &seq, const OnVerified& onVerified, int retry)
 {
-  ostringstream interestName;
-  interestName << prefix << "/" << seq.getSession() << "/" << seq.getSeq();
-  //std::cout << "Socket " << this << " Send Interest <" << interestName.str() << "> for raw data " << endl;
+  ostringstream sessionStream;
+  ostringstream seqStream;
+  sessionStream << seq.getSession();
+  seqStream << seq.getSeq();
 
-  const OnVerifyFailed& onVerifyFailed = bind(&SyncSocket::onChatDataVerifyFailed, this, _1);
+  Name interestName = prefix;
+  interestName.append(sessionStream.str()).append(seqStream.str());
+
+  const OnVerifyFailed& onVerifyFailed = bind(&SyncSocket::onDataVerifyFailed, this, _1);
   
   
-  shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(interestName.str());
-  m_face->expressInterest(*interest, 
-                          bind(&SyncSocket::onChatData, this, _1, _2, onVerified, onVerifyFailed), 
-                          bind(&SyncSocket::onChatDataTimeout, this, _1, retry, onVerified, onVerifyFailed));
+  ndn::Interest interest(interestName);
+  m_face->expressInterest(interest, 
+                          bind(&SyncSocket::onData, this, _1, _2, onVerified, onVerifyFailed), 
+                          bind(&SyncSocket::onDataTimeout, this, _1, retry, onVerified, onVerifyFailed));
 
 }
 
 void
-SyncSocket::onChatData(const shared_ptr<const ndn::Interest>& interest, 
-                       const shared_ptr<Data>& data,
-                       const OnVerified& onVerified,
-                       const OnVerifyFailed& onVerifyFailed)
+SyncSocket::onData(const shared_ptr<const ndn::Interest>& interest, 
+                   const shared_ptr<Data>& data,
+                   const OnVerified& onVerified,
+                   const OnVerifyFailed& onVerifyFailed)
 {
   m_verifier->verifyData(data, onVerified, onVerifyFailed);
 }
 
 void
-SyncSocket::onChatDataTimeout(const shared_ptr<const ndn::Interest>& interest, 
-                              int retry,
-                              const OnVerified& onVerified,
-                              const OnVerifyFailed& onVerifyFailed)
+SyncSocket::onDataTimeout(const shared_ptr<const ndn::Interest>& interest, 
+                          int retry,
+                          const OnVerified& onVerified,
+                          const OnVerifyFailed& onVerifyFailed)
 {
   if(retry > 0)
     {
       m_face->expressInterest(*interest,
-                              bind(&SyncSocket::onChatData,
+                              bind(&SyncSocket::onData,
                                    this,
                                    _1,
                                    _2,
                                    onVerified,
                                    onVerifyFailed),
-                              bind(&SyncSocket::onChatDataTimeout, 
+                              bind(&SyncSocket::onDataTimeout, 
                                    this,
                                    _1,
                                    retry - 1,
@@ -126,18 +133,18 @@
                               
     }
   else
-    _LOG_DEBUG("Chat interest eventually time out!");
+    _LOG_DEBUG("interest eventually time out!");
 }
 
 void
-SyncSocket::onChatDataVerifyFailed(const shared_ptr<Data>& data)
+SyncSocket::onDataVerifyFailed(const shared_ptr<Data>& data)
 {
-  _LOG_DEBUG("Chat data cannot be verified!");
+  _LOG_DEBUG("data cannot be verified!");
 }
 
 
 uint32_t
-SyncSocket::getNextSeq (const string &prefix, uint32_t session)
+SyncSocket::getNextSeq (const Name &prefix, uint32_t session)
 {
   SequenceLog::iterator i = m_sequenceLog.find (prefix);
 
diff --git a/src/sync-socket.h b/src/sync-socket.h
index e34857d..b558ff9 100644
--- a/src/sync-socket.h
+++ b/src/sync-socket.h
@@ -23,7 +23,6 @@
 
 #include "sync-logic.h"
 #include <boost/function.hpp>
-#include <boost/unordered_map.hpp>
 #include "sync-seq-no.h"
 #include <ndn-cpp-dev/face.hpp>
 #include <ndn-cpp-dev/security/verifier.hpp>
@@ -64,21 +63,21 @@
   ~SyncSocket ();
 
   bool 
-  publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness);
+  publishData(const ndn::Name &prefix, uint32_t session, const char *buf, size_t len, int freshness);
 
   void 
-  remove (const std::string &prefix) 
+  remove (const ndn::Name &prefix) 
   { m_syncLogic.remove(prefix); }
 
   void 
-  fetchData(const std::string &prefix, const SeqNo &seq, const ndn::OnVerified& onVerified, int retry = 0);
+  fetchData(const ndn::Name &prefix, const SeqNo &seq, const ndn::OnVerified& onVerified, int retry = 0);
 
   std::string 
   getRootDigest() 
   { return m_syncLogic.getRootDigest(); }
 
   uint32_t
-  getNextSeq (const std::string &prefix, uint32_t session);
+  getNextSeq (const ndn::Name &prefix, uint32_t session);
 
   SyncLogic &
   getLogic () 
@@ -95,22 +94,22 @@
   { m_newDataCallback(v, this); }
 
   void
-  onChatData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, 
+  onData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, 
              const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
              const ndn::OnVerified& onVerified,
              const ndn::OnVerifyFailed& onVerifyFailed);
 
   void
-  onChatDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, 
+  onDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, 
                     int retry,
                     const ndn::OnVerified& onVerified,
                     const ndn::OnVerifyFailed& onVerifyFailed);
 
   void
-  onChatDataVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
+  onDataVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
 
 private:
-  typedef boost::unordered_map<std::string, SeqNo> SequenceLog;
+  typedef std::map<ndn::Name, SeqNo> SequenceLog;
   NewDataCallback m_newDataCallback;
   SequenceLog m_sequenceLog;
   ndn::ptr_lib::shared_ptr<SecPolicySync> m_policy;