routing: Add routable prefix support

Change-Id: I31c860adf7aa985b96063dbf5444f308e5ea0b21
diff --git a/src/sync-socket.cc b/src/sync-socket.cc
index 1188441..ca015de 100644
--- a/src/sync-socket.cc
+++ b/src/sync-socket.cc
@@ -30,9 +30,15 @@
 
 using ndn::shared_ptr;
 
+static const uint8_t ROUTING_PREFIX_SEPARATOR[2] = {0xF0, 0x2E};
+
+const Name SyncSocket::EMPTY_NAME = Name();
+
 SyncSocket::SyncSocket (const Name& syncPrefix,
-                        const ndn::Name& dataPrefix,
+                        const Name& dataPrefix,
                         uint64_t dataSession,
+                        bool withRoutingPrefix,
+                        const Name& routingPrefix, 
                         shared_ptr<Face> face,
                         const IdentityCertificate& myCertificate,
                         shared_ptr<SecRuleRelative> dataRule,
@@ -40,11 +46,19 @@
                         RemoveCallback rmCallback )
   : m_dataPrefix(dataPrefix)
   , m_dataSession(dataSession)
+  , m_withRoutingPrefix(false)
   , m_newDataCallback(dataCallback)
   , m_myCertificate(myCertificate)
   , m_face(face)
   , m_ioService(face->ioService())
 {
+  if(withRoutingPrefix && !routingPrefix.isPrefixOf(m_dataPrefix))
+    {
+      m_withRoutingPrefix = true;
+      m_routableDataPrefix.append(routingPrefix).append(ROUTING_PREFIX_SEPARATOR, 2).append(m_dataPrefix);
+    }
+
+
   if(static_cast<bool>(dataRule))
     {
       m_withSecurity = true;
@@ -81,30 +95,50 @@
   data->setFreshnessPeriod(1000*freshness);
 
   m_ioService->post(bind(&SyncSocket::publishDataInternal, this, 
-                         data, m_dataPrefix, m_dataSession, isCert));
+                         data, isCert));
 }
 
 void
-SyncSocket::publishDataInternal(shared_ptr<Data> data, const Name &prefix, uint64_t session, bool isCert)
+SyncSocket::publishDataInternal(shared_ptr<Data> data, bool isCert)
 {
-  uint64_t sequence = getNextSeq(prefix, session);
-  Name dataName = prefix;
-  dataName.append(boost::lexical_cast<string>(session)).append(boost::lexical_cast<string>(sequence));
+  Name dataPrefix = (m_withRoutingPrefix ? m_routableDataPrefix : m_dataPrefix);
+
+  uint64_t sequence = getNextSeq();
+
+  Name dataName;
+  dataName.append(m_dataPrefix)
+    .append(boost::lexical_cast<string>(m_dataSession))
+    .append(boost::lexical_cast<string>(sequence));
   if(isCert)
     dataName.append("INTRO-CERT");
   data->setName(dataName);
-
   m_keyChain.sign(*data, m_myCertificate.getName());
-  m_face->put(*data);
 
-  SeqNo s(session, sequence + 1);
+  if(m_withRoutingPrefix)
+    {
+      Name wrappedName;
+      wrappedName.append(m_routableDataPrefix)
+        .append(boost::lexical_cast<string>(m_dataSession))
+        .append(boost::lexical_cast<string>(sequence));
 
-  m_sequenceLog[prefix] = s;
-  m_syncLogic->addLocalNames (prefix, session, sequence);
+      Data wrappedData(wrappedName);
+      wrappedData.setContent(data->wireEncode());
+      m_keyChain.sign(wrappedData, m_myCertificate.getName());
+
+      m_face->put(wrappedData);
+    }
+  else
+    {
+      m_face->put(*data);
+    }
+
+  SeqNo s(m_dataSession, sequence + 1);
+  m_sequenceLog[dataPrefix] = s;
+  m_syncLogic->addLocalNames (dataPrefix, m_dataSession, sequence); // If DNS works, we should use pure m_dataprefix rather than the one with routing prefix.
 }
 
 void 
