Merge with branch 'simulation'
diff --git a/.gitignore b/.gitignore
index f10621d..7ce2679 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,5 @@
 *.pyc
 doc/html
 .DS_Store
+animation.xml
+.R*
diff --git a/model/sync-app-data-fetch.cc b/ccnx/sync-app-data-fetch.cc
similarity index 100%
rename from model/sync-app-data-fetch.cc
rename to ccnx/sync-app-data-fetch.cc
diff --git a/model/sync-app-data-fetch.h b/ccnx/sync-app-data-fetch.h
similarity index 100%
rename from model/sync-app-data-fetch.h
rename to ccnx/sync-app-data-fetch.h
diff --git a/model/sync-app-data-publish.cc b/ccnx/sync-app-data-publish.cc
similarity index 100%
rename from model/sync-app-data-publish.cc
rename to ccnx/sync-app-data-publish.cc
diff --git a/model/sync-app-data-publish.h b/ccnx/sync-app-data-publish.h
similarity index 100%
rename from model/sync-app-data-publish.h
rename to ccnx/sync-app-data-publish.h
diff --git a/model/sync-app-socket-c.cc b/ccnx/sync-app-socket-c.cc
similarity index 100%
rename from model/sync-app-socket-c.cc
rename to ccnx/sync-app-socket-c.cc
diff --git a/model/sync-app-socket-c.h b/ccnx/sync-app-socket-c.h
similarity index 100%
rename from model/sync-app-socket-c.h
rename to ccnx/sync-app-socket-c.h
diff --git a/model/sync-app-socket.cc b/ccnx/sync-app-socket.cc
similarity index 100%
rename from model/sync-app-socket.cc
rename to ccnx/sync-app-socket.cc
diff --git a/model/sync-app-socket.h b/ccnx/sync-app-socket.h
similarity index 100%
rename from model/sync-app-socket.h
rename to ccnx/sync-app-socket.h
diff --git a/model/sync-ccnx-wrapper.cc b/ccnx/sync-ccnx-wrapper.cc
similarity index 100%
rename from model/sync-ccnx-wrapper.cc
rename to ccnx/sync-ccnx-wrapper.cc
diff --git a/model/sync-ccnx-wrapper.h b/ccnx/sync-ccnx-wrapper.h
similarity index 100%
rename from model/sync-ccnx-wrapper.h
rename to ccnx/sync-ccnx-wrapper.h
diff --git a/model/sync-log.cc b/ccnx/sync-log.cc
similarity index 100%
rename from model/sync-log.cc
rename to ccnx/sync-log.cc
diff --git a/model/sync-log.h b/ccnx/sync-log.h
similarity index 100%
rename from model/sync-log.h
rename to ccnx/sync-log.h
diff --git a/model/sync-logic-event-container.h b/ccnx/sync-logic-event-container.h
similarity index 97%
rename from model/sync-logic-event-container.h
rename to ccnx/sync-logic-event-container.h
index 65c8ab2..ebab72a 100644
--- a/model/sync-logic-event-container.h
+++ b/ccnx/sync-logic-event-container.h
@@ -23,6 +23,8 @@
 #ifndef SYNC_LOGIC_EVENT_CONTAINER_H
 #define SYNC_LOGIC_EVENT_CONTAINER_H
 
+#include "sync-event.h"
+
 #include <boost/function.hpp>
 #include <boost/date_time/posix_time/posix_time_types.hpp>
 
