ndn.cxx: Initial work to introduce full NDN application API to ndnSIM

Refs #1005 (http://redmine.named-data.net/)
diff --git a/examples/custom-apps/ndn-api-app.cc b/examples/custom-apps/ndn-api-app.cc
new file mode 100644
index 0000000..3df6a27
--- /dev/null
+++ b/examples/custom-apps/ndn-api-app.cc
@@ -0,0 +1,70 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013 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 "ndn-api-app.h"
+
+NS_LOG_COMPONENT_DEFINE ("ndn.ApiApp");
+
+namespace ns3 {
+namespace ndn {
+
+// Necessary if you are planning to use ndn::AppHelper
+NS_OBJECT_ENSURE_REGISTERED (ApiApp);
+
+TypeId
+ApiApp::GetTypeId ()
+{
+  static TypeId tid = TypeId ("ns3::ndn::ApiApp")
+    .SetParent<Application> ()
+    .AddConstructor<ApiApp> ()
+    ;
+
+  return tid;
+}
+
+ApiApp::ApiApp ()
+{
+  m_handler = CreateObject<Handler> ();
+}
+
+void
+ApiApp::RequestData ()
+{
+  m_handler->sendInterest ("/test/prefix", boost::bind (&ApiApp::OnData
+}
+
+void
+ApiApp::StartApplication ()
+{
+  m_handler->SetNode (GetNode ());
+  m_handler->StartApplication ();
+  
+  Simulator::Schedule (Seconds (1), &::ns3::ndn::ApiApp::RequestData, this);
+}
+
+void
+ApiApp::StopApplication ()
+{
+  m_handler->StopApplication ();
+}
+
+} // namespace ndn
+} // namespace ns3
+
diff --git a/examples/custom-apps/ndn-api-app.h b/examples/custom-apps/ndn-api-app.h
new file mode 100644
index 0000000..26df590
--- /dev/null
+++ b/examples/custom-apps/ndn-api-app.h
@@ -0,0 +1,60 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013 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 NDN_EXAMPLES_API_APP_H
+#define NDN_EXAMPLES_API_APP_H
+
+#include "ns3/core-module.h"
+#include "ns3/network-module.h"
+#include "ns3/ndnSIM-module.h"
+
+#include "ns3/ndnSIM/ndn.cxx/ndn-handler.h"
+
+namespace ns3 {
+namespace ndn {
+
+class ApiApp : public Application
+{
+public:
+  static TypeId
+  GetTypeId ();
+
+  ApiApp ();
+
+private:
+  void
+  RequestData ();
+  
+protected:
+  // inherited from Application base class.
+  virtual void
+  StartApplication ();
+
+  virtual void
+  StopApplication ();
+
+private:
+  Ptr<Handler> m_handler;
+};
+
+} // namespace ndn
+} // namespace ns3
+
+#endif // NDN_EXAMPLES_API_APP_H
diff --git a/examples/ndn-simple-api.cc b/examples/ndn-simple-api.cc
new file mode 100644
index 0000000..ba79af7
--- /dev/null
+++ b/examples/ndn-simple-api.cc
@@ -0,0 +1,96 @@
+/* -*-  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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+// ndn-simple.cc
+#include "ns3/core-module.h"
+#include "ns3/network-module.h"
+#include "ns3/point-to-point-module.h"
+#include "ns3/ndnSIM-module.h"
+
+using namespace ns3;
+
+/**
+ * This scenario simulates a very simple network topology:
+ *
+ *
+ *      +----------+     1Mbps      +--------+     1Mbps      +----------+
+ *      | consumer | <------------> | router | <------------> | producer |
+ *      +----------+         10ms   +--------+          10ms  +----------+
+ *
+ *
+ * Consumer requests data from producer with frequency 10 interests per second
+ * (interests contain constantly increasing sequence number).
+ *
+ * For every received interest, producer replies with a data packet, containing
+ * 1024 bytes of virtual payload.
+ *
+ * To run scenario and see what is happening, use the following command:
+ *
+ *     NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-simple
+ */
+
+int 
+main (int argc, char *argv[])
+{
+  // setting default parameters for PointToPoint links and channels
+  Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps"));
+  Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms"));
+  Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20"));
+
+  // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
+  CommandLine cmd;
+  cmd.Parse (argc, argv);
+
+  // Creating nodes
+  NodeContainer nodes;
+  nodes.Create (3);
+
+  // Connecting nodes using two links
+  PointToPointHelper p2p;
+  p2p.Install (nodes.Get (0), nodes.Get (1));
+  p2p.Install (nodes.Get (1), nodes.Get (2));
+
+  // Install NDN stack on all nodes
+  ndn::StackHelper ndnHelper;
+  ndnHelper.SetDefaultRoutes (true);
+  ndnHelper.InstallAll ();
+
+  // Installing applications
+
+  // Consumer
+  ndn::AppHelper consumerHelper ("ns3::ndn::ApiApp");
+  // // Consumer will request /prefix/0, /prefix/1, ...
+  // consumerHelper.SetPrefix ("/prefix");
+  // consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second
+  consumerHelper.Install (nodes.Get (0)); // first node
+
+  // Producer
+  ndn::AppHelper producerHelper ("ns3::ndn::Producer");
+  // Producer will reply to all requests starting with /prefix
+  producerHelper.SetPrefix ("/prefix");
+  producerHelper.SetAttribute ("PayloadSize", StringValue("1024"));
+  producerHelper.Install (nodes.Get (2)); // last node
+
+  Simulator::Stop (Seconds (20.0));
+
+  Simulator::Run ();
+  Simulator::Destroy ();
+
+  return 0;
+}
diff --git a/examples/wscript b/examples/wscript
index c9850ae..437ed26 100644
--- a/examples/wscript
+++ b/examples/wscript
@@ -85,3 +85,9 @@
 
     obj = bld.create_ns3_program('ndn-simple-with-pit-count-stats', all_modules)
     obj.source = 'ndn-simple-with-pit-count-stats.cc'
+
+    obj = bld.create_ns3_program('ndn-simple-api', all_modules)
+    obj.source = [
+        'ndn-simple-api.cc',
+        'custom-apps/ndn-api-app.cc'
+        ]
diff --git a/ndn.cxx/ndn-handler.cc b/ndn.cxx/ndn-handler.cc
new file mode 100644
index 0000000..c7c9e66
--- /dev/null
+++ b/ndn.cxx/ndn-handler.cc
@@ -0,0 +1,225 @@
+/* -*- 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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *         Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Chaoyi Bian <bcy@pku.edu.cn>
+ */
+
+#include "ndn-handler.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 <ns3/packet.h>
+
+#include <ns3/ndn-interest.h>
+#include <ns3/ndn-content-object.h>
+#include <ns3/ndn-face.h>
+#include <ns3/ndn-fib.h>
+#include <ns3/log.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;
+
+#define INIT_LOGGER NS_LOG_COMPONENT_DEFINE
+#define _LOG_DEBUG NS_LOG_DEBUG
+#define _LOG_TRACE NS_LOG_TRACE
+#define _LOG_INFO NS_LOG_INFO
+
+
+INIT_LOGGER ("ndn.Handler");
+
+namespace ns3 {
+namespace ndn {
+
+Handler::Handler()
+  : m_rand (0, std::numeric_limits<uint32_t>::max ())
+{
+}
+
+Handler::~Handler()
+{
+}
+
+void
+Handler::StartApplication ()
+{
+  ndn::App::StartApplication ();
+}
+
+void
+Handler::StopApplication ()
+{
+  ndn::App::StopApplication ();
+}
+
+int
+Handler::publishRawData (const std::string &name, const char *buf, size_t len, int freshness)
+{
+  // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
+  _LOG_INFO (">> publishRawData " << name);
+
+  static ndn::ContentObjectTail trailer;
+  
+  ndn::ContentObject data;
+  data.SetName (name);
+  data.SetFreshness (Seconds (freshness));
+
+  Ptr<Packet> packet = Create<Packet> (reinterpret_cast<const uint8_t*> (buf), len);
+  // packet->AddPacketTag (CreateObject<TypeTag> (TypeTag::DATA));
+  packet->AddHeader (data);
+  packet->AddTrailer (trailer);
+
+  m_protocolHandler (packet);
+
+  m_transmittedContentObjects (&data, packet, this, m_face);
+
+  return 0;
+}
+
+
+// void
+// RawDataCallback2StringDataCallback (Handler::StringDataCallback callback, std::string str, const char *buf, size_t len)
+// {
+//   callback (str, string (buf, len));
+// }
+
+// int
+// Handler::sendInterestForString (const std::string &strInterest, const StringDataCallback &strDataCallback/*, int retry*/)
+// {
+//   return sendInterest (strInterest, boost::bind (RawDataCallback2StringDataCallback, strDataCallback, _1, _2, _3));
+// }
+
+int Handler::sendInterest (const string &strInterest, const RawDataCallback &rawDataCallback)
+{
+  // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
+  _LOG_INFO (">> I " << strInterest);
+  Ptr<ndn::Name> name = Create<ndn::Name> (strInterest);
+
+  ndn::Interest interestHeader;
+  interestHeader.SetNonce            (m_rand.GetValue ());
+  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);
+  
+  // Record the callback
+  FilterEntryContainer<RawDataCallback>::iterator entry = m_dataCallbacks.find_exact (*name);
+  if (entry == m_dataCallbacks.end ())
+    {
+      pair<FilterEntryContainer<RawDataCallback>::iterator, bool> status =
+        m_dataCallbacks.insert (*name, Create< FilterEntry<RawDataCallback> > (name));
+
+      entry = status.first;
+    }
+  entry->payload ()->AddCallback (rawDataCallback);
+
+  m_protocolHandler (packet);
+  m_transmittedInterests (&interestHeader, this, m_face);
+  
+  return 0;
+}
+
+int Handler::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
+{
+  NS_LOG_DEBUG ("== setInterestFilter " << prefix << " (" << GetNode ()->GetId () << ")");
+  Ptr<ndn::Name> name = Create<ndn::Name> (prefix);
+
+  FilterEntryContainer<InterestCallback>::iterator entry = m_interestCallbacks.find_exact (*name);
+  if (entry == m_interestCallbacks.end ())
+    {
+      pair<FilterEntryContainer<InterestCallback>::iterator, bool> status =
+        m_interestCallbacks.insert (*name, Create < FilterEntry<InterestCallback> > (name));
+
+      entry = status.first;
+    }
+
+  entry->payload ()->AddCallback (interestCallback);
+
+  // creating actual face
+  Ptr<ndn::Fib> fib = GetNode ()->GetObject<ndn::Fib> ();
+  Ptr<ndn::fib::Entry> fibEntry = fib->Add (name, m_face, 0);
+  fibEntry->UpdateStatus (m_face, ndn::fib::FaceMetric::NDN_FIB_GREEN);
+
+  return 0;
+}
+
+void
+Handler::clearInterestFilter (const std::string &prefix)
+{
+  Ptr<ndn::Name> name = Create<ndn::Name> (prefix);
+
+  FilterEntryContainer<InterestCallback>::iterator entry = m_interestCallbacks.find_exact (*name);
+  if (entry == m_interestCallbacks.end ())
+    return;
+
+  entry->payload ()->ClearCallback ();
+}
+
+void
+Handler::OnInterest (const Ptr<const ndn::Interest> &interest, Ptr<Packet> packet)
+{
+  ndn::App::OnInterest (interest, packet);
+
+  // the app cannot set several filters for the same prefix
+  FilterEntryContainer<InterestCallback>::iterator entry = m_interestCallbacks.longest_prefix_match (interest->GetName ());
+  if (entry == m_interestCallbacks.end ())
+    {
+      _LOG_DEBUG ("No Interest callback set");
+      return;
+    }
+  
+  entry->payload ()->m_callback (lexical_cast<string> (interest->GetName ()));  
+}
+
+void
+Handler::OnContentObject (const Ptr<const ndn::ContentObject> &contentObject,
+                              Ptr<Packet> payload)
+{
+  ndn::App::OnContentObject (contentObject, payload);
+  NS_LOG_DEBUG ("<< D " << contentObject->GetName ());
+
+  FilterEntryContainer<RawDataCallback>::iterator entry = m_dataCallbacks.longest_prefix_match (contentObject->GetName ());
+  if (entry == m_dataCallbacks.end ())
+    {
+      _LOG_DEBUG ("No Data callback set");
+      return;
+    }
+
+  while (entry != m_dataCallbacks.end ())
+    {
+      entry->payload ()->m_callback (lexical_cast<string> (contentObject->GetName ()), payload);
+      m_dataCallbacks.erase (entry);
+
+      entry = m_dataCallbacks.longest_prefix_match (contentObject->GetName ());
+    }
+}
+
+}
+}
diff --git a/ndn.cxx/ndn-handler.h b/ndn.cxx/ndn-handler.h
new file mode 100644
index 0000000..420645d
--- /dev/null
+++ b/ndn.cxx/ndn-handler.h
@@ -0,0 +1,179 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 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>
+ *         Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Chaoyi Bian <bcy@pku.edu.cn>
+ */
+
+#ifndef NDN_HANDLER_H
+#define NDN_HANDLER_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/ndn-app.h>
+#include <ns3/ndn-name.h>
+
+#include <ns3/ndnSIM/utils/trie/trie-with-policy.h>
+#include <ns3/ndnSIM/utils/trie/counting-policy.h>
+
+namespace ns3 {
+namespace ndn {
+
+template<class Callback>
+struct FilterEntry : public ns3::SimpleRefCount< FilterEntry<Callback> >
+{
+public:
+  FilterEntry (ns3::Ptr<const ns3::ndn::Name> prefix)
+    : m_prefix (prefix)
+  { }
+  
+  const ns3::ndn::Name &
+  GetPrefix () const
+  { return *m_prefix; }
+
+  void
+  AddCallback (Callback callback)
+  { 
+    m_callback = callback;
+  }
+
+  void
+  ClearCallback ()
+  {
+    m_callback = 0;
+  }
+  
+public:
+  ns3::Ptr<const ns3::ndn::Name> m_prefix; ///< \brief Prefix of the PIT entry
+  Callback m_callback;
+};
+
+
+template<class Callback>
+struct FilterEntryContainer :
+    public ns3::ndn::ndnSIM::trie_with_policy<ns3::ndn::Name,
+                                              ns3::ndn::ndnSIM::smart_pointer_payload_traits< FilterEntry<Callback> >,
+                                              ns3::ndn::ndnSIM::counting_policy_traits>
+{
+};
+
+
+struct OperationException : virtual boost::exception, virtual std::exception { };
+/**
+ * \ingroup sync
+ * @brief A handler for NDN; clients of this code do not need to deal
+ * with NDN API directly
+ */
+class Handler
+  : public ns3::ndn::App
+{
+public:
+  typedef boost::function<void (std::string, std::string)> StringDataCallback;
+  typedef boost::function<void (std::string, Ptr<Packet>)> RawDataCallback;
+  typedef boost::function<void (std::string)> InterestCallback;
+
+  
+  /**
+   * @brief initialize the handler; 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
+   *
+   */
+  Handler();
+  ~Handler();
+
+  /**
+   * @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
+  // sendInterestForString (const std::string &strInterest, const StringDataCallback &strDataCallback/*, int retry = 0*/);
+
+  int
+  sendInterest (const std::string &strInterest, const RawDataCallback &rawDataCallback/*, int retry = 0*/);
+  
+  /**
+   * @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 NDN library calls, >0 if success
+   */
+  int
+  publishStringData (const std::string &name, const std::string &dataBuffer, int freshness)
+  {
+    return publishRawData (name, dataBuffer.c_str(), dataBuffer.length(), freshness);
+  }
+
+  int
+  publishRawData (const std::string &name, const char *buf, size_t len, int freshness);
+  
+  // from ndn::App
+  virtual void
+  OnInterest (const ns3::Ptr<const ns3::ndn::Interest> &interest, ns3::Ptr<ns3::Packet> packet);
+ 
+  virtual void
+  OnContentObject (const ns3::Ptr<const ns3::ndn::ContentObject> &contentObject,
+                   ns3::Ptr<ns3::Packet> payload);
+
+
+public:
+  // inherited from Application base class.
+  virtual void
+  StartApplication ();    // Called at time specified by Start
+
+  virtual void
+  StopApplication ();     // Called at time specified by Stop
+
+private:
+  ns3::UniformVariable m_rand; // nonce generator
+
+  FilterEntryContainer<RawDataCallback> m_dataCallbacks;
+  FilterEntryContainer<InterestCallback> m_interestCallbacks;
+};
+
+}
+}
+
+#endif // NDN_HANDLER_H
diff --git a/wscript b/wscript
index 52f2892..8dbff90 100644
--- a/wscript
+++ b/wscript
@@ -108,12 +108,14 @@
                                        'apps/*.cc',
                                        'utils/**/*.cc',
                                        'helper/**/*.cc',
+                                       'ndn.cxx/**/*.cc',
                                        ])
     module.full_headers = [p.path_from(bld.path) for p in bld.path.ant_glob([
                            'utils/**/*.h',
                            'model/**/*.h',
                            'apps/**/*.h',
                            'helper/**/*.h',
+                           'ndn.cxx/**/*.h',
                            ])]
 
     headers.source = [