-SyncSocket::fetchData(const Name &prefix, const SeqNo &seq, const OnDataValidated& dataCallback, int retry)
+SyncSocket::fetchData(const Name& prefix, const SeqNo& seq, const OnDataValidated& dataCallback, int retry)
 {
   Name interestName = prefix;
   interestName.append(boost::lexical_cast<string>(seq.getSession())).append(boost::lexical_cast<string>(seq.getSeq()));
@@ -112,28 +146,50 @@
   ndn::Interest interest(interestName);
   interest.setMustBeFresh(true);
 
-  const OnDataValidated& onValidated = bind(&SyncSocket::onDataValidated, this, _1, interestName.size(), dataCallback);
+  m_face->expressInterest(interest, 
+                          bind(&SyncSocket::onData, this, _1, _2, dataCallback), 
+                          bind(&SyncSocket::onDataTimeout, this, _1, retry, dataCallback));
+
+}
+
+void
+SyncSocket::onData(const ndn::Interest& interest, Data& data, const OnDataValidated& dataCallback)
+{
+  bool encaped = false;
+
+  Name interestName = interest.getName();
+  Name::const_iterator it = interestName.begin();
+  Name::const_iterator end = interestName.end();
+
+  size_t offset = interestName.size();
+  for(; it != end; it++)
+    {
+      offset--;
+      if(it->toEscapedString() == "%F0.")
+        {
+          encaped = true;
+          break;
+        }
+    }
+
+  if(!encaped)
+    offset = interestName.size();
+
+  const OnDataValidated& onValidated = bind(&SyncSocket::onDataValidated, this, _1, offset, dataCallback);
   const OnDataValidationFailed& onValidationFailed = bind(&SyncSocket::onDataValidationFailed, this, _1, _2);
 
-  m_face->expressInterest(interest, 
-                          bind(&SyncSocket::onData, this, _1, _2, onValidated, onValidationFailed), 
-                          bind(&SyncSocket::onDataTimeout, this, _1, retry, onValidated, onValidationFailed));
-
+  if(encaped)
+    {
+      shared_ptr<Data> innerData = make_shared<Data>();
+      innerData->wireDecode(data.getContent().blockFromValue());
+      m_syncValidator->validate(*innerData, onValidated, onValidationFailed);
+    }
+  else
+    m_syncValidator->validate(data, onValidated, onValidationFailed);
 }
 
 void
-SyncSocket::onData(const ndn::Interest& interest, Data& data,
-                   const OnDataValidated& onValidated,
-                   const OnDataValidationFailed& onValidationFailed)
-{
-  m_syncValidator->validate(data, onValidated, onValidationFailed);
-}
-
-void
-SyncSocket::onDataTimeout(const ndn::Interest& interest, 
-                          int retry,
-                          const OnDataValidated& onValidated,
-                          const OnDataValidationFailed& onValidationFailed)
+SyncSocket::onDataTimeout(const ndn::Interest& interest, int retry, const OnDataValidated& dataCallback)
 {
   if(retry > 0)
     {
@@ -142,14 +198,12 @@
                                    this,
                                    _1,
                                    _2,
-                                   onValidated,
-                                   onValidationFailed),
+                                   dataCallback),
                               bind(&SyncSocket::onDataTimeout, 
                                    this,
                                    _1,
                                    retry - 1,
-                                   onValidated,
-                                   onValidationFailed));
+                                   dataCallback));
                               
     }
   else
@@ -161,7 +215,6 @@
                             size_t interestNameSize,
                             const OnDataValidated& onValidated)
 {
-  _LOG_DEBUG("--------------------" << data->getName());
   if(data->getName().size() > interestNameSize 
      && data->getName().get(interestNameSize).toEscapedString() == "INTRO-CERT")
     {