Adapt sync to use new ndn.cxx api
diff --git a/src/sync-logic.cc b/src/sync-logic.cc
index 98160b5..ef96a86 100644
--- a/src/sync-logic.cc
+++ b/src/sync-logic.cc
@@ -38,6 +38,7 @@
 
 using namespace std;
 using namespace boost;
+using namespace ndn;
 
 INIT_LOGGER ("SyncLogic");
 
@@ -57,31 +58,36 @@
 namespace Sync
 {
 
-SyncLogic::SyncLogic (const std::string &syncPrefix,
-                      LogicUpdateCallback onUpdate,
-                      LogicRemoveCallback onRemove)
-  : m_state (new FullState)
-  , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
-  , m_syncPrefix (syncPrefix)
-  , m_onUpdate (onUpdate)
-  , m_onRemove (onRemove)
-  , m_perBranch (false)
-  , m_ccnxHandle(new CcnxWrapper ())
+  SyncLogic::SyncLogic (const Name& syncPrefix,
+                        Ptr<SyncPolicyManager> syncPolicyManager, 
+                        LogicUpdateCallback onUpdate,
+                        LogicRemoveCallback onRemove)
+    : m_state (new FullState)
+    , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
+    , m_syncPrefix (syncPrefix)
+    , m_onUpdate (onUpdate)
+    , m_onRemove (onRemove)
+    , m_perBranch (false)
+    , m_policyManager(syncPolicyManager)
 #ifndef NS3_MODULE
-  , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
-  , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
-  , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
+    , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
+    , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
+    , m_reexpressionJitter (m_randomGenerator, uniform_int<> (100,500))
 #else
-  , m_rangeUniformRandom (200,1000)
-  , m_reexpressionJitter (10,500)
+    , m_rangeUniformRandom (200,1000)
+    , m_reexpressionJitter (10,500)
 #endif
-  , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
+    , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
 { 
 #ifndef NS3_MODULE
   // In NS3 module these functions are moved to StartApplication method
-  
-  m_ccnxHandle->setInterestFilter (m_syncPrefix,
-                                   bind (&SyncLogic::respondSyncInterest, this, _1));
+
+  Ptr<security::Keychain> keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(),
+                                                                                    m_policyManager,
+                                                                                    NULL));
+  m_handler = Ptr<Wrapper>(new Wrapper(keychain));
+  m_handler->setInterestFilter (m_syncPrefix, 
+                                bind (&SyncLogic::respondSyncInterest, this, _1));
 
   m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
                         bind (&SyncLogic::sendSyncInterest, this),
@@ -89,14 +95,15 @@
 #endif
 }
 
-SyncLogic::SyncLogic (const std::string &syncPrefix,
+SyncLogic::SyncLogic (const Name& syncPrefix,
+                      Ptr<SyncPolicyManager> syncPolicyManager,
                       LogicPerBranchCallback onUpdateBranch)
   : m_state (new FullState)
   , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
   , m_syncPrefix (syncPrefix)
   , m_onUpdateBranch (onUpdateBranch)
   , m_perBranch(true)
-  , m_ccnxHandle(new CcnxWrapper())
+  , m_policyManager(syncPolicyManager)
 #ifndef NS3_MODULE
   , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
   , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (200,1000))
@@ -109,8 +116,13 @@
 { 
 #ifndef NS3_MODULE
   // In NS3 module these functions are moved to StartApplication method
+
+  Ptr<security::Keychain> keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(),
+                                                                                    m_policyManager,
+                                                                                    NULL));
+  m_handler = Ptr<Wrapper>(new Wrapper(keychain));
   