@@ -40,8 +42,6 @@
 namespace Sync
 {
 
-typedef boost::function< void ( ) > Event;
-
 struct LogicEvent
 {
   LogicEvent (const boost::system_time &_time, Event _event, uint32_t _label)
diff --git a/model/sync-scheduler.cc b/ccnx/sync-scheduler.cc
similarity index 90%
rename from model/sync-scheduler.cc
rename to ccnx/sync-scheduler.cc
index 4f9e082..866f742 100644
--- a/model/sync-scheduler.cc
+++ b/ccnx/sync-scheduler.cc
@@ -107,26 +107,18 @@
   // cout << "Exited...\n";  
 }
 
-
 void
-Scheduler::schedule (const boost::system_time &abstime, Event event, uint32_t label)
+Scheduler::schedule (const TimeDuration &reltime, Event event, uint32_t label)
 {
   {
     lock_guard<mutex> lock (m_eventsMutex);
-    m_events.insert (LogicEvent (abstime, event, label));
+    m_events.insert (LogicEvent (boost::get_system_time () + reltime, event, label));
   }
   m_eventsCondition.notify_one ();
   m_thread.interrupt (); // interrupt sleep, if currently sleeping
 }
 
 void
-Scheduler::schedule (const boost::posix_time::time_duration &reltime, Event event, uint32_t label)
-{
-  // cout << reltime << endl;
-  schedule (boost::get_system_time () + reltime, event, label);
-}
-
-void
 Scheduler::cancel (uint32_t label)
 {
   {
diff --git a/model/sync-scheduler.h b/ccnx/sync-scheduler.h
similarity index 85%
rename from model/sync-scheduler.h
rename to ccnx/sync-scheduler.h
index 10ef210..155c6ba 100644
--- a/model/sync-scheduler.h
+++ b/ccnx/sync-scheduler.h
@@ -28,6 +28,12 @@
 
 #include "sync-logic-event-container.h"
 
+#define TIME_SECONDS(number) boost::posix_time::seconds (number)
+#define TIME_MILLISECONDS(number) boost::posix_time::milliseconds(number)
+#define TIME_NOW boost::get_system_time ()
+typedef boost::posix_time::time_duration TimeDuration;
+typedef boost::system_time TimeAbsolute;
+
 namespace Sync {
 
 /**
@@ -49,22 +55,13 @@
   ~Scheduler ();
 
   /**
-   * @brief Schedule an event at absolute time 'abstime'
-   * @param abstime Absolute time
-   * @param event function to be called at the time
-   * @param label Label for the event
-   */
-  void
-  schedule (const boost::system_time &abstime, Event event, uint32_t label);
-
-  /**
    * @brief Schedule an event at relative time 'reltime'
    * @param reltime Relative time
    * @param event function to be called at the time
    * @param label Label for the event
    */
   void
-  schedule (const boost::posix_time::time_duration &reltime, Event event, uint32_t label); 
+  schedule (const TimeDuration &reltime, Event event, uint32_t label); 
 
   /**
    * @brief Cancel all events for the label
diff --git a/examples/sync-example.cc b/examples/sync-example.cc
index 57bf8b4..38edf4f 100644
--- a/examples/sync-example.cc
+++ b/examples/sync-example.cc
@@ -1,23 +1,139 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 
-#include "ns3/core-module.h"
-#include "ns3/sync-helper.h"
+#include <ns3/core-module.h>
+#include <ns3/network-module.h>
+#include <ns3/NDNabstraction-module.h>
+#include <ns3/point-to-point-module.h>
+
+#include "sync-logic.h"
+#include "sync-logic-helper.h"
 
 using namespace ns3;
+using namespace Sync;
 
+NS_LOG_COMPONENT_DEFINE ("SyncExample");
+
+void OnUpdate (const std::string &prefix, const SeqNo &newSeq, const SeqNo &/*oldSeq*/)
+{
+  NS_LOG_LOGIC (Simulator::Now ().ToDouble (Time::S) <<"s\tNode: " << Simulator::GetContext () << ", prefix: " << prefix << ", seqNo: " << newSeq);
+}
+
+void OnRemove (const std::string &prefix)
+{
+  NS_LOG_LOGIC (Simulator::Now ().ToDouble (Time::S) <<"s\tNode: " << Simulator::GetContext () << ", prefix: "<< prefix);
+}
 
 int 
 main (int argc, char *argv[])
 {
-  bool verbose = true;
+  Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps"));
+  Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms"));
+  Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20"));
+  
+  Config::SetDefault ("ns3::CcnxFloodingStrategy::SmartFlooding", StringValue ("false"));
+
+  LogComponentEnable ("SyncExample", LOG_ALL);
+  
+  Time finishTime = Seconds (3.0); 
 
   CommandLine cmd;
-  cmd.AddValue ("verbose", "Tell application to log if true", verbose);
+  cmd.AddValue ("finish", "Finish time", finishTime);
+  cmd.Parse (argc, argv);
 
-  cmd.Parse (argc,argv);
+  // Creating nodes
+  NodeContainer nodes;
+  nodes.Create (11);
 
-  /* ... */
+  // Connecting nodes using two links
+  PointToPointHelper p2p;
+  p2p.Install (nodes.Get (0), nodes.Get (4));
+  p2p.Install (nodes.Get (1), nodes.Get (2));
+  p2p.Install (nodes.Get (1), nodes.Get (4));
+  p2p.Install (nodes.Get (1), nodes.Get (5));
+  p2p.Install (nodes.Get (2), nodes.Get (6));
+  p2p.Install (nodes.Get (2), nodes.Get (7));
+  p2p.Install (nodes.Get (3), nodes.Get (4));
+  p2p.Install (nodes.Get (3), nodes.Get (8));
+  p2p.Install (nodes.Get (4), nodes.Get (5));
+  p2p.Install (nodes.Get (4), nodes.Get (8));
+  p2p.Install (nodes.Get (5), nodes.Get (6));
+  p2p.Install (nodes.Get (5), nodes.Get (9));
+  p2p.Install (nodes.Get (5), nodes.Get (10));
+  p2p.Install (nodes.Get (6), nodes.Get (10));
+  p2p.Install (nodes.Get (7), nodes.Get (10));
+  p2p.Install (nodes.Get (8), nodes.Get (9));
+  p2p.Install (nodes.Get (9), nodes.Get (10));
 
+  Names::Add ("0", nodes.Get (0));
+  Names::Add ("1", nodes.Get (1));
+  Names::Add ("2", nodes.Get (2));
+  Names::Add ("3", nodes.Get(3));
+  Names::Add ("4", nodes.Get(4));
+  Names::Add ("5", nodes.Get(5));
+  Names::Add ("6", nodes.Get(6));
+  Names::Add ("7", nodes.Get(7));
+  Names::Add ("8", nodes.Get(8));
+  Names::Add ("9", nodes.Get(9));
+  Names::Add ("10", nodes.Get(10));
+  
+  // Install CCNx stack on all nodes
+  NS_LOG_INFO ("Installing CCNx stack");
+  CcnxStackHelper ccnxHelper;
+  ccnxHelper.SetForwardingStrategy ("ns3::CcnxFloodingStrategy");
+  ccnxHelper.SetDefaultRoutes (false);
+  ccnxHelper.InstallAll ();
+
+  ccnxHelper.AddRoute ("0", "/sync", 0, 0);
+  ccnxHelper.AddRoute ("1", "/sync", 0, 0);
+  ccnxHelper.AddRoute ("1", "/sync", 1, 0);
+  ccnxHelper.AddRoute ("1", "/sync", 2, 0);
+  ccnxHelper.AddRoute ("2", "/sync", 0, 0);
+  ccnxHelper.AddRoute ("2", "/sync", 1, 0);
+  ccnxHelper.AddRoute ("2", "/sync", 2, 0);
+  ccnxHelper.AddRoute ("3", "/sync", 0, 0);
+  ccnxHelper.AddRoute ("3", "/sync", 1, 0);
+  ccnxHelper.AddRoute ("4", "/sync", 0, 0);
+  ccnxHelper.AddRoute ("4", "/sync", 1, 0);
+  ccnxHelper.AddRoute ("4", "/sync", 2, 0);
+  ccnxHelper.AddRoute ("4", "/sync", 3, 0);
+  ccnxHelper.AddRoute ("4", "/sync", 4, 0);
+  ccnxHelper.AddRoute ("5", "/sync", 0, 0);
+  ccnxHelper.AddRoute ("5", "/sync", 1, 0);
+  ccnxHelper.AddRoute ("5", "/sync", 2, 0);
+  ccnxHelper.AddRoute ("5", "/sync", 3, 0);
+  ccnxHelper.AddRoute ("5", "/sync", 4, 0);
+  ccnxHelper.AddRoute ("6", "/sync", 0, 0);
+  ccnxHelper.AddRoute ("6", "/sync", 1, 0);
+  ccnxHelper.AddRoute ("6", "/sync", 2, 0);
+  ccnxHelper.AddRoute ("7", "/sync", 0, 0);
+  ccnxHelper.AddRoute ("7", "/sync", 1, 0);
+  ccnxHelper.AddRoute ("8", "/sync", 0, 0);
+  ccnxHelper.AddRoute ("8", "/sync", 1, 0);
+  ccnxHelper.AddRoute ("8", "/sync", 2, 0);
+  ccnxHelper.AddRoute ("9", "/sync", 0, 0);
+  ccnxHelper.AddRoute ("9", "/sync", 1, 0);
+  ccnxHelper.AddRoute ("9", "/sync", 2, 0);
+  ccnxHelper.AddRoute ("10", "/sync", 0, 0);
+  ccnxHelper.AddRoute ("10", "/sync", 1, 0);
+  ccnxHelper.AddRoute ("10", "/sync", 2, 0);
+  ccnxHelper.AddRoute ("10", "/sync", 3, 0);
+
+  SyncLogicHelper logicHelper;
+  logicHelper.SetPrefix ("/sync");
+  logicHelper.SetCallbacks (OnUpdate, OnRemove);
+  ApplicationContainer apps = logicHelper.Install (NodeContainer (nodes.Get (0), nodes.Get (1)));
+  
+  // one data
+  Simulator::ScheduleWithContext (0, Seconds (0.5), &SyncLogic::addLocalNames, DynamicCast<SyncLogic> (apps.Get (0)), "/0", 1, 1);
+
+  // two producers at the same time
+  Simulator::ScheduleWithContext (0, Seconds (1.001), &SyncLogic::addLocalNames, DynamicCast<SyncLogic> (apps.Get (0)), "/0", 1, 2);
+  Simulator::ScheduleWithContext (1, Seconds (1.001), &SyncLogic::addLocalNames, DynamicCast<SyncLogic> (apps.Get (1)), "/1", 1, 2);
+
+  logicHelper.Install (nodes.Get (2)).
+    Start (Seconds (2.001));
+  
+  Simulator::Stop (finishTime);
   Simulator::Run ();
   Simulator::Destroy ();
   return 0;
diff --git a/log4cxx.properties b/log4cxx.properties
index 70f885a..23ff104 100644
--- a/log4cxx.properties
+++ b/log4cxx.properties
@@ -11,10 +11,10 @@
 #log4j.appender.A1.layout.ConversionPattern=%d{hh:mm:ss,SSS} %-14t %-14c  %m%n
 log4j.appender.A1.layout.ConversionPattern=%d{ss,SSS}  %-12c  %m%n
 
-log4j.logger.SyncLogic = TRACE
+log4j.logger.SyncLogic = DEBUG
 #log4j.logger.SyncInterestTable = TRACE
-# log4j.logger.AppDataFetch = TRACE
-# log4j.logger.Test = TRACE
+#log4j.logger.AppDataFetch = TRACE
+log4j.logger.Test = TRACE
 #log4j.logger.bgpparser=TRACE
 #log4j.logger.bgpparser.AttributeType=ERROR
 #log4j.logger.bgpparser.MRTCommonHeader=ERROR
diff --git a/model/sync-diff-state-container.h b/model/sync-diff-state-container.h
index 6e98aa4..2ae7c59 100644
--- a/model/sync-diff-state-container.h
+++ b/model/sync-diff-state-container.h
@@ -33,36 +33,16 @@
 #include <boost/multi_index/hashed_index.hpp>
 #include <boost/multi_index/sequenced_index.hpp>
 // #include <boost/multi_index/random_access_index.hpp>
-// #include <boost/multi_index/member.hpp>
+#include <boost/multi_index/member.hpp>
 #include <boost/multi_index/mem_fun.hpp>
 
 namespace mi = boost::multi_index;
 
 namespace Sync {
 
-struct DigestPtrHash : public std::unary_function<Digest, std::size_t>
-{
-  std::size_t
-  operator() (DigestConstPtr digest) const
-  {
-    // std::cout << "digest->getHash: " << digest->getHash () << " (" << *digest << ")" << std::endl;
-    return digest->getHash ();
-  }
-};
-
-struct DigestPtrEqual : public std::unary_function<Digest, std::size_t>
-{
-  bool
-  operator() (DigestConstPtr digest1, DigestConstPtr digest2) const
-  {
-    // std::cout << boost::cref(*digest1) << " == " << boost::cref(*digest2) << " : " << (*digest1 == *digest2) << std::endl;
-    return *digest1 == *digest2;
-  }
-};
-
-
 /// @cond include_hidden 
 struct sequenced { };
+struct timed { };
 /// @endcond
 
 /**
diff --git a/model/sync-digest.h b/model/sync-digest.h
index 8982019..97d2fd4 100644
--- a/model/sync-digest.h
+++ b/model/sync-digest.h
@@ -173,6 +173,26 @@
 //   return *this;
 // }
 
+struct DigestPtrHash : public std::unary_function<Digest, std::size_t>
+{
+  std::size_t
+  operator() (DigestConstPtr digest) const
+  {
+    // std::cout << "digest->getHash: " << digest->getHash () << " (" << *digest << ")" << std::endl;
+    return digest->getHash ();
+  }
+};
+
+struct DigestPtrEqual : public std::unary_function<Digest, std::size_t>
+{
+  bool
+  operator() (DigestConstPtr digest1, DigestConstPtr digest2) const
+  {
+    // std::cout << boost::cref(*digest1) << " == " << boost::cref(*digest2) << " : " << (*digest1 == *digest2) << std::endl;
+    return *digest1 == *digest2;
+  }
+};
+
 
 } // Sync
 
diff --git a/model/sync-event.h b/model/sync-event.h
new file mode 100644
index 0000000..7808947
--- /dev/null
+++ b/model/sync-event.h
@@ -0,0 +1,35 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Chaoyi Bian <bcy@pku.edu.cn>
+ *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef SYNC_EVENT_H
+#define SYNC_EVENT_H
+
+#include <boost/function.hpp>
+
+namespace Sync
+{
+
+typedef boost::function< void ( ) > Event;
+
+} // Sync
+
+#endif // SYNC_EVENT_H
diff --git a/model/sync-full-state.h b/model/sync-full-state.h
index cff1306..c39ec4f 100644
--- a/model/sync-full-state.h
+++ b/model/sync-full-state.h
@@ -37,6 +37,11 @@
 
 namespace Sync {
 
+class FullState;
+typedef boost::shared_ptr<FullState> FullStatePtr;
+typedef boost::shared_ptr<FullState> FullStateConstPtr;
+
+
 /**
  * \ingroup sync
  * @brief Cumulative SYNC state
diff --git a/model/sync-interest-container.h b/model/sync-interest-container.h
new file mode 100644
index 0000000..b998b18
--- /dev/null
+++ b/model/sync-interest-container.h
@@ -0,0 +1,97 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Chaoyi Bian <bcy@pku.edu.cn>
+ *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef SYNC_INTEREST_CONTAINER_H
+#define SYNC_INTEREST_CONTAINER_H
+
+#include "sync-digest.h"
+
+#include <boost/multi_index_container.hpp>
+#include <boost/multi_index/tag.hpp>
+// #include <boost/multi_index/ordered_index.hpp>
+// #include <boost/multi_index/composite_key.hpp>
+#include <boost/multi_index/hashed_index.hpp>
+#include <boost/multi_index/sequenced_index.hpp>
+#include <boost/multi_index/ordered_index.hpp>
+// #include <boost/multi_index/random_access_index.hpp>
+#include <boost/multi_index/member.hpp>
+#include <boost/multi_index/mem_fun.hpp>
+
+namespace mi = boost::multi_index;
+
+namespace Sync {
+
+struct Interest
+{
+  Interest (DigestConstPtr digest, const std::string &name, bool unknown=false)
+  : m_digest (digest)
+  , m_name (name)
+  , m_time (TIME_NOW)
+  , m_unknown (unknown)
+  {
+  }
+  
+  DigestConstPtr m_digest;
+  std::string    m_name;
+  TimeAbsolute   m_time;
+  bool           m_unknown;
+};
+
+/// @cond include_hidden 
+struct named { };
+struct hashed;
+struct timed;
+/// @endcond
+
+/**
+ * \ingroup sync
+ * @brief Container for interests (application PIT)
+ */
+struct InterestContainer : public mi::multi_index_container<
+  Interest,
+  mi::indexed_by<
+    mi::hashed_unique<
+      mi::tag<named>,
+      BOOST_MULTI_INDEX_MEMBER(Interest, std::string, m_name)
+    >
+    ,
+    
+    mi::hashed_non_unique<
+      mi::tag<hashed>,
+      BOOST_MULTI_INDEX_MEMBER(Interest, DigestConstPtr, m_digest),
+      DigestPtrHash,
+      DigestPtrEqual
+      >
+    ,
+    
+    mi::ordered_non_unique<
+      mi::tag<timed>,
+      BOOST_MULTI_INDEX_MEMBER(Interest, TimeAbsolute, m_time)
+      >
+    >
+  >
+{
+};
+
+} // Sync
+
+#endif // SYNC_INTEREST_CONTAINER_H
diff --git a/model/sync-interest-table.cc b/model/sync-interest-table.cc
index 7b9ed35..a05dba3 100644
--- a/model/sync-interest-table.cc
+++ b/model/sync-interest-table.cc
@@ -30,9 +30,10 @@
 namespace Sync
 {
 
-SyncInterestTable::SyncInterestTable ()
+SyncInterestTable::SyncInterestTable (TimeDuration lifetime)
+  : m_entryLifetime (lifetime)
 {
-  m_scheduler.schedule (posix_time::seconds (4),
+  m_scheduler.schedule (TIME_SECONDS (m_checkPeriod),
                         bind (&SyncInterestTable::expireInterests, this),
                         0);
 }
@@ -41,33 +42,36 @@
 {
 }
 
-vector<string>
-SyncInterestTable::fetchAll ()
+Interest
+SyncInterestTable::pop ()
 {
   expireInterests ();
   recursive_mutex::scoped_lock lock (m_mutex);
-  
-  vector<string> entries;
-  for (unordered_map<string, time_t>::iterator it = m_table.begin();
-       it != m_table.end();
-       ++it)
-    {
-      entries.push_back(it->first);
-    }
-  m_table.clear ();
 
-  return entries;
+  if (m_table.size () == 0)
+    BOOST_THROW_EXCEPTION (Error::InterestTableIsEmpty ());
+
+  Interest ret = *m_table.begin ();
+  m_table.erase (m_table.begin ());
+
+  return ret;
 }
 
 bool
-SyncInterestTable::insert(const string &interest)
+SyncInterestTable::insert (DigestConstPtr digest, const string &name, bool unknownState/*=false*/)
 {
+  bool existent = false;
+  
   recursive_mutex::scoped_lock lock (m_mutex);
-  TableContainer::iterator it = m_table.find (interest);
+  InterestContainer::index<named>::type::iterator it = m_table.get<named> ().find (name);
   if (it != m_table.end())
-    m_table.erase(it);
-  time_t currentTime = time(0);
-  m_table.insert (make_pair(interest, currentTime));
+    {
+      existent = true;
+      m_table.erase (it);
+    }
+  m_table.insert (Interest (digest, name, unknownState));
+
+  return existent;
 }
 
 uint32_t
@@ -78,42 +82,56 @@
 }
 
 bool
-SyncInterestTable::remove (const std::string &interest)
+SyncInterestTable::remove (const string &name)
 {
   recursive_mutex::scoped_lock lock (m_mutex);
-  TableContainer::iterator item = m_table.find (interest);
-  if (item != m_table.end ())
+
+  InterestContainer::index<named>::type::iterator item = m_table.get<named> ().find (name);
+  if (item != m_table.get<named> ().end ())
     {
-      m_table.erase (item);
+      m_table.get<named> ().erase (name);
+      return true;
+    }
+
+  return false;
+}
+
+bool
+SyncInterestTable::remove (DigestConstPtr digest)
+{
+  recursive_mutex::scoped_lock lock (m_mutex);
+  InterestContainer::index<hashed>::type::iterator item = m_table.get<hashed> ().find (digest);
+  if (item != m_table.get<hashed> ().end ())
+    {
+      m_table.get<hashed> ().erase (digest); // erase all records associated with the digest
       return true;
     }
   return false;
 }
 
-
 void SyncInterestTable::expireInterests ()
 { 
   recursive_mutex::scoped_lock lock (m_mutex);
 
   uint32_t count = 0;
-  time_t currentTime = time(0);
-  TableContainer::iterator it = m_table.begin (); 
-  while (it != m_table.end())
+  TimeAbsolute expireTime = TIME_NOW - m_entryLifetime;
+  
+  while (m_table.size () > 0)
     {
-    time_t timestamp = it->second;
-    _LOG_DEBUG ("expireInterests (): " << timestamp << ", " << currentTime);
-    if (currentTime - timestamp > m_checkPeriod)
-      {
-        it = m_table.erase (it);
-        count ++;
-      }
-    else
-      ++it;
+      InterestContainer::index<timed>::type::iterator item = m_table.get<timed> ().begin ();
+      
+      if (item->m_time < expireTime)
+        {
+          m_table.get<timed> ().erase (item);
+          count ++;
+        }
+      else
+        break;
   }
 
   _LOG_DEBUG ("expireInterests (): expired " << count);
   
-  m_scheduler.schedule (posix_time::seconds (4),
+  m_scheduler.schedule (TIME_SECONDS (m_checkPeriod),
                         bind (&SyncInterestTable::expireInterests, this),
                         0);
 }
diff --git a/model/sync-interest-table.h b/model/sync-interest-table.h
index 16119eb..824d1f9 100644
--- a/model/sync-interest-table.h
+++ b/model/sync-interest-table.h
@@ -22,17 +22,19 @@
 
 #ifndef SYNC_INTEREST_TABLE_H
 #define SYNC_INTEREST_TABLE_H
+
 #include <string>
 #include <vector>
-#include <boost/unordered_map.hpp>
-#include <boost/unordered_set.hpp>
 #include <boost/thread/recursive_mutex.hpp>
 #include <boost/thread/thread.hpp>
 #include <ctime>
 #include "sync-scheduler.h"
+#include "sync-digest.h"
+#include "sync-interest-container.h"
 
 namespace Sync {
 
+
 /**
  * \ingroup sync
  * @brief A table to keep unanswered Sync Interest
@@ -42,7 +44,7 @@
 class SyncInterestTable
 {
 public:
-  SyncInterestTable ();
+  SyncInterestTable (TimeDuration lifetime);
   ~SyncInterestTable ();
 
   /**
@@ -50,19 +52,25 @@
    * timestamp
    */
   bool
-  insert (const std::string &interest);
+  insert (DigestConstPtr interest, const std::string &name, bool unknownState=false);
 
   /**
-   * @brief Remove interest (e.g., when it was satisfied)
+   * @brief Remove interest by digest (e.g., when it was satisfied)
    */
   bool
-  remove (const std::string &interest);
+  remove (DigestConstPtr interest);
 
   /**
-   * @brief fetch all Interests and clear the table
+   * @brief Remove interest by name (e.g., when it was satisfied)
    */
-  std::vector<std::string>
-  fetchAll ();
+  bool
+  remove (const std::string &name);
+  
+  /**
+   * @brief pop a non-expired Interest from PIT
+   */
+  Interest
+  pop ();
 
   uint32_t
   size () const;
@@ -75,15 +83,20 @@
   expireInterests ();
 
 private:
-  typedef boost::unordered_map<std::string, time_t> TableContainer;
-  
-  static const int m_checkPeriod = 4;
-  TableContainer m_table; // pit entries
+  static const int m_checkPeriod = 4; // seconds
+
+  TimeDuration m_entryLifetime;
+  InterestContainer m_table;
 
   Scheduler m_scheduler;
   mutable boost::recursive_mutex m_mutex;
 };
 
+namespace Error {
+struct InterestTableIsEmpty : virtual boost::exception, virtual std::exception { };
+}
+
+
 } // Sync
 
 #endif // SYNC_INTEREST_TABLE_H
diff --git a/model/sync-logic.cc b/model/sync-logic.cc
index 6d56764..3bae752 100644
--- a/model/sync-logic.cc
+++ b/model/sync-logic.cc
@@ -20,6 +20,11 @@
  *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
  */
 
+#ifdef NS3_MODULE
+#include <ns3/ccnx-pit.h>
+#include <ns3/ccnx.h>
+#endif
+
 #include "sync-logic.h"
 #include "sync-diff-leaf.h"
 #include "sync-full-leaf.h"
@@ -28,7 +33,6 @@
 #include <boost/make_shared.hpp>
 #include <boost/foreach.hpp>
 #include <boost/lexical_cast.hpp>
-#include <boost/date_time/posix_time/posix_time.hpp>
 #include <vector>
 
 using namespace std;
@@ -36,103 +40,190 @@
 
 INIT_LOGGER ("SyncLogic");
 
+#ifdef NS3_MODULE
+#define GET_RANDOM(var) var.GetValue ()
+#else
+#define GET_RANDOM(var) var ()
+#endif
+
+#define TIME_SECONDS_WITH_JITTER(sec) \
+  (TIME_SECONDS (sec) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
+
+#define TIME_MILLISECONDS_WITH_JITTER(ms) \
+  (TIME_MILLISECONDS (ms) + TIME_MILLISECONDS (GET_RANDOM (m_reexpressionJitter)))
+
+
 namespace Sync
 {
 
 SyncLogic::SyncLogic (const std::string &syncPrefix,
                       LogicUpdateCallback onUpdate,
                       LogicRemoveCallback onRemove)
-  : m_syncPrefix (syncPrefix)
+  : m_state (new FullState)
+  , m_syncInterestTable (TIME_SECONDS (m_syncInterestReexpress))
+  , m_syncPrefix (syncPrefix)
   , m_onUpdate (onUpdate)
   , m_onRemove (onRemove)
   , m_ccnxHandle(new CcnxWrapper())
+  , m_recoveryRetransmissionInterval (m_defaultRecoveryRetransmitInterval)
+#ifndef NS3_MODULE
   , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
-  , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (20,80))
-{
-#ifdef _DEBUG
-#ifdef HAVE_LOG4CXX
-  // _LOG_FUNCTION (syncPrefix);
-  static int id = 0;
-  staticModuleLogger = log4cxx::Logger::getLogger ("SyncLogic." + lexical_cast<string> (id));
-  id ++;
+  , 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)
 #endif
-#endif
+{ 
+#ifndef NS3_MODULE
+  // In NS3 module these functions are moved to StartApplication method
   
   m_ccnxHandle->setInterestFilter (m_syncPrefix,
                                    bind (&SyncLogic::respondSyncInterest, this, _1));
 
-  m_scheduler.schedule (posix_time::seconds (0),
+  m_scheduler.schedule (TIME_SECONDS (0), // no need to add jitter
                         bind (&SyncLogic::sendSyncInterest, this),
                         REEXPRESSING_INTEREST);
+#endif
 }
 
 SyncLogic::~SyncLogic ()
 {
-  // _LOG_FUNCTION (this);
-  // cout << "SyncLogic::~SyncLogic ()" << endl;
-
   m_ccnxHandle.reset ();
 }
 
+#ifdef NS3_MODULE
 void
-SyncLogic::respondSyncInterest (const string &interest)
+SyncLogic::StartApplication ()
 {
-  _LOG_TRACE ("<< I " << interest);
-  //cout << "Respond Sync Interest" << endl;
-  string hash = interest.substr(interest.find_last_of("/") + 1);
-  // cout << "Received Sync Interest: " << hash << endl;
-  
-  DigestPtr digest = make_shared<Digest> ();
-  try
-    {
-      istringstream is (hash);
-      is >> *digest;
-    }
-  catch (Error::DigestCalculationError &e)
-    {
-      // log error. ignoring it for now, later we should log it
-      return;
-    }
+  m_ccnxHandle->SetNode (GetNode ());
+  m_ccnxHandle->StartApplication ();
 
-  processSyncInterest (digest, interest, false);
+  m_ccnxHandle->setInterestFilter (m_syncPrefix,
+                                   bind (&SyncLogic::respondSyncInterest, this, _1));
+
+  m_scheduler.schedule (TIME_SECONDS (0), // need to send first interests at exactly the same time
+                        bind (&SyncLogic::sendSyncInterest, this),
+                        REEXPRESSING_INTEREST);
 }
 
 void
-SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
+SyncLogic::StopApplication ()
+{
+  m_ccnxHandle->clearInterestFilter (m_syncPrefix);
+  m_ccnxHandle->StopApplication ();
+  m_scheduler.cancel (REEXPRESSING_INTEREST);
+  m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
+}
+#endif
+
+/**
+ * Two types of intersts
+ *
+ * Normal name:    .../<hash>  
+ * Recovery name:  .../recovery/<hash>
+ */
+boost::tuple<DigestConstPtr, std::string>
+SyncLogic::convertNameToDigestAndType (const std::string &name)
+{
+  BOOST_ASSERT (name.find (m_syncPrefix) == 0);
+
+  string hash = name.substr (m_syncPrefix.size (), name.size ()-m_syncPrefix.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);
+    }
+
+  _LOG_TRACE (hash << ", " << interestType);
+
+  DigestPtr digest = make_shared<Digest> ();
+  istringstream is (hash);
+  is >> *digest;
+
+  return make_tuple (digest, interestType);
+}
+
+void
+SyncLogic::respondSyncInterest (const string &name)
+{
+  try
+    {
+      _LOG_TRACE ("<< I " << name);
+
+      DigestConstPtr digest;
+      string type;
+      tie (digest, type) = convertNameToDigestAndType (name);
+
+      if (type == "normal") // kind of ineffective...
+        {
+          processSyncInterest (name, digest);
+        }
+      else if (type == "recovery") 
+        {
+          processSyncRecoveryInterest (name, digest);
+        }
+    }
+  catch (Error::DigestCalculationError &e)
+    {
+      _LOG_TRACE ("Something fishy happened...");
+      // log error. ignoring it for now, later we should log it
+      return ;
+    }
+}
+
+void
+SyncLogic::respondSyncData (const std::string &name, const std::string &dataBuffer)
+{
+  try
+    {
+      _LOG_TRACE ("<< D " << name);
+  
+      DigestConstPtr digest;
+      string type;
+      tie (digest, type) = convertNameToDigestAndType (name);
+
+      if (type == "normal")
+        {
+          processSyncData (name, digest, dataBuffer);
+        }
+      else
+        {
+          // timer is always restarted when we schedule recovery
+          m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
+          processSyncData (name, digest, dataBuffer);
+        }
+    }
+  catch (Error::DigestCalculationError &e)
+    {
+      _LOG_TRACE ("Something fishy happened...");
+      // log error. ignoring it for now, later we should log it
+      return;
+    }
+}
+
+
+void
+SyncLogic::processSyncInterest (const std::string &name, DigestConstPtr digest, bool timedProcessing/*=false*/)
 {
   recursive_mutex::scoped_lock lock (m_stateMutex);
 
   // Special case when state is not empty and we have received request with zero-root digest
-  if (digest->isZero () && !m_state.getDigest()->isZero ())
+  if (digest->isZero () && !m_state->getDigest()->isZero ())
     {
-      _LOG_TRACE (">> D " << interestName << "/state" << " (zero)");
-
-      m_syncInterestTable.remove (interestName + "/state");
-      m_ccnxHandle->publishData (interestName + "/state",
-                                 lexical_cast<string> (m_state),
-                                 m_syncResponseFreshness);
-      if (m_outstandingInterest == interestName)
-        {
-          m_scheduler.schedule (posix_time::seconds (0),
-                                bind (&SyncLogic::sendSyncInterest, this),
-                                REEXPRESSING_INTEREST);
-        }
+      sendSyncData (name, digest, m_state);
       return;
     }
 
-  if (*m_state.getDigest() == *digest)
+  if (*m_state->getDigest() == *digest)
     {
-      // cout << interestName << "\n";
-      if (digest->isZero ())
-        {
-          _LOG_TRACE ("processSyncInterest (): Digest is zero, adding /state to PIT");
-          m_syncInterestTable.insert (interestName + "/state");
-        }
-      else
-        {
-          _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
-          m_syncInterestTable.insert (interestName);
-        }
+      _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
+      m_syncInterestTable.insert (digest, name, false);
       return;
     }
   
@@ -141,82 +232,67 @@
   if (stateInDiffLog != m_log.end ())
   {
     DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
-    // string state = lexical_cast<string> (*stateDiff);
-    // erase_all (state, "\n");
-    // _LOG_TRACE (">> D " << interestName << ", state: " << state);
-    // _LOG_DEBUG ("Log size: " << m_log.size ());
 
-    // BOOST_FOREACH (DiffStateConstPtr ds, m_log.get<sequenced> ())
-    //   {
-    //     string state = lexical_cast<string> (*ds);
-    //     erase_all (state, "\n");
-    //     _LOG_DEBUG ("   " << state << ", " << *ds->getDigest ());
-    //   }
-
-    m_syncInterestTable.remove (interestName);
-    m_ccnxHandle->publishData (interestName,
-                               lexical_cast<string> (*stateDiff),
-                               m_syncResponseFreshness);
-    if (m_outstandingInterest == interestName)
-      {
-        m_scheduler.schedule (posix_time::seconds (0),
-                              bind (&SyncLogic::sendSyncInterest, this),
-                              REEXPRESSING_INTEREST);
-      }
+    sendSyncData (name, digest, stateDiff);
     return;
   }
 
   if (!timedProcessing)
     {
-      m_scheduler.schedule (posix_time::milliseconds (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
-                            bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
+      bool exists = m_syncInterestTable.insert (digest, name, 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");
+          m_scheduler.cancel (DELAYED_INTEREST_PROCESSING);
+        }
+
+      uint32_t waitDelay = GET_RANDOM (m_rangeUniformRandom);      
+      _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms");
+
+      m_scheduler.schedule (TIME_MILLISECONDS (waitDelay),
+                            bind (&SyncLogic::processSyncInterest, this, name, digest, true),
                             DELAYED_INTEREST_PROCESSING);
-      
     }
   else
     {
-      _LOG_TRACE (">> D " << interestName << "/state" << " (timed processing)");
-
-      m_syncInterestTable.remove (interestName + "/state");
-      m_ccnxHandle->publishData (interestName + "/state",
-                                 lexical_cast<string> (m_state),
-                                 m_syncResponseFreshness);
-
-      if (m_outstandingInterest == interestName)
-        {
-          m_scheduler.schedule (posix_time::seconds (0),
-                                bind (&SyncLogic::sendSyncInterest, this),
-                                REEXPRESSING_INTEREST);
-        }
+      _LOG_TRACE ("                                                      (timed processing)");
+      
+      m_recoveryRetransmissionInterval = m_defaultRecoveryRetransmitInterval;
+      sendSyncRecoveryInterests (digest);
     }
 }
 
 void
-SyncLogic::processSyncData (const string &name, const string &dataBuffer)
+SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const string &dataBuffer)
 {
-  _LOG_TRACE ("<< D " << name);
   DiffStatePtr diffLog = make_shared<DiffState> ();
+  bool ownInterestSatisfied = false;
   
   try
     {
       recursive_mutex::scoped_lock lock (m_stateMutex);
-      
-      string last = name.substr(name.find_last_of("/") + 1);
-      istringstream ss (dataBuffer);
 
-      if (last == "state")
+      m_syncInterestTable.remove (name); // Remove satisfied interest from PIT
+
+      ownInterestSatisfied = (name == m_outstandingInterestName);
+
+      DiffState diff;
+      istringstream ss (dataBuffer);
+      ss >> diff;
+      BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
         {
-          FullState full;
-          ss >> full;
-          BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
+          DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
+          BOOST_ASSERT (diffLeaf != 0);
+
+          NameInfoConstPtr info = diffLeaf->getInfo();
+          if (diffLeaf->getOperation() == UPDATE)
             {
-              NameInfoConstPtr info = leaf->getInfo ();
-              SeqNo seq = leaf->getSeq ();
+              SeqNo seq = diffLeaf->getSeq();
 
               bool inserted = false;
               bool updated = false;
               SeqNo oldSeq;
-              tie (inserted, updated, oldSeq) = m_state.update (info, seq);
+              tie (inserted, updated, oldSeq) = m_state->update (info, seq);
 
               if (inserted || updated)
                 {
@@ -224,44 +300,17 @@
                   m_onUpdate (info->toString (), seq, oldSeq);
                 }
             }
-        }
-      else
-        {
-          DiffState diff;
-          ss >> diff;
-          BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
+          else if (diffLeaf->getOperation() == REMOVE)
             {
-              DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
-              BOOST_ASSERT (diffLeaf != 0);
-
-              NameInfoConstPtr info = diffLeaf->getInfo();
-              if (diffLeaf->getOperation() == UPDATE)
+              if (m_state->remove (info))
                 {
-                  SeqNo seq = diffLeaf->getSeq();
-
-                  bool inserted = false;
-                  bool updated = false;
-                  SeqNo oldSeq;
-                  tie (inserted, updated, oldSeq) = m_state.update (info, seq);
-
-                  if (inserted || updated)
-                    {
-                      diffLog->update (info, seq);
-                      m_onUpdate (info->toString (), seq, oldSeq);
-                    }
+                  diffLog->remove (info);
+                  m_onRemove (info->toString ());
                 }
-              else if (diffLeaf->getOperation() == REMOVE)
-                {
-                  if (m_state.remove (info))
-                    {
-                      diffLog->remove (info);
-                      m_onRemove (info->toString ());
-                    }
-                }
-              else
-                {
-                  BOOST_ASSERT (false); // just in case
-                }
+            }
+          else
+            {
+              BOOST_ASSERT (false); // just in case
             }
         }
 
@@ -269,75 +318,95 @@
     }
   catch (Error::SyncXmlDecodingFailure &e)
     {
+      _LOG_TRACE ("Something really fishy happened during state decoding " <<
+                  diagnostic_information (e));
       diffLog.reset ();
       // don't do anything
     }
 
-  // if state has changed, then it is safe to express a new interest
-  if (diffLog->getLeaves ().size () > 0)
+  if ((diffLog != 0 && diffLog->getLeaves ().size () > 0) ||
+      ownInterestSatisfied)
     {
-      m_scheduler.schedule (posix_time::seconds (0),
-                            bind (&SyncLogic::sendSyncInterest, this),
-                            REEXPRESSING_INTEREST);
-    }
-  else
-    {
-      // should not reexpress the same interest. Need at least wait for data lifetime
-      // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization
+      // Do it only if everything went fine and state changed
+
+      // this is kind of wrong
+      // satisfyPendingSyncInterests (diffLog); // if there are interests in PIT, there is a point to satisfy them using new state
+  
+      // if state has changed, then it is safe to express a new interest
       m_scheduler.cancel (REEXPRESSING_INTEREST);
-      // m_scheduler.schedule (posix_time::seconds (0),
-      m_scheduler.schedule (posix_time::seconds (m_syncResponseFreshness) + posix_time::milliseconds (1),
+      m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
                             bind (&SyncLogic::sendSyncInterest, this),
                             REEXPRESSING_INTEREST);
     }
 }
 
 void
-SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog)
+SyncLogic::processSyncRecoveryInterest (const std::string &name, DigestConstPtr digest)
 {
-  vector<string> pis = m_syncInterestTable.fetchAll ();
-  if (pis.size () > 0)
+  recursive_mutex::scoped_lock lock (m_stateMutex);
+  
+  DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
+
+  if (stateInDiffLog == m_log.end ())
     {
-      stringstream ss;
-      ss << *diffLog;
-      bool satisfiedOwnInterest = false;
-      
-      for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
-        {
-          _LOG_TRACE (">> D " << *ii);
-          m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
+      _LOG_TRACE ("Could not find " << *digest << " in digest log");
+      return;
+    }
 
-          {
-            recursive_mutex::scoped_lock lock (m_stateMutex);
-            // _LOG_DEBUG (*ii << " == " << m_outstandingInterest << " = " << (*ii == m_outstandingInterest));
-            satisfiedOwnInterest = satisfiedOwnInterest || (*ii == m_outstandingInterest) || (*ii == (m_outstandingInterest + "/state"));
-          }
-        }
+  sendSyncData (name, digest, m_state);
+}
 
-      if (satisfiedOwnInterest)
+void
+SyncLogic::satisfyPendingSyncInterests (DiffStateConstPtr diffLog)
+{
+  DiffStatePtr fullStateLog = make_shared<DiffState> ();
+  {
+    recursive_mutex::scoped_lock lock (m_stateMutex);
+    BOOST_FOREACH (LeafConstPtr leaf, m_state->getLeaves ()/*.get<timed> ()*/)
+      {
+        fullStateLog->update (leaf->getInfo (), leaf->getSeq ());
+        /// @todo Impose limit on how many state info should be send out
+      }
+  }
+  
+  try
+    {
+      uint32_t counter = 0;
+      while (m_syncInterestTable.size () > 0)
         {
-          _LOG_DEBUG ("Have satisfied our own interest. Scheduling interest reexpression");
-          // we need to reexpress interest only if we satisfied our own interest
-          m_scheduler.schedule (posix_time::milliseconds (0),
-                                bind (&SyncLogic::sendSyncInterest, this),
-                                REEXPRESSING_INTEREST);
+          Interest interest = m_syncInterestTable.pop ();
+
+          if (!interest.m_unknown)
+            {
+              _LOG_TRACE (">> D " << interest.m_name);
+              sendSyncData (interest.m_name, interest.m_digest, diffLog);
+            }
+          else
+            {
+              _LOG_TRACE (">> D (unknown)" << interest.m_name);
+              sendSyncData (interest.m_name, interest.m_digest, fullStateLog);
+            }
+          counter ++;
         }
+      _LOG_DEBUG ("Satisfied " << counter << " pending interests");
+    }
+  catch (Error::InterestTableIsEmpty &e)
+    {
+      // ok. not really an error
     }
 }
 
 void
 SyncLogic::insertToDiffLog (DiffStatePtr diffLog) 
 {
-  //cout << "Process Pending Interests" <<endl;
-  diffLog->setDigest (m_state.getDigest());  
+  diffLog->setDigest (m_state->getDigest());  
   if (m_log.size () > 0)
     {
       m_log.get<sequenced> ().front ()->setNext (diffLog);
     }
-  m_log.erase (m_state.getDigest()); // remove diff state with the same digest.  next pointers are still valid
+  m_log.erase (m_state->getDigest()); // remove diff state with the same digest.  next pointers are still valid
   /// @todo Optimization
   m_log.get<sequenced> ().push_front (diffLog);
-  // _LOG_DEBUG (*diffLog->getDigest () << " " << m_log.size ());
 }
 
 void
@@ -349,10 +418,12 @@
     recursive_mutex::scoped_lock lock (m_stateMutex);
     NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
 
-    SeqNo seqN (session, seq);
-    m_state.update(info, seqN);
+    _LOG_DEBUG ("addLocalNames (): old state " << *m_state->getDigest ());
 
-    _LOG_DEBUG ("addLocalNames (): new state " << *m_state.getDigest ());
+    SeqNo seqN (session, seq);
+    m_state->update(info, seqN);
+
+    _LOG_DEBUG ("addLocalNames (): new state " << *m_state->getDigest ());
     
     diff = make_shared<DiffState>();
     diff->update(info, seqN);
@@ -370,7 +441,7 @@
   {
     recursive_mutex::scoped_lock lock (m_stateMutex);
     NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
-    m_state.remove(info);	
+    m_state->remove(info);	
 
     diff = make_shared<DiffState>();
     diff->remove(info);
@@ -387,23 +458,71 @@
   ostringstream os;
 
   {
-    // cout << "Sending Sync Interest" << endl;
     recursive_mutex::scoped_lock lock (m_stateMutex);
 
-    os << m_syncPrefix << "/" << *m_state.getDigest();
-
+    os << m_syncPrefix << "/" << *m_state->getDigest();
+    m_outstandingInterestName = os.str ();
     _LOG_TRACE (">> I " << os.str ());
-
-    m_outstandingInterest = os.str ();
   }
-  
-  m_ccnxHandle->sendInterest (os.str (),
-                              bind (&SyncLogic::processSyncData, this, _1, _2));
 
   m_scheduler.cancel (REEXPRESSING_INTEREST);
-  m_scheduler.schedule (posix_time::seconds (4),
+  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));
 }
 
+void
+SyncLogic::sendSyncRecoveryInterests (DigestConstPtr digest)
+{
+  ostringstream os;
+  os << m_syncPrefix << "/recovery/" << *digest;
+  _LOG_TRACE (">> I " << os.str ());
+
+  TimeDuration nextRetransmission = TIME_MILLISECONDS_WITH_JITTER (m_recoveryRetransmissionInterval);
+  m_recoveryRetransmissionInterval <<= 1;
+    
+  m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
+  if (m_recoveryRetransmissionInterval < 100*1000) // <100 seconds
+    {
+      m_scheduler.schedule (nextRetransmission,
+                            bind (&SyncLogic::sendSyncRecoveryInterests, this, digest),
+                            REEXPRESSING_RECOVERY_INTEREST);
+    }
+
+  m_ccnxHandle->sendInterest (os.str (),
+                              bind (&SyncLogic::respondSyncData, this, _1, _2));
+}
+
+
+void
+SyncLogic::sendSyncData (const std::string &name, DigestConstPtr digest, StateConstPtr state)
+{
+  _LOG_TRACE (">> D " << name);
+  // sending
+  m_ccnxHandle->publishData (name,
+                             lexical_cast<string> (*state),
+                             m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
+
+  // checking if our own interest got satisfied
+  bool satisfiedOwnInterest = false;
+  {
+    recursive_mutex::scoped_lock lock (m_stateMutex);
+    satisfiedOwnInterest = (m_outstandingInterestName == name);
+  }
+  
+  if (satisfiedOwnInterest)
+    {
+      _LOG_TRACE ("Satisfied our own Interest. Re-expressing (hopefully with a new digest)");
+      
+      m_scheduler.cancel (REEXPRESSING_INTEREST);
+      m_scheduler.schedule (TIME_SECONDS_WITH_JITTER (0),
+                            bind (&SyncLogic::sendSyncInterest, this),
+                            REEXPRESSING_INTEREST);
+    }
+}
+
+
 }
diff --git a/model/sync-logic.h b/model/sync-logic.h
index ad0a208..e205fc0 100644
--- a/model/sync-logic.h
+++ b/model/sync-logic.h
@@ -43,6 +43,11 @@
 #endif
 #endif
 
+#ifdef NS3_MODULE
+#include <ns3/application.h>
+#include <ns3/random-variable.h>
+#endif
+
 namespace Sync {
 
 /**
@@ -51,6 +56,9 @@
  * interests and data)
  */
 class SyncLogic
+#ifdef NS3_MODULE
+  : public ns3::Application
+#endif
 {
 public:
   typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
@@ -86,7 +94,7 @@
    * @param name the data name
    * @param dataBuffer the sync data
    */
-  void processSyncData (const std::string &name, const std::string &dataBuffer);
+  void respondSyncData (const std::string &name, const std::string &dataBuffer);
 
   /**
    * @brief remove a participant's subtree from the sync tree
@@ -98,29 +106,54 @@
   Scheduler &
   getScheduler () { return m_scheduler; }
 #endif
+
+#ifdef NS3_MODULE
+public:
+  virtual void StartApplication ();
+  virtual void StopApplication ();
+#endif
   
 private:
   void
   delayedChecksLoop ();
 
   void
-  processSyncInterest (DigestConstPtr digest, const std::string &interestname, bool timedProcessing=false);
+  processSyncInterest (const std::string &name,
+                       DigestConstPtr digest, bool timedProcessing=false);
+
+  void
+  processSyncData (const std::string &name,
+                   DigestConstPtr digest, const std::string &dataBuffer);
   
   void
-  sendSyncInterest ();
-
+  processSyncRecoveryInterest (const std::string &name,
+                               DigestConstPtr digest);
+  
   void 
   insertToDiffLog (DiffStatePtr diff);
 
   void
-  satisfyPendingSyncInterests (DiffStatePtr diff);
+  satisfyPendingSyncInterests (DiffStateConstPtr diff);
 
+  boost::tuple<DigestConstPtr, std::string>
+  convertNameToDigestAndType (const std::string &name);
+
+  void
+  sendSyncInterest ();
+
+  void
+  sendSyncRecoveryInterests (DigestConstPtr digest);
+
+  void
+  sendSyncData (const std::string &name,
+                DigestConstPtr digest, StateConstPtr state);
+  
 private:
-  FullState m_state;
+  FullStatePtr m_state;
   DiffStateContainer m_log;
   boost::recursive_mutex m_stateMutex;
 
-  std::string m_outstandingInterest;
+  std::string m_outstandingInterestName;
   SyncInterestTable m_syncInterestTable;
 
   std::string m_syncPrefix;
@@ -130,22 +163,34 @@
 
   Scheduler m_scheduler;
 
+#ifndef NS3_MODULE
   boost::mt19937 m_randomGenerator;
   boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
-  
-  static const int m_syncResponseFreshness = 2;
+  boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter;
+#else
+  ns3::UniformVariable m_rangeUniformRandom;
+  ns3::UniformVariable m_reexpressionJitter;
+#endif
 
+  static const int m_unknownDigestStoreTime = 10; // seconds
+#ifdef NS3_MODULE
+  static const int m_syncResponseFreshness = 100; // milliseconds
+  static const int m_syncInterestReexpress = 10; // seconds
+  // don't forget to adjust value in SyncCcnxWrapper
+#else
+  static const int m_syncResponseFreshness = 2000;
+  static const int m_syncInterestReexpress = 4;
+#endif
+
+  static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds
+  uint32_t m_recoveryRetransmissionInterval; // milliseconds
+  
   enum EventLabels
     {
       DELAYED_INTEREST_PROCESSING = 1,
-      REEXPRESSING_INTEREST = 2
+      REEXPRESSING_INTEREST = 2,
+      REEXPRESSING_RECOVERY_INTEREST = 3
     };
-
-#ifdef _DEBUG
-#ifdef HAVE_LOG4CXX
-  log4cxx::LoggerPtr staticModuleLogger;
-#endif
-#endif  
 };
 
 
diff --git a/model/sync-state.h b/model/sync-state.h
index faaad02..826e84d 100644
--- a/model/sync-state.h
+++ b/model/sync-state.h
@@ -34,6 +34,10 @@
  */
 namespace Sync {
 
+class State;
+typedef boost::shared_ptr<State> StatePtr;
+typedef boost::shared_ptr<State> StateConstPtr;
+
 /**
  * \ingroup sync
  * @brief Container for state leaves and definition of the abstract interface to work with State objects
diff --git a/ns3/sync-ccnx-wrapper.cc b/ns3/sync-ccnx-wrapper.cc
new file mode 100644
index 0000000..d0e769a
--- /dev/null
+++ b/ns3/sync-ccnx-wrapper.cc
@@ -0,0 +1,263 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Chaoyi Bian <bcy@pku.edu.cn>
+ *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "sync-ccnx-wrapper.h"
+#include "sync-log.h"
+#include <boost/throw_exception.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/lambda/lambda.hpp>
+#include <boost/lambda/bind.hpp>
+
+namespace ll = boost::lambda;
+
+#include "../evaluation/type-tag.h"
+
+#include <ns3/ccnx-name-components.h>
+#include <ns3/ccnx-interest-header.h>
+#include <ns3/ccnx-content-object-header.h>
+#include <ns3/ccnx-face.h>
+#include <ns3/packet.h>
+#include <ns3/ccnx-fib.h>
+
+typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
+typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
+
+using namespace std;
+using namespace boost;
+using namespace ns3;
+
+INIT_LOGGER ("CcnxWrapper");
+
+namespace Sync {
+
+CcnxWrapper::CcnxWrapper()
+  : m_rand (0, std::numeric_limits<uint32_t>::max ())
+{
+}
+
+CcnxWrapper::~CcnxWrapper()
+{
+}
+
+void
+CcnxWrapper::StartApplication ()
+{
+  CcnxApp::StartApplication ();
+}
+
+void
+CcnxWrapper::StopApplication ()
+{
+  CcnxApp::StopApplication ();
+}
+
+int
+CcnxWrapper::publishData (const string &dataName, const string &dataBuffer, int freshness)
+{
+  // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
+  _LOG_INFO ("> Data for " << dataName);
+
+  Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> ();
+  istringstream is (dataName);
+  is >> *name;
+
+  static CcnxContentObjectTail trailer;
+  
+  CcnxContentObjectHeader data;
+  data.SetName (name);
+  data.SetFreshness (Seconds (freshness));
+
+  Ptr<Packet> packet = Create<Packet> (reinterpret_cast<const uint8_t*> (dataBuffer.c_str ()), dataBuffer.size ());
+  packet->AddPacketTag (CreateObject<TypeTag> (TypeTag::DATA));
+  packet->AddHeader (data);
+  packet->AddTrailer (trailer);
+
+  m_protocolHandler (packet);
+
+  m_transmittedContentObjects (&data, packet, this, m_face);
+
+  return 0;
+}
+
+int CcnxWrapper::sendInterest (const string &strInterest, const DataCallback &dataCallback)
+{
+  // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
+  _LOG_INFO ("> Interest for " << strInterest);
+
+  Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> ();
+  istringstream is (strInterest);
+  is >> *name;
+  
+  CcnxInterestHeader interestHeader;
+  uint32_t nonce = m_rand.GetValue ();
+  _LOG_DEBUG ("Nonce: " << nonce);
+  interestHeader.SetNonce            (nonce);
+  interestHeader.SetName             (name);
+  interestHeader.SetInterestLifetime (Seconds (9.9)); // really long-lived interests
+
+  Ptr<Packet> packet = Create<Packet> ();
+  packet->AddPacketTag (CreateObject<TypeTag> (TypeTag::INTEREST));
+  packet->AddHeader (interestHeader);
+
+  // NS_LOG_DEBUG (interestHeader);
+  
+  m_protocolHandler (packet);
+
+  m_transmittedInterests (&interestHeader, this, m_face);
+
+  // Record the callback
+  CcnxFilterEntryContainer<DataCallback>::type::iterator entry = m_dataCallbacks.find (*name);
+  if (entry == m_dataCallbacks.end ())
+    {
+      pair<CcnxFilterEntryContainer<DataCallback>::type::iterator, bool> status =
+        m_dataCallbacks.insert (CcnxFilterEntry<DataCallback> (name));
+
+      entry = status.first;
+    }
+  m_dataCallbacks.modify (entry, ll::bind (&CcnxFilterEntry<DataCallback>::AddCallback, ll::_1, dataCallback));
+  
+  return 0;
+}
+
+int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
+{
+  Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> ();
+  istringstream is (prefix);
+  is >> *name;
+
+  CcnxFilterEntryContainer<InterestCallback>::type::iterator entry = m_interestCallbacks.find (*name);
+  if (entry == m_interestCallbacks.end ())
+    {
+      pair<CcnxFilterEntryContainer<InterestCallback>::type::iterator, bool> status =
+        m_interestCallbacks.insert (CcnxFilterEntry<InterestCallback> (name));
+
+      entry = status.first;
+    }
+
+  m_interestCallbacks.modify (entry, ll::bind (&CcnxFilterEntry<InterestCallback>::AddCallback, ll::_1, interestCallback));
+
+  // creating actual face
+  
+  Ptr<CcnxFib> fib = GetNode ()->GetObject<CcnxFib> ();
+  CcnxFibEntryContainer::type::iterator fibEntry = fib->Add (*name, m_face, 0);
+
+  // make face green, so it will be used primarily
+  fib->m_fib.modify (fibEntry,
+                     ll::bind (&CcnxFibEntry::UpdateStatus,
+                               ll::_1, m_face, CcnxFibFaceMetric::NDN_FIB_GREEN));
+
+  return 0;
+}
+
+void
+CcnxWrapper::clearInterestFilter (const std::string &prefix)
+{
+  Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> ();
+  istringstream is (prefix);
+  is >> *name;
+
+  CcnxFilterEntryContainer<InterestCallback>::type::iterator entry = m_interestCallbacks.find (*name);
+  if (entry == m_interestCallbacks.end ())
+    return;
+
+  m_interestCallbacks.modify (entry, ll::bind (&CcnxFilterEntry<InterestCallback>::ClearCallback, ll::_1));  
+}
+
+CcnxFilterEntryContainer<CcnxWrapper::InterestCallback>::type::iterator
+CcnxWrapper::InterestCallbackLookup (const ns3::CcnxNameComponents &name)
+{
+  CcnxFilterEntryContainer<InterestCallback>::type::iterator entry = m_interestCallbacks.end ();
+
+  // do the longest prefix match
+  for (size_t componentsCount = name.GetComponents ().size ()+1;
+       componentsCount > 0;
+       componentsCount--)
+    {
+      CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount-1));
+
+      entry = m_interestCallbacks.find (subPrefix);
+      if (entry != m_interestCallbacks.end())
+        return entry;
+    }
+
+  return entry;
+}
+
+CcnxFilterEntryContainer<CcnxWrapper::DataCallback>::type::iterator
+CcnxWrapper::DataCallbackLookup (const ns3::CcnxNameComponents &name)
+{
+  CcnxFilterEntryContainer<DataCallback>::type::iterator entry = m_dataCallbacks.end ();
+
+  // do the longest prefix match
+  for (size_t componentsCount = name.GetComponents ().size ()+1;
+       componentsCount > 0;
+       componentsCount--)
+    {
+      CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount-1));
+
+      entry = m_dataCallbacks.find (subPrefix);
+      if (entry != m_dataCallbacks.end())
+        return entry;
+    }
+
+  return entry;  
+}
+
+void
+CcnxWrapper::OnInterest (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> packet)
+{
+  CcnxApp::OnInterest (interest, packet);
+
+  CcnxFilterEntryContainer<InterestCallback>::type::iterator entry = InterestCallbackLookup (interest->GetName ());
+  if (entry == m_interestCallbacks.end ())
+    {
+      _LOG_DEBUG ("No Interest callback set");
+      return;
+    }
+  
+  entry->m_callback (lexical_cast<string> (interest->GetName ()));  
+}
+
+void
+CcnxWrapper::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
+                              Ptr<Packet> payload)
+{
+  CcnxApp::OnContentObject (contentObject, payload);
+
+  CcnxFilterEntryContainer<DataCallback>::type::iterator entry = DataCallbackLookup (contentObject->GetName ());
+  if (entry == m_dataCallbacks.end ())
+    {
+      _LOG_DEBUG ("No Data callback set");
+      return;
+    }
+
+  ostringstream content;
+  payload->CopyData (&content, payload->GetSize ());
+  
+  entry->m_callback (lexical_cast<string> (contentObject->GetName ()), content.str ());
+  
+  // i guess it make sense to remove callback when interest is satisfied
+  m_dataCallbacks.erase (entry);
+}
+
+
+}
diff --git a/ns3/sync-ccnx-wrapper.h b/ns3/sync-ccnx-wrapper.h
new file mode 100644
index 0000000..c9ffe27
--- /dev/null
+++ b/ns3/sync-ccnx-wrapper.h
@@ -0,0 +1,195 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Chaoyi Bian <bcy@pku.edu.cn>
+ *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef SYNC_CCNX_WRAPPER_H
+#define SYNC_CCNX_WRAPPER_H
+
+#include <boost/exception/all.hpp>
+#include <boost/function.hpp>
+#include <string>
+
+#include <ns3/ptr.h>
+#include <ns3/node.h>
+#include <ns3/random-variable.h>
+#include <ns3/ccnx-app.h>
+#include <ns3/ccnx-name-components.h>
+#include <ns3/ccnx-name-components-hash-helper.h>
+
+#include <boost/multi_index_container.hpp>
+#include <boost/multi_index/hashed_index.hpp>
+#include <boost/multi_index/mem_fun.hpp>
+
+/**
+ * \defgroup sync SYNC protocol
+ *
+ * Implementation of SYNC protocol
+ */
+namespace Sync {
+
+template<class Callback>
+struct CcnxFilterEntry
+{
+public:
+  CcnxFilterEntry (ns3::Ptr<const ns3::CcnxNameComponents> prefix)
+    : m_prefix (prefix)
+  { }
+  
+  const ns3::CcnxNameComponents &
+  GetPrefix () const
+  { return *m_prefix; }
+
+  void
+  AddCallback (Callback callback)
+  { 
+    m_callback = callback;
+  }
+
+  void
+  ClearCallback ()
+  {
+    m_callback = 0;
+  }
+  
+public:
+  ns3::Ptr<const ns3::CcnxNameComponents> m_prefix; ///< \brief Prefix of the PIT entry
+  Callback m_callback;
+};
+
+
+template<class Callback>
+struct CcnxFilterEntryContainer
+{
+  typedef
+  boost::multi_index::multi_index_container<
+    CcnxFilterEntry<Callback>,
+    boost::multi_index::indexed_by<
+      // indexed by hash
+      boost::multi_index::hashed_unique<
+        boost::multi_index::const_mem_fun< CcnxFilterEntry<Callback>, const ns3::CcnxNameComponents&, &CcnxFilterEntry<Callback>::GetPrefix >,
+        ns3::CcnxPrefixHash
+        >
+      >
+    > type;
+};
+
+
+
+struct CcnxOperationException : virtual boost::exception, virtual std::exception { };
+/**
+ * \ingroup sync
+ * @brief A wrapper for ccnx library; clients of this code do not need to deal
+ * with ccnx library
+ */
+class CcnxWrapper
+  : public ns3::CcnxApp
+{
+public:
+  typedef boost::function<void (std::string, std::string)> DataCallback;
+  typedef boost::function<void (std::string)> InterestCallback;
+  
+  /**
+   * @brief initialize the wrapper; a lot of things needs to be done. 1) init
+   * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
+   *
+   */
+  CcnxWrapper();
+  ~CcnxWrapper();
+
+  // from CcnxApp
+  /**
+   * @brief Should be called after Node pointer is set to create face and start application
+   */
+  virtual void
+  StartApplication ();
+
+  /**
+   * @brief Stop application
+   */
+  virtual void
+  StopApplication ();
+
+  /**
+   * @brief send Interest; need to grab lock m_mutex first
+   *
+   * @param strInterest the Interest name
+   * @param dataCallback the callback function to deal with the returned data
+   * @return the return code of ccn_express_interest
+   */
+  int
+  sendInterest (const std::string &strInterest, const DataCallback &dataCallback);
+
+  /**
+   * @brief set Interest filter (specify what interest you want to receive)
+   *
+   * @param prefix the prefix of Interest
+   * @param interestCallback the callback function to deal with the returned data
+   * @return the return code of ccn_set_interest_filter
+   */
+  int
+  setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback);
+
+  /**
+   * @brief clear Interest filter
+   * @param prefix the prefix of Interest
+   */
+  void
+  clearInterestFilter (const std::string &prefix);
+
+  /**
+   * @brief publish data and put it to local ccn content store; need to grab
+   * lock m_mutex first
+   *
+   * @param name the name for the data object
+   * @param dataBuffer the data to be published
+   * @param freshness the freshness time for the data object
+   * @return code generated by ccnx library calls, >0 if success
+   */
+  int
+  publishData (const std::string &name, const std::string &dataBuffer, int freshness);
+
+  // from CcnxApp
+  virtual void
+  OnInterest (const ns3::Ptr<const ns3::CcnxInterestHeader> &interest, ns3::Ptr<ns3::Packet> packet);
+ 
+  virtual void
+  OnContentObject (const ns3::Ptr<const ns3::CcnxContentObjectHeader> &contentObject,
+                   ns3::Ptr<ns3::Packet> payload);
+
+private:
+  CcnxFilterEntryContainer<InterestCallback>::type::iterator
+  InterestCallbackLookup (const ns3::CcnxNameComponents &name);
+
+  CcnxFilterEntryContainer<DataCallback>::type::iterator
+  DataCallbackLookup (const ns3::CcnxNameComponents &name);
+
+private:
+  ns3::UniformVariable m_rand; // nonce generator
+
+  CcnxFilterEntryContainer<DataCallback>::type m_dataCallbacks;
+  CcnxFilterEntryContainer<InterestCallback>::type m_interestCallbacks;
+};
+
+typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
+
+} // Sync
+
+#endif // SYNC_CCNX_WRAPPER_H
diff --git a/ns3/sync-log.h b/ns3/sync-log.h
new file mode 100644
index 0000000..9da0dc8
--- /dev/null
+++ b/ns3/sync-log.h
@@ -0,0 +1,108 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Chaoyi Bian <bcy@pku.edu.cn>
+ *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef SYNC_LOG_H
+#define SYNC_LOG_H
+
+#ifdef NS3_MODULE
+
+#include <ns3/log.h>
+
+#ifdef _DEBUG
+
+#define INIT_LOGGER(name) NS_LOG_COMPONENT_DEFINE(name);
+
+#define _LOG_INFO(x) NS_LOG_INFO(x)
+
+#define _LOG_DEBUG(x) NS_LOG_DEBUG(x)
+
+#define _LOG_TRACE(x) NS_LOG_LOGIC(x)
+
+#define _LOG_FUNCTION(x) NS_LOG_FUNCTION(x)
+
+#define _LOG_FUNCTION_NOARGS NS_LOG_FUNCTION_NOARGS
+
+#else
+
+#define INIT_LOGGER(name) 
+#define _LOG_INFO(x) 
+#define _LOG_DEBUG(x) 
+#define _LOG_TRACE(x)
+#define _LOG_FUNCTION(x)
+#define _LOG_FUNCTION_NOARGS
+
+#endif
+
+#else
+
+#ifdef HAVE_LOG4CXX
+
+#include <log4cxx/logger.h>
+
+#define INIT_LOGGER(name) \
+  static log4cxx::LoggerPtr staticModuleLogger = log4cxx::Logger::getLogger (name);
+
+#define _LOG_INFO(x) \
+  LOG4CXX_INFO(staticModuleLogger, x);
+
+#define _LOG_DEBUG(x)                           \
+  LOG4CXX_DEBUG(staticModuleLogger, x);
+
+#define _LOG_TRACE(x) \
+  LOG4CXX_TRACE(staticModuleLogger, x);
+
+#define _LOG_FUNCTION(x) \
+  LOG4CXX_TRACE(staticModuleLogger, __FUNCTION__ << "(" << x << ")");
+
+#define _LOG_FUNCTION_NOARGS \
+  LOG4CXX_TRACE(staticModuleLogger, __FUNCTION__ << "()");
+
+void
+INIT_LOGGERS ();
+
+#else
+
+#define INIT_LOGGER(name)
+#define _LOG_FUNCTION(x)
+#define _LOG_FUNCTION_NOARGS
+#define _LOG_TRACE(x)
+#define _LOG_INFO(x)
+#define INIT_LOGGERS(x)
+
+#ifdef _DEBUG
+
+#include <boost/thread/thread.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <iostream>
+
+#define _LOG_DEBUG(x) \
+  std::clog << boost::get_system_time () << " " << boost::this_thread::get_id () << " " << x << endl;
+
+#else
+#define _LOG_DEBUG(x)
+#endif
+
+#endif // HAVE_LOG4CXX
+
+#endif // NS3_MODULE
+
+#endif // SYNC_LOG_H
diff --git a/ns3/sync-logic-helper.cc b/ns3/sync-logic-helper.cc
new file mode 100644
index 0000000..2425ec3
--- /dev/null
+++ b/ns3/sync-logic-helper.cc
@@ -0,0 +1,94 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "sync-logic-helper.h"
+#include "sync-logic.h"
+
+#include "ns3/log.h"
+#include "ns3/string.h"
+#include "ns3/names.h"
+
+NS_LOG_COMPONENT_DEFINE ("SyncLogicHelper");
+
+using namespace ns3;
+
+namespace Sync 
+{
+
+SyncLogicHelper::SyncLogicHelper ()
+{
+  // m_factory.SetTypeId ("Sync::SyncLogic");
+}
+
+void
+SyncLogicHelper::SetPrefix (const std::string &prefix)
+{
+  m_prefix = prefix;
+}
+
+
+void
+SyncLogicHelper::SetCallbacks (LogicUpdateCallback onUpdate, LogicRemoveCallback onRemove)
+{
+  m_onUpdate = onUpdate;
+  m_onRemove = onRemove;
+}
+
+// void 
+// CcnxAppHelper::SetAttribute (std::string name, const AttributeValue &value)
+// {
+//   m_factory.Set (name, value);
+// }
+    
+ApplicationContainer
+SyncLogicHelper::Install (Ptr<Node> node)
+{
+  return ApplicationContainer (InstallPriv (node));
+}
+    
+ApplicationContainer
+SyncLogicHelper::Install (std::string nodeName)
+{
+  Ptr<Node> node = Names::Find<Node> (nodeName);
+  return ApplicationContainer (InstallPriv (node));
+}
+    
+ApplicationContainer
+SyncLogicHelper::Install (NodeContainer c)
+{
+  ApplicationContainer apps;
+  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
+    {
+      apps.Add (InstallPriv (*i));
+    }
+    
+  return apps;
+}
+    
+Ptr<Application>
+SyncLogicHelper::InstallPriv (Ptr<Node> node)
+{
+  Ptr<SyncLogic> app = CreateObject<SyncLogic> ("/sync", m_onUpdate, m_onRemove);
+  node->AddApplication (app);
+        
+  return app;
+}
+
+}
diff --git a/ns3/sync-logic-helper.h b/ns3/sync-logic-helper.h
new file mode 100644
index 0000000..20ad746
--- /dev/null
+++ b/ns3/sync-logic-helper.h
@@ -0,0 +1,115 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef SYNC_LOGIC_HELPER_H
+#define SYNC_LOGIC_HELPER_H
+
+#include "ns3/object-factory.h"
+#include "ns3/attribute.h"
+#include "ns3/node-container.h"
+#include "ns3/application-container.h"
+#include "ns3/ptr.h"
+
+#include <boost/function.hpp>
+
+namespace Sync 
+{
+
+class SeqNo;
+
+/**
+ * \brief A helper to make it easier to instantiate an ns3::CcnxConsumer Application
+ * on a set of nodes.
+ */
+class SyncLogicHelper
+{        
+public:
+  typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
+  typedef boost::function< void ( const std::string &/*prefix*/ ) > LogicRemoveCallback;
+
+  /**
+   * \brief Create an CcnxAppHelper to make it easier to work with CCNx apps
+   *
+   * \param app Class of the application
+   */
+  SyncLogicHelper ();
+
+  /**
+   * @brief Set the sync prefix
+   */
+  void
+  SetPrefix (const std::string &prefix);
+
+  /**
+   * @brief Set onUpdate and onRemove callbacks
+   */
+  void
+  SetCallbacks (LogicUpdateCallback onUpdate, LogicRemoveCallback onRemove);
+  
+  /**
+   * Install an ns3::CcnxConsumer on each node of the input container
+   * configured with all the attributes set with SetAttribute.
+   *
+   * \param c NodeContainer of the set of nodes on which an CcnxConsumer 
+   * will be installed.
+   * \returns Container of Ptr to the applications installed.
+   */
+  ns3::ApplicationContainer
+  Install (ns3::NodeContainer c);
+        
+  /**
+   * Install an ns3::CcnxConsumer on the node configured with all the 
+   * attributes set with SetAttribute.
+   *
+   * \param node The node on which an CcnxConsumer will be installed.
+   * \returns Container of Ptr to the applications installed.
+   */
+  ns3::ApplicationContainer
+  Install (ns3::Ptr<ns3::Node> node);
+        
+  /**
+   * Install an ns3::CcnxConsumer on the node configured with all the 
+   * attributes set with SetAttribute.
+   *
+   * \param nodeName The node on which an CcnxConsumer will be installed.
+   * \returns Container of Ptr to the applications installed.
+   */
+  ns3::ApplicationContainer
+  Install (std::string nodeName);
+        
+private:
+  /**
+   * \internal
+   * Install an ns3::CcnxConsumer on the node configured with all the 
+   * attributes set with SetAttribute.
+   *
+   * \param node The node on which an CcnxConsumer will be installed.
+   * \returns Ptr to the application installed.
+   */
+  ns3::Ptr<ns3::Application> InstallPriv (ns3::Ptr<ns3::Node> node);
+  std::string m_prefix; // sync prefix
+  LogicUpdateCallback m_onUpdate;
+  LogicRemoveCallback m_onRemove;
+};
+
+} // namespace Sync
+
+#endif // SYNC_LOGIC_HELPER_H
+
diff --git a/model/sync-ns3-name-info.cc b/ns3/sync-ns3-name-info.cc
similarity index 82%
rename from model/sync-ns3-name-info.cc
rename to ns3/sync-ns3-name-info.cc
index b46d1f6..dfd6179 100644
--- a/model/sync-ns3-name-info.cc
+++ b/ns3/sync-ns3-name-info.cc
@@ -35,25 +35,35 @@
 
 namespace Sync {
 
-
 NameInfoConstPtr
 Ns3NameInfo::FindOrCreate (ns3::Ptr<const ns3::CcnxNameComponents> name)
 {
+  mutex::scoped_lock namesLock (m_namesMutex);
+  
+  NameInfoConstPtr ret;
   string key = lexical_cast<string> (*name);
-
+  
   NameMap::iterator item = m_names.find (key);
-  if (item == m_names.end ())
+  if (item != m_names.end ())
     {
-      NameInfoPtr value = NameInfoPtr (new Ns3NameInfo (name));
+      ret = item->second.lock ();
+      BOOST_ASSERT (ret != 0);
+    }
+  else
+    {
+      ret = NameInfoPtr (new Ns3NameInfo (name));
+      weak_ptr<const NameInfo> value (ret);
       pair<NameMap::iterator,bool> inserted =
         m_names.insert (make_pair (key, value));
+      
       BOOST_ASSERT (inserted.second); // previous call has to insert value
       item = inserted.first;
     }
 
-  return item->second;
+  return ret;
 }
 
+
 Ns3NameInfo::Ns3NameInfo (ns3::Ptr<const ns3::CcnxNameComponents> name)
   : m_name (name)
 {
@@ -81,6 +91,19 @@
     }
 }
 
+bool
+Ns3NameInfo::operator < (const NameInfo &info) const
+{
+  try
+    {
+      return *m_name < *dynamic_cast<const Ns3NameInfo&> (info).m_name;
+    }
+  catch (...)
+    {
+      return false;
+    }
+}
+
 Digest &
 operator << (Digest &digest, const ns3::CcnxNameComponents &name)
 {
diff --git a/model/sync-ns3-name-info.h b/ns3/sync-ns3-name-info.h
similarity index 96%
rename from model/sync-ns3-name-info.h
rename to ns3/sync-ns3-name-info.h
index 9363665..347ba1a 100644
--- a/model/sync-ns3-name-info.h
+++ b/ns3/sync-ns3-name-info.h
@@ -47,6 +47,9 @@
   virtual bool
   operator == (const NameInfo &info) const;
 
+  virtual bool
+  operator < (const NameInfo &info) const;
+
   virtual std::string
   toString () const;
 
diff --git a/ns3/sync-scheduler.cc b/ns3/sync-scheduler.cc
new file mode 100644
index 0000000..025eb3e
--- /dev/null
+++ b/ns3/sync-scheduler.cc
@@ -0,0 +1,91 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Chaoyi Bian <bcy@pku.edu.cn>
+ *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "sync-scheduler.h"
+#include "sync-log.h"
+
+#include "ns3/simulator.h"
+
+using namespace boost;
+using namespace std;
+using namespace ns3;
+
+INIT_LOGGER ("Scheduler");
+
+namespace Sync {
+
+Scheduler::Scheduler ()
+{
+}
+
+Scheduler::~Scheduler ()
+{
+}
+
+void
+Scheduler::eventWrapper (Event event)
+{
+  event ();
+}
+
+void
+Scheduler::schedule (const TimeDuration &reltime, Event event, uint32_t label)
+{
+  _LOG_DEBUG ("Schedule event for " << (Simulator::Now () +reltime).ToDouble (Time::S) << "s for label " << label);
+  
+  list< EventId > &eventsForLabel = m_labeledEvents [label];
+  list< EventId >::iterator i = eventsForLabel.begin ();
+  while (i != eventsForLabel.end ())
+    {
+      if (i->IsExpired ())
+        {
+          list< EventId >::iterator next = i;
+          next ++;
+          eventsForLabel.erase (i);
+          i = next;
+        }
+      else
+        i ++;
+    }
+
+  ns3::EventId eventId = ns3::Simulator::Schedule (reltime, Scheduler::eventWrapper, event);
+  eventsForLabel.push_back (eventId);
+}
+
+void
+Scheduler::cancel (uint32_t label)
+{
+  list< EventId > &eventsForLabel = m_labeledEvents [label];
+  _LOG_DEBUG ("Canceling events for label " << label << " (" << eventsForLabel.size () << " events)");
+
+  for (list< EventId >::iterator i = eventsForLabel.begin ();
+       i != eventsForLabel.end ();
+       i++)
+    {
+      i->Cancel ();
+    }
+
+  eventsForLabel.clear ();
+}
+
+
+} // Sync
diff --git a/model/sync-scheduler.h b/ns3/sync-scheduler.h
similarity index 75%
copy from model/sync-scheduler.h
copy to ns3/sync-scheduler.h
index 10ef210..a98f8c1 100644
--- a/model/sync-scheduler.h
+++ b/ns3/sync-scheduler.h
@@ -23,10 +23,19 @@
 #ifndef SYNC_SCHEDULER_H
 #define SYNC_SCHEDULER_H
 
-#include <boost/thread/thread.hpp>
-#include <boost/thread/mutex.hpp>
+#include <ns3/nstime.h>
+#include <ns3/event-id.h>
+#include <ns3/simulator.h>
+#include <list>
+#include <map>
 
-#include "sync-logic-event-container.h"
+#include "sync-event.h"
+
+#define TIME_SECONDS(number) ns3::Seconds(number)
+#define TIME_MILLISECONDS(number) ns3::MilliSeconds(number)
+#define TIME_NOW ns3::Simulator::Now ()
+typedef ns3::Time TimeDuration;
+typedef ns3::Time TimeAbsolute;
 
 namespace Sync {
 
@@ -54,8 +63,8 @@
    * @param event function to be called at the time
    * @param label Label for the event
    */
-  void
-  schedule (const boost::system_time &abstime, Event event, uint32_t label);
+  // void
+  // schedule (const boost::system_time &abstime, Event event, uint32_t label);
 
   /**
    * @brief Schedule an event at relative time 'reltime'
@@ -64,7 +73,7 @@
    * @param label Label for the event
    */
   void
-  schedule (const boost::posix_time::time_duration &reltime, Event event, uint32_t label); 
+  schedule (const TimeDuration &reltime, Event event, uint32_t label); 
 
   /**
    * @brief Cancel all events for the label
@@ -73,26 +82,12 @@
   void
   cancel (uint32_t label);
 
-#ifdef _DEBUG
-  size_t
-  getEventsSize ()
-  {
-    boost::lock_guard<boost::mutex> lock (m_eventsMutex);
-    return m_events.size ();
-  }
-#endif
-  
 private:
-  void
-  threadLoop ();
-    
-private:
-  EventsContainer m_events;
-  boost::condition_variable m_eventsCondition;
-  boost::mutex  m_eventsMutex;
+  static void
+  eventWrapper (Event event);
 
-  boost::thread m_thread;
-  bool          m_threadRunning;
+private:
+  std::map< uint32_t, std::list< ns3::EventId > > m_labeledEvents;
 };
   
 }
diff --git a/test/test_app_socket.cc b/test/test_app_socket.cc
index e9a87fc..985dac3 100644
--- a/test/test_app_socket.cc
+++ b/test/test_app_socket.cc
@@ -27,9 +27,9 @@
 #include <boost/make_shared.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 
-#include "../model/sync-log.h"
+#include "sync-log.h"
 
-#include "../model/sync-app-socket.h"
+#include "sync-app-socket.h"
 extern "C" {
 #include <unistd.h>
 }
diff --git a/test/test_ccnx_wrapper.cc b/test/test_ccnx_wrapper.cc
index ac5150c..6934584 100644
--- a/test/test_ccnx_wrapper.cc
+++ b/test/test_ccnx_wrapper.cc
@@ -26,7 +26,7 @@
 
 #include <boost/make_shared.hpp>
 
-#include "../model/sync-ccnx-wrapper.h"
+#include "sync-ccnx-wrapper.h"
 
 using namespace Sync;
 using namespace std;
diff --git a/test/test_data_fetch_and_publish.cc b/test/test_data_fetch_and_publish.cc
index 0d5417d..157ce7f 100644
--- a/test/test_data_fetch_and_publish.cc
+++ b/test/test_data_fetch_and_publish.cc
@@ -27,9 +27,9 @@
 
 #include <boost/make_shared.hpp>
 
-#include "../model/sync-ccnx-wrapper.h"
-#include "../model/sync-app-data-fetch.h"
-#include "../model/sync-app-data-publish.h"
+#include "sync-ccnx-wrapper.h"
+#include "sync-app-data-fetch.h"
+#include "sync-app-data-publish.h"
 
 using namespace Sync;
 using namespace std;
diff --git a/test/test_digest.cc b/test/test_digest.cc
index 5a0b4de..3cb124e 100644
--- a/test/test_digest.cc
+++ b/test/test_digest.cc
@@ -25,7 +25,7 @@
 #include <boost/test/output_test_stream.hpp> 
 using boost::test_tools::output_test_stream;
 
-#include "../model/sync-digest.h"
+#include "sync-digest.h"
 #include <iostream>
 #include <sstream>
 
diff --git a/test/test_interest_table.cc b/test/test_interest_table.cc
index 6727bcc..34ea00f 100644
--- a/test/test_interest_table.cc
+++ b/test/test_interest_table.cc
@@ -27,7 +27,7 @@
 
 #include <boost/make_shared.hpp>
 
-#include "../model/sync-interest-table.h"
+#include "sync-interest-table.h"
 
 using namespace Sync;
 using namespace std;
diff --git a/test/test_leaf.cc b/test/test_leaf.cc
index 0ba4401..49afcc5 100644
--- a/test/test_leaf.cc
+++ b/test/test_leaf.cc
@@ -26,9 +26,9 @@
 
 #include <boost/make_shared.hpp>
 
-#include "../model/sync-full-leaf.h"
-#include "../model/sync-diff-leaf.h"
-#include "../model/sync-std-name-info.h"
+#include "sync-full-leaf.h"
+#include "sync-diff-leaf.h"
+#include "sync-std-name-info.h"
 
 using namespace Sync;
 using namespace std;
diff --git a/test/test_pit.cc b/test/test_pit.cc
index 7b2c7af..dca697e 100644
--- a/test/test_pit.cc
+++ b/test/test_pit.cc
@@ -27,8 +27,8 @@
 
 #include <boost/make_shared.hpp>
 
-#include "../model/sync-interest-table.h"
-#include "../model/sync-log.h"
+#include "sync-interest-table.h"
+#include "sync-log.h"
 
 using namespace Sync;
 using namespace std;
diff --git a/test/test_scheduler.cc b/test/test_scheduler.cc
index 276fd50..59687e4 100644
--- a/test/test_scheduler.cc
+++ b/test/test_scheduler.cc
@@ -26,8 +26,8 @@
 using boost::test_tools::output_test_stream;
 
 #include <boost/make_shared.hpp>
-#include "../model/sync-scheduler.h"
-#include "../model/sync-logic.h"
+#include "sync-scheduler.h"
+#include "sync-logic.h"
 
 using namespace Sync;
 using namespace std;
diff --git a/test/test_state.cc b/test/test_state.cc
index 86d4c6f..35fc79c 100644
--- a/test/test_state.cc
+++ b/test/test_state.cc
@@ -30,9 +30,9 @@
 #include <boost/make_shared.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 
-#include "../model/sync-std-name-info.h"
-#include "../model/sync-full-state.h"
-#include "../model/sync-diff-state.h"
+#include "sync-std-name-info.h"
+#include "sync-full-state.h"
+#include "sync-diff-state.h"
 
 using namespace Sync;
 using namespace std;
diff --git a/test/test_sync_logic.cc b/test/test_sync_logic.cc
index 5b36ba8..f6b6808 100644
--- a/test/test_sync_logic.cc
+++ b/test/test_sync_logic.cc
@@ -27,9 +27,9 @@
 
 #include <boost/make_shared.hpp>
 
-#include "../model/sync-ccnx-wrapper.h"
-#include "../model/sync-logic.h"
-#include "../model/sync-seq-no.h"
+#include "sync-ccnx-wrapper.h"
+#include "sync-logic.h"
+#include "sync-seq-no.h"
 
 using namespace std;
 using namespace boost;
diff --git a/waf-tools/ns3.py b/waf-tools/ns3.py
new file mode 100644
index 0000000..8a1f33f
--- /dev/null
+++ b/waf-tools/ns3.py
@@ -0,0 +1,71 @@
+## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
+
+import waflib
+from waflib.Configure import conf
+from waflib import Utils,Logs,Errors
+
+@conf
+def _print_optional_features(conf):
+    # Write a summary of optional features status
+    print "---- Summary of optional NS-3 features:"
+    Logs.pprint ('RED', "---- Summary of optional NS-3 features:")
+    # for (name, caption, was_enabled, reason_not_enabled) in conf.env['NS3_OPTIONAL_FEATURES']:
+    #     if was_enabled:
+    #         status = 'enabled'
+    #     else:
+    #         status = 'not enabled (%s)' % reason_not_enabled
+    #     print "%-30s: %s" % (caption, status)
+
+@conf
+def _check_dependencies(conf, required, mandatory):
+    # Logs.pprint ('CYAN', '  + %s' % required)
+    found = []
+    
+    libversion = "optimized"
+    if conf.options.ns3_debug:
+        libversion = "debug"
+    
+    for module in required:
+        retval = conf.check_cfg(package = 'libns3-dev-%s-%s' % (module, libversion),
+                                args='--cflags --libs', mandatory=mandatory,
+                                msg="Checking for ns3-%s" % module,
+                                uselib_store='NS3_%s' % module.upper())
+        # Logs.pprint ('CYAN', 'NS3_%s' % module.upper())
+        if not retval is None:
+            found.append(module)
+    import copy
+    if not 'NS3_MODULES_FOUND' in conf.env:
+        conf.env['NS3_MODULES_FOUND'] = []
+    conf.env['NS3_MODULES_FOUND'] = conf.env['NS3_MODULES_FOUND'] + copy.copy(found)
+
+def modules_uselib(bld, names):
+    return ['NS3_%s' % name.upper() for name in names] + \
+        ['NS3_LIBRARY_%s' % name.upper() for name in names] + \
+        ['NS3_HEADERS_%s' % name.upper() for name in names]
+
+def modules_found(bld, needed):
+    for module in needed:
+        if not module in bld.env['NS3_MODULES_FOUND']:
+            return False
+    return True
+
+@conf
+def check_modules(conf, modules, mandatory = True):
+    import os
+
+    if not 'NS3_CHECK_MODULE_ONCE' in conf.env:
+        conf.env['NS3_CHECK_MODULE_ONCE'] = ''
+
+        conf.check_cfg(atleast_pkgconfig_version='0.0.0')
+
+        if conf.options.log4cxx:
+            conf.env.append_value('DEFINES', 'NS3_LOG_ENABLE')
+
+    conf._check_dependencies(modules, mandatory)
+    conf._print_optional_features
+
+@conf
+def print_ns3_feature_summary(conf):
+    Logs.pprint ('CYAN', "---- Summary of optional NS-3 features:")
+    conf._print_optional_features
+
diff --git a/wscript b/wscript
index 6a0a629..ce56610 100644
--- a/wscript
+++ b/wscript
@@ -5,16 +5,17 @@
 
 def options(opt):
     opt.add_option('--no-debug',action='store_true',default=False,dest='no_debug',help='''Make an optimized build of the library (remove debugging code)''')
-    opt.add_option('--log4cxx',action='store_true',default=False,dest='log4cxx',help='''Compile with log4cxx support''')
+    opt.add_option('--log4cxx', action='store_true',default=False,dest='log4cxx',help='''Compile with log4cxx/native NS3 logging support''')
+    opt.add_option('--ns3',     action='store_true',default=False,dest='ns3_enable',help='''Compile as NS-3 module''')
+    opt.add_option('--ns3-debug', action='store_true',default=False,dest='ns3_debug',help='''Link against debug NS3 libraries. Optimized version will be used otherwise''')
     opt.load('compiler_c')
     opt.load('compiler_cxx')
     opt.load('boost')
     opt.load('doxygen')
-    opt.load('ccnx tinyxml', tooldir=["waf-tools"])
+    opt.load('ccnx tinyxml ns3', tooldir=["waf-tools"])
 
 def configure(conf):
     conf.load("compiler_cxx")
-    conf.env.append_value('CXXFLAGS', ['-O0', '-g3'])
 
     if not conf.check_cfg(package='openssl', args=['--cflags', '--libs'], uselib_store='SSL', mandatory=False):
       libcrypto = conf.check_cc(lib='crypto',
@@ -25,40 +26,136 @@
         conf.fatal ("Cannot find SSL libraries")
 
     conf.load('boost')
-    conf.check_boost(lib='system iostreams test thread')
-    
+
+    if conf.options.ns3_enable:
+        conf.load('ns3')
+        conf.define('NS3_MODULE', 1)
+        conf.check_modules(['core', 'network', 'internet'], mandatory = True)
+        conf.check_modules(['NDNabstraction'], mandatory = True)
+        conf.check_modules(['point-to-point'], mandatory = False)
+        conf.check_modules(['point-to-point-layout'], mandatory = False)
+
+        conf.check_boost(lib='system iostreams thread')
+        conf.define ('NS3_LOG_ENABLE', 1)
+    else:
+        conf.check_boost(lib='system iostreams test thread')
+        conf.define ('STANDALONE', 1)
+
+        conf.load ('ccnx')
+        conf.check_ccnx (path=conf.options.ccnx_dir)
+
+        if conf.options.log4cxx:
+            conf.check_cfg(package='liblog4cxx', args=['--cflags', '--libs'], uselib_store='LOG4CXX', mandatory=True)
+
+    if not conf.options.no_debug:
+        conf.define ('_DEBUG', 1)
+        conf.env.append_value('CXXFLAGS', ['-O0', '-g3'])
+    else:
+        conf.env.append_value('CXXFLAGS', ['-O3'])
+
     try:
         conf.load('doxygen')
     except:
         pass
 
-    conf.load('ccnx tinyxml')
-    conf.check_ccnx (path=conf.options.ccnx_dir)
-    conf.check_tinyxml (path=conf.options.ccnx_dir)
-
-    conf.define ('STANDALONE', 1)
-    if not conf.options.no_debug:
-        conf.define ('_DEBUG', 1)
-
-    if conf.options.log4cxx:
-        conf.check_cfg(package='liblog4cxx', args=['--cflags', '--libs'], uselib_store='LOG4CXX', mandatory=True)
+    conf.load('tinyxml')
+    conf.check_tinyxml ()
                    
 def build (bld):
-    libsync = bld.shlib (target=APPNAME, 
-                         features=['cxx', 'cxxshlib'],
-                         source = bld.path.ant_glob(['model/sync-*.cc',
-                                                     'helper/sync-*.cc']),
-                         use = 'BOOST BOOST_IOSTREAMS BOOST_THREAD SSL TINYXML CCNX')
+    if bld.get_define ("NS3_MODULE"):
+        sync_ns3 = bld.shlib (
+            target = "sync-ns3",
+            features=['cxx', 'cxxshlib'],
+            source =  [
+                'ns3/sync-ccnx-wrapper.cc',
+                'ns3/sync-ns3-name-info.cc',
+                'ns3/sync-scheduler.cc',
+                'ns3/sync-logic-helper.cc',
+                
+                # 'model/sync-app-data-fetch.cc',
+                # 'model/sync-app-data-publish.cc',
+                # 'ns3/sync-app.cc',
 
-    # Unit tests
-    unittests = bld.program (target="unit-tests",
-                             source = bld.path.ant_glob(['test/**/*.cc']),
-                             features=['cxx', 'cxxprogram'],
-                             use = 'BOOST_TEST sync')
+                'model/sync-diff-leaf.cc',
+                'model/sync-diff-state.cc',
+                'model/sync-digest.cc',
+                'model/sync-full-leaf.cc',
+                'model/sync-full-state.cc',
+                'model/sync-interest-table.cc',
+                'model/sync-leaf.cc',
+                'model/sync-logic.cc',
+                'model/sync-name-info.cc',
+                'model/sync-seq-no.cc',
+                'model/sync-state.cc',
+                'model/sync-std-name-info.cc',
+                ],
+            use = 'BOOST BOOST_IOSTREAMS SSL TINYXML ' + ' '.join (['ns3_'+dep for dep in ['core', 'network', 'internet', 'NDNabstraction']]).upper (),
+            includes = ['model', 'ns3', 'helper'],
+            )
 
-    if bld.get_define ("HAVE_LOG4CXX"):
-        libsync.use += ' LOG4CXX'
-        unittests.use += ' LOG4CXX'
+        example = bld.program (
+            target = "sync-example",
+            features=['cxx', 'cxxprogram'],
+            source = ['examples/sync-example.cc'],
+            use = 'sync-ns3',
+            includes = ['model', 'ns3', 'helper'],
+            )
+
+        sync_eval = bld.program (
+            target = "sync-eval",
+            features=['cxx', 'cxxprogram'],
+            source = ['evaluation/sync-eval.cc',
+                      'evaluation/standard-muc.cc',
+                      'evaluation/sync-muc.cc',
+                      ],
+            use = 'sync-ns3',
+            includes = ['model', 'ns3', 'helper'],
+            )
+        # from waflib import Utils,Logs,Errors
+        # Logs.pprint ('CYAN', program.use)
+        
+    else:
+        libsync = bld.shlib (
+            target=APPNAME, 
+            features=['cxx', 'cxxshlib'],
+            source =  [
+                'ccnx/sync-ccnx-wrapper.cc',
+                'ccnx/sync-scheduler.cc',
+                'ccnx/sync-log.cc',
+                'ccnx/sync-app-data-fetch.cc',
+                'ccnx/sync-app-data-publish.cc',
+                'ccnx/sync-app-socket-c.cc',
+                'ccnx/sync-app-socket.cc',
+                
+                'model/sync-diff-leaf.cc',
+                'model/sync-diff-state.cc',
+                'model/sync-digest.cc',
+                'model/sync-full-leaf.cc',
+                'model/sync-full-state.cc',
+                'model/sync-interest-table.cc',
+                'model/sync-leaf.cc',
+                'model/sync-logic.cc',
+                'model/sync-name-info.cc',
+                'model/sync-seq-no.cc',
+                'model/sync-state.cc',
+                'model/sync-std-name-info.cc',
+                ],
+            use = 'BOOST BOOST_IOSTREAMS BOOST_THREAD SSL TINYXML CCNX',
+            includes = ['model', 'ccnx', 'helper'],
+            )
+        
+        # Unit tests
+        unittests = bld.program (
+            target="unit-tests",
+            source = bld.path.ant_glob(['test/**/*.cc']),
+            features=['cxx', 'cxxprogram'],
+            use = 'BOOST_TEST sync',
+            includes = ['model', 'ccnx', 'helper'],
+            )
+
+        if bld.get_define ("HAVE_LOG4CXX"):
+            libsync.use += ' LOG4CXX'
+            unittests.use += ' LOG4CXX'
 
 # doxygen docs
 from waflib.Build import BuildContext