forked from cawka/ndn.cxx
diff --git a/ndn-cpp/common.h b/ndn-cpp/common.h
new file mode 100644
index 0000000..e45f067
--- /dev/null
+++ b/ndn-cpp/common.h
@@ -0,0 +1,221 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_COMMON_H
+#define NDN_COMMON_H
+
+#include <boost/shared_ptr.hpp>
+#include <boost/make_shared.hpp>
+#include <boost/date_time/posix_time/posix_time_types.hpp>
+
+namespace ndn
+{
+template<class T>
+struct Ptr : public boost::shared_ptr<T>
+{
+ Ptr () { }
+ Ptr (boost::shared_ptr<T> ptr) : boost::shared_ptr<T>(ptr) { }
+ Ptr (T *ptr) : boost::shared_ptr<T>(ptr) { }
+
+ template<class Y>
+ Ptr & operator = (boost::shared_ptr<Y> ptr)
+ {
+ boost::static_pointer_cast<T> (ptr).swap (*this);
+ // *this = boost::static_pointer_cast<T> (ptr);
+ return *this;
+ }
+
+ operator Ptr<const T> () const { return *this; }
+
+ static Ptr
+ Create () { return boost::make_shared<T> (); }
+};
+
+typedef boost::posix_time::ptime Time;
+typedef boost::posix_time::time_duration TimeInterval;
+
+namespace time
+{
+inline TimeInterval Seconds (int secs) { return boost::posix_time::seconds (secs); }
+inline TimeInterval Milliseconds (int msecs) { return boost::posix_time::milliseconds (msecs); }
+inline TimeInterval Microseconds (int musecs) { return boost::posix_time::microseconds (musecs); }
+
+inline TimeInterval Seconds (double fractionalSeconds)
+{
+ double seconds, microseconds;
+ seconds = std::modf (fractionalSeconds, µseconds);
+ microseconds *= 1000000;
+
+ return time::Seconds((int)seconds) + time::Microseconds((int)microseconds);
+}
+
+inline Time Now () { return boost::posix_time::microsec_clock::universal_time (); }
+
+const Time UNIX_EPOCH_TIME = Time (boost::gregorian::date (1970, boost::gregorian::Jan, 1));
+inline TimeInterval NowUnixTimestamp ()
+{
+ return TimeInterval (time::Now () - UNIX_EPOCH_TIME);
+}
+} // time
+} // ndn
+
+
+extern "C" {
+#include <ccn/ccn.h>
+#include <ccn/charbuf.h>
+#include <ccn/keystore.h>
+#include <ccn/uri.h>
+#include <ccn/bloom.h>
+#include <ccn/signing.h>
+}
+#include <vector>
+#include <boost/shared_ptr.hpp>
+#include <boost/exception/all.hpp>
+#include <boost/function.hpp>
+#include <string>
+#include <sstream>
+#include <map>
+#include <utility>
+#include <string.h>
+#include <boost/iostreams/filter/gzip.hpp>
+#include <boost/iostreams/filtering_stream.hpp>
+#include <boost/iostreams/device/back_inserter.hpp>
+#include <boost/range/iterator_range.hpp>
+#include <boost/make_shared.hpp>
+
+namespace ndn {
+typedef std::vector<unsigned char> Bytes;
+typedef std::vector<std::string>Comps;
+
+typedef boost::shared_ptr<Bytes> BytesPtr;
+
+inline
+const unsigned char *
+head(const Bytes &bytes)
+{
+ return &bytes[0];
+}
+
+inline
+unsigned char *
+head (Bytes &bytes)
+{
+ return &bytes[0];
+}
+
+// --- Bytes operations start ---
+inline void
+readRaw(Bytes &bytes, const unsigned char *src, size_t len)
+{
+ if (len > 0)
+ {
+ bytes.resize(len);
+ memcpy (head (bytes), src, len);
+ }
+}
+
+inline BytesPtr
+readRawPtr (const unsigned char *src, size_t len)
+{
+ if (len > 0)
+ {
+ BytesPtr ret (new Bytes (len));
+ memcpy (head (*ret), src, len);
+
+ return ret;
+ }
+ else
+ return BytesPtr ();
+}
+
+template<class Msg>
+BytesPtr
+serializeMsg(const Msg &msg)
+{
+ int size = msg.ByteSize ();
+ BytesPtr bytes (new Bytes (size));
+ msg.SerializeToArray (head(*bytes), size);
+ return bytes;
+}
+
+template<class Msg>
+boost::shared_ptr<Msg>
+deserializeMsg (const Bytes &bytes)
+{
+ boost::shared_ptr<Msg> retval (new Msg ());
+ if (!retval->ParseFromArray (head (bytes), bytes.size ()))
+ {
+ // to indicate an error
+ return boost::shared_ptr<Msg> ();
+ }
+ return retval;
+}
+
+template<class Msg>
+boost::shared_ptr<Msg>
+deserializeMsg (const void *buf, size_t length)
+{
+ boost::shared_ptr<Msg> retval (new Msg ());
+ if (!retval->ParseFromArray (buf, length))
+ {
+ // to indicate an error
+ return boost::shared_ptr<Msg> ();
+ }
+ return retval;
+}
+
+
+template<class Msg>
+BytesPtr
+serializeGZipMsg(const Msg &msg)
+{
+ std::vector<char> bytes; // Bytes couldn't work
+ {
+ boost::iostreams::filtering_ostream out;
+ out.push(boost::iostreams::gzip_compressor()); // gzip filter
+ out.push(boost::iostreams::back_inserter(bytes)); // back_inserter sink
+
+ msg.SerializeToOstream(&out);
+ }
+ BytesPtr uBytes = boost::make_shared<Bytes>(bytes.size());
+ memcpy(&(*uBytes)[0], &bytes[0], bytes.size());
+ return uBytes;
+}
+
+template<class Msg>
+boost::shared_ptr<Msg>
+deserializeGZipMsg(const Bytes &bytes)
+{
+ std::vector<char> sBytes(bytes.size());
+ memcpy(&sBytes[0], &bytes[0], bytes.size());
+ boost::iostreams::filtering_istream in;
+ in.push(boost::iostreams::gzip_decompressor()); // gzip filter
+ in.push(boost::make_iterator_range(sBytes)); // source
+
+ boost::shared_ptr<Msg> retval = boost::make_shared<Msg>();
+ if (!retval->ParseFromIstream(&in))
+ {
+ // to indicate an error
+ return boost::shared_ptr<Msg> ();
+ }
+
+ return retval;
+}
+
+
+// --- Bytes operations end ---
+
+// Exceptions
+typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
+
+} // ndn
+#endif // NDN_COMMON_H
diff --git a/ndn-cpp/data.cc b/ndn-cpp/data.cc
new file mode 100644
index 0000000..5a44da8
--- /dev/null
+++ b/ndn-cpp/data.cc
@@ -0,0 +1,23 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "data.h"
+
+namespace ndn {
+
+Data::Data ()
+{
+}
+
+Data::~Data ()
+{
+}
+
+} // namespace ndn
diff --git a/ndn-cpp/data.h b/ndn-cpp/data.h
new file mode 100644
index 0000000..212dfbd
--- /dev/null
+++ b/ndn-cpp/data.h
@@ -0,0 +1,204 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_DATA_H
+#define NDN_DATA_H
+
+#include "ndn-cpp/fields/name.h"
+#include "ndn-cpp/fields/content.h"
+#include "ndn-cpp/fields/signature.h"
+#include "ndn-cpp/fields/signed-blob.h"
+
+namespace ndn {
+
+/**
+ * @brief Class implementing abstractions to work with NDN Data packets
+ */
+class Data
+{
+public:
+ /**
+ * @brief Create an empty Data with empty payload
+ **/
+ Data ();
+
+ /**
+ * @brief Destructor
+ */
+ ~Data ();
+
+ /**
+ * @brief Set data packet name
+ * @param name name of the data packet
+ * @return reference to self (to allow method chaining)
+ *
+ * In some cases, a direct access to and manipulation of name using getName is more efficient
+ */
+ inline Data &
+ setName (const Name &name);
+
+ /**
+ * @brief Get data packet name (const reference)
+ * @returns name of the data packet
+ */
+ inline const Name &
+ getName () const;
+
+ /**
+ * @brief Get data packet name (reference)
+ * @returns name of the data packet
+ */
+ inline Name &
+ getName ();
+
+ /**
+ * @brief Get const smart pointer to signature object
+ */
+ inline Ptr<const Signature>
+ getSignature () const;
+
+ /**
+ * @brief Get smart pointer to signature object
+ */
+ inline Ptr<Signature>
+ getSignature ();
+
+ /**
+ * @brief Set signature object
+ * @param signature smart pointer to a signature object
+ */
+ inline void
+ setSignature (Ptr<Signature> sigature);
+
+ /**
+ * @brief Get const reference to content object (content info + actual content)
+ */
+ inline const Content &
+ getContent () const;
+
+ /**
+ * @brief Get reference to content object (content info + actual content)
+ */
+ inline Content &
+ getContent ();
+
+ /**
+ * @brief Set content object (content info + actual content)
+ * @param content reference to a content object
+ *
+ * More efficient way (that avoids copying):
+ * @code
+ * Content content (...);
+ * getContent ().swap (content);
+ * @endcode
+ */
+ inline void
+ setContent (const Content &content);
+
+ /**
+ * @brief A helper method to directly access actual content data (const reference)
+ *
+ * This method is equivalent to
+ * @code
+ * getContent ().getContent ()
+ * @endcode
+ */
+ inline const Blob &
+ content () const;
+
+ /**
+ * @brief A helper method to directly access actual content data (reference)
+ *
+ * This method is equivalent to
+ * @code
+ * getContent ().getContent ()
+ * @endcode
+ */
+ inline Blob &
+ content ();
+
+private:
+ Name m_name;
+ Ptr<Signature> m_signature; // signature with its parameters "binds" name and content
+ Content m_content;
+
+ Ptr<SignedBlob> m_wire;
+};
+
+inline Data &
+Data::setName (const Name &name)
+{
+ m_name = name;
+ return *this;
+}
+
+inline const Name &
+Data::getName () const
+{
+ return m_name;
+}
+
+inline Name &
+Data::getName ()
+{
+ return m_name;
+}
+
+inline Ptr<const Signature>
+Data::getSignature () const
+{
+ return m_signature;
+}
+
+inline Ptr<Signature>
+Data::getSignature ()
+{
+ return m_signature;
+}
+
+inline void
+Data::setSignature (Ptr<Signature> signature)
+{
+ m_signature = signature;
+}
+
+inline const Content &
+Data::getContent () const
+{
+ return m_content;
+}
+
+inline Content &
+Data::getContent ()
+{
+ return m_content;
+}
+
+inline void
+Data::setContent (const Content &content)
+{
+ m_content = content;
+}
+
+inline const Blob &
+Data::content () const
+{
+ return getContent ().getContent ();
+}
+
+inline Blob &
+Data::content ()
+{
+ return getContent ().getContent ();
+}
+
+} // namespace ndn
+
+#endif // NDN_DATA_H
diff --git a/ndn-cpp/error.h b/ndn-cpp/error.h
new file mode 100644
index 0000000..d6379c6
--- /dev/null
+++ b/ndn-cpp/error.h
@@ -0,0 +1,119 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+/**
+ * @file error.h
+ * @brief This file defines basic elements for the library reporting
+ *
+ * The library throws a number of exceptions.
+ * In general, the following example shows how to print out diagnostic information
+ * when one of the exceptions is thrown
+ * @code
+ * try
+ * {
+ * ... operations with ndn::Name
+ * }
+ * catch (boost::exception &e)
+ * {
+ * std::cerr << boost::diagnostic_information (e) << std::endl;
+ * }
+ * @endcode
+ */
+
+#ifndef NDN_ERROR_H
+#define NDN_ERROR_H
+
+#include <boost/exception/all.hpp>
+
+namespace ndn
+{
+namespace error
+{
+
+struct Error : public virtual boost::exception, public virtual std::exception {}; ///< @brief Some error with error reporting engine
+struct Uri : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with URI processing
+struct StringTransform : public virtual boost::exception, public virtual std::exception {};
+struct Name : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with Name
+namespace name {
+struct Component : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with name::Component
+}
+struct Exclude : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with Exclude
+struct KeyLocator : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with KeyLocator
+namespace wire {
+struct Ccnb : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with wire::Ccnb encoding
+}
+struct Keychain : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with security::Keychain
+
+// Diagnostic information fields
+
+/**
+ * @brief Free-formatted text message explaining the error
+ *
+ * @code
+ * ...
+ * catch (boost::exception &e)
+ * {
+ * if (const std::string *error = boost::get_error_info<error::msg> (e))
+ * ...
+ * }
+ * @endcode
+ *
+ * @see get_msg
+ */
+typedef boost::error_info<struct tag_msg, std::string> msg;
+
+/**
+ * @brief Helper method to get error message from the exception
+ *
+ * Method assumes that message is present, if not, an exception will be thrown
+ */
+inline const std::string &
+get_msg (boost::exception &e)
+{
+ const std::string *error = boost::get_error_info<msg> (e);
+ if (error == 0)
+ BOOST_THROW_EXCEPTION (Error ());
+ return *error;
+}
+
+/**
+ * @brief Report of the position of the error (error-specific meaning)
+ *
+ * @code
+ * ...
+ * catch (boost::exception &e)
+ * {
+ * if (const int *error = boost::get_error_info<error::pos> (e))
+ * ...
+ * }
+ * @endcode
+ *
+ * @see get_pos
+ */
+typedef boost::error_info<struct tag_pos, int> pos;
+
+/**
+ * @brief Helper method to get position of the error from the exception
+ *
+ * Method assumes that position is present, if not, an exception will be thrown
+ */
+inline int
+get_pos (boost::exception &e)
+{
+ const int *position = boost::get_error_info<pos> (e);
+ if (position == 0)
+ BOOST_THROW_EXCEPTION (Error ());
+ return *position;
+}
+
+} // error
+} // ndn
+
+#endif // NDN_ERROR_H
diff --git a/ndn-cpp/face.cc b/ndn-cpp/face.cc
new file mode 100644
index 0000000..455f283
--- /dev/null
+++ b/ndn-cpp/face.cc
@@ -0,0 +1,118 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "face.h"
+
+namespace ndn {
+
+Face::sent_interest
+Face::sendInterest (Ptr<const Interest> interest, const SatisfiedInterestCallback &dataCallback)
+{
+ // magic to be done in child class
+
+ sent_interest item = m_sentInterests.find_exact (interest->getName ());
+ if (item == m_sentInterests.end ())
+ {
+ std::pair<sent_interest, bool> insertRes =
+ m_sentInterests.insert (interest->getName (), Ptr<sent_interest_container::payload_traits::payload_type>::Create ());
+
+ item = insertRes.first;
+ }
+ item->payload ()->push_back (dataCallback);
+
+ return item;
+}
+
+void
+Face::clearInterest (Face::sent_interest interest)
+{
+ m_sentInterests.erase (interest);
+}
+
+Face::registered_prefix
+Face::setInterestFilter (Ptr<const Name> prefix, const ExpectedInterestCallback &interestCallback)
+{
+ // magic to be done in child class
+
+ registered_prefix item = m_registeredPrefixes.find_exact (*prefix);
+ if (item == m_registeredPrefixes.end ())
+ {
+ std::pair<registered_prefix, bool> insertRes =
+ m_registeredPrefixes.insert (*prefix, Ptr<registered_prefix_container::payload_traits::payload_type>::Create ());
+
+ item = insertRes.first;
+ }
+ item->payload ()->push_back (interestCallback);
+
+ return item;
+}
+
+void
+Face::clearInterestFilter (const Name &prefix)
+{
+ registered_prefix item = m_registeredPrefixes.find_exact (prefix);
+ if (item == m_registeredPrefixes.end ())
+ return;
+
+ clearInterestFilter (item);
+}
+
+void
+Face::clearInterestFilter (Face::registered_prefix filter)
+{
+ m_registeredPrefixes.erase (filter);
+}
+
+} // ndn
+
+// void
+// CcnxWrapper::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
+// CcnxFilterEntryContainer<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
+// CcnxWrapper::OnContentObject (const Ptr<const ndn::ContentObject> &contentObject,
+// Ptr<Packet> payload)
+// {
+// ndn::App::OnContentObject (contentObject, payload);
+// NS_LOG_DEBUG ("<< D " << contentObject->GetName ());
+
+// CcnxFilterEntryContainer<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 ())
+// {
+// ostringstream content;
+// payload->CopyData (&content, payload->GetSize ());
+
+// entry->payload ()->m_callback (lexical_cast<string> (contentObject->GetName ()), content.str ().c_str (), content.str ().size ());
+
+// m_dataCallbacks.erase (entry);
+
+// entry = m_dataCallbacks.longest_prefix_match (contentObject->GetName ());
+// }
+// }
+
+// }
diff --git a/ndn-cpp/face.h b/ndn-cpp/face.h
new file mode 100644
index 0000000..38a618c
--- /dev/null
+++ b/ndn-cpp/face.h
@@ -0,0 +1,70 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_CXX_H
+#define NDN_CXX_H
+
+#include "ndn-cpp/common.h"
+#include "ndn-cpp/fields/name.h"
+#include "ndn-cpp/interest.h"
+#include "ndn-cpp/data.h"
+
+#include "trie/trie-with-policy.h"
+#include "trie/policies/counting-policy.h"
+
+#include <list>
+
+namespace ndn {
+
+template<class Callback>
+struct CallbackTable :
+ public trie::trie_with_policy< Name,
+ trie::ptr_payload_traits< std::list<Callback> >,
+ trie::counting_policy_traits >
+{
+};
+
+
+class Face
+{
+public:
+ typedef boost::function<void (Ptr<Data> incomingData, Ptr<const Interest> satisfiedInterest)> SatisfiedInterestCallback;
+ typedef boost::function<void (Ptr<Interest> incomingInterest, Ptr<const Name> registeredPrefix)> ExpectedInterestCallback;
+
+ // some internal definitions
+ typedef CallbackTable< SatisfiedInterestCallback > sent_interest_container;
+ typedef CallbackTable< ExpectedInterestCallback > registered_prefix_container;
+
+ typedef sent_interest_container::iterator sent_interest;
+ typedef registered_prefix_container::iterator registered_prefix;
+
+ sent_interest
+ sendInterest (Ptr<const Interest> interest, const SatisfiedInterestCallback &dataCallback);
+
+ void
+ clearInterest (sent_interest interest);
+
+ registered_prefix
+ setInterestFilter (Ptr<const Name> prefix, const ExpectedInterestCallback &interestCallback);
+
+ void
+ clearInterestFilter (const Name &prefix);
+
+ void
+ clearInterestFilter (registered_prefix filter);
+
+private:
+ sent_interest_container m_sentInterests;
+ registered_prefix_container m_registeredPrefixes;
+};
+
+} // ndn
+
+#endif // NDN_CXX_H
diff --git a/ndn-cpp/fields/blob.h b/ndn-cpp/fields/blob.h
new file mode 100644
index 0000000..85fd71b
--- /dev/null
+++ b/ndn-cpp/fields/blob.h
@@ -0,0 +1,60 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_BLOB_H
+#define NDN_BLOB_H
+
+#include <vector>
+#include <cstddef>
+
+namespace ndn {
+
+/**
+ * @brief Class representing a general-use binary blob
+ */
+class Blob : public std::vector<char>
+{
+public:
+ /**
+ * @brief Creates an empty blob
+ */
+ Blob ()
+ {
+ }
+
+ Blob (const void *buf, size_t length)
+ : std::vector<char> (reinterpret_cast<const char*> (buf), reinterpret_cast<const char*> (buf) + length)
+ {
+ }
+
+ /**
+ * @brief Get pointer to the first byte of the binary blob
+ */
+ inline char*
+ buf ()
+ {
+ return &front ();
+ }
+
+ /**
+ * @brief Get const pointer to the first byte of the binary blob
+ */
+ inline const char*
+ buf () const
+ {
+ return &front ();
+ }
+};
+
+} // ndn
+
+#endif // NDN_BLOB_H
diff --git a/ndn-cpp/fields/content.cc b/ndn-cpp/fields/content.cc
new file mode 100644
index 0000000..0216bd2
--- /dev/null
+++ b/ndn-cpp/fields/content.cc
@@ -0,0 +1,53 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "content.h"
+#include "ndn-cpp/error.h"
+
+namespace ndn
+{
+
+const name::Component Content::noFinalBlock = name::Component ();
+const TimeInterval Content::noFreshness;
+const TimeInterval Content::maxFreshness = time::Seconds (2147);
+
+Content::Content ()
+{
+}
+
+Content::Content (const void *buffer, size_t size,
+ const Time ×tamp,
+ Type type/* = DATA*/,
+ const TimeInterval &freshness/* = maxFreshness*/,
+ const name::Component &finalBlock/* = noFinalBlock*/)
+ : m_timestamp (timestamp)
+ , m_type (type)
+ , m_freshness (freshness)
+ , m_finalBlockId (finalBlock)
+
+ , m_content (buffer, size)
+{
+}
+
+Content::Content (const void *buffer, size_t size,
+ Type type/* = DATA*/,
+ const TimeInterval &freshness/* = maxFreshness*/,
+ const name::Component &finalBlock/* = noFinalBlock*/)
+ : m_timestamp (time::Now ())
+ , m_type (type)
+ , m_freshness (freshness)
+ , m_finalBlockId (finalBlock)
+
+ , m_content (buffer, size)
+{
+}
+
+
+} // ndn
diff --git a/ndn-cpp/fields/content.h b/ndn-cpp/fields/content.h
new file mode 100644
index 0000000..752f24c
--- /dev/null
+++ b/ndn-cpp/fields/content.h
@@ -0,0 +1,306 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_CONTENT_H
+#define NDN_CONTENT_H
+
+#include "ndn-cpp/common.h"
+#include "ndn-cpp/fields/blob.h"
+#include "ndn-cpp/fields/name-component.h"
+
+namespace ndn {
+
+/**
+ * @brief Class providing an interface to work with content NDN data packets
+ *
+ * Content of data packets consists of two parts: information about content
+ * (timestamp, freshness, type, etc.) and raw content itself
+ *
+ * @code
+ * Content ::= ContentInfo
+ * ContentData
+ *
+ * ContentInfo ::= Timestamp
+ * Type?
+ * Freshness?
+ * FinalBlockID?
+ * @endcode
+ */
+class Content
+{
+public:
+ /**
+ * @brief Enum of content types
+ */
+ enum Type
+ {
+ DATA = 0, ///< @brief No semantics is defined for the content
+ ENCR, ///< @brief Indicate that the content is encrypted
+ GONE, ///< @brief ?
+ KEY, ///< @brief Content is a key object
+ LINK, ///< @brief Content contains a LINK object
+ NACK ///< @brief Negative acknowledgment by the content producer, indicating that there is no data with the requested name
+ };
+
+ /**
+ * @brief Create an empty content
+ */
+ Content ();
+
+ /**
+ * @brief Create a content from a memory buffer
+ * @param buffer pointer to first byte of the memory buffer
+ * @param size size of the memory buffer
+ * @param timestamp content generation timestamp
+ * @param type type of content (default is Content::DATA)
+ * @param freshness amount of time the content is considered "fresh" (default is 2147 seconds, maximum possible value for CCNx)
+ * @param finalBlock name of the final DATA
+ *
+ * Use the other version of the constructor, if timestamp needs to be automatically generated
+ */
+ Content (const void *buffer, size_t size,
+ const Time ×tamp,
+ Type type = DATA,
+ const TimeInterval &freshness = maxFreshness,
+ const name::Component &finalBlock = noFinalBlock);
+
+ /**
+ * @brief Create a content from a memory buffer
+ * @param buffer pointer to first byte of the memory buffer
+ * @param size size of the memory buffer
+ * @param type type of content (default is Content::DATA)
+ * @param freshness amount of time the content is considered "fresh" (default is 2147 seconds, maximum possible value for CCNx)
+ * @param finalBlock name of the final DATA
+ *
+ * This method automatically sets timestamp of the created content to the current time (UTC clock)
+ */
+ Content (const void *buffer, size_t size,
+ Type type = DATA,
+ const TimeInterval &freshness = maxFreshness,
+ const name::Component &finalBlock = noFinalBlock);
+
+ /**
+ * @brief Get content timestamp (const reference)
+ */
+ inline const Time &
+ getTimestamp () const;
+
+ /**
+ * @brief Get content timestamp (reference)
+ */
+ inline Time &
+ getTimestamp ();
+
+ /**
+ * @brief Set content timestamp
+ * @param timestamp content timestamp (default is empty ptime object)
+ *
+ * If parameter is omitted, then the current time (UTC clock) is used
+ */
+ inline void
+ setTimeStamp (const Time ×tamp = Time ());
+
+ /**
+ * @brief Get type of content
+ */
+ inline Type
+ getType () const;
+
+ /**
+ * @brief Set type of content
+ * @param type content type @see Content::Type
+ */
+ inline void
+ setType (Type type);
+
+ /**
+ * @brief Get content freshness (const reference)
+ */
+ inline const TimeInterval &
+ getFreshness () const;
+
+ /**
+ * @brief Get content freshness (reference)
+ */
+ inline TimeInterval &
+ getFreshness ();
+
+ /**
+ * @brief Set content freshness
+ * @param freshness content freshness (default value is Content::maxFreshness = 2147 seconds)
+ */
+ inline void
+ setFreshness (const TimeInterval &freshness = maxFreshness);
+
+ /**
+ * @brief Get final block ID of the content (const reference)
+ */
+ inline const name::Component &
+ getFinalBlockId () const;
+
+ /**
+ * @brief Get final block ID of the content (reference)
+ */
+ inline name::Component &
+ getFinalBlockId ();
+
+ /**
+ * @brief Set final block ID of the content
+ * @param finalBlock component name of the final block
+ */
+ inline void
+ setFinalBlockId (const name::Component &finalBlock);
+
+ /**
+ * @brief Get const reference to content bits
+ */
+ inline const Blob &
+ getContent () const;
+
+ /**
+ * @brief Get reference to content bits
+ */
+ inline Blob &
+ getContent ();
+
+ /**
+ * @brief Set content bits from blob
+ * @param content blob that holds content bits
+ *
+ * In certain cases, getContent ().swap (content); is more appropriate,
+ * since it would avoid object copying
+ */
+ inline void
+ setContent (const Blob &content);
+
+ /**
+ * @brief Set content bits from memory buffer
+ * @param buf pointer to first byte of memory buffer
+ * @param length size of memory buffer
+ */
+ inline void
+ setContent (const void *buf, size_t length);
+
+public:
+ static const name::Component noFinalBlock; ///< @brief Not a final block == name::Component ()
+ static const TimeInterval noFreshness; ///< @brief Minimum freshness == seconds (0)
+ static const TimeInterval maxFreshness; ///< @brief Maximum freshnes == seconds (2147)
+
+private:
+ // ContentInfo
+ Time m_timestamp;
+ Type m_type;
+ TimeInterval m_freshness;
+ name::Component m_finalBlockId;
+
+ // ContentData
+ Blob m_content;
+};
+
+inline const Time &
+Content::getTimestamp () const
+{
+ return m_timestamp;
+}
+
+inline Time &
+Content::getTimestamp ()
+{
+ return m_timestamp;
+}
+
+inline void
+Content::setTimeStamp (const Time ×tamp/* = Time ()*/)
+{
+ if (timestamp != Time ())
+ {
+ m_timestamp = timestamp;
+ }
+ else
+ {
+ m_timestamp = time::Now ();
+ }
+}
+
+inline Content::Type
+Content::getType () const
+{
+ return m_type;
+}
+
+inline void
+Content::setType (Content::Type type)
+{
+ m_type = type;
+}
+
+inline const TimeInterval &
+Content::getFreshness () const
+{
+ return m_freshness;
+}
+
+inline TimeInterval &
+Content::getFreshness ()
+{
+ return m_freshness;
+}
+
+inline void
+Content::setFreshness (const TimeInterval &freshness/* = Content::maxFreshness*/)
+{
+ m_freshness = freshness;
+}
+
+inline const name::Component &
+Content::getFinalBlockId () const
+{
+ return m_finalBlockId;
+}
+
+inline name::Component &
+Content::getFinalBlockId ()
+{
+ return m_finalBlockId;
+}
+
+inline void
+Content::setFinalBlockId (const name::Component &finalBlock)
+{
+ m_finalBlockId = finalBlock;
+}
+
+inline const Blob &
+Content::getContent () const
+{
+ return m_content;
+}
+
+inline Blob &
+Content::getContent ()
+{
+ return m_content;
+}
+
+inline void
+Content::setContent (const Blob &content)
+{
+ m_content = content;
+}
+
+inline void
+Content::setContent (const void *buf, size_t length)
+{
+ Blob (buf, length).swap (m_content);
+}
+
+} // ndn
+
+#endif // NDN_SIGNATURE_H
diff --git a/ndn-cpp/fields/exclude.cc b/ndn-cpp/fields/exclude.cc
new file mode 100644
index 0000000..b637c01
--- /dev/null
+++ b/ndn-cpp/fields/exclude.cc
@@ -0,0 +1,168 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "exclude.h"
+
+#include "ndn-cpp/error.h"
+
+namespace ndn
+{
+
+Exclude::Exclude ()
+{
+}
+
+// example: ANY /b /d ANY /f
+//
+// ordered in map as:
+//
+// /f (false); /d (true); /b (false); / (true)
+//
+// lower_bound (/) -> / (true) <-- excluded (equal)
+// lower_bound (/a) -> / (true) <-- excluded (any)
+// lower_bound (/b) -> /b (false) <--- excluded (equal)
+// lower_bound (/c) -> /b (false) <--- not excluded (not equal and no ANY)
+// lower_bound (/d) -> /d (true) <- excluded
+// lower_bound (/e) -> /d (true) <- excluded
+bool
+Exclude::isExcluded (const name::Component &comp) const
+{
+ const_iterator lowerBound = m_exclude.lower_bound (comp);
+ if (lowerBound == end ())
+ return false;
+
+ if (lowerBound->second)
+ return true;
+ else
+ return lowerBound->first == comp;
+
+ return false;
+}
+
+Exclude &
+Exclude::excludeOne (const name::Component &comp)
+{
+ if (!isExcluded (comp))
+ {
+ m_exclude.insert (std::make_pair (comp, false));
+ }
+ return *this;
+}
+
+
+// example: ANY /b0 /d0 ANY /f0
+//
+// ordered in map as:
+//
+// /f0 (false); /d0 (true); /b0 (false); / (true)
+//
+// lower_bound (/) -> / (true) <-- excluded (equal)
+// lower_bound (/a0) -> / (true) <-- excluded (any)
+// lower_bound (/b0) -> /b0 (false) <--- excluded (equal)
+// lower_bound (/c0) -> /b0 (false) <--- not excluded (not equal and no ANY)
+// lower_bound (/d0) -> /d0 (true) <- excluded
+// lower_bound (/e0) -> /d0 (true) <- excluded
+
+
+// examples with desired outcomes
+// excludeRange (/, /f0) -> ANY /f0
+// /f0 (false); / (true)
+// excludeRange (/, /f1) -> ANY /f1
+// /f1 (false); / (true)
+// excludeRange (/a0, /e0) -> ANY /f0
+// /f0 (false); / (true)
+// excludeRange (/a0, /e0) -> ANY /f0
+// /f0 (false); / (true)
+
+// excludeRange (/b1, /c0) -> ANY /b0 /b1 ANY /c0 /d0 ANY /f0
+// /f0 (false); /d0 (true); /c0 (false); /b1 (true); /b0 (false); / (true)
+
+Exclude &
+Exclude::excludeRange (const name::Component &from, const name::Component &to)
+{
+ if (from >= to)
+ {
+ BOOST_THROW_EXCEPTION (error::Exclude ()
+ << error::msg ("Invalid exclude range (for single name exclude use Exclude::excludeOne)")
+ << error::msg (from.toUri ())
+ << error::msg (to.toUri ()));
+ }
+
+ iterator newFrom = m_exclude.lower_bound (from);
+ if (newFrom == end () || !newFrom->second /*without ANY*/)
+ {
+ std::pair<iterator, bool> fromResult = m_exclude.insert (std::make_pair (from, true));
+ newFrom = fromResult.first;
+ if (!fromResult.second)
+ {
+ // this means that the lower bound is equal to the item itself. So, just update ANY flag
+ newFrom->second = true;
+ }
+ }
+ // else
+ // nothing special if start of the range already exists with ANY flag set
+
+ iterator newTo = m_exclude.lower_bound (to); // !newTo cannot be end ()
+ if (newTo == newFrom || !newTo->second)
+ {
+ std::pair<iterator, bool> toResult = m_exclude.insert (std::make_pair (to, false));
+ newTo = toResult.first;
+ ++ newTo;
+ }
+ else
+ {
+ // nothing to do really
+ }
+
+ m_exclude.erase (newTo, newFrom); // remove any intermediate node, since all of the are excluded
+
+ return *this;
+}
+
+Exclude &
+Exclude::excludeAfter (const name::Component &from)
+{
+ iterator newFrom = m_exclude.lower_bound (from);
+ if (newFrom == end () || !newFrom->second /*without ANY*/)
+ {
+ std::pair<iterator, bool> fromResult = m_exclude.insert (std::make_pair (from, true));
+ newFrom = fromResult.first;
+ if (!fromResult.second)
+ {
+ // this means that the lower bound is equal to the item itself. So, just update ANY flag
+ newFrom->second = true;
+ }
+ }
+ // else
+ // nothing special if start of the range already exists with ANY flag set
+
+ if (newFrom != m_exclude.begin ())
+ {
+ m_exclude.erase (m_exclude.begin (), newFrom); // remove any intermediate node, since all of the are excluded
+ }
+
+ return *this;
+}
+
+
+std::ostream&
+operator << (std::ostream &os, const Exclude &exclude)
+{
+ for (Exclude::const_reverse_iterator i = exclude.rbegin (); i != exclude.rend (); i++)
+ {
+ os << i->first.toUri () << " ";
+ if (i->second)
+ os << "----> ";
+ }
+ return os;
+}
+
+
+} // ndn
diff --git a/ndn-cpp/fields/exclude.h b/ndn-cpp/fields/exclude.h
new file mode 100644
index 0000000..265a809
--- /dev/null
+++ b/ndn-cpp/fields/exclude.h
@@ -0,0 +1,169 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_EXCLUDE_H
+#define NDN_EXCLUDE_H
+
+#include "ndn-cpp/fields/name-component.h"
+
+#include <map>
+
+namespace ndn {
+
+/**
+ * @brief Class to represent Exclude component in NDN interests
+ */
+class Exclude
+{
+public:
+ typedef std::map< name::Component, bool /*any*/, std::greater<name::Component> > exclude_type;
+
+ typedef exclude_type::iterator iterator;
+ typedef exclude_type::const_iterator const_iterator;
+ typedef exclude_type::reverse_iterator reverse_iterator;
+ typedef exclude_type::const_reverse_iterator const_reverse_iterator;
+
+ /**
+ * @brief Default constructor an empty exclude
+ */
+ Exclude ();
+
+ /**
+ * @brief Check if name component is excluded
+ * @param comp Name component to check against exclude filter
+ */
+ bool
+ isExcluded (const name::Component &comp) const;
+
+ /**
+ * @brief Exclude specific name component
+ * @param comp component to exclude
+ * @returns *this to allow chaining
+ */
+ Exclude &
+ excludeOne (const name::Component &comp);
+
+ /**
+ * @brief Exclude components from range [from, to]
+ * @param from first element of the range
+ * @param to last element of the range
+ * @returns *this to allow chaining
+ */
+ Exclude &
+ excludeRange (const name::Component &from, const name::Component &to);
+
+ /**
+ * @brief Exclude all components from range [/, to]
+ * @param to last element of the range
+ * @returns *this to allow chaining
+ */
+ inline Exclude &
+ excludeBefore (const name::Component &to);
+
+ /**
+ * @brief Exclude all components from range [from, +Inf]
+ * @param to last element of the range
+ * @returns *this to allow chaining
+ */
+ Exclude &
+ excludeAfter (const name::Component &from);
+
+ /**
+ * @brief Method to directly append exclude element
+ * @param name excluded name component
+ * @param any flag indicating if there is a postfix ANY component after the name
+ *
+ * This method is used during conversion from wire format of exclude filter
+ *
+ * If there is an error with ranges (e.g., order of components is wrong) an exception is thrown
+ */
+ void
+ appendExclude (const name::Component &name, bool any);
+
+ /**
+ * @brief Get number of exclude terms
+ */
+ inline size_t
+ size () const;
+
+ /**
+ * @brief Get begin iterator of the exclude terms
+ */
+ inline const_iterator
+ begin () const;
+
+ /**
+ * @brief Get end iterator of the exclude terms
+ */
+ inline const_iterator
+ end () const;
+
+ /**
+ * @brief Get begin iterator of the exclude terms
+ */
+ inline const_reverse_iterator
+ rbegin () const;
+
+ /**
+ * @brief Get end iterator of the exclude terms
+ */
+ inline const_reverse_iterator
+ rend () const;
+
+private:
+ Exclude &
+ excludeRange (iterator fromLowerBound, iterator toLowerBound);
+
+private:
+ exclude_type m_exclude;
+};
+
+std::ostream&
+operator << (std::ostream &os, const Exclude &name);
+
+inline Exclude &
+Exclude::excludeBefore (const name::Component &to)
+{
+ return excludeRange (name::Component (), to);
+}
+
+inline size_t
+Exclude::size () const
+{
+ return m_exclude.size ();
+}
+
+inline Exclude::const_iterator
+Exclude::begin () const
+{
+ return m_exclude.begin ();
+}
+
+inline Exclude::const_iterator
+Exclude::end () const
+{
+ return m_exclude.end ();
+}
+
+inline Exclude::const_reverse_iterator
+Exclude::rbegin () const
+{
+ return m_exclude.rbegin ();
+}
+
+inline Exclude::const_reverse_iterator
+Exclude::rend () const
+{
+ return m_exclude.rend ();
+}
+
+} // ndn
+
+#endif // NDN_EXCLUDE_H
diff --git a/ndn-cpp/fields/key-locator.cc b/ndn-cpp/fields/key-locator.cc
new file mode 100644
index 0000000..84fd60c
--- /dev/null
+++ b/ndn-cpp/fields/key-locator.cc
@@ -0,0 +1,200 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "key-locator.h"
+#include "ndn-cpp/error.h"
+
+namespace ndn {
+
+KeyLocator::KeyLocator ()
+ : m_type (NOTSET)
+ , m_data (0)
+{
+}
+
+KeyLocator::KeyLocator (const KeyLocator &keyLocator)
+ : m_type (keyLocator.getType ())
+ , m_data (0)
+{
+ switch (m_type)
+ {
+ case NOTSET:
+ break;
+ case KEY:
+ m_data = new Blob (keyLocator.getKey ());
+ break;
+ case CERTIFICATE:
+ m_data = new Blob (keyLocator.getCertificate ());
+ break;
+ case KEYNAME:
+ m_data = new Name (keyLocator.getKeyName ());
+ break;
+ }
+}
+
+KeyLocator::~KeyLocator ()
+{
+ deleteData ();
+}
+
+KeyLocator &
+KeyLocator::operator = (const KeyLocator &keyLocator)
+{
+ if (this == &keyLocator)
+ return *this;
+
+ deleteData ();
+ m_type = keyLocator.getType ();
+
+ switch (m_type)
+ {
+ case NOTSET:
+ break;
+ case KEY:
+ m_data = new Blob (keyLocator.getKey ());
+ break;
+ case CERTIFICATE:
+ m_data = new Blob (keyLocator.getCertificate ());
+ break;
+ case KEYNAME:
+ m_data = new Name (keyLocator.getKeyName ());
+ break;
+ }
+
+ return *this;
+}
+
+void
+KeyLocator::deleteData ()
+{
+ switch (m_type)
+ {
+ case NOTSET: // nothing to clean up
+ break;
+ case KEY:
+ delete reinterpret_cast<Blob*> (m_data);
+ break;
+ case CERTIFICATE:
+ delete reinterpret_cast<Blob*> (m_data);
+ break;
+ case KEYNAME:
+ delete reinterpret_cast<Name*> (m_data);
+ break;
+ }
+}
+
+void
+KeyLocator::setType (KeyLocator::Type type)
+{
+ if (m_type == type)
+ return;
+
+ deleteData ();
+ m_type = type;
+
+ switch (m_type)
+ {
+ case NOTSET:
+ m_data = 0;
+ case KEY:
+ m_data = new Blob;
+ break;
+ case CERTIFICATE:
+ m_data = new Blob;
+ break;
+ case KEYNAME:
+ m_data = new Name;
+ break;
+ }
+}
+
+const Blob &
+KeyLocator::getKey () const
+{
+ if (m_type != KEY)
+ BOOST_THROW_EXCEPTION (error::KeyLocator ()
+ << error::msg ("getKey called, but KeyLocator is not of type KeyLocator::KEY"));
+ return *reinterpret_cast<const Blob*> (m_data);
+}
+
+Blob &
+KeyLocator::getKey ()
+{
+ if (m_type != KEY)
+ BOOST_THROW_EXCEPTION (error::KeyLocator ()
+ << error::msg ("getKey called, but KeyLocator is not of type KeyLocator::KEY"));
+ return *reinterpret_cast<Blob*> (m_data);
+}
+
+void
+KeyLocator::setKey (const Blob &key)
+{
+ if (m_type != KEY)
+ BOOST_THROW_EXCEPTION (error::KeyLocator ()
+ << error::msg ("setKey called, but KeyLocator is not of type KeyLocator::KEY"));
+ *reinterpret_cast<Blob*> (m_data) = key;
+}
+
+const Blob &
+KeyLocator::getCertificate () const
+{
+ if (m_type != CERTIFICATE)
+ BOOST_THROW_EXCEPTION (error::KeyLocator ()
+ << error::msg ("getCertificate called, but KeyLocator is not of type KeyLocator::CERTIFICATE"));
+ return *reinterpret_cast<const Blob*> (m_data);
+}
+
+Blob &
+KeyLocator::getCertificate ()
+{
+ if (m_type != CERTIFICATE)
+ BOOST_THROW_EXCEPTION (error::KeyLocator ()
+ << error::msg ("getCertificate called, but KeyLocator is not of type KeyLocator::CERTIFICATE"));
+ return *reinterpret_cast<Blob*> (m_data);
+}
+
+void
+KeyLocator::setCertificate (const Blob &certificate)
+{
+ if (m_type != CERTIFICATE)
+ BOOST_THROW_EXCEPTION (error::KeyLocator ()
+ << error::msg ("setCertificate called, but KeyLocator is not of type KeyLocator::CERTIFICATE"));
+ *reinterpret_cast<Blob*> (m_data) = certificate;
+}
+
+const Name &
+KeyLocator::getKeyName () const
+{
+ if (m_type != KEYNAME)
+ BOOST_THROW_EXCEPTION (error::KeyLocator ()
+ << error::msg ("getKeyName called, but KeyLocator is not of type KeyLocator::KEYNAME"));
+ return *reinterpret_cast<const Name*> (m_data);
+}
+
+Name &
+KeyLocator::getKeyName ()
+{
+ if (m_type != KEYNAME)
+ BOOST_THROW_EXCEPTION (error::KeyLocator ()
+ << error::msg ("getKeyName called, but KeyLocator is not of type KeyLocator::KEYNAME"));
+ return *reinterpret_cast<Name*> (m_data);
+}
+
+
+void
+KeyLocator::setKeyName (const Name &name)
+{
+ if (m_type != KEYNAME)
+ BOOST_THROW_EXCEPTION (error::KeyLocator ()
+ << error::msg ("setKeyName called, but KeyLocator is not of type KeyLocator::KEYNAME"));
+ *reinterpret_cast<Name*> (m_data) = name;
+}
+
+}
diff --git a/ndn-cpp/fields/key-locator.h b/ndn-cpp/fields/key-locator.h
new file mode 100644
index 0000000..cf1c6ea
--- /dev/null
+++ b/ndn-cpp/fields/key-locator.h
@@ -0,0 +1,168 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_KEY_LOCATOR_H
+#define NDN_KEY_LOCATOR_H
+
+#include "ndn-cpp/fields/blob.h"
+#include "ndn-cpp/fields/name.h"
+
+namespace ndn {
+
+/**
+ * @brief Class providing an interface to work with key locators in NDN data packets
+ */
+class KeyLocator
+{
+public:
+ /**
+ * @brief Key locator type
+ *
+ * Key locator can be only of the defined types, i.e., it cannot contain key bits and key name
+ */
+ enum Type
+ {
+ NOTSET=-1, ///< @brief Unset key locator type, any attempt to use KeyLocator of NOTSET type will cause an exception
+ KEY, ///< @brief Key locator contains key bits
+ CERTIFICATE, ///< @brief Key locator contains certificate bits
+ KEYNAME ///< @brief key locator contains name of the key
+ };
+
+ /**
+ * @brief Default constructor
+ */
+ KeyLocator ();
+
+ /**
+ * @brief Copy constructor
+ */
+ KeyLocator (const KeyLocator &keyLocator);
+
+ /**
+ * @brief Destructor
+ */
+ ~KeyLocator ();
+
+ /**
+ * @brief Copy operator
+ */
+ KeyLocator &
+ operator = (const KeyLocator &keyLocator);
+
+ /**
+ * @brief Set type of the key locator
+ * @param type key locator type, @see Type
+ *
+ * If type of the key locator changes, setType will delete any previously allocated
+ * data, allocate appropriate type and store it in m_data
+ */
+ void
+ setType (Type type);
+
+ /**
+ * @brief Get type of the key locator
+ */
+ inline Type
+ getType () const;
+
+ /**
+ * @brief Get const reference to key bits, associated with key locator
+ *
+ * If key locator type is not KEY, then an exception will be thrown
+ */
+ const Blob &
+ getKey () const;
+
+ /**
+ * @brief Get reference to key bits, associated with key locator
+ *
+ * If key locator type is not KEY, then an exception will be thrown
+ */
+ Blob &
+ getKey ();
+
+ /**
+ * @brief Set key bits, associated with key locator
+ * @param key const reference to key bits
+ *
+ * If key locator type is not KEY, then an exception will be thrown
+ */
+ void
+ setKey (const Blob &key);
+
+ /**
+ * @brief Get const reference to certificated bits, associated with key locator
+ *
+ * If key locator type is not CERTIFICATE, then an exception will be thrown
+ */
+ const Blob &
+ getCertificate () const;
+
+ /**
+ * @brief Get reference to certificated bits, associated with key locator
+ *
+ * If key locator type is not CERTIFICATE, then an exception will be thrown
+ */
+ Blob &
+ getCertificate ();
+
+ /**
+ * @brief Set certificated bits, associated with key locator
+ * @param certificate const reference to certificate bits
+ *
+ * If key locator type is not CERTIFICATE, then an exception will be thrown
+ */
+ void
+ setCertificate (const Blob &certificate);
+
+ /**
+ * @brief Get const reference to key name, associated with key locator
+ *
+ * If key locator type is not KEYNAME, then an exception will be thrown
+ */
+ const Name &
+ getKeyName () const;
+
+ /**
+ * @brief Get reference to key name, associated with key locator
+ *
+ * If key locator type is not KEYNAME, then an exception will be thrown
+ */
+ Name &
+ getKeyName ();
+
+ /**
+ * @brief Set key name, associated with key locator
+ * @param name const reference to key name
+ *
+ * If key locator type is not KEYNAME, then an exception will be thrown
+ */
+ void
+ setKeyName (const Name &name);
+
+private:
+ void
+ deleteData ();
+
+private:
+ Type m_type;
+ void *m_data;
+};
+
+inline KeyLocator::Type
+KeyLocator::getType () const
+{
+ return m_type;
+}
+
+
+} // ndn
+
+#endif // NDN_KEY_LOCATOR_H
diff --git a/ndn-cpp/fields/name-component.cc b/ndn-cpp/fields/name-component.cc
new file mode 100644
index 0000000..ecc87b1
--- /dev/null
+++ b/ndn-cpp/fields/name-component.cc
@@ -0,0 +1,170 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "name-component.h"
+
+#include "ndn-cpp/error.h"
+#include "ndn-cpp/helpers/uri.h"
+
+using namespace std;
+
+namespace ndn
+{
+namespace name
+{
+
+Component::Component ()
+{
+}
+
+Component::Component (const std::string &uri)
+{
+ try
+ {
+ Uri::fromEscaped (uri.begin (), uri.end (), back_inserter (*this));
+ }
+ catch (error::Uri &err)
+ {
+ // re-throwing different exception
+ BOOST_THROW_EXCEPTION (error::name::Component ()
+ << error::msg (uri)
+ << error::pos (error::get_pos (err)));
+ }
+}
+
+Component::Component (std::string::const_iterator begin, std::string::const_iterator end)
+{
+ try
+ {
+ Uri::fromEscaped (begin, end, back_inserter (*this));
+ }
+ catch (error::Uri &err)
+ {
+ // re-throwing different exception
+ BOOST_THROW_EXCEPTION (error::name::Component ()
+ << error::msg (string (begin, end))
+ << error::pos (error::get_pos (err)));
+ }
+}
+
+Component::Component (const void *buf, size_t length)
+{
+ copy (static_cast<const char*> (buf),
+ static_cast<const char*> (buf)+length,
+ back_inserter (*this));
+}
+
+int
+Component::compare (const Component &other) const
+{
+ if (size () < other.size ())
+ return -1;
+
+ if (size () > other.size ())
+ return +1;
+
+ // now we know that sizes are equal
+
+ pair<const_iterator, const_iterator> diff = mismatch (begin (), end (), other.begin ());
+ if (diff.first == end ()) // components are actually equal
+ return 0;
+
+ return (std::lexicographical_compare (diff.first, end (), diff.second, other.end ())) ? -1 : +1;
+}
+
+Component
+Component::fromNumber (uint64_t number)
+{
+ Component comp;
+ while (number > 0)
+ {
+ comp.push_back (static_cast<unsigned char> (number & 0xFF));
+ number >>= 8;
+ }
+ std::reverse (comp.begin (), comp.end ());
+ return comp;
+}
+
+Component
+Component::fromNumberWithMarker (uint64_t number, unsigned char marker)
+{
+ Component comp;
+ comp.push_back (marker);
+
+ while (number > 0)
+ {
+ comp.push_back (static_cast<unsigned char> (number & 0xFF));
+ number >>= 8;
+ }
+
+ std::reverse (comp.begin () + 1, comp.end ());
+ return comp;
+}
+
+std::string
+Component::toBlob () const
+{
+ return std::string (begin (), end ());
+}
+
+void
+Component::toBlob (std::ostream &os) const
+{
+ os.write (buf (), size ());
+}
+
+std::string
+Component::toUri () const
+{
+ ostringstream os;
+ toUri (os);
+ return os.str ();
+}
+
+void
+Component::toUri (std::ostream &os) const
+{
+ Uri::toEscaped (begin (), end (), ostream_iterator<char> (os));
+}
+
+uint64_t
+Component::toNumber () const
+{
+ uint64_t ret = 0;
+ for (const_iterator i = begin (); i != end (); i++)
+ {
+ ret <<= 8;
+ ret |= static_cast<unsigned char> (*i);
+ }
+ return ret;
+}
+
+uint64_t
+Component::toNumberWithMarker (unsigned char marker) const
+{
+ if (empty () ||
+ static_cast<unsigned char> (*(begin ())) != marker)
+ {
+ BOOST_THROW_EXCEPTION (error::name::Component ()
+ << error::msg ("Name component does not have required marker [" + toUri () + "]"));
+ }
+
+ uint64_t ret = 0;
+ for (const_iterator i = begin () + 1; i != end (); i++)
+ {
+ ret <<= 8;
+ ret |= static_cast<unsigned char> (*i);
+ }
+ return ret;
+}
+
+
+} // name
+} // ndn
diff --git a/ndn-cpp/fields/name-component.h b/ndn-cpp/fields/name-component.h
new file mode 100644
index 0000000..0bcf862
--- /dev/null
+++ b/ndn-cpp/fields/name-component.h
@@ -0,0 +1,282 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_NAME_COMPONENT_H
+#define NDN_NAME_COMPONENT_H
+
+#include <string>
+#include <vector>
+
+#include "blob.h"
+#include <stdint.h>
+
+namespace ndn {
+
+namespace name {
+
+/**
+ * @brief Class to representing binary blob of NDN name component
+ *
+ * This class is based on std::vector<char> and just provides several helpers
+ * to work with name components, as well as operator to apply canonical
+ * ordering on name components
+ */
+class Component : public Blob
+{
+public:
+ /**
+ * @brief Default constructor an empty exclude
+ */
+ Component ();
+
+ /**
+ * @brief Create component from URI encoded string
+ * @param uri URI encoded name component (convert escaped with % characters)
+ */
+ Component (const std::string &uri);
+
+ /**
+ * @brief Create component from URI encoded string, with string specified by a pair of iterators
+ * @param begin begin iterator pointing to the URI encoded name
+ * @param end end iterator
+ */
+ Component (std::string::const_iterator begin, std::string::const_iterator end);
+
+ /**
+ * @brief Create component using a binary blob
+ * @param buf pointer to first byte of binary blob to store as a name component
+ * @param length length of the binary blob
+ */
+ Component (const void *buf, size_t length);
+
+ /**
+ * @brief Apply canonical ordering on component comparison
+ * @return 0 They compare equal
+ * <0 If *this comes before other in the canonical ordering
+ * >0 If *this comes after in the canonical ordering
+ *
+ * @see http://www.ccnx.org/releases/latest/doc/technical/CanonicalOrder.html
+ */
+ int
+ compare (const Component &other) const;
+
+ /**
+ * @brief Apply canonical ordering on component comparison (less or equal)
+ *
+ * @see http://www.ccnx.org/releases/latest/doc/technical/CanonicalOrder.html
+ */
+ inline bool
+ operator <= (const Component &other) const;
+
+ /**
+ * @brief Apply canonical ordering on component comparison (less)
+ *
+ * @see http://www.ccnx.org/releases/latest/doc/technical/CanonicalOrder.html
+ */
+ inline bool
+ operator < (const Component &other) const;
+
+ /**
+ * @brief Apply canonical ordering on component comparison (greater or equal)
+ *
+ * @see http://www.ccnx.org/releases/latest/doc/technical/CanonicalOrder.html
+ */
+ inline bool
+ operator >= (const Component &other) const;
+
+ /**
+ * @brief Apply canonical ordering on component comparison (greater)
+ *
+ * @see http://www.ccnx.org/releases/latest/doc/technical/CanonicalOrder.html
+ */
+ inline bool
+ operator > (const Component &other) const;
+
+ ////////////////////////////////////
+ // Component construction helpers //
+ ////////////////////////////////////
+
+ /**
+ * @brief Create network-ordered numeric component
+ *
+ * @param number number to be encoded and added as a component
+ *
+ * Number is encoded and added in network order. Tail zero-bytes are not included.
+ * For example, if the number is 1, then 1-byte binary blob will be added 0x01.
+ * If the number is 256, then 2 binary blob will be added: 0x01 0x01
+ *
+ * If the number is zero, an empty component will be created
+ */
+ static Component
+ fromNumber (uint64_t number);
+
+ /**
+ * @brief Create network-ordered numeric component to the name with marker
+ *
+ * @param number number to be encoded and added as a component
+ * @param marker byte marker, specified by the desired naming convention
+ *
+ * Currently defined naming conventions of the marker:
+ * - 0x00 sequence number
+ * - 0xC1 control number
+ * - 0xFB block id
+ * - 0xFD version number
+ *
+ * This version is almost exactly as appendNumber, with exception that it adds initial marker.
+ * The number is formatted in the exactly the same way.
+ *
+ * @see fromNumber
+ */
+ static Component
+ fromNumberWithMarker (uint64_t number, unsigned char marker);
+
+ //////////////////////////////////////////////////////////////////////////////////
+ // helpers
+ //////////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * @brief Convert binary blob name component to std::string (no conversion is made)
+ * @param comp name component to be converted
+ * @see asUriString
+ */
+ std::string
+ toBlob () const;
+
+ /**
+ * @brief Write blob of the name component to the specified output stream
+ * @param os output stream
+ */
+ void
+ toBlob (std::ostream &os) const;
+
+ /**
+ * @brief Convert binary blob name component to std::string, escaping all non-printable characters in URI format
+ * @param comp name component to be converted
+ * @see asString
+ */
+ std::string
+ toUri () const;
+
+ /**
+ * @brief Write name as URI to the specified output stream
+ * @param os output stream
+ */
+ void
+ toUri (std::ostream &os) const;
+
+ /**
+ * @brief Convert binary blob name component (network-ordered number) to number
+ * @param comp name component to be converted
+ */
+ uint64_t
+ toNumber () const;
+
+ /**
+ * @brief Convert binary blob name component (network-ordered number) to number, using appropriate marker from the naming convention
+ * @param comp name component to be converted
+ * @param marker required marker from the naming convention
+ *
+ * If the required marker does not exist, an exception will be thrown
+ */
+ uint64_t
+ toNumberWithMarker (unsigned char marker) const;
+
+ /**
+ * @brief Convert binary blob name component, assuming sequence number naming convention (marker = 0x00)
+ * @param comp name component to be converted
+ * @see asNumberWithMarker
+ */
+ inline uint64_t
+ toSeqNum () const;
+
+ /**
+ * @brief Convert binary blob name component, assuming control number naming convention (marker = 0xC1)
+ * @param comp name component to be converted
+ * @see asNumberWithMarker
+ */
+ inline uint64_t
+ toControlNum () const;
+
+ /**
+ * @brief Convert binary blob name component, assuming block ID naming convention (marker = 0xFB)
+ * @param comp name component to be converted
+ * @see asNumberWithMarker
+ */
+ inline uint64_t
+ toBlkId () const;
+
+ /**
+ * @brief Convert binary blob name component, assuming time-stamping version naming convention (marker = 0xFD)
+ * @param comp name component to be converted
+ * @see asNumberWithMarker
+ */
+ inline uint64_t
+ toVersion () const;
+};
+
+bool
+Component::operator <= (const Component &other) const
+{
+ return (compare (other) <= 0);
+}
+
+bool
+Component::operator < (const Component &other) const
+{
+ return (compare (other) < 0);
+}
+
+bool
+Component::operator >= (const Component &other) const
+{
+ return (compare (other) >= 0);
+}
+
+bool
+Component::operator > (const Component &other) const
+{
+ return (compare (other) > 0);
+}
+
+inline uint64_t
+Component::toSeqNum () const
+{
+ return toNumberWithMarker (0x00);
+}
+
+inline uint64_t
+Component::toControlNum () const
+{
+ return toNumberWithMarker (0xC1);
+}
+
+inline uint64_t
+Component::toBlkId () const
+{
+ return toNumberWithMarker (0xFB);
+}
+
+inline uint64_t
+Component::toVersion () const
+{
+ return toNumberWithMarker (0xFD);
+}
+
+/**
+ * @brief Stream output operator (output in escaped URI format)
+ */
+std::ostream&
+operator <<(std::ostream &os, const Component &name);
+
+} // name
+
+} // ndn
+
+#endif // NDN_EXCLUDE_H
diff --git a/ndn-cpp/fields/name.cc b/ndn-cpp/fields/name.cc
new file mode 100644
index 0000000..d3e2ec6
--- /dev/null
+++ b/ndn-cpp/fields/name.cc
@@ -0,0 +1,264 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#include "name.h"
+
+#include "ndn-cpp/error.h"
+#include <boost/algorithm/string.hpp>
+
+#include <ctype.h>
+
+using namespace std;
+
+namespace ndn
+{
+
+///////////////////////////////////////////////////////////////////////////////
+// CONSTRUCTORS //
+///////////////////////////////////////////////////////////////////////////////
+
+Name::Name ()
+{
+}
+
+Name::Name (const string &uri)
+{
+ string::const_iterator i = uri.begin ();
+ string::const_iterator end = uri.end ();
+
+ string::const_iterator firstSlash = std::find (i, end, '/');
+ if (firstSlash == end)
+ {
+ BOOST_THROW_EXCEPTION (error::Name ()
+ << error::msg ("Name should include at least one slash (did you forget to specify initial /?)"));
+ }
+
+ if (firstSlash != i)
+ {
+ string schema (i, firstSlash);
+ if (*schema.rbegin () != ':')
+ {
+ BOOST_THROW_EXCEPTION (error::Name ()
+ << error::msg ("First component of the name does not start with a slash (did you forget to specify initial /?)"));
+ }
+
+ i = firstSlash;
+
+ if (!boost::iequals (schema, "ccnx:") &&
+ !boost::iequals (schema, "ndn:"))
+ {
+ BOOST_THROW_EXCEPTION (error::Name ()
+ << error::msg ("URI schema is not supported (only ccnx: or ndn: is allowed)")
+ << error::msg (schema));
+ }
+ }
+
+ string::const_iterator secondSlash = i;
+ secondSlash ++;
+ if (secondSlash != end && *secondSlash == '/')
+ {
+ // The authority component (the part after the initial "//" in the familiar http and ftp URI schemes) is present,
+ // but it is not relevant to NDN name.
+ // skipping it
+ secondSlash ++;
+ i = std::find (secondSlash, end, '/');
+ }
+
+ if (i == end)
+ {
+ BOOST_THROW_EXCEPTION (error::Name ()
+ << error::msg ("Invalid URI")
+ << error::msg (uri));
+ }
+
+ while (i != end)
+ {
+ // skip any extra slashes
+ while (i != end && *i == '/')
+ {
+ i ++;
+ }
+ if (i == end)
+ break;
+
+ string::const_iterator endOfComponent = std::find (i, end, '/');
+ append (name::Component (i, endOfComponent));
+
+ i = endOfComponent;
+ }
+}
+
+Name::Name (const Name &other)
+{
+ m_comps = other.m_comps;
+}
+
+Name &
+Name::operator= (const Name &other)
+{
+ m_comps = other.m_comps;
+ return *this;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// SETTERS //
+///////////////////////////////////////////////////////////////////////////////
+
+Name &
+Name::appendVersion (uint64_t version/* = Name::nversion*/)
+{
+ if (version != Name::nversion)
+ return appendNumberWithMarker (version, 0xFD);
+ else
+ {
+ TimeInterval now = time::NowUnixTimestamp ();
+ version = (now.total_seconds () << 12) | (0xFFF & (now.fractional_seconds () / 244 /*( 1000,000 microseconds / 4096.0 resolution = last 12 bits)*/));
+ return appendNumberWithMarker (version, 0xFD);
+ }
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+// GETTERS //
+///////////////////////////////////////////////////////////////////////////////
+
+const name::Component &
+Name::get (int index) const
+{
+ if (index < 0)
+ {
+ index = size () - (-index);
+ }
+
+ if (static_cast<unsigned int> (index) >= size ())
+ {
+ BOOST_THROW_EXCEPTION (error::Name ()
+ << error::msg ("Index out of range")
+ << error::pos (index));
+ }
+ return m_comps [index];
+}
+
+name::Component &
+Name::get (int index)
+{
+ if (index < 0)
+ {
+ index = size () - (-index);
+ }
+
+ if (static_cast<unsigned int> (index) >= size())
+ {
+ BOOST_THROW_EXCEPTION (error::Name ()
+ << error::msg ("Index out of range")
+ << error::pos (index));
+ }
+ return m_comps [index];
+}
+
+
+/////
+///// Static helpers to convert name component to appropriate value
+/////
+
+
+Name
+Name::getSubName (size_t pos/* = 0*/, size_t len/* = Name::npos*/) const
+{
+ Name retval;
+
+ if (len == npos)
+ {
+ len = size () - pos;
+ }
+
+ if (pos + len > size ())
+ {
+ BOOST_THROW_EXCEPTION (error::Name ()
+ << error::msg ("getSubName parameter out of range")
+ << error::pos (pos)
+ << error::pos (len));
+ }
+
+ for (size_t i = pos; i < pos + len; i++)
+ {
+ retval.append (get (i));
+ }
+
+ return retval;
+}
+
+Name
+Name::operator+ (const Name &name) const
+{
+ Name newName;
+ newName
+ .append (*this)
+ .append (name);
+
+ return newName;
+}
+
+std::string
+Name::toUri () const
+{
+ ostringstream os;
+ toUri (os);
+ return os.str ();
+}
+
+void
+Name::toUri (std::ostream &os) const
+{
+ for (Name::const_iterator comp = begin (); comp != end (); comp++)
+ {
+ os << "/";
+ comp->toUri (os);
+ }
+ if (size () == 0)
+ os << "/";
+}
+
+// ostream &
+// operator << (ostream &os, const Name &name)
+// {
+// for (Name::const_iterator comp = name.begin (); comp != name.end (); comp++)
+// {
+// os << "/" << *comp;
+// }
+// if (name.size () == 0)
+// os << "/";
+// return os;
+// }
+
+int
+Name::compare (const Name &name) const
+{
+ Name::const_iterator i = this->begin ();
+ Name::const_iterator j = name.begin ();
+
+ for (; i != this->end () && j != name.end (); i++, j++)
+ {
+ int res = i->compare (*j);
+ if (res == 0)
+ continue;
+ else
+ return res;
+ }
+
+ if (i == this->end () && j == name.end ())
+ return 0; // prefixes are equal
+
+ return (i == this->end ()) ? -1 : +1;
+}
+
+} // ndn
diff --git a/ndn-cpp/fields/name.h b/ndn-cpp/fields/name.h
new file mode 100644
index 0000000..18e2412
--- /dev/null
+++ b/ndn-cpp/fields/name.h
@@ -0,0 +1,637 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#ifndef NDN_NAME_H
+#define NDN_NAME_H
+
+#include "ndn-cpp/fields/name-component.h"
+#include "ndn-cpp/common.h"
+
+namespace ndn {
+
+/**
+ * @brief Class for NDN Name
+ */
+class Name
+{
+public:
+ typedef std::vector<name::Component>::iterator iterator;
+ typedef std::vector<name::Component>::const_iterator const_iterator;
+ typedef std::vector<name::Component>::reverse_iterator reverse_iterator;
+ typedef std::vector<name::Component>::const_reverse_iterator const_reverse_iterator;
+ typedef std::vector<name::Component>::reference reference;
+ typedef std::vector<name::Component>::const_reference const_reference;
+
+ typedef name::Component partial_type;
+
+ ///////////////////////////////////////////////////////////////////////////////
+ // CONSTRUCTORS //
+ ///////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * @brief Default constructor to create an empty name (zero components, or "/")
+ */
+ Name ();
+
+ /**
+ * @brief Copy constructor
+ *
+ * @param other reference to a NDN name object
+ */
+ Name (const Name &other);
+
+ /**
+ * @brief Create a name from URL string
+ *
+ * @param url URI-represented name
+ */
+ Name (const std::string &url);
+
+ /**
+ * @brief Create a name from a container of elements [begin, end)
+ *
+ * @param begin begin iterator of the container
+ * @param end end iterator of the container
+ */
+ template<class Iterator>
+ Name (Iterator begin, Iterator end);
+
+ /**
+ * @brief Assignment operator
+ */
+ Name &
+ operator= (const Name &other);
+
+
+ ///////////////////////////////////////////////////////////////////////////////
+ // SETTERS //
+ ///////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * @brief Append a binary blob as a name component
+ *
+ * @param comp a binary blob
+ * @returns reference to self (to allow chaining of append methods)
+ */
+ inline Name &
+ append (const name::Component &comp);
+
+ /**
+ * @brief Append a binary blob as a name component
+ * @param comp a binary blob
+ *
+ * This version is a little bit more efficient, since it swaps contents of comp and newly added component
+ *
+ * Attention!!! This method has an intended side effect: content of comp becomes empty
+ */
+ inline Name &
+ appendBySwap (name::Component &comp);
+
+ /**
+ * @brief Append components a container of elements [begin, end)
+ *
+ * @param begin begin iterator of the container
+ * @param end end iterator of the container
+ * @returns reference to self (to allow chaining of append methods)
+ */
+ template<class Iterator>
+ inline Name &
+ append (Iterator begin, Iterator end);
+
+ /**
+ * @brief Append components from another ndn::Name object
+ *
+ * @param comp reference to Name object
+ * @returns reference to self (to allow chaining of append methods)
+ */
+ inline Name &
+ append (const Name &comp);
+
+ /**
+ * @brief Append a string as a name component
+ *
+ * @param compStr a string
+ * @returns reference to self (to allow chaining of append methods)
+ *
+ * No conversions will be done to the string. The string is included in raw form,
+ * without any leading '\0' symbols.
+ */
+ inline Name &
+ append (const std::string &compStr);
+
+ /**
+ * @brief Append a binary blob as a name component
+ *
+ * @param buf pointer to the first byte of the binary blob
+ * @param size length of the binary blob
+ * @returns reference to self (to allow chaining of append methods)
+ */
+ inline Name &
+ append (const void *buf, size_t size);
+
+ /**
+ * @brief Append network-ordered numeric component to the name
+ *
+ * @param number number to be encoded and added as a component
+ *
+ * Number is encoded and added in network order. Tail zero-bytes are not included.
+ * For example, if the number is 1, then 1-byte binary blob will be added 0x01.
+ * If the number is 256, then 2 binary blob will be added: 0x01 0x01
+ *
+ * If the number is zero, an empty component will be added
+ */
+ inline Name &
+ appendNumber (uint64_t number);
+
+ /**
+ * @brief Append network-ordered numeric component to the name with marker
+ *
+ * @param number number to be encoded and added as a component
+ * @param marker byte marker, specified by the desired naming convention
+ *
+ * Currently defined naming conventions of the marker:
+ * - 0x00 sequence number
+ * - 0xC1 control number
+ * - 0xFB block id
+ * - 0xFD version number
+ *
+ * This version is almost exactly as appendNumber, with exception that it adds initial marker.
+ * The number is formatted in the exactly the same way.
+ *
+ * @see appendNumber
+ */
+ inline Name &
+ appendNumberWithMarker (uint64_t number, unsigned char marker);
+
+ /**
+ * @brief Helper method to add sequence number to the name (marker = 0x00)
+ * @param seqno sequence number
+ * @see appendNumberWithMarker
+ */
+ inline Name &
+ appendSeqNum (uint64_t seqno);
+
+ /**
+ * @brief Helper method to add control number to the name (marker = 0xC1)
+ * @param control control number
+ * @see appendNumberWithMarker
+ */
+ inline Name &
+ appendControlNum (uint64_t control);
+
+ /**
+ * @brief Helper method to add block ID to the name (marker = 0xFB)
+ * @param blkid block ID
+ * @see appendNumberWithMarker
+ */
+ inline Name &
+ appendBlkId (uint64_t blkid);
+
+ /**
+ * @brief Helper method to add version to the name (marker = 0xFD)
+ * @param version fully formatted version in a desired format (e.g., timestamp).
+ * If version is Name::nversion, then the version number is automatically
+ * assigned based on UTC timestamp
+ * @see appendNumberWithMarker
+ */
+ Name &
+ appendVersion (uint64_t version = Name::nversion);
+
+ ///////////////////////////////////////////////////////////////////////////////
+ // GETTERS //
+ ///////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * @brief Get number of the name components
+ * @return number of name components
+ */
+ inline size_t
+ size () const;
+
+ /**
+ * @brief Get binary blob of name component
+ * @param index index of the name component. If less than 0, then getting component from the back:
+ * get(-1) getting the last component, get(-2) is getting second component from back, etc.
+ * @returns const reference to binary blob of the requested name component
+ *
+ * If index is out of range, an exception will be thrown
+ */
+ const name::Component &
+ get (int index) const;
+
+ /**
+ * @brief Get binary blob of name component
+ * @param index index of the name component. If less than 0, then getting component from the back
+ * @returns reference to binary blob of the requested name component
+ *
+ * If index is out of range, an exception will be thrown
+ */
+ name::Component &
+ get (int index);
+
+ /////
+ ///// Iterator interface to name components
+ /////
+ inline Name::const_iterator
+ begin () const; ///< @brief Begin iterator (const)
+
+ inline Name::iterator
+ begin (); ///< @brief Begin iterator
+
+ inline Name::const_iterator
+ end () const; ///< @brief End iterator (const)
+
+ inline Name::iterator
+ end (); ///< @brief End iterator
+
+ inline Name::const_reverse_iterator
+ rbegin () const; ///< @brief Reverse begin iterator (const)
+
+ inline Name::reverse_iterator
+ rbegin (); ///< @brief Reverse begin iterator
+
+ inline Name::const_reverse_iterator
+ rend () const; ///< @brief Reverse end iterator (const)
+
+ inline Name::reverse_iterator
+ rend (); ///< @brief Reverse end iterator
+
+
+ /////
+ ///// Static helpers to convert name component to appropriate value
+ /////
+
+ /**
+ * @brief Get a new name, constructed as a subset of components
+ * @param pos Position of the first component to be copied to the subname
+ * @param len Number of components to be copied. Value Name::npos indicates that all components till the end of the name.
+ */
+ Name
+ getSubName (size_t pos = 0, size_t len = npos) const;
+
+ /**
+ * @brief Get prefix of the name
+ * @param len length of the prefix
+ * @param skip number of components to skip from beginning of the name
+ */
+ inline Name
+ getPrefix (size_t len, size_t skip = 0) const;
+
+ /**
+ * @brief Get postfix of the name
+ * @param len length of the postfix
+ * @param skip number of components to skip from end of the name
+ */
+ inline Name
+ getPostfix (size_t len, size_t skip = 0) const;
+
+ /**
+ * @brief Get text representation of the name (URI)
+ */
+ std::string
+ toUri () const;
+
+ /**
+ * @brief Write name as URI to the specified output stream
+ * @param os output stream
+ */
+ void
+ toUri (std::ostream &os) const;
+
+ /////////////////////////////////////////////////
+ // Helpers and compatibility wrappers
+ /////////////////////////////////////////////////
+
+ /**
+ * @brief Compare two names, using canonical ordering for each component
+ * @return 0 They compare equal
+ * <0 If *this comes before other in the canonical ordering
+ * >0 If *this comes after in the canonical ordering
+ */
+ int
+ compare (const Name &name) const;
+
+ /**
+ * @brief Check if to Name objects are equal (have the same number of components with the same binary data)
+ */
+ inline bool
+ operator == (const Name &name) const;
+
+ /**
+ * @brief Check if two Name objects are not equal
+ */
+ inline bool
+ operator != (const Name &name) const;
+
+ /**
+ * @brief Less or equal comparison of two name objects
+ */
+ inline bool
+ operator <= (const Name &name) const;
+
+ /**
+ * @brief Less comparison of two name objects
+ */
+ inline bool
+ operator < (const Name &name) const;
+
+ /**
+ * @brief Great or equal comparison of two name objects
+ */
+ inline bool
+ operator >= (const Name &name) const;
+
+ /**
+ * @brief Great comparison of two name objects
+ */
+ inline bool
+ operator > (const Name &name) const;
+
+ /**
+ * @brief Operator [] to simplify access to name components
+ * @see get
+ */
+ inline name::Component &
+ operator [] (int index);
+
+ /**
+ * @brief Operator [] to simplify access to name components
+ * @see get
+ */
+ inline const name::Component &
+ operator [] (int index) const;
+
+ /**
+ * @brief Create a new Name object, by copying components from first and second name
+ */
+ Name
+ operator + (const Name &name) const;
+
+ /**
+ * @brief A wrapper for append method
+ */
+ template<class T>
+ inline void
+ push_back (const T &comp);
+
+public:
+ // Data Members (public):
+ /// Value returned by various member functions when they fail.
+ const static size_t npos = static_cast<size_t> (-1);
+ const static uint64_t nversion = static_cast<uint64_t> (-1);
+
+private:
+ std::vector<name::Component> m_comps;
+};
+
+typedef boost::shared_ptr<Name> NamePtr;
+
+inline std::ostream &
+operator << (std::ostream &os, const Name &name)
+{
+ name.toUri (os);
+ return os;
+}
+
+
+/////////////////////////////////////////////////////////////////////////////////////
+// Definition of inline methods
+/////////////////////////////////////////////////////////////////////////////////////
+
+template<class Iterator>
+Name::Name (Iterator begin, Iterator end)
+{
+ append (begin, end);
+}
+
+inline Name &
+Name::append (const name::Component &comp)
+{
+ if (comp.size () != 0)
+ m_comps.push_back (comp);
+ return *this;
+}
+
+inline Name &
+Name::appendBySwap (name::Component &comp)
+{
+ if (comp.size () != 0)
+ {
+ Name::iterator newComp = m_comps.insert (end (), name::Component ());
+ newComp->swap (comp);
+ }
+ return *this;
+}
+
+template<class Iterator>
+inline Name &
+Name::append (Iterator begin, Iterator end)
+{
+ for (Iterator i = begin; i != end; i++)
+ {
+ append (*i);
+ }
+ return *this;
+}
+
+Name &
+Name::append (const Name &comp)
+{
+ if (this == &comp)
+ {
+ // have to double-copy if the object is self, otherwise results very frustrating (because we use vector...)
+ return append (Name (comp.begin (), comp.end ()));
+ }
+ return append (comp.begin (), comp.end ());
+}
+
+Name &
+Name::append (const std::string &compStr)
+{
+ name::Component comp (compStr);
+ return appendBySwap (comp);
+}
+
+Name &
+Name::append (const void *buf, size_t size)
+{
+ name::Component comp (buf, size);
+ return appendBySwap (comp);
+}
+
+Name &
+Name::appendNumber (uint64_t number)
+{
+ name::Component comp;
+ name::Component::fromNumber (number).swap (comp);
+ return appendBySwap (comp);
+}
+
+Name &
+Name::appendNumberWithMarker (uint64_t number, unsigned char marker)
+{
+ name::Component comp;
+ name::Component::fromNumberWithMarker (number, marker).swap (comp);
+ return appendBySwap (comp);
+}
+
+inline Name &
+Name::appendSeqNum (uint64_t seqno)
+{
+ return appendNumberWithMarker (seqno, 0x00);
+}
+
+inline Name &
+Name::appendControlNum (uint64_t control)
+{
+ return appendNumberWithMarker (control, 0xC1);
+}
+
+inline Name &
+Name::appendBlkId (uint64_t blkid)
+{
+ return appendNumberWithMarker (blkid, 0xFB);
+}
+
+inline size_t
+Name::size () const
+{
+ return m_comps.size ();
+}
+
+/////
+///// Iterator interface to name components
+/////
+inline Name::const_iterator
+Name::begin () const
+{
+ return m_comps.begin ();
+}
+
+inline Name::iterator
+Name::begin ()
+{
+ return m_comps.begin ();
+}
+
+inline Name::const_iterator
+Name::end () const
+{
+ return m_comps.end ();
+}
+
+inline Name::iterator
+Name::end ()
+{
+ return m_comps.end ();
+}
+
+inline Name::const_reverse_iterator
+Name::rbegin () const
+{
+ return m_comps.rbegin ();
+}
+
+inline Name::reverse_iterator
+Name::rbegin ()
+{
+ return m_comps.rbegin ();
+}
+
+inline Name::const_reverse_iterator
+Name::rend () const
+{
+ return m_comps.rend ();
+}
+
+
+inline Name::reverse_iterator
+Name::rend ()
+{
+ return m_comps.rend ();
+}
+
+
+//// helpers
+
+
+inline Name
+Name::getPrefix (size_t len, size_t skip/* = 0*/) const
+{
+ return getSubName (skip, len);
+}
+
+inline Name
+Name::getPostfix (size_t len, size_t skip/* = 0*/) const
+{
+ return getSubName (size () - len - skip, len);
+}
+
+
+template<class T>
+inline void
+Name::push_back (const T &comp)
+{
+ append (comp);
+}
+
+inline bool
+Name::operator ==(const Name &name) const
+{
+ return (compare (name) == 0);
+}
+
+inline bool
+Name::operator !=(const Name &name) const
+{
+ return (compare (name) != 0);
+}
+
+inline bool
+Name::operator <= (const Name &name) const
+{
+ return (compare (name) <= 0);
+}
+
+inline bool
+Name::operator < (const Name &name) const
+{
+ return (compare (name) < 0);
+}
+
+inline bool
+Name::operator >= (const Name &name) const
+{
+ return (compare (name) >= 0);
+}
+
+inline bool
+Name::operator > (const Name &name) const
+{
+ return (compare (name) > 0);
+}
+
+inline name::Component &
+Name::operator [] (int index)
+{
+ return get (index);
+}
+
+inline const name::Component &
+Name::operator [] (int index) const
+{
+ return get (index);
+}
+
+} // ndn
+
+#endif
diff --git a/ndn-cpp/fields/signature-sha256-with-rsa.cc b/ndn-cpp/fields/signature-sha256-with-rsa.cc
new file mode 100644
index 0000000..c84ef5a
--- /dev/null
+++ b/ndn-cpp/fields/signature-sha256-with-rsa.cc
@@ -0,0 +1,30 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "signature-sha256-with-rsa.h"
+#include "ndn-cpp/wire/base.h"
+
+namespace ndn {
+namespace signature {
+
+const std::string Sha256WithRsa::s_oid = "2.16.840.1.101.3.4.2.1";
+
+Sha256WithRsa::~Sha256WithRsa ()
+{
+}
+
+void
+Sha256WithRsa::doubleDispatch (std::ostream &os, wire::Base &wire, void *userData) const
+{
+ wire.appendSignature (os, *this, userData);
+}
+
+} // signature
+} // ndn
diff --git a/ndn-cpp/fields/signature-sha256-with-rsa.h b/ndn-cpp/fields/signature-sha256-with-rsa.h
new file mode 100644
index 0000000..be6766b
--- /dev/null
+++ b/ndn-cpp/fields/signature-sha256-with-rsa.h
@@ -0,0 +1,172 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_SIGNATURE_SHA256_WITH_RSA_H
+#define NDN_SIGNATURE_SHA256_WITH_RSA_H
+
+#include "signature.h"
+
+#include "ndn-cpp/fields/blob.h"
+#include "ndn-cpp/fields/key-locator.h"
+
+#include <string>
+
+namespace ndn {
+
+namespace signature {
+
+/**
+ * @brief Class providing operations to work with SHA256withRSA (OID: "2.16.840.1.101.3.4.2.1")
+ */
+class Sha256WithRsa : public virtual Signature
+{
+public:
+ /**
+ * @brief Virtual destructor
+ */
+ virtual
+ ~Sha256WithRsa ();
+
+ /**
+ * @brief Get OID of the signature algorithm
+ */
+ inline const std::string &
+ getDigestAlgorithm () const;
+
+ /**
+ * @brief Get reference to signature bits
+ */
+ inline Blob &
+ getSignatureBits ();
+
+ /**
+ * @brief Get const reference to signature bits
+ */
+ inline const Blob &
+ getSignatureBits () const;
+
+ /**
+ * @brief Set signature bits
+ */
+ inline void
+ setSignatureBits (const Blob &signatureBits);
+
+ /**
+ * @brief Get reference to publisher key digest bits
+ */
+ inline Blob &
+ getPublisherKeyDigest ();
+
+ /**
+ * @brief Get const reference to publisher key digest bits
+ */
+ inline const Blob &
+ getPublisherKeyDigest () const;
+
+ /**
+ * @brief Set publisher key digest bits
+ */
+ inline void
+ setPublisherKeyDigest (const Blob &publisherKeyDigest);
+
+ /**
+ * @brief Get reference to key locator object
+ */
+ inline KeyLocator &
+ getKeyLocator ();
+
+ /**
+ * @brief Get const reference to key locator object
+ */
+ inline const KeyLocator &
+ getKeyLocator () const;
+
+ /**
+ * @brief Set key locator object
+ */
+ inline void
+ setKeyLocator (const KeyLocator &keyLocator);
+
+ // from Signature
+ virtual void
+ doubleDispatch (std::ostream &os, wire::Base &wire, void *userData) const;
+
+private:
+ static const std::string s_oid;
+
+ Blob m_signatureBits;
+ Blob m_publisherKeyDigest;
+ KeyLocator m_keyLocator;
+};
+
+const std::string &
+Sha256WithRsa::getDigestAlgorithm () const
+{
+ return s_oid;
+}
+
+inline Blob &
+Sha256WithRsa::getSignatureBits ()
+{
+ return m_signatureBits;
+}
+
+inline const Blob &
+Sha256WithRsa::getSignatureBits () const
+{
+ return m_signatureBits;
+}
+
+inline void
+Sha256WithRsa::setSignatureBits (const Blob &signatureBits)
+{
+ m_signatureBits = signatureBits;
+}
+
+inline Blob &
+Sha256WithRsa::getPublisherKeyDigest ()
+{
+ return m_publisherKeyDigest;
+}
+
+inline const Blob &
+Sha256WithRsa::getPublisherKeyDigest () const
+{
+ return m_publisherKeyDigest;
+}
+
+inline void
+Sha256WithRsa::setPublisherKeyDigest (const Blob &publisherKeyDigest)
+{
+ m_publisherKeyDigest = publisherKeyDigest;
+}
+
+inline KeyLocator &
+Sha256WithRsa::getKeyLocator ()
+{
+ return m_keyLocator;
+}
+
+inline const KeyLocator &
+Sha256WithRsa::getKeyLocator () const
+{
+ return m_keyLocator;
+}
+
+inline void
+Sha256WithRsa::setKeyLocator (const KeyLocator &keyLocator)
+{
+ m_keyLocator = keyLocator;
+}
+
+} // signature
+} // ndn
+
+#endif // NDN_EXCLUDE_H
diff --git a/ndn-cpp/fields/signature.cc b/ndn-cpp/fields/signature.cc
new file mode 100644
index 0000000..4c7a3a5
--- /dev/null
+++ b/ndn-cpp/fields/signature.cc
@@ -0,0 +1,18 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "signature.h"
+#include "ndn-cpp/error.h"
+
+namespace ndn
+{
+
+
+} // ndn
diff --git a/ndn-cpp/fields/signature.h b/ndn-cpp/fields/signature.h
new file mode 100644
index 0000000..a9fb39a
--- /dev/null
+++ b/ndn-cpp/fields/signature.h
@@ -0,0 +1,44 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_SIGNATURE_H
+#define NDN_SIGNATURE_H
+
+#include <iostream>
+
+namespace ndn {
+
+namespace wire { class Base; }
+
+/**
+ * @brief Pure virtual class providing an interface to work with signatures for NDN data packets
+ */
+class Signature
+{
+public:
+ /**
+ * @brief Virtual destructor
+ */
+ virtual
+ ~Signature () { }
+
+ /**
+ * @brief A double dispatch pattern to call the right wireFormatter method to format signature
+ * @param os reference to output stream
+ * @param wireFormatter a reference to a wireFormatter object
+ * @param userData any user-specific data
+ */
+ virtual void
+ doubleDispatch (std::ostream &os, wire::Base &wireFormatter, void *userData) const = 0;
+};
+
+} // ndn
+
+#endif // NDN_SIGNATURE_H
diff --git a/ndn-cpp/fields/signed-blob.h b/ndn-cpp/fields/signed-blob.h
new file mode 100644
index 0000000..2686977
--- /dev/null
+++ b/ndn-cpp/fields/signed-blob.h
@@ -0,0 +1,98 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_SIGNED_BLOB_H
+#define NDN_SIGNED_BLOB_H
+
+#include "blob.h"
+
+namespace ndn {
+
+/**
+ * @brief Class representing a blob, which has a signed portion (e.g., bytes of DATA packet)
+ */
+class SignedBlob : public Blob
+{
+public:
+ /**
+ * @brief Set signed portion of the blob
+ * @param offset An offset from the beginning of the blob
+ * @param size Size of the signed portion of the blob
+ */
+ inline void
+ setSignedPortion (size_t offset, size_t size);
+
+ /**
+ * @brief Get begin iterator to a signed portion of the blob
+ */
+ inline const_iterator
+ signed_begin () const;
+
+ /**
+ * @brief Get end iterator of a signed portion of the blob
+ */
+ inline const_iterator
+ signed_end () const;
+
+ /**
+ * @brief Get pointer to a first byte of the signed blob
+ */
+ inline const char*
+ signed_buf () const;
+
+ /**
+ * @brief Get size of the signed blob
+ */
+ inline size_t
+ signed_size () const;
+
+private:
+ const_iterator m_signedBegin;
+ const_iterator m_signedEnd;
+};
+
+
+inline void
+SignedBlob::setSignedPortion (size_t offset, size_t size)
+{
+ m_signedBegin = begin () + offset;
+ m_signedEnd = begin () + offset + size;
+}
+
+inline SignedBlob::const_iterator
+SignedBlob::signed_begin () const
+{
+ return m_signedBegin;
+}
+
+inline SignedBlob::const_iterator
+SignedBlob::signed_end () const
+{
+ return m_signedEnd;
+}
+
+inline const char*
+SignedBlob::signed_buf () const
+{
+ return &*m_signedBegin;
+}
+
+inline size_t
+SignedBlob::signed_size () const
+{
+ return m_signedEnd - m_signedBegin;
+}
+
+
+} // ndn
+
+#endif // NDN_SIGNED_BLOB_H
diff --git a/ndn-cpp/helpers/hash.cc b/ndn-cpp/helpers/hash.cc
new file mode 100644
index 0000000..0ecf927
--- /dev/null
+++ b/ndn-cpp/helpers/hash.cc
@@ -0,0 +1,130 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#include "hash.h"
+#include "ndn-cpp/helpers/uri.h"
+
+#include <boost/lexical_cast.hpp>
+#include <openssl/evp.h>
+#include <fstream>
+
+using namespace boost;
+using namespace std;
+
+// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
+#define HASH_FUNCTION EVP_sha256
+
+namespace ndn
+{
+
+std::ostream &
+operator << (std::ostream &os, const Hash &hash)
+{
+ if (hash.m_length == 0)
+ return os;
+
+ ostreambuf_iterator<char> out_it (os); // ostream iterator
+ // need to encode to base64
+ copy (detail::string_from_binary (reinterpret_cast<const char*> (hash.m_buf)),
+ detail::string_from_binary (reinterpret_cast<const char*> (hash.m_buf+hash.m_length)),
+ out_it);
+
+ return os;
+}
+
+std::string
+Hash::shortHash () const
+{
+ return lexical_cast<string> (*this).substr (0, 10);
+}
+
+
+unsigned char Hash::_origin = 0;
+HashPtr Hash::Origin(new Hash(&Hash::_origin, sizeof(unsigned char)));
+
+HashPtr
+Hash::FromString (const std::string &hashInTextEncoding)
+{
+ HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
+
+ if (hashInTextEncoding.size () == 0)
+ {
+ return retval;
+ }
+
+ if (hashInTextEncoding.size () > EVP_MAX_MD_SIZE * 2)
+ {
+ cerr << "Input hash is too long. Returning an empty hash" << endl;
+ return retval;
+ }
+
+ retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+ unsigned char *end = copy (detail::string_to_binary (hashInTextEncoding.begin ()),
+ detail::string_to_binary (hashInTextEncoding.end ()),
+ retval->m_buf);
+
+ retval->m_length = end - retval->m_buf;
+
+ return retval;
+}
+
+HashPtr
+Hash::FromFileContent (const char *filename)
+{
+ HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
+ retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+ EVP_MD_CTX *hash_context = EVP_MD_CTX_create ();
+ EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0);
+
+ ifstream iff (filename, std::ios::in | std::ios::binary);
+ while (iff.good ())
+ {
+ char buf[1024];
+ iff.read (buf, 1024);
+ EVP_DigestUpdate (hash_context, buf, iff.gcount ());
+ }
+
+ retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+ EVP_DigestFinal_ex (hash_context,
+ retval->m_buf, &retval->m_length);
+
+ EVP_MD_CTX_destroy (hash_context);
+
+ return retval;
+}
+
+HashPtr
+Hash::FromBytes (const ndn::Bytes &bytes)
+{
+ HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
+ retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+ EVP_MD_CTX *hash_context = EVP_MD_CTX_create ();
+ EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0);
+
+ // not sure whether it's bad to do so if bytes.size is huge
+ EVP_DigestUpdate(hash_context, ndn::head(bytes), bytes.size());
+
+ retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+ EVP_DigestFinal_ex (hash_context,
+ retval->m_buf, &retval->m_length);
+
+ EVP_MD_CTX_destroy (hash_context);
+
+ return retval;
+}
+
+} // ndn
diff --git a/ndn-cpp/helpers/hash.h b/ndn-cpp/helpers/hash.h
new file mode 100644
index 0000000..1652f2b
--- /dev/null
+++ b/ndn-cpp/helpers/hash.h
@@ -0,0 +1,162 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#ifndef NDN_HASH_H
+#define NDN_HASH_H
+
+#include <string.h>
+#include <iostream>
+#include <boost/shared_ptr.hpp>
+#include <boost/exception/all.hpp>
+
+#include "ndn-cpp/common.h"
+
+namespace ndn
+{
+
+class Hash;
+typedef boost::shared_ptr<Hash> HashPtr;
+
+class Hash
+{
+public:
+ static unsigned char _origin;
+ static HashPtr Origin;
+
+ Hash ()
+ : m_buf(0)
+ , m_length(0)
+ {
+ }
+
+ Hash (const void *buf, unsigned int length)
+ : m_length (length)
+ {
+ if (m_length != 0)
+ {
+ m_buf = new unsigned char [length];
+ memcpy (m_buf, buf, length);
+ }
+ }
+
+ Hash (const Hash &otherHash)
+ : m_length (otherHash.m_length)
+ {
+ if (m_length != 0)
+ {
+ m_buf = new unsigned char [m_length];
+ memcpy (m_buf, otherHash.m_buf, otherHash.m_length);
+ }
+ }
+
+ static HashPtr
+ FromString (const std::string &hashInTextEncoding);
+
+ static HashPtr
+ FromFileContent (const char *fileName);
+
+ static HashPtr
+ FromBytes (const ndn::Bytes &bytes);
+
+ ~Hash ()
+ {
+ if (m_length != 0)
+ delete [] m_buf;
+ }
+
+ Hash &
+ operator = (const Hash &otherHash)
+ {
+ if (m_length != 0)
+ delete [] m_buf;
+
+ m_length = otherHash.m_length;
+ if (m_length != 0)
+ {
+ m_buf = new unsigned char [m_length];
+ memcpy (m_buf, otherHash.m_buf, otherHash.m_length);
+ }
+ return *this;
+ }
+
+ bool
+ IsZero () const
+ {
+ return m_length == 0 ||
+ (m_length == 1 && m_buf[0] == 0);
+ }
+
+ bool
+ operator == (const Hash &otherHash) const
+ {
+ if (m_length != otherHash.m_length)
+ return false;
+
+ return memcmp (m_buf, otherHash.m_buf, m_length) == 0;
+ }
+
+ bool operator < (const Hash &otherHash) const
+ {
+ if (m_length < otherHash.m_length)
+ return true;
+
+ if (m_length > otherHash.m_length)
+ return false;
+
+ for (unsigned int i = 0; i < m_length; i++)
+ {
+ if (m_buf [i] < otherHash.m_buf [i])
+ return true;
+
+ if (m_buf [i] > otherHash.m_buf [i])
+ return false;
+
+ // if equal, continue
+ }
+
+ return false;
+ }
+
+ const void *
+ GetHash () const
+ {
+ return m_buf;
+ }
+
+ unsigned int
+ GetHashBytes () const
+ {
+ return m_length;
+ }
+
+ std::string
+ shortHash () const;
+
+private:
+ unsigned char *m_buf;
+ unsigned int m_length;
+
+ friend std::ostream &
+ operator << (std::ostream &os, const Hash &digest);
+};
+
+namespace Error {
+struct HashConversion : virtual boost::exception, virtual std::exception { };
+}
+
+
+std::ostream &
+operator << (std::ostream &os, const Hash &digest);
+
+}
+
+#endif // NDN_HASH_H
diff --git a/ndn-cpp/helpers/uri.h b/ndn-cpp/helpers/uri.h
new file mode 100644
index 0000000..3fbda7c
--- /dev/null
+++ b/ndn-cpp/helpers/uri.h
@@ -0,0 +1,155 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_DETAIL_URI_H
+#define NDN_DETAIL_URI_H
+
+#include "ndn-cpp/error.h"
+
+#include <boost/archive/iterators/transform_width.hpp>
+#include <boost/iterator/transform_iterator.hpp>
+
+namespace ndn
+{
+
+namespace detail
+{
+
+static const bool ESCAPE_CHARACTER [256] = {
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 26
+ 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 53
+ 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 107
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, // 134
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 161
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 188
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 215
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 242
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // 255
+};
+
+/// @cond include_hidden
+template<class CharType>
+struct hex_from_4_bit
+{
+ typedef CharType result_type;
+ CharType operator () (CharType ch) const
+ {
+ const char lookup_table [16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
+ // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
+ BOOST_ASSERT (ch < 16);
+ return lookup_table[static_cast<size_t>(ch)];
+ }
+};
+
+typedef boost::transform_iterator<hex_from_4_bit<std::string::const_iterator::value_type>,
+ boost::archive::iterators::transform_width<std::string::const_iterator, 4, 8, std::string::const_iterator::value_type> > string_from_binary;
+
+
+
+template<class CharType>
+struct hex_to_4_bit
+{
+ typedef CharType result_type;
+ CharType operator () (CharType ch) const
+ {
+ const signed char lookup_table [] = {
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
+ -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
+ };
+
+ signed char value = -1;
+ if ((unsigned)ch < 128)
+ value = lookup_table [(unsigned)ch];
+ if (value == -1)
+ boost::throw_exception (error::StringTransform ());
+
+ return value;
+ }
+};
+
+typedef boost::archive::iterators::transform_width<boost::transform_iterator<hex_to_4_bit<std::string::const_iterator::value_type>, std::string::const_iterator>, 8, 4> string_to_binary;
+/// @endcond
+
+} // detail
+
+/**
+ * @brief A helper class to convert to/from URI
+ */
+class Uri
+{
+public:
+ template<class Iterator1, class Iterator2>
+ inline static void
+ fromEscaped (Iterator1 begin, Iterator1 end, Iterator2 inserter)
+ {
+ Iterator1 i = begin;
+ while (i != end)
+ {
+ if (*i == '%')
+ {
+ try
+ {
+ ++i;
+ Iterator1 j = i;
+ advance (i, 2);
+
+ std::copy (detail::string_to_binary (j), detail::string_to_binary (i), inserter);
+ }
+ catch (ndn::error::StringTransform &e)
+ {
+ BOOST_THROW_EXCEPTION (error::Uri ()
+ << error::pos (std::distance (i, begin)));
+ }
+ }
+ else if (!detail::ESCAPE_CHARACTER[static_cast<unsigned char> (*i)])
+ {
+ *inserter = *i;
+ ++inserter; ++i;
+ }
+ else
+ {
+ BOOST_THROW_EXCEPTION (error::Uri ()
+ << error::pos (std::distance (i, begin)));
+ }
+ }
+ }
+
+ template<class Iterator1, class Iterator2>
+ inline static void
+ toEscaped (Iterator1 begin, Iterator1 end, Iterator2 inserter)
+ {
+ const char lookup_table [16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
+
+ for (Iterator1 i = begin; i != end; i++)
+ {
+ if (detail::ESCAPE_CHARACTER[static_cast<unsigned char> (*i)])
+ {
+ *inserter = '%'; ++inserter;
+ *inserter = lookup_table [(*i >> 4) & 0x0F]; ++inserter;
+ *inserter = lookup_table [(*i & 0x0F)]; ++inserter;
+ }
+ else
+ {
+ *inserter = *i; ++inserter;
+ }
+ }
+ }
+};
+
+} // ndn
+
+#endif // NDN_DETAIL_URI_H
diff --git a/ndn-cpp/interest.cc b/ndn-cpp/interest.cc
new file mode 100644
index 0000000..3825677
--- /dev/null
+++ b/ndn-cpp/interest.cc
@@ -0,0 +1,103 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#include "interest.h"
+#include <boost/lexical_cast.hpp>
+
+using namespace std;
+
+namespace ndn {
+
+Interest::Interest ()
+ // m_name
+ : m_maxSuffixComponents (Interest::ncomps)
+ , m_minSuffixComponents (Interest::ncomps)
+ , m_answerOriginKind (AOK_DEFAULT)
+ , m_interestLifetime (time::Seconds (-1.0))
+ , m_scope (NO_SCOPE)
+ , m_childSelector (CHILD_DEFAULT)
+ // m_publisherKeyDigest
+{
+}
+
+Interest::Interest (const Name &name)
+ : m_name (name)
+ , m_maxSuffixComponents (Interest::ncomps)
+ , m_minSuffixComponents (Interest::ncomps)
+ , m_answerOriginKind(AOK_DEFAULT)
+ , m_interestLifetime (time::Seconds (-1.0))
+ , m_scope (NO_SCOPE)
+ , m_childSelector (CHILD_DEFAULT)
+ // m_publisherKeyDigest
+{
+}
+
+Interest::Interest (const Interest &other)
+{
+ m_name = other.m_name;
+ m_maxSuffixComponents = other.m_maxSuffixComponents;
+ m_minSuffixComponents = other.m_minSuffixComponents;
+ m_answerOriginKind = other.m_answerOriginKind;
+ m_interestLifetime = other.m_interestLifetime;
+ m_scope = other.m_scope;
+ m_childSelector = other.m_childSelector;
+ m_publisherPublicKeyDigest = other.m_publisherPublicKeyDigest;
+}
+
+Interest::Interest (const ccn_parsed_interest *pi)
+ : m_maxSuffixComponents (Interest::ncomps)
+ , m_minSuffixComponents (Interest::ncomps)
+ , m_answerOriginKind (AOK_DEFAULT)
+ , m_interestLifetime (time::Seconds (-1.0))
+ , m_scope (NO_SCOPE)
+ , m_childSelector (CHILD_DEFAULT)
+{
+ if (pi != NULL)
+ {
+ m_maxSuffixComponents = pi->max_suffix_comps;
+ m_minSuffixComponents = pi->min_suffix_comps;
+ switch(pi->orderpref)
+ {
+ case 0: m_childSelector = CHILD_LEFT; break;
+ case 1: m_childSelector = CHILD_RIGHT; break;
+ default: m_childSelector = CHILD_DEFAULT; break;
+ }
+
+ switch(pi->answerfrom)
+ {
+ case 0x1: m_answerOriginKind = AOK_CS; break;
+ case 0x2: m_answerOriginKind = AOK_NEW; break;
+ case 0x3: m_answerOriginKind = AOK_DEFAULT; break;
+ case 0x4: m_answerOriginKind = AOK_STALE; break;
+ case 0x10: m_answerOriginKind = AOK_EXPIRE; break;
+ default: break;
+ }
+ m_scope = static_cast<Scope> (pi->scope);
+ }
+
+ /// @todo copy publisher key digest
+}
+
+bool
+Interest::operator == (const Interest &other)
+{
+ return
+ m_name == other.m_name
+ && m_maxSuffixComponents == other.m_maxSuffixComponents
+ && m_minSuffixComponents == other.m_minSuffixComponents
+ && m_answerOriginKind == other.m_answerOriginKind
+ && m_interestLifetime == other.m_interestLifetime
+ && m_scope == other.m_scope
+ && m_childSelector == other.m_childSelector;
+}
+
+} // ndn
diff --git a/ndn-cpp/interest.h b/ndn-cpp/interest.h
new file mode 100644
index 0000000..d929efa
--- /dev/null
+++ b/ndn-cpp/interest.h
@@ -0,0 +1,461 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#ifndef NDN_INTEREST_H
+#define NDN_INTEREST_H
+
+#include <ndn-cpp/common.h>
+#include <ndn-cpp/fields/name.h>
+#include <ndn-cpp/fields/exclude.h>
+#include <ndn-cpp/helpers/hash.h>
+
+namespace ndn {
+
+/**
+ * @brief Class abstracting operations with Interests (constructing and getting access to Interest fields)
+ */
+class Interest
+{
+public:
+ /**
+ * @brief Default constructor, creates an interest for / prefix without any selectors
+ */
+ Interest ();
+
+ /**
+ * @brief Create an interest for the name
+ * @param name name of the data to request
+ */
+ Interest (const Name &name);
+
+ /**
+ * @brief Copy constructor
+ * @param interest interest to copy
+ */
+ Interest (const Interest &interest);
+
+ /**
+ * @brief Create an interest based on ccn_parsed_interest data structure
+ * @param interest pointer to ccn_parsed_interest data structure
+ *
+ * This method will create an interest with empty name, since ccn_parsed_interest structure
+ * has limited amount of information
+ */
+ Interest (const ccn_parsed_interest *interest);
+
+ /**
+ * @brief Set interest name
+ * @param name name of the interest
+ * @return reference to self (to allow method chaining)
+ *
+ * In some cases, a direct access to and manipulation of name using getName is more efficient
+ */
+ inline Interest &
+ setName (const Name &name);
+
+ /**
+ * @brief Get interest name (const reference)
+ * @returns name of the interest
+ */
+ inline const Name &
+ getName () const;
+
+ /**
+ * @brief Get interest name (reference)
+ * @returns name of the interest
+ */
+ inline Name &
+ getName ();
+
+ /**
+ * @brief Set interest lifetime (time_duration)
+ * @param interestLifetime interest lifetime specified as a time_duration value.
+ * Negative value means that InterestLifetime is not set.
+ * @return reference to self (to allow method chaining)
+ */
+ inline Interest &
+ setInterestLifetime (const TimeInterval &interestLifetime);
+
+ /**
+ * @brief Set interest lifetime (double)
+ * @param interestLifetime interest lifetime expressed in seconds, with possible fractional seconds (double).
+ * Negative value means that InterestLifetime is not set.
+ * @return reference to self (to allow method chaining)
+ */
+ inline Interest &
+ setInterestLifetime (double interestLifetimeSeconds);
+
+ /**
+ * @brief Get interest lifetime
+ * @return TimeInterval representing lifetime of the interest.
+ * Use time_duration::total_seconds () or time_duration::total_microseconds (),
+ * if you need interest lifetime as a plain number.
+ * @see http://www.boost.org/doc/libs/1_53_0/doc/html/date_time/posix_time.html
+ */
+ inline const TimeInterval &
+ getInterestLifetime () const;
+
+ /**
+ * @brief Set intended interest scope
+ * @param scope requested scope of the interest @see Scope
+ * @return reference to self (to allow method chaining)
+ */
+ inline Interest &
+ setScope (uint8_t scope);
+
+ /**
+ * @brief Get intended interest scope
+ * @return intended interest scope @see Scope
+ */
+ inline uint8_t
+ getScope () const;
+
+ ///////////////////////////////////////////////////////////////////////
+ // SELECTORS //
+ ///////////////////////////////////////////////////////////////////////
+
+ /**
+ * @brief Enum defining constants for AnswerOriginKind selector field
+ */
+ enum AnswerOriginKind
+ {
+ AOK_CS = 0x1, ///< @brief request item from the content store
+ AOK_NEW = 0x2, ///< @brief request item from the original producer
+ AOK_DEFAULT = 0x3, ///< @brief default: either from content store or original producer
+ AOK_STALE = 0x4, ///< @brief Allow stale data
+ AOK_EXPIRE = 0x10 ///< @brief Allow expired data (?)
+ };
+
+ /**
+ * @brief Enum defining constants for ChildSelector field
+ */
+ enum ChildSelector
+ {
+ CHILD_LEFT = 0, ///< @brief request left child
+ CHILD_RIGHT = 1, ///< @brief request right child
+ CHILD_DEFAULT = 2 ///< @brief do not specify which child is requested
+ };
+
+ /**
+ * @brief Enum defining constants for Scope field
+ */
+ enum Scope
+ {
+ NO_SCOPE = 255, ///< @brief Interest scope is not defined
+ SCOPE_LOCAL_CCND = 0, ///< @brief Interest scope is only toward local NDN daemon
+ SCOPE_LOCAL_HOST = 1, ///< @brief Interest scope is within local host (any local application only)
+ SCOPE_NEXT_HOST = 2 ///< @brief Interest scope is within local host and immediate neighboring node
+ };
+
+ /**
+ * @brief Set interest selector for maximum suffix components
+ * @param maxSuffixComponents maximum number of suffix components. If Interest::ncomps, then not restricted
+ * @return reference to self (to allow method chaining)
+ */
+ inline Interest &
+ setMaxSuffixComponents (uint32_t maxSuffixComponents);
+
+ /**
+ * \brief Get interest selector for maximum suffix components
+ *
+ * MaxSuffixComponents refer to the number of name components beyond those in the prefix,
+ * and counting the implicit digest, that may occur in the matching ContentObject.
+ * For more information, see http://www.ccnx.org/releases/latest/doc/technical/InterestMessage.html
+ **/
+ inline uint32_t
+ getMaxSuffixComponents () const;
+
+ /**
+ * @brief Set interest selector for minimum suffix components
+ * @param minSuffixComponents minimum number of suffix components. If Interest::ncomps, then not restricted
+ * @return reference to self (to allow method chaining)
+ */
+ inline Interest &
+ setMinSuffixComponents (uint32_t minSuffixComponents);
+
+ /**
+ * \brief Get interest selector for minimum suffix components
+ *
+ * MinSuffixComponents refer to the number of name components beyond those in the prefix,
+ * and counting the implicit digest, that may occur in the matching ContentObject.
+ * For more information, see http://www.ccnx.org/releases/latest/doc/technical/InterestMessage.html
+ **/
+ inline uint32_t
+ getMinSuffixComponents () const;
+
+ /**
+ * @brief Set interest selector for answer origin kind
+ * @param answerOriginKind type of answer @see AnswerOriginKind
+ * @return reference to self (to allow method chaining)
+ */
+ inline Interest &
+ setAnswerOriginKind (uint32_t answerOriginKind);
+
+ /**
+ * @brief Get interest selector for answer origin kind
+ */
+ inline uint32_t
+ getAnswerOriginKind () const;
+
+ /**
+ * @brief Set interest selector for child selector
+ * @param child child selector @see ChildSelector
+ * @return reference to self (to allow method chaining)
+ *
+ * Often a given interest will match more than one ContentObject within a given content store.
+ * The ChildSelector provides a way of expressing a preference for which of these should be returned.
+ * If the value is false, the leftmost child is preferred. If true, the rightmost child is preferred.
+ * \see http://www.ccnx.org/releases/latest/doc/technical/InterestMessage.html for more information.
+ */
+ inline Interest &
+ setChildSelector (uint8_t child);
+
+ /**
+ * @brief Get interest selector for child selector
+ */
+ inline uint8_t
+ getChildSelector () const;
+
+ /**
+ * @brief Set interest selector for publisher public key digest
+ * @param digest publisher public key digest
+ * @return reference to self (to allow method chaining)
+ *
+ * Currently, this method has no effect
+ * @todo Implement PublisherPublicKeyDigest
+ */
+ inline Interest &
+ setPublisherPublicKeyDigest(const Hash &digest);
+
+ /**
+ * @brief Get interest selector for publisher public key digest
+ *
+ * @todo Implement
+ */
+ inline const Hash&
+ getPublisherPublicKeyDigest () const;
+
+ /**
+ * @brief Set exclude filter
+ * @param exclude An exclude filter to set
+ *
+ * In some cases, a direct access to and manipulation of exclude filter using getExclude is more efficient
+ */
+ inline void
+ setExclude (const Exclude &exclude);
+
+ /**
+ * @brief Get exclude filter (const reference)
+ */
+ inline const Exclude &
+ getExclude () const;
+
+ /**
+ * @brief Get exclude filter (reference)
+ */
+ inline Exclude &
+ getExclude ();
+
+ ///////////////////////////////////////////////////////////////////////
+ // HELPERS //
+ ///////////////////////////////////////////////////////////////////////
+
+ /**
+ * @brief Compare equality of two interests
+ */
+ bool
+ operator== (const Interest &interest);
+
+public:
+ // Data Members (public):
+ /// @brief Value indicating that number of components parameter is invalid
+ const static uint32_t ncomps = static_cast<uint32_t> (-1);
+
+private:
+ Name m_name;
+ uint32_t m_maxSuffixComponents;
+ uint32_t m_minSuffixComponents;
+ uint32_t m_answerOriginKind;
+ TimeInterval m_interestLifetime; // lifetime in seconds
+
+ uint8_t m_scope;
+ uint8_t m_childSelector;
+ // not used now
+ Hash m_publisherPublicKeyDigest;
+ Exclude m_exclude;
+
+ Ptr<Blob> m_wire;
+};
+
+typedef boost::shared_ptr<Interest> InterestPtr;
+
+namespace Error
+{
+/**
+ * @brief Exception that is thrown in case of error during interest construction or parsing
+ */
+struct Interest:
+ virtual boost::exception, virtual std::exception {};
+}
+
+
+
+inline Interest &
+Interest::setName (const Name &name)
+{
+ m_name = name;
+ return *this;
+}
+
+inline const Name &
+Interest::getName () const
+{
+ return m_name;
+}
+
+inline Name &
+Interest::getName ()
+{
+ return m_name;
+}
+
+inline Interest &
+Interest::setInterestLifetime (const TimeInterval &interestLifetime)
+{
+ m_interestLifetime = interestLifetime;
+ return *this;
+}
+
+inline Interest &
+Interest::setInterestLifetime (double interestLifetimeSeconds)
+{
+ m_interestLifetime = time::Seconds (interestLifetimeSeconds);
+ return *this;
+}
+
+inline const TimeInterval &
+Interest::getInterestLifetime () const
+{
+ return m_interestLifetime;
+}
+
+inline Interest &
+Interest::setScope (uint8_t scope)
+{
+ m_scope = scope;
+ return *this;
+}
+
+inline uint8_t
+Interest::getScope () const
+{
+ return m_scope;
+}
+
+///////////////////////////////////////////////////////////////////////
+// SELECTORS //
+///////////////////////////////////////////////////////////////////////
+
+
+inline Interest &
+Interest::setMaxSuffixComponents (uint32_t maxSuffixComponents)
+{
+ m_maxSuffixComponents = maxSuffixComponents;
+ return *this;
+}
+
+inline uint32_t
+Interest::getMaxSuffixComponents () const
+{
+ return m_maxSuffixComponents;
+}
+
+inline Interest &
+Interest::setMinSuffixComponents (uint32_t minSuffixComponents)
+{
+ m_minSuffixComponents = minSuffixComponents;
+ return *this;
+}
+
+inline uint32_t
+Interest::getMinSuffixComponents () const
+{
+ return m_minSuffixComponents;
+}
+
+inline Interest &
+Interest::setAnswerOriginKind (uint32_t answerOriginKind)
+{
+ m_answerOriginKind = answerOriginKind;
+ return *this;
+}
+
+inline uint32_t
+Interest::getAnswerOriginKind () const
+{
+ return m_answerOriginKind;
+}
+
+inline Interest &
+Interest::setChildSelector (uint8_t childSelector)
+{
+ m_childSelector = childSelector;
+ return *this;
+}
+
+inline uint8_t
+Interest::getChildSelector () const
+{
+ return m_childSelector;
+}
+
+inline Interest &
+Interest::setPublisherPublicKeyDigest(const Hash &publisherPublicKeyDigest)
+{
+ m_publisherPublicKeyDigest = publisherPublicKeyDigest;
+ return *this;
+}
+
+inline const Hash&
+Interest::getPublisherPublicKeyDigest () const
+{
+ return m_publisherPublicKeyDigest;
+}
+
+inline void
+Interest::setExclude (const Exclude &exclude)
+{
+ m_exclude = exclude;
+}
+
+/**
+ * @brief Get exclude filter (const reference)
+ */
+inline const Exclude &
+Interest::getExclude () const
+{
+ return m_exclude;
+}
+
+/**
+ * @brief Get exclude filter (reference)
+ */
+inline Exclude &
+Interest::getExclude ()
+{
+ return m_exclude;
+}
+
+
+} // ndn
+
+#endif // NDN_INTEREST_H
diff --git a/ndn-cpp/security/certificate.h b/ndn-cpp/security/certificate.h
new file mode 100644
index 0000000..e0efa91
--- /dev/null
+++ b/ndn-cpp/security/certificate.h
@@ -0,0 +1,43 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_CERTIFICATE_H
+#define NDN_CERTIFICATE_H
+
+#include "ndn-cpp/data.h"
+
+namespace ndn {
+
+/**
+ * @brief Class representing NDN identity
+ *
+ * - name
+ * - full NDN name of the NDN certificate
+ * - /ndn/ucla.edu/alex/cert/<pubkey.sha256>/<issuer>
+ * - content
+ * - X.509 certificate in DER format (X.509 certificate can include any necessary identity information, as well as is fully extendable)
+ * - Subject:
+ * - full real name, associated with the certificate
+ * - full affiliation, associated with the certificate
+ * - Subject Public Key Info
+ * - Validity
+ * - signature
+ * - issuerCertName (KeyLocator/CertName)
+ * - /ndn/ucla.edu/cert/<pubkey.sha256>/<issuer>
+ *
+ */
+class Certificate : public Data
+{
+public:
+};
+
+} // ndn
+
+#endif // NDN_CERTIFICATE_H
diff --git a/ndn-cpp/security/identity.h b/ndn-cpp/security/identity.h
new file mode 100644
index 0000000..676b3b3
--- /dev/null
+++ b/ndn-cpp/security/identity.h
@@ -0,0 +1,46 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_IDENTITY_H
+#define NDN_IDENTITY_H
+
+namespace ndn {
+
+/**
+ * @brief Class representing NDN identity
+ *
+ * - name
+ * - (indexing and lookup)
+ * - /ndn/ucla.edu/alex
+ * - contents
+ * - privateKeyName
+ * - unique name of the private key
+ * - private key bits are not exposed anywhere
+ * - /ndn/ucla.edu/alex/privKey/<pubkey.sha256>
+ * - indexed list of certificates
+ * - NDN DATA packets
+ * - easy access data structure of NDN certificate (“parsed DATA packet”)
+ * - link to default certificate
+ * - revocation list
+ * - one or more NDN DATA packets
+ * - name: <identity-name>/revocation-list/<version>(/<seqno>)?
+ * - /ndn/ucla.edu/alex/revocation-list/%FD...01
+ * - initially empty, updated whenever an issued certificate is getting revoked
+ * - revocation always exists
+ *
+ */
+class Identity
+{
+public:
+};
+
+} // ndn
+
+#endif // NDN_IDENTITY_H
diff --git a/ndn-cpp/security/keychain-pkcs12.cc b/ndn-cpp/security/keychain-pkcs12.cc
new file mode 100644
index 0000000..8a01fd5
--- /dev/null
+++ b/ndn-cpp/security/keychain-pkcs12.cc
@@ -0,0 +1,69 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "keychain-pkcs12.h"
+
+namespace ndn {
+
+/////////////////////////////////////////////////////
+// interface to manage certificates and identities //
+/////////////////////////////////////////////////////
+
+Ptr<const Identity>
+KeychainKeystoreOpenssl::getDefaultIdentity ()
+{
+ return Ptr<const Identity> ();
+}
+
+Ptr<const Identity>
+KeychainKeystoreOpenssl::getIdentity (const Name &identityName)
+{
+ return Ptr<const Identity> ();
+}
+
+Ptr<const Identity>
+KeychainKeystoreOpenssl::generateIdentity (const Name &identityName)
+{
+ return Ptr<const Identity> ();
+}
+
+void
+KeychainKeystoreOpenssl::requestIdentityCertificate (const Identity &identity, std::ostream &os)
+{
+}
+
+Ptr<const Certificate>
+KeychainKeystoreOpenssl::issueCertificate (const Identity &identity, std::istream &is)
+{
+ return Ptr<const Certificate> ();
+}
+
+Ptr<const Certificate>
+KeychainKeystoreOpenssl::issueCertificate (std::istream &is)
+{
+ return Ptr<const Certificate> ();
+}
+
+void
+KeychainKeystoreOpenssl::installIdentityCertificate (const Certificate &cert)
+{
+}
+
+/////////////////////////////////////////////////////
+// interface to sign and encrypt data //
+/////////////////////////////////////////////////////
+Ptr<Signature>
+KeychainKeystoreOpenssl::sign (const Identity &identity, const void *buffer, size_t size)
+{
+ return Ptr<Signature> ();
+}
+
+
+} // ndn
diff --git a/ndn-cpp/security/keychain-pkcs12.h b/ndn-cpp/security/keychain-pkcs12.h
new file mode 100644
index 0000000..898d792
--- /dev/null
+++ b/ndn-cpp/security/keychain-pkcs12.h
@@ -0,0 +1,74 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_KEYCHAIN_PKCS12_H
+#define NDN_KEYCHAIN_PKCS12_H
+
+#include "keychain.h"
+#include "ndn-cpp/helpers/hash.h"
+
+namespace ndn
+{
+
+/**
+ * @brief Class implementing logic to work with pkcs12 CCNx keystore file (.ccnx_keystore)
+ */
+class KeychainKeystoreOpenssl : public virtual Keychain
+{
+public:
+ KeychainKeystoreOpenssl ();
+ KeychainKeystoreOpenssl (const std::string &path);
+
+public:
+ /////////////////////////////////////////////////////
+ // interface to manage certificates and identities //
+ /////////////////////////////////////////////////////
+
+ virtual Ptr<const Identity>
+ getDefaultIdentity ();
+
+ virtual Ptr<const Identity>
+ getIdentity (const Name &identityName);
+
+ virtual Ptr<const Identity>
+ generateIdentity (const Name &identityName);
+
+ virtual void
+ requestIdentityCertificate (const Identity &identity, std::ostream &os);
+
+ virtual Ptr<const Certificate>
+ issueCertificate (const Identity &identity, std::istream &is);
+
+ virtual Ptr<const Certificate>
+ issueCertificate (std::istream &is);
+
+ virtual void
+ installIdentityCertificate (const Certificate &cert);
+
+public:
+ /////////////////////////////////////////////////////
+ // interface to sign and encrypt data //
+ /////////////////////////////////////////////////////
+ virtual Ptr<Signature>
+ sign (const Identity &identity, const void *buffer, size_t size);
+
+
+private:
+ void
+ initialize (const std::string &pkcs12);
+
+private:
+ Name m_publicKeyName;
+ Hash m_publicKeyDigest;
+};
+
+} // ndn
+
+#endif // NDN_KEYCHAIN_KEYSTORE_OPENSSL_H
diff --git a/ndn-cpp/security/keychain.cc b/ndn-cpp/security/keychain.cc
new file mode 100644
index 0000000..2f7fbe5
--- /dev/null
+++ b/ndn-cpp/security/keychain.cc
@@ -0,0 +1,15 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "keychain.h"
+
+ndn::Keychain::~Keychain ()
+{
+}
diff --git a/ndn-cpp/security/keychain.h b/ndn-cpp/security/keychain.h
new file mode 100644
index 0000000..83a09dd
--- /dev/null
+++ b/ndn-cpp/security/keychain.h
@@ -0,0 +1,130 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_KEYCHAIN_H
+#define NDN_KEYCHAIN_H
+
+// #include "ndn-cpp/fields/blob.h"
+// #include "ndn-cpp/fields/name.h"
+
+#include "identity.h"
+#include "certificate.h"
+
+#include <iostream>
+
+namespace ndn {
+
+/**
+ * @brief Interface for a keychain operations
+ *
+ * Keychain has the following set of operations:
+ *
+ * --- interface to manage certificates and identities
+ * - identities are permanently stored
+ * - certificates can be cached (or stored permanently, if user is willing to)
+ * --- interface to sign and encrypt data
+ *
+ */
+class Keychain
+{
+public:
+ /**
+ * @brief Virtual destructor
+ */
+ virtual
+ ~Keychain ();
+
+ /////////////////////////////////////////////////////
+ // interface to manage certificates and identities //
+ /////////////////////////////////////////////////////
+
+ /**
+ * @brief Get default identity
+ */
+ virtual Ptr<const Identity>
+ getDefaultIdentity () = 0;
+
+ /**
+ * @brief Get identity by name
+ * @param identityName name of the requested identity
+ */
+ virtual Ptr<const Identity>
+ getIdentity (const Name &identityName) = 0;
+
+ /**
+ * @brief Create a self-certified identity
+ * @param identityName name of the identity to create
+ */
+ virtual Ptr<const Identity>
+ generateIdentity (const Name &identityName) = 0;
+
+ /**
+ * @brief Create identity certification request
+ * @param identity identity for which create the request
+ * @param os output stream which will receive the request
+ */
+ virtual void
+ requestIdentityCertificate (const Identity &identity, std::ostream &os) = 0;
+
+ /**
+ * @brief Issue a certificate using parameters from the input stream (formatted as request)
+ * @param identity Identity which will be used to issue the certificate
+ * @param is input stream from which to read parameters of the certificate
+ *
+ * @returns smart pointer to a signed certificate
+ */
+ virtual Ptr<const Certificate>
+ issueCertificate (const Identity &identity, std::istream &is) = 0;
+
+ /**
+ * @brief Issue a certificate using parameters from the input stream (formatted as request)
+ *
+ * Same as another version, but using the default identity
+ *
+ * @returns smart pointer to a signed certificate
+ */
+ virtual Ptr<const Certificate>
+ issueCertificate (std::istream &is) = 0;
+
+ /**
+ * @brief Install identity certificate
+ * @param cert certificate to install
+ */
+ virtual void
+ installIdentityCertificate (const Certificate &cert) = 0;
+
+public:
+ /////////////////////////////////////////////////////
+ // interface to sign and encrypt data //
+ /////////////////////////////////////////////////////
+
+ /**
+ * @brief Sign data using specified identity
+ * @param identity selected identity to sign data
+ * @param buffer pointer to the data to sign
+ * @param size length of data to sign
+ *
+ * @return pointer to base class of a signature object (depending on identity,
+ * different types signature can be produced)
+ */
+ virtual Ptr<Signature>
+ sign (const Identity &identity, const void *buffer, size_t size) = 0;
+
+ // TBD
+ // /**
+ // * @brief Decrypt data using the specified identity
+ // */
+ // virtual ?
+ // decrypt (Ptr<Identity> identity, const void *buffer, size_t size, ?) = 0;
+};
+
+} // ndn
+
+#endif // NDN_KEYCHAIN_H
diff --git a/ndn-cpp/security/verifier.h b/ndn-cpp/security/verifier.h
new file mode 100644
index 0000000..cb98243
--- /dev/null
+++ b/ndn-cpp/security/verifier.h
@@ -0,0 +1,38 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_VERIFIER_H
+#define NDN_VERIFIER_H
+
+namespace ndn {
+
+/**
+ * @brief Interface for NDN DATA packet verification (trust model)
+ *
+ * --- interface to verify DATA packets
+ * - application selects required implementation
+ * - at least two build-in models:
+ * = hierarchical (default)
+ * = simple chain (without authorization)
+ * - uses NDN keychain as a backend to cache certificates
+ */
+class Verifier
+{
+public:
+ /**
+ * @brief Virtual destructor
+ */
+ virtual
+ ~Verifier ();
+};
+
+} // ndn
+
+#endif // NDN_VERIFIER_H
diff --git a/ndn-cpp/trie/detail/functor-hook.h b/ndn-cpp/trie/detail/functor-hook.h
new file mode 100644
index 0000000..f34969c
--- /dev/null
+++ b/ndn-cpp/trie/detail/functor-hook.h
@@ -0,0 +1,70 @@
+/* -*- 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>
+ */
+
+#ifndef FUNCTOR_HOOK_H_
+#define FUNCTOR_HOOK_H_
+
+#include <boost/intrusive/parent_from_member.hpp>
+
+namespace ns3 {
+namespace ndn {
+namespace ndnSIM {
+namespace detail {
+
+template<class BaseHook, class ValueType, int N>
+struct FunctorHook
+{
+ typedef typename BaseHook::template index<N>::type hook_type;
+ typedef hook_type* hook_ptr;
+ typedef const hook_type* const_hook_ptr;
+
+ typedef ValueType value_type;
+ typedef value_type* pointer;
+ typedef const value_type* const_pointer;
+
+ //Required static functions
+ static hook_ptr to_hook_ptr (value_type &value)
+ { return &value.policy_hook_.template get<N> (); }
+
+ static const_hook_ptr to_hook_ptr(const value_type &value)
+ { return &value.policy_hook_.template get<N> (); }
+
+ static pointer to_value_ptr(hook_ptr n)
+ {
+ return
+ boost::intrusive::get_parent_from_member<value_type>
+ (static_cast<BaseHook*> (boost::intrusive::get_parent_from_member< wrap<hook_type> >(n, &wrap<hook_type>::value_)),
+ &value_type::policy_hook_);
+ }
+ static const_pointer to_value_ptr(const_hook_ptr n)
+ {
+ return
+ boost::intrusive::get_parent_from_member<value_type>
+ (static_cast<const BaseHook*> (boost::intrusive::get_parent_from_member< wrap<hook_type> >(n, &wrap<hook_type>::value_)),
+ &value_type::policy_hook_);
+ }
+};
+
+} // detail
+} // ndnSIM
+} // ndn
+} // ns3
+
+#endif // FUNCTOR_HOOK_H_
diff --git a/ndn-cpp/trie/detail/multi-policy-container.h b/ndn-cpp/trie/detail/multi-policy-container.h
new file mode 100644
index 0000000..c1251e9
--- /dev/null
+++ b/ndn-cpp/trie/detail/multi-policy-container.h
@@ -0,0 +1,175 @@
+/* -*- 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 MULTI_POLICY_CONTAINER_H_
+#define MULTI_POLICY_CONTAINER_H_
+
+#include <boost/mpl/inherit_linearly.hpp>
+#include <boost/mpl/at.hpp>
+
+namespace ns3 {
+namespace ndn {
+namespace ndnSIM {
+namespace detail {
+
+template< class Base, class Value >
+struct policy_wrap
+{
+ policy_wrap (Base &base) : value_ (base) { }
+ Value value_;
+};
+
+template< class Base, class Super/*empy_wrap/previous level*/, class Value/*policy_wrap< element in vector >*/ >
+struct inherit_with_base : Super, Value
+{
+ inherit_with_base (Base &base) : Super (base), Value (base) { }
+
+ void
+ update (typename Base::iterator item)
+ {
+ Value::value_.update (item);
+ Super::update (item);
+ }
+
+ bool
+ insert (typename Base::iterator item)
+ {
+ bool ok = Value::value_.insert (item);
+ if (!ok)
+ return false;
+
+ ok = Super::insert (item);
+ if (!ok)
+ {
+ Value::value_.erase (item);
+ return false;
+ }
+ return true;
+ }
+
+ void
+ lookup (typename Base::iterator item)
+ {
+ Value::value_.lookup (item);
+ Super::lookup (item);
+ }
+
+ void
+ erase (typename Base::iterator item)
+ {
+ Value::value_.erase (item);
+ Super::erase (item);
+ }
+
+ void
+ clear ()
+ {
+ Value::value_.clear ();
+ Super::clear ();
+ }
+};
+
+template< class Base >
+struct empty_policy_wrap
+{
+ empty_policy_wrap (Base &base) { }
+
+ void update (typename Base::iterator item) {}
+ bool insert (typename Base::iterator item) { return true; }
+ void lookup (typename Base::iterator item) {}
+ void erase (typename Base::iterator item) {}
+ void clear () {}
+};
+
+template< class Base, class Vector >
+struct multi_policy_container
+ : public boost::mpl::fold< Vector,
+ empty_policy_wrap<Base>,
+ inherit_with_base<Base,
+ boost::mpl::_1/*empty/previous*/,
+ policy_wrap<Base, boost::mpl::_2>/*element in vector*/>
+ >::type
+{
+ typedef typename boost::mpl::fold< Vector,
+ empty_policy_wrap<Base>,
+ inherit_with_base<Base,
+ boost::mpl::_1/*empty/previous*/,
+ policy_wrap<Base, boost::mpl::_2>/*element in vector*/>
+ >::type super;
+
+ typedef typename boost::mpl::at_c<Vector, 0>::type::iterator iterator;
+ typedef typename boost::mpl::at_c<Vector, 0>::type::const_iterator const_iterator;
+
+ iterator begin () { return this->get<0> ().begin (); }
+ const_iterator begin () const { return this->get<0> ().begin (); }
+
+ iterator end () { return this->get<0> ().end (); }
+ const_iterator end () const { return this->get<0> ().end (); }
+
+ size_t size () const { return this->get<0> ().size (); }
+
+ multi_policy_container (Base &base)
+ : super (base)
+ { }
+
+ template<int N>
+ struct index
+ {
+ typedef typename boost::mpl::at_c<Vector, N>::type type;
+ };
+
+ template<class T>
+ T &
+ get ()
+ {
+ return static_cast< policy_wrap<Base, T> &> (*this).value_;
+ }
+
+ template<class T>
+ const T &
+ get () const
+ {
+ return static_cast< const policy_wrap<Base, T> &> (*this).value_;
+ }
+
+ template<int N>
+ typename boost::mpl::at_c<Vector, N>::type &
+ get ()
+ {
+ typedef typename boost::mpl::at_c<Vector, N>::type T;
+ return static_cast< policy_wrap<Base, T> &> (*this).value_;
+ }
+
+ template<int N>
+ const typename boost::mpl::at_c<Vector, N>::type &
+ get () const
+ {
+ typedef typename boost::mpl::at_c<Vector, N>::type T;
+ return static_cast< const policy_wrap<Base, T> &> (*this).value_;
+ }
+};
+
+
+} // detail
+} // ndnSIM
+} // ndn
+} // ns3
+
+#endif // MULTI_POLICY_CONTAINER_H_
diff --git a/ndn-cpp/trie/detail/multi-type-container.h b/ndn-cpp/trie/detail/multi-type-container.h
new file mode 100644
index 0000000..d4971c4
--- /dev/null
+++ b/ndn-cpp/trie/detail/multi-type-container.h
@@ -0,0 +1,86 @@
+/* -*- 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 MULTI_TYPE_CONTAINER_H_
+#define MULTI_TYPE_CONTAINER_H_
+
+#include <boost/mpl/inherit_linearly.hpp>
+#include <boost/mpl/inherit.hpp>
+#include <boost/mpl/at.hpp>
+
+namespace ns3 {
+namespace ndn {
+namespace ndnSIM {
+namespace detail {
+
+template <class T>
+struct wrap
+{
+ T value_;
+};
+
+template< class Vector >
+struct multi_type_container
+ : public boost::mpl::inherit_linearly< Vector, boost::mpl::inherit<wrap<boost::mpl::_2>, boost::mpl::_1 >
+ >::type
+{
+ template<int N>
+ struct index
+ {
+ typedef typename boost::mpl::at_c<Vector, N>::type type;
+ };
+
+ template<class T>
+ T &
+ get ()
+ {
+ return static_cast< wrap<T> &> (*this).value_;
+ }
+
+ template<class T>
+ const T &
+ get () const
+ {
+ return static_cast< const wrap<T> &> (*this).value_;
+ }
+
+ template<int N>
+ typename boost::mpl::at_c<Vector, N>::type &
+ get ()
+ {
+ typedef typename boost::mpl::at_c<Vector, N>::type T;
+ return static_cast< wrap<T> &> (*this).value_;
+ }
+
+ template<int N>
+ const typename boost::mpl::at_c<Vector, N>::type &
+ get () const
+ {
+ typedef typename boost::mpl::at_c<Vector, N>::type T;
+ return static_cast< const wrap<T> &> (*this).value_;
+ }
+};
+
+} // detail
+} // ndnSIM
+} // ndn
+} // ns3
+
+#endif // MULTI_TYPE_CONTAINER_H_
diff --git a/ndn-cpp/trie/payload-traits/pointer.h b/ndn-cpp/trie/payload-traits/pointer.h
new file mode 100644
index 0000000..8290fbe
--- /dev/null
+++ b/ndn-cpp/trie/payload-traits/pointer.h
@@ -0,0 +1,40 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_TRIE_PAYLOAD_TRAITS_POINTER_H
+#define NDN_TRIE_PAYLOAD_TRAITS_POINTER_H
+
+namespace ndn {
+namespace trie {
+
+template<typename Payload, typename BasePayload = Payload>
+struct pointer_payload_traits
+{
+ typedef Payload payload_type; // general type of the payload
+ typedef Payload* storage_type; // how the payload is actually stored
+ typedef Payload* insert_type; // what parameter is inserted
+
+ typedef Payload* return_type; // what is returned on access
+ typedef const Payload* const_return_type; // what is returned on const access
+
+ typedef BasePayload* base_type; // base type of the entry (when implementation details need to be hidden)
+ typedef const BasePayload* const_base_type; // const base type of the entry (when implementation details need to be hidden)
+
+ static Payload* empty_payload;
+};
+
+template<typename Payload, typename BasePayload>
+Payload*
+pointer_payload_traits<Payload, BasePayload>::empty_payload = NULL;
+
+} // trie
+} // ndn
+
+#endif // NDN_TRIE_PAYLOAD_TRAITS_POINTER_H
diff --git a/ndn-cpp/trie/payload-traits/ptr.h b/ndn-cpp/trie/payload-traits/ptr.h
new file mode 100644
index 0000000..3caf020
--- /dev/null
+++ b/ndn-cpp/trie/payload-traits/ptr.h
@@ -0,0 +1,40 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_TRIE_PAYLOAD_TRAITS_PTR_H
+#define NDN_TRIE_PAYLOAD_TRAITS_PTR_H
+
+namespace ndn {
+namespace trie {
+
+template<typename Payload, typename BasePayload = Payload>
+struct ptr_payload_traits
+{
+ typedef Payload payload_type;
+ typedef Ptr<Payload> storage_type;
+ typedef Ptr<Payload> insert_type;
+
+ typedef Ptr<Payload> return_type;
+ typedef Ptr<const Payload> const_return_type;
+
+ typedef Ptr<BasePayload> base_type;
+ typedef Ptr<const BasePayload> const_base_type;
+
+ static Ptr<Payload> empty_payload;
+};
+
+template<typename Payload, typename BasePayload>
+Ptr<Payload>
+ptr_payload_traits<Payload, BasePayload>::empty_payload; // = Ptr<Payload> ();
+
+} // trie
+} // ndn
+
+#endif // NDN_TRIE_PAYLOAD_TRAITS_PTR_H
diff --git a/ndn-cpp/trie/policies/counting-policy.h b/ndn-cpp/trie/policies/counting-policy.h
new file mode 100644
index 0000000..d27f915
--- /dev/null
+++ b/ndn-cpp/trie/policies/counting-policy.h
@@ -0,0 +1,101 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef COUNTING_POLICY_H_
+#define COUNTING_POLICY_H_
+
+#include <boost/intrusive/options.hpp>
+#include <boost/intrusive/list.hpp>
+
+namespace ndn {
+namespace trie {
+
+/**
+ * @brief Traits for policy that just keeps track of number of elements
+ * It's doing a rather expensive job, but just in case it needs to be extended later
+ */
+struct counting_policy_traits
+{
+ /// @brief Name that can be used to identify the policy (e.g., for logging)
+ static std::string GetName () { return "Counting"; }
+
+ struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
+
+ template<class Container>
+ struct container_hook
+ {
+ // could be class/struct implementation
+ typedef boost::intrusive::member_hook< Container,
+ policy_hook_type,
+ &Container::policy_hook_ > type;
+ };
+
+ template<class Base,
+ class Container,
+ class Hook>
+ struct policy
+ {
+ typedef typename boost::intrusive::list< Container, Hook > policy_container;
+
+ // could be just typedef
+ class type : public policy_container
+ {
+ public:
+ typedef Container parent_trie;
+
+ type (Base &base)
+ : base_ (base)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ // do nothing
+ }
+
+ inline bool
+ insert (typename parent_trie::iterator item)
+ {
+ policy_container::push_back (*item);
+ return true;
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ // do nothing
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
+
+ private:
+ type () : base_(*((Base*)0)) { };
+
+ private:
+ Base &base_;
+ };
+ };
+};
+
+} // trie
+} // ndn
+
+#endif // COUNTING_POLICY_H_
diff --git a/ndn-cpp/trie/policies/empty-policy.h b/ndn-cpp/trie/policies/empty-policy.h
new file mode 100644
index 0000000..0cfe962
--- /dev/null
+++ b/ndn-cpp/trie/policies/empty-policy.h
@@ -0,0 +1,50 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef EMPTY_POLICY_H_
+#define EMPTY_POLICY_H_
+
+namespace ndn {
+namespace trie {
+
+/**
+ * @brief Traits for empty (bogus) replacement policy
+ */
+struct empty_policy_traits
+{
+ /// @brief Name that can be used to identify the policy (e.g., for logging)
+ static std::string GetName () { return ""; }
+
+ typedef void* policy_hook_type;
+
+ template<class Container> struct container_hook { typedef void* type; };
+
+ template<class Base,
+ class Container,
+ class Hook>
+ struct policy
+ {
+ struct type
+ {
+ inline type (Base &base) {}
+
+ inline void update (typename Container::iterator) { }
+ inline bool insert (typename Container::iterator) { return true; }
+ inline void lookup (typename Container::iterator item) { }
+ inline void erase (typename Container::iterator item) { }
+ inline void clear () { }
+ };
+ };
+};
+
+} // trie
+} // ndn
+
+#endif // EMPTY_POLICY_H_
diff --git a/ndn-cpp/trie/policies/fifo-policy.h b/ndn-cpp/trie/policies/fifo-policy.h
new file mode 100644
index 0000000..7acad2b
--- /dev/null
+++ b/ndn-cpp/trie/policies/fifo-policy.h
@@ -0,0 +1,119 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef FIFO_POLICY_H_
+#define FIFO_POLICY_H_
+
+#include <boost/intrusive/options.hpp>
+#include <boost/intrusive/list.hpp>
+
+namespace ndn {
+namespace trie {
+
+/**
+ * @brief Traits for First In First Out replacement policy
+ */
+struct fifo_policy_traits
+{
+ /// @brief Name that can be used to identify the policy (e.g., for logging)
+ static std::string GetName () { return "Fifo"; }
+
+ struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
+
+ template<class Container>
+ struct container_hook
+ {
+ // could be class/struct implementation
+ typedef boost::intrusive::member_hook< Container,
+ policy_hook_type,
+ &Container::policy_hook_ > type;
+ };
+
+ template<class Base,
+ class Container,
+ class Hook>
+ struct policy
+ {
+ typedef typename boost::intrusive::list< Container, Hook > policy_container;
+
+ // could be just typedef
+ class type : public policy_container
+ {
+ public:
+ typedef Container parent_trie;
+
+ type (Base &base)
+ : base_ (base)
+ , max_size_ (100)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ // do nothing
+ }
+
+ inline bool
+ insert (typename parent_trie::iterator item)
+ {
+ if (max_size_ != 0 && policy_container::size () >= max_size_)
+ {
+ base_.erase (&(*policy_container::begin ()));
+ }
+
+ policy_container::push_back (*item);
+ return true;
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ // do nothing
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
+
+ inline void
+ set_max_size (size_t max_size)
+ {
+ max_size_ = max_size;
+ }
+
+ inline size_t
+ get_max_size () const
+ {
+ return max_size_;
+ }
+
+ private:
+ type () : base_(*((Base*)0)) { };
+
+ private:
+ Base &base_;
+ size_t max_size_;
+ };
+ };
+};
+
+} // trie
+} // ndn
+
+#endif
diff --git a/ndn-cpp/trie/policies/lfu-policy.h b/ndn-cpp/trie/policies/lfu-policy.h
new file mode 100644
index 0000000..802f64b
--- /dev/null
+++ b/ndn-cpp/trie/policies/lfu-policy.h
@@ -0,0 +1,149 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef LFU_POLICY_H_
+#define LFU_POLICY_H_
+
+#include <boost/intrusive/options.hpp>
+#include <boost/intrusive/set.hpp>
+
+namespace ndn {
+namespace trie {
+
+/**
+ * @brief Traits for LFU replacement policy
+ */
+struct lfu_policy_traits
+{
+ /// @brief Name that can be used to identify the policy (e.g., for logging)
+ static std::string GetName () { return "Lfu"; }
+
+ struct policy_hook_type : public boost::intrusive::set_member_hook<> { double frequency; };
+
+ template<class Container>
+ struct container_hook
+ {
+ typedef boost::intrusive::member_hook< Container,
+ policy_hook_type,
+ &Container::policy_hook_ > type;
+ };
+
+ template<class Base,
+ class Container,
+ class Hook>
+ struct policy
+ {
+ static double& get_order (typename Container::iterator item)
+ {
+ return static_cast<policy_hook_type*>
+ (policy_container::value_traits::to_node_ptr(*item))->frequency;
+ }
+
+ static const double& get_order (typename Container::const_iterator item)
+ {
+ return static_cast<const policy_hook_type*>
+ (policy_container::value_traits::to_node_ptr(*item))->frequency;
+ }
+
+ template<class Key>
+ struct MemberHookLess
+ {
+ bool operator () (const Key &a, const Key &b) const
+ {
+ return get_order (&a) < get_order (&b);
+ }
+ };
+
+ typedef boost::intrusive::multiset< Container,
+ boost::intrusive::compare< MemberHookLess< Container > >,
+ Hook > policy_container;
+
+ // could be just typedef
+ class type : public policy_container
+ {
+ public:
+ typedef policy policy_base; // to get access to get_order methods from outside
+ typedef Container parent_trie;
+
+ type (Base &base)
+ : base_ (base)
+ , max_size_ (100)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ get_order (item) += 1;
+ policy_container::insert (*item);
+ }
+
+ inline bool
+ insert (typename parent_trie::iterator item)
+ {
+ get_order (item) = 0;
+
+ if (max_size_ != 0 && policy_container::size () >= max_size_)
+ {
+ // this erases the "least frequently used item" from cache
+ base_.erase (&(*policy_container::begin ()));
+ }
+
+ policy_container::insert (*item);
+ return true;
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ get_order (item) += 1;
+ policy_container::insert (*item);
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
+
+ inline void
+ set_max_size (size_t max_size)
+ {
+ max_size_ = max_size;
+ }
+
+ inline size_t
+ get_max_size () const
+ {
+ return max_size_;
+ }
+
+ private:
+ type () : base_(*((Base*)0)) { };
+
+ private:
+ Base &base_;
+ size_t max_size_;
+ };
+ };
+};
+
+} // trie
+} // ndn
+
+#endif // LFU_POLICY_H
diff --git a/ndn-cpp/trie/policies/lru-policy.h b/ndn-cpp/trie/policies/lru-policy.h
new file mode 100644
index 0000000..ad3a382
--- /dev/null
+++ b/ndn-cpp/trie/policies/lru-policy.h
@@ -0,0 +1,124 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef LRU_POLICY_H_
+#define LRU_POLICY_H_
+
+#include <boost/intrusive/options.hpp>
+#include <boost/intrusive/list.hpp>
+
+namespace ndn {
+namespace trie {
+
+/**
+ * @brief Traits for Least Recently Used replacement policy
+ */
+struct lru_policy_traits
+{
+ /// @brief Name that can be used to identify the policy (e.g., for logging)
+ static std::string GetName () { return "Lru"; }
+
+ struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
+
+ template<class Container>
+ struct container_hook
+ {
+ typedef boost::intrusive::member_hook< Container,
+ policy_hook_type,
+ &Container::policy_hook_ > type;
+ };
+
+ template<class Base,
+ class Container,
+ class Hook>
+ struct policy
+ {
+ typedef typename boost::intrusive::list< Container, Hook > policy_container;
+
+ // could be just typedef
+ class type : public policy_container
+ {
+ public:
+ typedef Container parent_trie;
+
+ type (Base &base)
+ : base_ (base)
+ , max_size_ (100)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ // do relocation
+ policy_container::splice (policy_container::end (),
+ *this,
+ policy_container::s_iterator_to (*item));
+ }
+
+ inline bool
+ insert (typename parent_trie::iterator item)
+ {
+ if (max_size_ != 0 && policy_container::size () >= max_size_)
+ {
+ base_.erase (&(*policy_container::begin ()));
+ }
+
+ policy_container::push_back (*item);
+ return true;
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ // do relocation
+ policy_container::splice (policy_container::end (),
+ *this,
+ policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
+
+ inline void
+ set_max_size (size_t max_size)
+ {
+ max_size_ = max_size;
+ }
+
+ inline size_t
+ get_max_size () const
+ {
+ return max_size_;
+ }
+
+ private:
+ type () : base_(*((Base*)0)) { };
+
+ private:
+ Base &base_;
+ size_t max_size_;
+ };
+ };
+};
+
+} // trie
+} // ndn
+
+#endif
diff --git a/ndn-cpp/trie/policies/multi-policy.h b/ndn-cpp/trie/policies/multi-policy.h
new file mode 100644
index 0000000..739de7b
--- /dev/null
+++ b/ndn-cpp/trie/policies/multi-policy.h
@@ -0,0 +1,177 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef MULTI_POLICY_H_
+#define MULTI_POLICY_H_
+
+#include "detail/multi-type-container.h"
+#include "detail/multi-policy-container.h"
+#include "detail/functor-hook.h"
+
+#include <boost/mpl/size.hpp>
+#include <boost/mpl/at.hpp>
+#include <boost/mpl/range_c.hpp>
+#include <boost/mpl/transform.hpp>
+#include <boost/mpl/back_inserter.hpp>
+#include <boost/mpl/vector.hpp>
+#include <boost/mpl/for_each.hpp>
+
+#include <boost/intrusive/options.hpp>
+
+namespace ndn {
+namespace trie {
+
+template<typename Policies> // e.g., mpl::vector1< lru_policy_traits >
+struct multi_policy_traits
+{
+ typedef Policies policy_traits;
+
+ struct getHook { template<class Item> struct apply { typedef typename Item::policy_hook_type type; }; };
+ typedef detail::multi_type_container< typename boost::mpl::transform1<policy_traits, getHook>::type > policy_hook_type;
+
+ template<class Container>
+ struct container_hook
+ {
+ typedef policy_hook_type type;
+ };
+
+ template<class Base,
+ class Container,
+ class Hook>
+ struct policy
+ {
+ typedef boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value> policies_range;
+
+ struct getPolicy
+ {
+ template<class Number>
+ struct apply
+ {
+ typedef
+ typename boost::mpl::at_c<policy_traits, Number::value>::type::
+ template policy<Base,
+ Container,
+ boost::intrusive::function_hook< detail::FunctorHook <Hook,
+ Container,
+ Number::value> > >::type
+ type;
+ };
+ };
+
+ typedef
+ typename boost::mpl::transform1<policies_range,
+ getPolicy,
+ boost::mpl::back_inserter< boost::mpl::vector0<> > >::type policies;
+
+
+ typedef detail::multi_policy_container< Base, policies > policy_container;
+
+ class type : public policy_container
+ {
+ public:
+ typedef policy policy_base; // to get access to get_time methods from outside
+ typedef Container parent_trie;
+
+ type (Base &base)
+ : policy_container (base)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ policy_container::update (item);
+ }
+
+ inline bool
+ insert (typename parent_trie::iterator item)
+ {
+ return policy_container::insert (item);
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ policy_container::lookup (item);
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (item);
+ }
+
+ inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
+
+ struct max_size_setter
+ {
+ max_size_setter (policy_container &container, size_t size) : m_container (container), m_size (size) { }
+
+ template< typename U > void operator() (U index)
+ {
+ m_container.template get<U::value> ().set_max_size (m_size);
+ }
+
+ private:
+ policy_container &m_container;
+ size_t m_size;
+ };
+
+ inline void
+ set_max_size (size_t max_size)
+ {
+ boost::mpl::for_each< boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value> >
+ (max_size_setter (*this, max_size));
+ }
+
+ inline size_t
+ get_max_size () const
+ {
+ // as max size should be the same everywhere, get the value from the first available policy
+ return policy_container::template get<0> ().get_max_size ();
+ }
+
+ };
+ };
+
+
+ struct name_getter
+ {
+ name_getter (std::string &name) : m_name (name) { }
+
+ template< typename U > void operator() (U index)
+ {
+ if (!m_name.empty ())
+ m_name += "::";
+ m_name += boost::mpl::at_c< policy_traits, U::value >::type::GetName ();
+ }
+
+ std::string &m_name;
+ };
+
+ /// @brief Name that can be used to identify the policy (e.g., for logging)
+ static std::string GetName ()
+ {
+ // combine names of all internal policies
+ std::string name;
+ boost::mpl::for_each< boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value> > (name_getter (name));
+
+ return name;
+ }
+};
+
+} // trie
+} // ndn
+
+#endif // MULTI_POLICY_H_
diff --git a/ndn-cpp/trie/policies/persistent-policy.h b/ndn-cpp/trie/policies/persistent-policy.h
new file mode 100644
index 0000000..d34f373
--- /dev/null
+++ b/ndn-cpp/trie/policies/persistent-policy.h
@@ -0,0 +1,119 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef PERSISTENT_POLICY_H_
+#define PERSISTENT_POLICY_H_
+
+#include <boost/intrusive/options.hpp>
+#include <boost/intrusive/list.hpp>
+
+namespace ndn {
+namespace trie {
+
+/**
+ * @brief Traits for persistent replacement policy
+ *
+ * In this policy entries are added until there is a space (controlled by set_max_size call).
+ * If maximum is reached, new entries will not be added and nothing will be removed from the container
+ */
+struct persistent_policy_traits
+{
+ /// @brief Name that can be used to identify the policy (e.g., for logging)
+ static std::string GetName () { return "Persistent"; }
+
+ struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
+
+ template<class Container>
+ struct container_hook
+ {
+ typedef boost::intrusive::member_hook< Container,
+ policy_hook_type,
+ &Container::policy_hook_ > type;
+ };
+
+ template<class Base,
+ class Container,
+ class Hook>
+ struct policy
+ {
+ typedef typename boost::intrusive::list< Container, Hook > policy_container;
+
+ // could be just typedef
+ class type : public policy_container
+ {
+ public:
+ typedef Container parent_trie;
+
+ type (Base &base)
+ : base_ (base)
+ , max_size_ (100) // when 0, policy is not enforced
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ // do nothing
+ }
+
+ inline bool
+ insert (typename parent_trie::iterator item)
+ {
+ if (max_size_ != 0 && policy_container::size () >= max_size_)
+ return false;
+
+ policy_container::push_back (*item);
+ return true;
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ // do nothing
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
+
+ inline void
+ set_max_size (size_t max_size)
+ {
+ max_size_ = max_size;
+ }
+
+ inline size_t
+ get_max_size () const
+ {
+ return max_size_;
+ }
+
+ private:
+ // type () : base_(*((Base*)0)) { };
+
+ private:
+ Base &base_;
+ size_t max_size_;
+ };
+ };
+};
+
+} // trie
+} // ndn
+
+#endif // PERSISTENT_POLICY_H_
diff --git a/ndn-cpp/trie/policies/random-policy.h b/ndn-cpp/trie/policies/random-policy.h
new file mode 100644
index 0000000..d896e89
--- /dev/null
+++ b/ndn-cpp/trie/policies/random-policy.h
@@ -0,0 +1,158 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef RANDOM_POLICY_H_
+#define RANDOM_POLICY_H_
+
+#include "ns3/random-variable.h"
+
+#include <boost/intrusive/options.hpp>
+#include <boost/intrusive/set.hpp>
+
+namespace ndn {
+namespace trie {
+
+/**
+ * @brief Traits for random replacement policy
+ */
+struct random_policy_traits
+{
+ /// @brief Name that can be used to identify the policy (e.g., for logging)
+ static std::string GetName () { return "Random"; }
+
+ struct policy_hook_type : public boost::intrusive::set_member_hook<> { uint32_t randomOrder; };
+
+ template<class Container>
+ struct container_hook
+ {
+ typedef boost::intrusive::member_hook< Container,
+ policy_hook_type,
+ &Container::policy_hook_ > type;
+ };
+
+ template<class Base,
+ class Container,
+ class Hook>
+ struct policy
+ {
+ static uint32_t& get_order (typename Container::iterator item)
+ {
+ return static_cast<typename policy_container::value_traits::hook_type*>
+ (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
+ }
+
+ static const uint32_t& get_order (typename Container::const_iterator item)
+ {
+ return static_cast<const typename policy_container::value_traits::hook_type*>
+ (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
+ }
+
+ template<class Key>
+ struct MemberHookLess
+ {
+ bool operator () (const Key &a, const Key &b) const
+ {
+ return get_order (&a) < get_order (&b);
+ }
+ };
+
+ typedef boost::intrusive::multiset< Container,
+ boost::intrusive::compare< MemberHookLess< Container > >,
+ Hook > policy_container;
+
+ // could be just typedef
+ class type : public policy_container
+ {
+ public:
+ typedef policy policy_base; // to get access to get_order methods from outside
+ typedef Container parent_trie;
+
+ type (Base &base)
+ : base_ (base)
+ , u_rand (0, std::numeric_limits<uint32_t>::max ())
+ , max_size_ (100)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ // do nothing. it's random policy
+ }
+
+ inline bool
+ insert (typename parent_trie::iterator item)
+ {
+ get_order (item) = u_rand.GetValue ();
+
+ if (max_size_ != 0 && policy_container::size () >= max_size_)
+ {
+ if (MemberHookLess<Container>() (*item, *policy_container::begin ()))
+ {
+ // std::cout << "Cannot add. Signaling fail\n";
+ // just return false. Indicating that insert "failed"
+ return false;
+ }
+ else
+ {
+ // removing some random element
+ base_.erase (&(*policy_container::begin ()));
+ }
+ }
+
+ policy_container::insert (*item);
+ return true;
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ // do nothing. it's random policy
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ }
+
+ inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
+
+ inline void
+ set_max_size (size_t max_size)
+ {
+ max_size_ = max_size;
+ }
+
+ inline size_t
+ get_max_size () const
+ {
+ return max_size_;
+ }
+
+ private:
+ type () : base_(*((Base*)0)) { };
+
+ private:
+ Base &base_;
+ ns3::UniformVariable u_rand;
+ size_t max_size_;
+ };
+ };
+};
+
+} // trie
+} // ndn
+
+#endif // RANDOM_POLICY_H
diff --git a/ndn-cpp/trie/trie-with-policy.h b/ndn-cpp/trie/trie-with-policy.h
new file mode 100644
index 0000000..5a0d896
--- /dev/null
+++ b/ndn-cpp/trie/trie-with-policy.h
@@ -0,0 +1,263 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef TRIE_TRIE_WITH_POLICY_H_
+#define TRIE_TRIE_WITH_POLICY_H_
+
+#include "trie.h"
+
+namespace ndn {
+namespace trie {
+
+template<typename FullKey,
+ typename PayloadTraits,
+ typename PolicyTraits
+ >
+class trie_with_policy
+{
+public:
+ typedef trie< FullKey,
+ PayloadTraits,
+ typename PolicyTraits::policy_hook_type > parent_trie;
+
+ typedef typename parent_trie::iterator iterator;
+ typedef typename parent_trie::const_iterator const_iterator;
+ typedef typename parent_trie::payload_traits payload_traits;
+
+ typedef typename PolicyTraits::template policy<
+ trie_with_policy<FullKey, PayloadTraits, PolicyTraits>,
+ parent_trie,
+ typename PolicyTraits::template container_hook<parent_trie>::type >::type policy_container;
+
+ inline
+ trie_with_policy (size_t bucketSize = 10, size_t bucketIncrement = 10)
+ : trie_ ("", bucketSize, bucketIncrement)
+ , policy_ (*this)
+ {
+ }
+
+ inline std::pair< iterator, bool >
+ insert (const FullKey &key, typename PayloadTraits::insert_type payload)
+ {
+ std::pair<iterator, bool> item =
+ trie_.insert (key, payload);
+
+ if (item.second) // real insert
+ {
+ bool ok = policy_.insert (s_iterator_to (item.first));
+ if (!ok)
+ {
+ item.first->erase (); // cannot insert
+ return std::make_pair (end (), false);
+ }
+ }
+ else
+ {
+ return std::make_pair (s_iterator_to (item.first), false);
+ }
+
+ return item;
+ }
+
+ inline void
+ erase (const FullKey &key)
+ {
+ iterator foundItem, lastItem;
+ bool reachLast;
+ boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
+
+ if (!reachLast || lastItem->payload () == PayloadTraits::empty_payload)
+ return; // nothing to invalidate
+
+ erase (lastItem);
+ }
+
+ inline void
+ erase (iterator node)
+ {
+ if (node == end ()) return;
+
+ policy_.erase (s_iterator_to (node));
+ node->erase (); // will do cleanup here
+ }
+
+ inline void
+ clear ()
+ {
+ policy_.clear ();
+ trie_.clear ();
+ }
+
+ template<typename Modifier>
+ bool
+ modify (iterator position, Modifier mod)
+ {
+ if (position == end ()) return false;
+ if (position->payload () == PayloadTraits::empty_payload) return false;
+
+ mod (*position->payload ());
+ policy_.update (position);
+ return true;
+ }
+
+ /**
+ * @brief Find a node that has the exact match with the key
+ */
+ inline iterator
+ find_exact (const FullKey &key)
+ {
+ iterator foundItem, lastItem;
+ bool reachLast;
+ boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
+
+ if (!reachLast || lastItem->payload () == PayloadTraits::empty_payload)
+ return end ();
+
+ return lastItem;
+ }
+
+ /**
+ * @brief Find a node that has the longest common prefix with key (FIB/PIT lookup)
+ */
+ inline iterator
+ longest_prefix_match (const FullKey &key)
+ {
+ iterator foundItem, lastItem;
+ bool reachLast;
+ boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
+ if (foundItem != trie_.end ())
+ {
+ policy_.lookup (s_iterator_to (foundItem));
+ }
+ return foundItem;
+ }
+
+ /**
+ * @brief Find a node that has the longest common prefix with key (FIB/PIT lookup)
+ */
+ template<class Predicate>
+ inline iterator
+ longest_prefix_match_if (const FullKey &key, Predicate pred)
+ {
+ iterator foundItem, lastItem;
+ bool reachLast;
+ boost::tie (foundItem, reachLast, lastItem) = trie_.find_if (key, pred);
+ if (foundItem != trie_.end ())
+ {
+ policy_.lookup (s_iterator_to (foundItem));
+ }
+ return foundItem;
+ }
+
+ // /**
+ // * @brief Const version of the longest common prefix match
+ // * (semi-const, because there could be update of the policy anyways)
+ // */
+ // inline const_iterator
+ // longest_prefix_match (const FullKey &key) const
+ // {
+ // return static_cast<trie_with_policy*> (this)->longest_prefix_match (key);
+ // }
+
+ /**
+ * @brief Find a node that has prefix at least as the key (cache lookup)
+ */
+ inline iterator
+ deepest_prefix_match (const FullKey &key)
+ {
+ iterator foundItem, lastItem;
+ bool reachLast;
+ boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
+
+ // guard in case we don't have anything in the trie
+ if (lastItem == trie_.end ())
+ return trie_.end ();
+
+ if (reachLast)
+ {
+ if (foundItem == trie_.end ())
+ {
+ foundItem = lastItem->find (); // should be something
+ }
+ policy_.lookup (s_iterator_to (foundItem));
+ return foundItem;
+ }
+ else
+ { // couldn't find a node that has prefix at least as key
+ return trie_.end ();
+ }
+ }
+
+ /**
+ * @brief Find a node that has prefix at least as the key
+ */
+ template<class Predicate>
+ inline iterator
+ deepest_prefix_match (const FullKey &key, Predicate pred)
+ {
+ iterator foundItem, lastItem;
+ bool reachLast;
+ boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
+
+ // guard in case we don't have anything in the trie
+ if (lastItem == trie_.end ())
+ return trie_.end ();
+
+ if (reachLast)
+ {
+ foundItem = lastItem->find_if (pred); // may or may not find something
+ if (foundItem == trie_.end ())
+ {
+ return trie_.end ();
+ }
+ policy_.lookup (s_iterator_to (foundItem));
+ return foundItem;
+ }
+ else
+ { // couldn't find a node that has prefix at least as key
+ return trie_.end ();
+ }
+ }
+
+ iterator end () const
+ {
+ return 0;
+ }
+
+ const parent_trie &
+ getTrie () const { return trie_; }
+
+ parent_trie &
+ getTrie () { return trie_; }
+
+ const policy_container &
+ getPolicy () const { return policy_; }
+
+ policy_container &
+ getPolicy () { return policy_; }
+
+ static inline iterator
+ s_iterator_to (typename parent_trie::iterator item)
+ {
+ if (item == 0)
+ return 0;
+ else
+ return &(*item);
+ }
+
+private:
+ parent_trie trie_;
+ mutable policy_container policy_;
+};
+
+} // trie
+} // ndn
+
+#endif // TRIE_TRIE_WITH_POLICY_H_
diff --git a/ndn-cpp/trie/trie.h b/ndn-cpp/trie/trie.h
new file mode 100644
index 0000000..edc8a1b
--- /dev/null
+++ b/ndn-cpp/trie/trie.h
@@ -0,0 +1,636 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_TRIE_TRIE_H_
+#define NDN_TRIE_TRIE_H_
+
+#include <boost/intrusive/unordered_set.hpp>
+#include <boost/intrusive/list.hpp>
+#include <boost/intrusive/set.hpp>
+#include <boost/functional/hash.hpp>
+#include <boost/interprocess/smart_ptr/unique_ptr.hpp>
+#include <boost/tuple/tuple.hpp>
+#include <boost/foreach.hpp>
+#include <boost/mpl/if.hpp>
+
+#include "payload-traits/pointer.h"
+#include "payload-traits/ptr.h"
+
+namespace ndn {
+namespace trie {
+
+////////////////////////////////////////////////////
+// forward declarations
+//
+template<typename FullKey,
+ typename PayloadTraits,
+ typename PolicyHook >
+class trie;
+
+template<typename FullKey, typename PayloadTraits, typename PolicyHook>
+inline std::ostream&
+operator << (std::ostream &os,
+ const trie<FullKey, PayloadTraits, PolicyHook> &trie_node);
+
+template<typename FullKey, typename PayloadTraits, typename PolicyHook>
+bool
+operator== (const trie<FullKey, PayloadTraits, PolicyHook> &a,
+ const trie<FullKey, PayloadTraits, PolicyHook> &b);
+
+template<typename FullKey, typename PayloadTraits, typename PolicyHook >
+std::size_t
+hash_value (const trie<FullKey, PayloadTraits, PolicyHook> &trie_node);
+
+///////////////////////////////////////////////////
+// actual definition
+//
+template<class T, class NonConstT>
+class trie_iterator;
+
+template<class T>
+class trie_point_iterator;
+
+template<typename FullKey,
+ typename PayloadTraits,
+ typename PolicyHook >
+class trie
+{
+public:
+ typedef typename FullKey::partial_type Key;
+
+ typedef trie* iterator;
+ typedef const trie* const_iterator;
+
+ typedef trie_iterator<trie, trie> recursive_iterator;
+ typedef trie_iterator<const trie, trie> const_recursive_iterator;
+
+ typedef trie_point_iterator<trie> point_iterator;
+ typedef trie_point_iterator<const trie> const_point_iterator;
+
+ typedef PayloadTraits payload_traits;
+
+ inline
+ trie (const Key &key, size_t bucketSize = 10, size_t bucketIncrement = 10)
+ : key_ (key)
+ , initialBucketSize_ (bucketSize)
+ , bucketIncrement_ (bucketIncrement)
+ , bucketSize_ (initialBucketSize_)
+ , buckets_ (new bucket_type [bucketSize_]) //cannot use normal pointer, because lifetime of buckets should be larger than lifetime of the container
+ , children_ (bucket_traits (buckets_.get (), bucketSize_))
+ , payload_ (PayloadTraits::empty_payload)
+ , parent_ (0)
+ {
+ }
+
+ inline
+ ~trie ()
+ {
+ payload_ = PayloadTraits::empty_payload; // necessary for smart pointers...
+ children_.clear_and_dispose (trie_delete_disposer ());
+ }
+
+ void
+ clear ()
+ {
+ children_.clear_and_dispose (trie_delete_disposer ());
+ }
+
+ template<class Predicate>
+ void
+ clear_if (Predicate cond)
+ {
+ recursive_iterator trieNode (this);
+ recursive_iterator end (0);
+
+ while (trieNode != end)
+ {
+ if (cond (*trieNode))
+ {
+ trieNode = recursive_iterator (trieNode->erase ());
+ }
+ trieNode ++;
+ }
+ }
+
+ // actual entry
+ friend bool
+ operator== <> (const trie<FullKey, PayloadTraits, PolicyHook> &a,
+ const trie<FullKey, PayloadTraits, PolicyHook> &b);
+
+ friend std::size_t
+ hash_value <> (const trie<FullKey, PayloadTraits, PolicyHook> &trie_node);
+
+ inline std::pair<iterator, bool>
+ insert (const FullKey &key,
+ typename PayloadTraits::insert_type payload)
+ {
+ trie *trieNode = this;
+
+ BOOST_FOREACH (const Key &subkey, key)
+ {
+ typename unordered_set::iterator item = trieNode->children_.find (subkey);
+ if (item == trieNode->children_.end ())
+ {
+ trie *newNode = new trie (subkey, initialBucketSize_, bucketIncrement_);
+ // std::cout << "new " << newNode << "\n";
+ newNode->parent_ = trieNode;
+
+ if (trieNode->children_.size () >= trieNode->bucketSize_)
+ {
+ trieNode->bucketSize_ += trieNode->bucketIncrement_;
+ trieNode->bucketIncrement_ *= 2; // increase bucketIncrement exponentially
+
+ buckets_array newBuckets (new bucket_type [trieNode->bucketSize_]);
+ trieNode->children_.rehash (bucket_traits (newBuckets.get (), trieNode->bucketSize_));
+ trieNode->buckets_.swap (newBuckets);
+ }
+
+ std::pair< typename unordered_set::iterator, bool > ret =
+ trieNode->children_.insert (*newNode);
+
+ trieNode = &(*ret.first);
+ }
+ else
+ trieNode = &(*item);
+ }
+
+ if (trieNode->payload_ == PayloadTraits::empty_payload)
+ {
+ trieNode->payload_ = payload;
+ return std::make_pair (trieNode, true);
+ }
+ else
+ return std::make_pair (trieNode, false);
+ }
+
+ /**
+ * @brief Removes payload (if it exists) and if there are no children, prunes parents trie
+ */
+ inline iterator
+ erase ()
+ {
+ payload_ = PayloadTraits::empty_payload;
+ return prune ();
+ }
+
+ /**
+ * @brief Do exactly as erase, but without erasing the payload
+ */
+ inline iterator
+ prune ()
+ {
+ if (payload_ == PayloadTraits::empty_payload &&
+ children_.size () == 0)
+ {
+ if (parent_ == 0) return this;
+
+ trie *parent = parent_;
+ parent->children_.erase_and_dispose (*this, trie_delete_disposer ()); // delete this; basically, committing a suicide
+
+ return parent->prune ();
+ }
+ return this;
+ }
+
+ /**
+ * @brief Perform prune of the node, but without attempting to parent of the node
+ */
+ inline void
+ prune_node ()
+ {
+ if (payload_ == PayloadTraits::empty_payload &&
+ children_.size () == 0)
+ {
+ if (parent_ == 0) return;
+
+ trie *parent = parent_;
+ parent->children_.erase_and_dispose (*this, trie_delete_disposer ()); // delete this; basically, committing a suicide
+ }
+ }
+
+ // inline boost::tuple<const iterator, bool, const iterator>
+ // find (const FullKey &key) const
+ // {
+ // return const_cast<trie*> (this)->find (key);
+ // }
+
+ /**
+ * @brief Perform the longest prefix match
+ * @param key the key for which to perform the longest prefix match
+ *
+ * @return ->second is true if prefix in ->first is longer than key
+ */
+ inline boost::tuple<iterator, bool, iterator>
+ find (const FullKey &key)
+ {
+ trie *trieNode = this;
+ iterator foundNode = (payload_ != PayloadTraits::empty_payload) ? this : 0;
+ bool reachLast = true;
+
+ BOOST_FOREACH (const Key &subkey, key)
+ {
+ typename unordered_set::iterator item = trieNode->children_.find (subkey);
+ if (item == trieNode->children_.end ())
+ {
+ reachLast = false;
+ break;
+ }
+ else
+ {
+ trieNode = &(*item);
+
+ if (trieNode->payload_ != PayloadTraits::empty_payload)
+ foundNode = trieNode;
+ }
+ }
+
+ return boost::make_tuple (foundNode, reachLast, trieNode);
+ }
+
+ /**
+ * @brief Perform the longest prefix match satisfying preficate
+ * @param key the key for which to perform the longest prefix match
+ *
+ * @return ->second is true if prefix in ->first is longer than key
+ */
+ template<class Predicate>
+ inline boost::tuple<iterator, bool, iterator>
+ find_if (const FullKey &key, Predicate pred)
+ {
+ trie *trieNode = this;
+ iterator foundNode = (payload_ != PayloadTraits::empty_payload) ? this : 0;
+ bool reachLast = true;
+
+ BOOST_FOREACH (const Key &subkey, key)
+ {
+ typename unordered_set::iterator item = trieNode->children_.find (subkey);
+ if (item == trieNode->children_.end ())
+ {
+ reachLast = false;
+ break;
+ }
+ else
+ {
+ trieNode = &(*item);
+
+ if (trieNode->payload_ != PayloadTraits::empty_payload &&
+ pred (trieNode->payload_))
+ {
+ foundNode = trieNode;
+ }
+ }
+ }
+
+ return boost::make_tuple (foundNode, reachLast, trieNode);
+ }
+
+ /**
+ * @brief Find next payload of the sub-trie
+ * @returns end() or a valid iterator pointing to the trie leaf (order is not defined, enumeration )
+ */
+ inline iterator
+ find ()
+ {
+ if (payload_ != PayloadTraits::empty_payload)
+ return this;
+
+ typedef trie<FullKey, PayloadTraits, PolicyHook> trie;
+ for (typename trie::unordered_set::iterator subnode = children_.begin ();
+ subnode != children_.end ();
+ subnode++ )
+ // BOOST_FOREACH (trie &subnode, children_)
+ {
+ iterator value = subnode->find ();
+ if (value != 0)
+ return value;
+ }
+
+ return 0;
+ }
+
+ /**
+ * @brief Find next payload of the sub-trie satisfying the predicate
+ * @param pred predicate
+ * @returns end() or a valid iterator pointing to the trie leaf (order is not defined, enumeration )
+ */
+ template<class Predicate>
+ inline const iterator
+ find_if (Predicate pred)
+ {
+ if (payload_ != PayloadTraits::empty_payload && pred (payload_))
+ return this;
+
+ typedef trie<FullKey, PayloadTraits, PolicyHook> trie;
+ for (typename trie::unordered_set::iterator subnode = children_.begin ();
+ subnode != children_.end ();
+ subnode++ )
+ // BOOST_FOREACH (const trie &subnode, children_)
+ {
+ iterator value = subnode->find_if (pred);
+ if (value != 0)
+ return value;
+ }
+
+ return 0;
+ }
+
+ iterator end ()
+ {
+ return 0;
+ }
+
+ const_iterator end () const
+ {
+ return 0;
+ }
+
+ typename PayloadTraits::const_return_type
+ payload () const
+ {
+ return payload_;
+ }
+
+ typename PayloadTraits::return_type
+ payload ()
+ {
+ return payload_;
+ }
+
+ void
+ set_payload (typename PayloadTraits::insert_type payload)
+ {
+ payload_ = payload;
+ }
+
+ Key key () const
+ {
+ return key_;
+ }
+
+ inline void
+ PrintStat (std::ostream &os) const;
+
+private:
+ //The disposer object function
+ struct trie_delete_disposer
+ {
+ void operator() (trie *delete_this)
+ {
+ delete delete_this;
+ }
+ };
+
+ template<class D>
+ struct array_disposer
+ {
+ void operator() (D *array)
+ {
+ delete [] array;
+ }
+ };
+
+ friend
+ std::ostream&
+ operator<< < > (std::ostream &os, const trie &trie_node);
+
+public:
+ PolicyHook policy_hook_;
+
+private:
+ boost::intrusive::unordered_set_member_hook<> unordered_set_member_hook_;
+
+ // necessary typedefs
+ typedef trie self_type;
+ typedef boost::intrusive::member_hook< trie,
+ boost::intrusive::unordered_set_member_hook< >,
+ &trie::unordered_set_member_hook_ > member_hook;
+
+ typedef boost::intrusive::unordered_set< trie, member_hook > unordered_set;
+ typedef typename unordered_set::bucket_type bucket_type;
+ typedef typename unordered_set::bucket_traits bucket_traits;
+
+ template<class T, class NonConstT>
+ friend class trie_iterator;
+
+ template<class T>
+ friend class trie_point_iterator;
+
+ ////////////////////////////////////////////////
+ // Actual data
+ ////////////////////////////////////////////////
+
+ Key key_; ///< name component
+
+ size_t initialBucketSize_;
+ size_t bucketIncrement_;
+
+ size_t bucketSize_;
+ typedef boost::interprocess::unique_ptr< bucket_type, array_disposer<bucket_type> > buckets_array;
+ buckets_array buckets_;
+ unordered_set children_;
+
+ typename PayloadTraits::storage_type payload_;
+ trie *parent_; // to make cleaning effective
+};
+
+
+
+
+template<typename FullKey, typename PayloadTraits, typename PolicyHook>
+inline std::ostream&
+operator << (std::ostream &os, const trie<FullKey, PayloadTraits, PolicyHook> &trie_node)
+{
+ os << "# " << trie_node.key_ << ((trie_node.payload_ != PayloadTraits::empty_payload)?"*":"") << std::endl;
+ typedef trie<FullKey, PayloadTraits, PolicyHook> trie;
+
+ for (typename trie::unordered_set::const_iterator subnode = trie_node.children_.begin ();
+ subnode != trie_node.children_.end ();
+ subnode++ )
+ // BOOST_FOREACH (const trie &subnode, trie_node.children_)
+ {
+ os << "\"" << &trie_node << "\"" << " [label=\"" << trie_node.key_ << ((trie_node.payload_ != PayloadTraits::empty_payload)?"*":"") << "\"]\n";
+ os << "\"" << &(*subnode) << "\"" << " [label=\"" << subnode->key_ << ((subnode->payload_ != PayloadTraits::empty_payload)?"*":"") << "\"]""\n";
+
+ os << "\"" << &trie_node << "\"" << " -> " << "\"" << &(*subnode) << "\"" << "\n";
+ os << *subnode;
+ }
+
+ return os;
+}
+
+template<typename FullKey, typename PayloadTraits, typename PolicyHook>
+inline void
+trie<FullKey, PayloadTraits, PolicyHook>
+::PrintStat (std::ostream &os) const
+{
+ os << "# " << key_ << ((payload_ != PayloadTraits::empty_payload)?"*":"") << ": " << children_.size() << " children" << std::endl;
+ for (size_t bucket = 0, maxbucket = children_.bucket_count ();
+ bucket < maxbucket;
+ bucket++)
+ {
+ os << " " << children_.bucket_size (bucket);
+ }
+ os << "\n";
+
+ typedef trie<FullKey, PayloadTraits, PolicyHook> trie;
+ for (typename trie::unordered_set::const_iterator subnode = children_.begin ();
+ subnode != children_.end ();
+ subnode++ )
+ // BOOST_FOREACH (const trie &subnode, children_)
+ {
+ subnode->PrintStat (os);
+ }
+}
+
+
+template<typename FullKey, typename PayloadTraits, typename PolicyHook>
+inline bool
+operator == (const trie<FullKey, PayloadTraits, PolicyHook> &a,
+ const trie<FullKey, PayloadTraits, PolicyHook> &b)
+{
+ return a.key_ == b.key_;
+}
+
+template<typename FullKey, typename PayloadTraits, typename PolicyHook>
+inline std::size_t
+hash_value (const trie<FullKey, PayloadTraits, PolicyHook> &trie_node)
+{
+ return boost::hash_value (trie_node.key_);
+}
+
+
+
+template<class Trie, class NonConstTrie> // hack for boost < 1.47
+class trie_iterator
+{
+public:
+ trie_iterator () : trie_ (0) {}
+ trie_iterator (typename Trie::iterator item) : trie_ (item) {}
+ trie_iterator (Trie &item) : trie_ (&item) {}
+
+ Trie & operator* () { return *trie_; }
+ const Trie & operator* () const { return *trie_; }
+ Trie * operator-> () { return trie_; }
+ const Trie * operator-> () const { return trie_; }
+ bool operator== (trie_iterator<const Trie, NonConstTrie> &other) const { return (trie_ == other.trie_); }
+ bool operator== (trie_iterator<Trie, NonConstTrie> &other) { return (trie_ == other.trie_); }
+ bool operator!= (trie_iterator<const Trie, NonConstTrie> &other) const { return !(*this == other); }
+ bool operator!= (trie_iterator<Trie, NonConstTrie> &other) { return !(*this == other); }
+
+ trie_iterator<Trie,NonConstTrie> &
+ operator++ (int)
+ {
+ if (trie_->children_.size () > 0)
+ trie_ = &(*trie_->children_.begin ());
+ else
+ trie_ = goUp ();
+ return *this;
+ }
+
+ trie_iterator<Trie,NonConstTrie> &
+ operator++ ()
+ {
+ (*this)++;
+ return *this;
+ }
+
+private:
+ typedef typename boost::mpl::if_< boost::is_same<Trie, NonConstTrie>,
+ typename Trie::unordered_set::iterator,
+ typename Trie::unordered_set::const_iterator>::type set_iterator;
+
+ Trie* goUp ()
+ {
+ if (trie_->parent_ != 0)
+ {
+ // typename Trie::unordered_set::iterator item =
+ set_iterator item = const_cast<NonConstTrie*>(trie_)->parent_->children_.iterator_to (const_cast<NonConstTrie&> (*trie_));
+ item++;
+ if (item != trie_->parent_->children_.end ())
+ {
+ return &(*item);
+ }
+ else
+ {
+ trie_ = trie_->parent_;
+ return goUp ();
+ }
+ }
+ else
+ return 0;
+ }
+private:
+ Trie *trie_;
+};
+
+
+template<class Trie>
+class trie_point_iterator
+{
+private:
+ typedef typename boost::mpl::if_< boost::is_same<Trie, const Trie>,
+ typename Trie::unordered_set::const_iterator,
+ typename Trie::unordered_set::iterator>::type set_iterator;
+
+public:
+ trie_point_iterator () : trie_ (0) {}
+ trie_point_iterator (typename Trie::iterator item) : trie_ (item) {}
+ trie_point_iterator (Trie &item)
+ {
+ if (item.children_.size () != 0)
+ trie_ = &*item.children_.begin ();
+ else
+ trie_ = 0;
+ }
+
+ Trie & operator* () { return *trie_; }
+ const Trie & operator* () const { return *trie_; }
+ Trie * operator-> () { return trie_; }
+ const Trie * operator-> () const { return trie_; }
+ bool operator== (trie_point_iterator<const Trie> &other) const { return (trie_ == other.trie_); }
+ bool operator== (trie_point_iterator<Trie> &other) { return (trie_ == other.trie_); }
+ bool operator!= (trie_point_iterator<const Trie> &other) const { return !(*this == other); }
+ bool operator!= (trie_point_iterator<Trie> &other) { return !(*this == other); }
+
+ trie_point_iterator<Trie> &
+ operator++ (int)
+ {
+ if (trie_->parent_ != 0)
+ {
+ set_iterator item = trie_->parent_->children_.iterator_to (*trie_);
+ item ++;
+ if (item == trie_->parent_->children_.end ())
+ trie_ = 0;
+ else
+ trie_ = &*item;
+ }
+ else
+ {
+ trie_ = 0;
+ }
+ return *this;
+ }
+
+ trie_point_iterator<Trie> &
+ operator++ ()
+ {
+ (*this)++;
+ return *this;
+ }
+
+private:
+ Trie *trie_;
+};
+
+
+} // trie
+} // ndn
+
+#endif // NDN_TRIE_TRIE_H_
diff --git a/ndn-cpp/version.h b/ndn-cpp/version.h
new file mode 100644
index 0000000..34b65de
--- /dev/null
+++ b/ndn-cpp/version.h
@@ -0,0 +1,22 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_CXX_VERSION_H
+#define NDN_CXX_VERSION_H
+
+// NDN_CXX_VERSION % 100 is the minor version
+// NDN_CXX_VERSION / 100 % 1000 is the major version
+
+#define NDN_CXX_VERSION 0001
+
+#define NDN_CXX_VERSION_MAJOR 0
+#define NDN_CXX_VERSION_MINOR 1
+
+#endif // NDN_CXX_VERSION_H
diff --git a/ndn-cpp/wire/base.h b/ndn-cpp/wire/base.h
new file mode 100644
index 0000000..8247d09
--- /dev/null
+++ b/ndn-cpp/wire/base.h
@@ -0,0 +1,33 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_WIRE_BASE_H
+#define NDN_WIRE_BASE_H
+
+#include "ndn-cpp/fields/signature-sha256-with-rsa.h"
+
+namespace ndn {
+namespace wire {
+
+/**
+ * @brief Class defining interface for double dispatch pattern (=visitor pattern)
+ * to format variable fields in wire format
+ */
+class Base
+{
+public:
+ virtual void
+ appendSignature (std::ostream &os, const signature::Sha256WithRsa &signature, void *userData) = 0;
+};
+
+} // wire
+} // ndn
+
+#endif // NDN_WIRE_BASE_H
diff --git a/ndn-cpp/wire/ccnb.cc b/ndn-cpp/wire/ccnb.cc
new file mode 100644
index 0000000..86c3f5f
--- /dev/null
+++ b/ndn-cpp/wire/ccnb.cc
@@ -0,0 +1,259 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "ccnb.h"
+#include "ndn-cpp/error.h"
+
+#include <boost/lexical_cast.hpp>
+
+namespace ndn {
+namespace wire {
+
+#define CCN_TT_BITS 3
+#define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1)
+#define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1)
+#define CCN_TT_HBIT ((unsigned char)(1 << 7))
+
+void
+Ccnb::appendBlockHeader (std::ostream &os, size_t val, Ccnb::ccn_tt tt)
+{
+ unsigned char buf[1+8*((sizeof(val)+6)/7)];
+ unsigned char *p = &(buf[sizeof(buf)-1]);
+ size_t n = 1;
+ p[0] = (CCN_TT_HBIT & ~Ccnb::CCN_CLOSE_TAG) |
+ ((val & CCN_MAX_TINY) << CCN_TT_BITS) |
+ (CCN_TT_MASK & tt);
+ val >>= (7-CCN_TT_BITS);
+ while (val != 0) {
+ (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | Ccnb::CCN_CLOSE_TAG;
+ n++;
+ val >>= 7;
+ }
+ os.write (reinterpret_cast<const char*> (p), n);
+ // return n;
+}
+
+void
+Ccnb::appendNumber (std::ostream &os, uint32_t number)
+{
+ std::string numberStr = boost::lexical_cast<std::string> (number);
+
+ appendBlockHeader (os, numberStr.size (), Ccnb::CCN_UDATA);
+ numberStr.size ();
+ os.write (numberStr.c_str (), numberStr.size ());
+}
+
+void
+Ccnb::appendName (std::ostream &os, const Name &name)
+{
+ Ccnb::appendBlockHeader (os, Ccnb::CCN_DTAG_Name, Ccnb::CCN_DTAG); // <Name>
+ for (Name::const_iterator component = name.begin (); component != name.end (); component ++)
+ {
+ appendTaggedBlob (os, Ccnb::CCN_DTAG_Component, component->buf (), component->size ());
+ }
+ Ccnb::appendCloser (os); // </Name>
+}
+
+void
+Ccnb::appendTimestampBlob (std::ostream &os, const TimeInterval &time)
+{
+ // CCNx method function implements some markers, which are not really defined anywhere else...
+
+ // Determine miminal number of bytes required to store the timestamp
+ int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
+ intmax_t ts = time.total_seconds () >> 4;
+ for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
+ required_bytes++;
+
+ appendBlockHeader(os, required_bytes, Ccnb::CCN_BLOB);
+
+ // write part with seconds
+ ts = time.total_seconds () >> 4;
+ for (int i = 0; i < required_bytes - 2; i++)
+ os.put ( ts >> (8 * (required_bytes - 3 - i)) );
+
+ /* arithmetic contortions are to avoid overflowing 31 bits */
+ ts = ((time.total_seconds () & 15) << 12) +
+ (((time.total_nanoseconds () % 1000000000) / 5 * 8 + 195312) / 390625);
+ for (int i = required_bytes - 2; i < required_bytes; i++)
+ os.put ( ts >> (8 * (required_bytes - 1 - i)) );
+
+ // return len + required_bytes;
+}
+
+void
+Ccnb::appendExclude (std::ostream &os, const Exclude &exclude)
+{
+ appendBlockHeader (os, Ccnb::CCN_DTAG_Exclude, Ccnb::CCN_DTAG); // <Exclude>
+
+ for (Exclude::const_reverse_iterator item = exclude.rbegin (); item != exclude.rend (); item ++)
+ {
+ if (!item->first.empty ())
+ appendTaggedBlob (os, Ccnb::CCN_DTAG_Component, item->first.buf (), item->first.size ());
+ if (item->second)
+ {
+ appendBlockHeader (os, Ccnb::CCN_DTAG_Any, Ccnb::CCN_DTAG); // <Any>
+ appendCloser (os); // </Any>
+ }
+ }
+ appendCloser (os); // </Exclude>
+}
+
+void
+Ccnb::appendInterest (std::ostream &os, const Interest &interest)
+{
+ Ccnb::appendBlockHeader (os, Ccnb::CCN_DTAG_Interest, Ccnb::CCN_DTAG); // <Interest>
+
+ // this is used for now as an interest template. Name should be empty
+ // Ccnb::appendName (os, interest.getName ());
+ Ccnb::appendName (os, Name ()); // <Component>...</Component>...
+
+ if (interest.getMinSuffixComponents () != Interest::ncomps)
+ {
+ appendTaggedNumber (os, Ccnb::CCN_DTAG_MinSuffixComponents, interest.getMinSuffixComponents ());
+ }
+ if (interest.getMaxSuffixComponents () != Interest::ncomps)
+ {
+ appendTaggedNumber (os, Ccnb::CCN_DTAG_MaxSuffixComponents, interest.getMaxSuffixComponents ());
+ }
+ if (interest.getExclude ().size () > 0)
+ {
+ appendExclude (os, interest.getExclude ());
+ }
+ if (interest.getChildSelector () != Interest::CHILD_DEFAULT)
+ {
+ appendTaggedNumber (os, Ccnb::CCN_DTAG_ChildSelector, interest.getChildSelector ());
+ }
+ if (interest.getAnswerOriginKind () != Interest::AOK_DEFAULT)
+ {
+ appendTaggedNumber (os, Ccnb::CCN_DTAG_AnswerOriginKind, interest.getAnswerOriginKind ());
+ }
+ if (interest.getScope () != Interest::NO_SCOPE)
+ {
+ appendTaggedNumber (os, Ccnb::CCN_DTAG_Scope, interest.getScope ());
+ }
+ if (!interest.getInterestLifetime ().is_negative ())
+ {
+ Ccnb::appendBlockHeader (os, Ccnb::CCN_DTAG_InterestLifetime, Ccnb::CCN_DTAG);
+ Ccnb::appendTimestampBlob (os, interest.getInterestLifetime ());
+ Ccnb::appendCloser (os);
+ }
+ // if (GetNonce()>0)
+ // {
+ // uint32_t nonce = interest.GetNonce();
+ // appendTaggedBlob (start, Ccnb::CCN_DTAG_Nonce, nonce);
+ // }
+
+ // if (GetNack ()>0)
+ // {
+ // appendBlockHeader (start, Ccnb::CCN_DTAG_Nack, Ccnb::CCN_DTAG);
+ // appendNumber (start, interest.GetNack ());
+ // appendCloser (start);
+ // }
+ Ccnb::appendCloser (os); // </Interest>
+}
+
+static void *SIGNATURE_Block = 0;
+static void *SINATURE_INFO_PublisherPublicKeyDigest = reinterpret_cast<void *> (1);
+static void *SINATURE_INFO_KeyLocator = reinterpret_cast<void *> (2);
+
+static const char TYPES [][3] = {
+ {0x0C, 0x04, 0xC0},
+ {0x10, 0xD0, 0x91},
+ {0x18, 0xE3, 0x44},
+ {0x28, 0x46, 0x3F},
+ {0x2C, 0x83, 0x4A},
+ {0x34, 0x00, 0x8A}
+};
+
+void
+Ccnb::appendSignature (std::ostream &os, const signature::Sha256WithRsa &signature, void *userData)
+{
+ if (userData == SIGNATURE_Block)
+ {
+ Ccnb::appendBlockHeader (os, Ccnb::CCN_DTAG_Signature, Ccnb::CCN_DTAG); // <Signature>
+ // if (signature.getDigestAlgorithm () != "2.16.840.1.101.3.4.2.1")
+ // {
+ // appendString (os, Ccnb::CCN_DTAG_DigestAlgorithm, signature.getDigestAlgorithm ());
+ // }
+ appendTaggedBlob (os, Ccnb::CCN_DTAG_SignatureBits, signature.getSignatureBits ());
+ Ccnb::appendCloser (os); // </Signature>
+ }
+ else if (userData == SINATURE_INFO_PublisherPublicKeyDigest)
+ {
+ Ccnb::appendTaggedBlob (os, Ccnb::CCN_DTAG_PublisherPublicKeyDigest, signature.getPublisherKeyDigest ());
+ }
+ else if (userData == SINATURE_INFO_KeyLocator)
+ {
+ Ccnb::appendBlockHeader (os, Ccnb::CCN_DTAG_Signature, Ccnb::CCN_DTAG); // <Signature>
+ switch (signature.getKeyLocator ().getType ())
+ {
+ case KeyLocator::NOTSET:
+ break;
+ case KeyLocator::KEY:
+ Ccnb::appendTaggedBlob (os, Ccnb::CCN_DTAG_Key, signature.getKeyLocator ().getKey ());
+ break;
+ case KeyLocator::CERTIFICATE:
+ Ccnb::appendTaggedBlob (os, Ccnb::CCN_DTAG_Key, signature.getKeyLocator ().getCertificate ());
+ break;
+ case KeyLocator::KEYNAME:
+ Ccnb::appendBlockHeader (os, Ccnb::CCN_DTAG_KeyName, Ccnb::CCN_DTAG); // <KeyName>
+ Ccnb::appendName (os, signature.getKeyLocator ().getKeyName ());
+ Ccnb::appendCloser (os); // </KeyName>
+ break;
+ }
+ Ccnb::appendCloser (os); // </Signature>
+ }
+ // other cases should not be possible, but don't do anything
+}
+
+void
+Ccnb::appendData (std::ostream &os, const Data &data)
+{
+ if (!data.getSignature ())
+ BOOST_THROW_EXCEPTION (error::wire::Ccnb ()
+ << error::msg ("Signature is required, but not set"));
+
+ Ccnb::appendBlockHeader (os, Ccnb::CCN_DTAG_ContentObject, Ccnb::CCN_DTAG); // <ContentObject>
+
+ // necessary for now, because of the changed storage order
+ data.getSignature ()->doubleDispatch (os, *this, SIGNATURE_Block);
+
+ Ccnb::appendName (os, data.getName ());
+
+ Ccnb::appendBlockHeader (os, Ccnb::CCN_DTAG_SignedInfo, Ccnb::CCN_DTAG); // <SignedInfo>
+ data.getSignature ()->doubleDispatch (os, *this, SINATURE_INFO_PublisherPublicKeyDigest);
+
+ Ccnb::appendTimestampBlob (os, data.getContent ().getTimestamp ());
+
+ BOOST_ASSERT (sizeof (TYPES) == 3 * (static_cast<int> (Content::NACK)+1));
+ Ccnb::appendTaggedBlob (os, Ccnb::CCN_DTAG_Type, TYPES [data.getContent ().getType ()], 3);
+
+ if (data.getContent ().getFreshness () != Content::noFreshness)
+ {
+ Ccnb::appendTaggedNumber (os, Ccnb::CCN_DTAG_FreshnessSeconds,
+ data.getContent ().getFreshness ().total_seconds ());
+ }
+
+ if (data.getContent ().getFinalBlockId () != Content::noFinalBlock)
+ {
+ Ccnb::appendTaggedBlob (os, Ccnb::CCN_DTAG_FinalBlockID, data.getContent ().getFinalBlockId ());
+ }
+
+ data.getSignature ()->doubleDispatch (os, *this, SINATURE_INFO_KeyLocator);
+ Ccnb::appendCloser (os); // </SignedInfo>
+
+ Ccnb::appendTaggedBlob (os, Ccnb::CCN_DTAG_Content, data.content ());
+
+ Ccnb::appendCloser (os); // </ContentObject>
+}
+
+} // namespace wire
+} // namespace ndn
diff --git a/ndn-cpp/wire/ccnb.h b/ndn-cpp/wire/ccnb.h
new file mode 100644
index 0000000..fc99bad
--- /dev/null
+++ b/ndn-cpp/wire/ccnb.h
@@ -0,0 +1,350 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_WIRE_CCNB_H
+#define NDN_WIRE_CCNB_H
+
+#include "base.h"
+
+#include "ndn-cpp/interest.h"
+#include "ndn-cpp/data.h"
+
+namespace ndn {
+namespace wire {
+
+/**
+ * @brief Class for working with ccnb encoding
+ */
+class Ccnb : public Base
+{
+public:
+ /**
+ * \brief Type tag for a ccnb start marker.
+ *
+ * \see http://www.ccnx.org/releases/latest/doc/technical/DTAG.html
+ */
+ enum ccn_tt {
+ CCN_EXT, /**< starts composite extension - numval is subtype */
+ CCN_TAG, /**< starts composite - numval is tagnamelen-1 */
+ CCN_DTAG, /**< starts composite - numval is tagdict index (enum ccn_dtag) */
+ CCN_ATTR, /**< attribute - numval is attrnamelen-1, value follows */
+ CCN_DATTR, /**< attribute numval is attrdict index */
+ CCN_BLOB, /**< opaque binary data - numval is byte count */
+ CCN_UDATA, /**< UTF-8 encoded character data - numval is byte count */
+ CCN_NO_TOKEN /**< should not occur in encoding */
+ };
+
+ /** \brief CCN_CLOSE_TAG terminates composites */
+ enum {CCN_CLOSE_TAG = 0};
+
+ /**
+ * \brief DTAG identifies ccnb-encoded elements.
+ *
+ * \see http://www.ccnx.org/releases/latest/doc/technical/DTAG.html
+ */
+ enum ccn_dtag {
+ CCN_DTAG_Any = 13,
+ CCN_DTAG_Name = 14,
+ CCN_DTAG_Component = 15,
+ CCN_DTAG_Certificate = 16,
+ CCN_DTAG_Collection = 17,
+ CCN_DTAG_CompleteName = 18,
+ CCN_DTAG_Content = 19,
+ CCN_DTAG_SignedInfo = 20,
+ CCN_DTAG_ContentDigest = 21,
+ CCN_DTAG_ContentHash = 22,
+ CCN_DTAG_Count = 24,
+ CCN_DTAG_Header = 25,
+ CCN_DTAG_Interest = 26, /* 20090915 */
+ CCN_DTAG_Key = 27,
+ CCN_DTAG_KeyLocator = 28,
+ CCN_DTAG_KeyName = 29,
+ CCN_DTAG_Length = 30,
+ CCN_DTAG_Link = 31,
+ CCN_DTAG_LinkAuthenticator = 32,
+ CCN_DTAG_NameComponentCount = 33, /* DeprecatedInInterest */
+ CCN_DTAG_RootDigest = 36,
+ CCN_DTAG_Signature = 37,
+ CCN_DTAG_Start = 38,
+ CCN_DTAG_Timestamp = 39,
+ CCN_DTAG_Type = 40,
+ CCN_DTAG_Nonce = 41,
+ CCN_DTAG_Scope = 42,
+ CCN_DTAG_Exclude = 43,
+ CCN_DTAG_Bloom = 44,
+ CCN_DTAG_BloomSeed = 45,
+ CCN_DTAG_AnswerOriginKind = 47,
+ CCN_DTAG_InterestLifetime = 48,
+ CCN_DTAG_Witness = 53,
+ CCN_DTAG_SignatureBits = 54,
+ CCN_DTAG_DigestAlgorithm = 55,
+ CCN_DTAG_BlockSize = 56,
+ CCN_DTAG_FreshnessSeconds = 58,
+ CCN_DTAG_FinalBlockID = 59,
+ CCN_DTAG_PublisherPublicKeyDigest = 60,
+ CCN_DTAG_PublisherCertificateDigest = 61,
+ CCN_DTAG_PublisherIssuerKeyDigest = 62,
+ CCN_DTAG_PublisherIssuerCertificateDigest = 63,
+ CCN_DTAG_ContentObject = 64, /* 20090915 */
+ CCN_DTAG_WrappedKey = 65,
+ CCN_DTAG_WrappingKeyIdentifier = 66,
+ CCN_DTAG_WrapAlgorithm = 67,
+ CCN_DTAG_KeyAlgorithm = 68,
+ CCN_DTAG_Label = 69,
+ CCN_DTAG_EncryptedKey = 70,
+ CCN_DTAG_EncryptedNonceKey = 71,
+ CCN_DTAG_WrappingKeyName = 72,
+ CCN_DTAG_Action = 73,
+ CCN_DTAG_FaceID = 74,
+ CCN_DTAG_IPProto = 75,
+ CCN_DTAG_Host = 76,
+ CCN_DTAG_Port = 77,
+ CCN_DTAG_MulticastInterface = 78,
+ CCN_DTAG_ForwardingFlags = 79,
+ CCN_DTAG_FaceInstance = 80,
+ CCN_DTAG_ForwardingEntry = 81,
+ CCN_DTAG_MulticastTTL = 82,
+ CCN_DTAG_MinSuffixComponents = 83,
+ CCN_DTAG_MaxSuffixComponents = 84,
+ CCN_DTAG_ChildSelector = 85,
+ CCN_DTAG_RepositoryInfo = 86,
+ CCN_DTAG_Version = 87,
+ CCN_DTAG_RepositoryVersion = 88,
+ CCN_DTAG_GlobalPrefix = 89,
+ CCN_DTAG_LocalName = 90,
+ CCN_DTAG_Policy = 91,
+ CCN_DTAG_Namespace = 92,
+ CCN_DTAG_GlobalPrefixName = 93,
+ CCN_DTAG_PolicyVersion = 94,
+ CCN_DTAG_KeyValueSet = 95,
+ CCN_DTAG_KeyValuePair = 96,
+ CCN_DTAG_IntegerValue = 97,
+ CCN_DTAG_DecimalValue = 98,
+ CCN_DTAG_StringValue = 99,
+ CCN_DTAG_BinaryValue = 100,
+ CCN_DTAG_NameValue = 101,
+ CCN_DTAG_Entry = 102,
+ CCN_DTAG_ACL = 103,
+ CCN_DTAG_ParameterizedName = 104,
+ CCN_DTAG_Prefix = 105,
+ CCN_DTAG_Suffix = 106,
+ CCN_DTAG_Root = 107,
+ CCN_DTAG_ProfileName = 108,
+ CCN_DTAG_Parameters = 109,
+ CCN_DTAG_InfoString = 110,
+ CCN_DTAG_StatusResponse = 112,
+ CCN_DTAG_StatusCode = 113,
+ CCN_DTAG_StatusText = 114,
+ CCN_DTAG_Nack = 200,
+ CCN_DTAG_SequenceNumber = 256,
+ CCN_DTAG_CCNProtocolDataUnit = 17702112
+ };
+
+
+ /**
+ * @brief Append CCNB block header
+ * @param os output stream to write
+ * @param value dictionary id of the block header
+ * @param block_type Type of CCNB block
+ */
+ static void
+ appendBlockHeader (std::ostream &os, size_t value, ccn_tt block_type);
+
+ /**
+ * @brief Add number in CCNB encoding
+ * @param os output stream to write
+ * @param number Number to be written
+ *
+ * @returns written length
+ */
+ static void
+ appendNumber (std::ostream &os, uint32_t number);
+
+ /**
+ * @brief Append CCNB closer tag (size is 1)
+ * @param os output stream to write
+ */
+ inline static void
+ appendCloser (std::ostream &os);
+
+ /**
+ * @brief Append Name in CCNB encoding
+ * @param os output stream to write
+ * @param name constant reference to Name object
+ *
+ * @returns written length
+ */
+ static void
+ appendName (std::ostream &os, const Name &name);
+
+ /**
+ * Append a binary timestamp as a BLOB using the ccn binary
+ * Timestamp representation (12-bit fraction).
+ *
+ * @param os output stream to write
+ * @param time reference to time duration object
+ */
+ static void
+ appendTimestampBlob (std::ostream &os, const TimeInterval ×tamp);
+
+ /**
+ * Append a binary timestamp as a BLOB using the ccn binary
+ * Timestamp representation (12-bit fraction).
+ *
+ * @param os output stream to write
+ * @param time reference to Time (posix_time::ptime) object.
+ * This method automatically calculates duration between time and gregorian::date(1970,1,1)
+ * and calls the other version of the method
+ */
+ inline static void
+ appendTimestampBlob (std::ostream &os, const Time &time);
+
+ /**
+ * Append a tagged BLOB
+ *
+ * This is a ccnb-encoded element with containing the BLOB as content
+ *
+ * @param os output stream to write
+ * @param dtag is the element's dtag
+ * @param data points to the binary data
+ * @param size is the size of the data, in bytes
+ */
+ inline static void
+ appendTaggedBlob (std::ostream &os, ccn_dtag dtag, const void *data, size_t size);
+
+ /**
+ * Append a tagged BLOB
+ *
+ * This is a ccnb-encoded element with containing the BLOB as content
+ *
+ * @param os output stream to write
+ * @param dtag is the element's dtag
+ * @param blob reference to the data blob
+ */
+ inline static void
+ appendTaggedBlob (std::ostream &os, ccn_dtag dtag, const Blob &blob);
+
+ /**
+ * Append a tagged BLOB
+ *
+ * This is a ccnb-encoded element with containing the BLOB as content
+ *
+ * @param os output stream to write
+ * @param dtag is the element's dtag
+ * @param data points to the binary data
+ * @param size is the size of the data, in bytes
+ */
+ inline static void
+ appendTaggedNumber (std::ostream &os, ccn_dtag dtag, uint32_t number);
+
+ /**
+ * Append a tagged string (should be a valid UTF-8 coded string)
+ *
+ * This is a ccnb-encoded element with containing UDATA as content
+ *
+ * @param os output stream to write
+ * @param dtag is the element's dtag
+ * @param string UTF-8 string to be written
+ */
+ inline static void
+ appendString (std::ostream &os, ccn_dtag dtag, const std::string &string);
+
+ /**
+ * @brief Format interest in CCNb encoding
+ * @param os output stream to write
+ * @param interest Interest to be formatted
+ *
+ * @todo For now, this method is used to create Interest template, which doesn't output name to the stream
+ */
+ static void
+ appendInterest (std::ostream &os, const Interest &interest);
+
+ /**
+ * @brief Append exclude filter in CCNb encoding
+ * @param os output stream to write
+ * @param exclude Exclude filter to be formatted
+ */
+ static void
+ appendExclude (std::ostream &os, const Exclude &exclude);
+
+ /**
+ * @brief Append signature in SHA256withRSA format
+ */
+ virtual void
+ appendSignature (std::ostream &os, const signature::Sha256WithRsa &signature, void *userData);
+
+ /**
+ * @brief Format data in CCNb encoding
+ * @param os output stream to write
+ * @param data data to be formatted
+ */
+ void
+ appendData (std::ostream &os, const Data &data);
+};
+
+
+inline void
+Ccnb::appendCloser (std::ostream &os)
+{
+ os.put (Ccnb::CCN_CLOSE_TAG);
+}
+
+inline void
+Ccnb::appendTimestampBlob (std::ostream &os, const Time &time)
+{
+ appendTimestampBlob (os, time - time::UNIX_EPOCH_TIME);
+}
+
+inline void
+Ccnb::appendTaggedBlob (std::ostream &os, Ccnb::ccn_dtag dtag, const void *data, size_t size)
+{
+ appendBlockHeader (os, dtag, Ccnb::CCN_DTAG);
+ /* 2 */
+ if (size>0)
+ {
+ appendBlockHeader (os, size, Ccnb::CCN_BLOB);
+ os.write (reinterpret_cast<const char*> (data), size);
+ /* size */
+ }
+ appendCloser (os);
+ /* 1 */
+}
+
+inline void
+Ccnb::appendTaggedBlob (std::ostream &os, ccn_dtag dtag, const Blob &blob)
+{
+ appendTaggedBlob (os, dtag, blob.buf (), blob.size ());
+}
+
+inline void
+Ccnb::appendTaggedNumber (std::ostream &os, Ccnb::ccn_dtag dtag, uint32_t number)
+{
+ appendBlockHeader (os, dtag, Ccnb::CCN_DTAG);
+ {
+ appendNumber (os, number);
+ }
+ appendCloser (os);
+}
+
+inline void
+Ccnb::appendString (std::ostream &os, Ccnb::ccn_dtag dtag, const std::string &string)
+{
+ appendBlockHeader (os, dtag, Ccnb::CCN_DTAG);
+ {
+ appendBlockHeader (os, string.size (), Ccnb::CCN_UDATA);
+ os.write (string.c_str (), string.size ());
+ }
+ appendCloser (os);
+}
+
+} // wire
+} // ndn
+
+#endif // NDN_WIRE_CCNB_H