model: A basic implementation of ndn::ApiFace that can be used as an NDN handler in any "normal" NS-3 application
Refs #1005 (http://redmine.named-data.net/)
diff --git a/ndn.cxx/ndn-api-face.h b/ndn.cxx/ndn-api-face.h
index 133f29c..8c589bb 100644
--- a/ndn.cxx/ndn-api-face.h
+++ b/ndn.cxx/ndn-api-face.h
@@ -23,144 +23,90 @@
#ifndef NDN_API_FACE_H
#define NDN_API_FACE_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/callback.h>
+#include <ns3/ndn-face.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; }
+class ApiFacePriv;
- 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 ApiFace
- // : private ns3::ndn::Face
+ : public ns3::ndn::Face
{
public:
- // typedef boost::function<void (const ndn::Name &, Ptr<Packet>)> DataCallback;
- // typedef boost::function<void (const ndn::Name &)> 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
- // *
- // */
- // ApiFace (Ptr<Node> node);
- // ~ApiFace ();
-
- // /**
- // * @brief send Interest; need to grab lock m_mutex first
- // *
- // * @param name 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 ndn::Name &name, const DataCallback &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 ndn::Name &prefix, const InterestCallback &interestCallback);
-
- // /**
- // * @brief clear Interest filter
- // * @param prefix the prefix of Interest
- // */
- // void
- // clearInterestFilter (const std::string &prefix);
+ typedef Callback<void, Ptr<const Name>, Ptr<const Interest> > InterestCallback;
+ typedef Callback<void, Ptr<const Interest>, Ptr<const ContentObject> > DataCallback;
+ typedef Callback<void, Ptr<const Interest> > TimeoutCallback;
/**
- * @brief publish data and put it to local ccn content store; need to grab
- * lock m_mutex first
+ * @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
*
- * @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);
- // }
+ ApiFace (Ptr<Node> node);
+ ~ApiFace ();
- // int
- // publishRawData (const std::string &name, const char *buf, size_t len, int freshness);
+ /**
+ * @brief Express Interest
+ *
+ * @param name the Interest name
+ * @param onData the callback function to deal with the returned data
+ * @param onTimeout the callback function to deal with timeouts
+ */
+ void
+ ExpressInterest (Ptr<Interest> interest,
+ DataCallback onData,
+ TimeoutCallback onTimeout); // = MakeNullCallback< void, Ptr<Interest> > ()
+
+ /**
+ * @brief set Interest filter (specify what interest you want to receive)
+ *
+ * @param prefix the prefix of Interest
+ * @param onInterest the callback function to deal with the returned data
+ */
+ void
+ SetInterestFilter (Ptr<const Name> prefix, InterestCallback onInterest);
+
+ /**
+ * @brief clear Interest filter
+ * @param prefix the prefix of Interest
+ */
+ void
+ ClearInterestFilter (Ptr<const Name> prefix);
+
+ /**
+ * @brief Publish data
+ * @param data Data packet to publish
+ */
+ void
+ Put (Ptr<ContentObject> data);
+
+public:
+ virtual bool
+ SendInterest (Ptr<const Interest> interest);
+
+ virtual bool
+ SendData (Ptr<const ContentObject> data);
+
+ virtual std::ostream&
+ Print (std::ostream &os) const;
private:
- // // 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);
+ ApiFace () : Face (0) {}
+ ApiFace (const ApiFace &) : Face (0) {}
+ ApiFace& operator= (const ApiFace &) { return *this; }
private:
- ns3::UniformVariable m_rand; // nonce generator
-
- FilterEntryContainer<RawDataCallback> m_dataCallbacks;
- FilterEntryContainer<InterestCallback> m_interestCallbacks;
+ ApiFacePriv *m_this;
};
}