-  m_ccnxHandle->setInterestFilter (m_syncPrefix,
+  m_handler->setInterestFilter (m_syncPrefix,
                                    bind (&SyncLogic::respondSyncInterest, this, _1));
 
   m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
@@ -121,7 +133,7 @@
 
 SyncLogic::~SyncLogic ()
 {
-  m_ccnxHandle->clearInterestFilter (m_syncPrefix);
+  m_handler->clearInterestFilter (Name(m_syncPrefix));
 }
 
 #ifdef NS3_MODULE
@@ -152,7 +164,7 @@
 void
 SyncLogic::stop()
 {
-  m_ccnxHandle->clearInterestFilter (m_syncPrefix);
+  m_handler->clearInterestFilter (Name(m_syncPrefix));
   m_scheduler.cancel (REEXPRESSING_INTEREST);
   m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
 }
@@ -166,9 +178,9 @@
 boost::tuple<DigestConstPtr, std::string>
 SyncLogic::convertNameToDigestAndType (const std::string &name)
 {
-  BOOST_ASSERT (name.find (m_syncPrefix) == 0);
+  BOOST_ASSERT (name.find (m_syncPrefix.toUri()) == 0);
 
-  string hash = name.substr (m_syncPrefix.size (), name.size ()-m_syncPrefix.size ());
+  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";
@@ -190,8 +202,10 @@
 }
 
 void
-SyncLogic::respondSyncInterest (const string &name)
+SyncLogic::respondSyncInterest (ndn::Ptr<ndn::Interest> interest)
 {
+  _LOG_DEBUG("respondSyncInterest: " << interest->getName().toUri());
+  string name = interest->getName().toUri();
   try
     {
       _LOG_TRACE ("<< I " << name);
@@ -218,8 +232,12 @@
 }
 
 void
-SyncLogic::respondSyncData (const std::string &name, const char *wireData, size_t len)
+SyncLogic::respondSyncData (Ptr<Data> data)
 {
+  string name = data->getName().toUri();
+  const char *wireData = data->content().buf();
+  size_t len = data->content().size();
+
   try
     {
       _LOG_TRACE ("<< D " << name);
@@ -247,10 +265,32 @@
     }
 }
 
+void
+SyncLogic::onSyncDataTimeout(Ptr<Closure> closure, Ptr<ndn::Interest> interest, int retry)
+{
+  if(retry > 0)
+    {
+      Ptr<Closure> newClosure = Ptr<Closure>(new Closure(closure->m_dataCallback,
+                                                         boost::bind(&SyncLogic::onSyncDataTimeout, 
+                                                                     this, 
+                                                                     _1, 
+                                                                     _2, 
+                                                                     retry - 1),
+                                                         closure->m_unverifiedCallback,
+                                                         closure->m_stepCount)
+                                             );
+      m_handler->sendInterest(interest, newClosure);
+    }
+}
+
+void
+SyncLogic::onSyncDataUnverified()
+{}
 
 void
 SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
 {
+  _LOG_DEBUG("processSyncInterest");
   DigestConstPtr rootDigest;
   {
     recursive_mutex::scoped_lock lock (m_stateMutex);
@@ -435,7 +475,7 @@
 void
 SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
 {
-  
+  _LOG_DEBUG("processSyncRecoveryInterest");
   DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
 
   if (stateInDiffLog == m_log.end ())
@@ -564,6 +604,7 @@
 void
 SyncLogic::sendSyncInterest ()
 {
+  _LOG_DEBUG("sendSyncInterest");
   ostringstream os;
 
   {
@@ -574,13 +615,25 @@
     _LOG_TRACE (">> I " << os.str ());
   }
 
+  _LOG_DEBUG("sendSyncInterest: " << os.str());
+
   m_scheduler.cancel (REEXPRESSING_INTEREST);
   m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (m_syncInterestReexpress),
                         bind (&SyncLogic::sendSyncInterest, this),
                         REEXPRESSING_INTEREST);
-  
-  m_ccnxHandle->sendInterest (os.str (),
-                              bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
+
+  Ptr<ndn::Interest> interestPtr = Ptr<ndn::Interest>(new ndn::Interest(os.str()));
+  Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&SyncLogic::respondSyncData, 
+                                                               this, 
+                                                               _1),
+						   boost::bind(&SyncLogic::onSyncDataTimeout,
+                                                               this,
+                                                               _1, 
+                                                               _2,
+                                                               0),
+						   boost::bind(&SyncLogic::onSyncDataUnverified,
+                                                               this)));
+  m_handler->sendInterest(interestPtr, closure);
 }
 
 void
@@ -601,8 +654,18 @@
                             REEXPRESSING_RECOVERY_INTEREST);
     }
 
-  m_ccnxHandle->sendInterest (os.str (),
-                              bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
+  Ptr<ndn::Interest> interestPtr = Ptr<ndn::Interest>(new ndn::Interest(os.str()));
+  Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&SyncLogic::respondSyncData, 
+                                                               this, 
+                                                               _1),
+						   boost::bind(&SyncLogic::onSyncDataTimeout,
+                                                               this,
+                                                               _1, 
+                                                               _2,
+                                                               0),
+						   boost::bind(&SyncLogic::onSyncDataUnverified,
+                                                               this)));
+  m_handler->sendInterest (interestPtr, closure);
 }
 
 
@@ -623,10 +686,13 @@
   int size = ssm.ByteSize();
   char *wireData = new char[size];
   ssm.SerializeToArray(wireData, size);
-  m_ccnxHandle->publishRawData (name,
-                             wireData,
-                             size,
-                             m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
+  Blob blob(wireData, size);
+  Name dataName(name);
+  Name signingIdentity = m_policyManager->inferSigningIdentity(dataName);
+  m_handler->publishDataByIdentity (dataName,
+                                    blob,
+                                    signingIdentity,
+                                    m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
   delete []wireData;
 
   // checking if our own interest got satisfied