model: Another major change: switching to NDN.cxx API for ndn::Name
NDN.cxx code is almost exactly the same, with minor NS-3 specific customizations
Refs #1011 (http://redmine.named-data.net/issues/1011)
diff --git a/model/ndn-common.h b/model/ndn-common.h
index 3581a4c..de21c29 100644
--- a/model/ndn-common.h
+++ b/model/ndn-common.h
@@ -11,7 +11,29 @@
#ifndef NDN_COMMON_H
#define NDN_COMMON_H
+#include "ns3/nstime.h"
+#include "ns3/simulator.h"
+
+#define NDNSIM_MODE 1
+
#define NDN_NAMESPACE_BEGIN namespace ns3 { namespace ndn {
#define NDN_NAMESPACE_END } /*ndn*/ } /*ns3*/
+NDN_NAMESPACE_BEGIN
+
+typedef Time TimeInterval;
+
+namespace time
+{
+
+inline Time
+NowUnixTimestamp ()
+{
+ return Simulator::Now ();
+}
+
+}
+
+NDN_NAMESPACE_END
+
#endif // NDN_COMMON_H
diff --git a/model/ndn-name.cc b/model/ndn-name.cc
deleted file mode 100644
index 60d1b82..0000000
--- a/model/ndn-name.cc
+++ /dev/null
@@ -1,161 +0,0 @@
-/* -*- 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>
- * Ilya Moiseenko <iliamo@cs.ucla.edu>
- */
-
-#include "ndn-name.h"
-#include <boost/foreach.hpp>
-#include "ns3/log.h"
-
-#include <iostream>
-
-using namespace std;
-
-NS_LOG_COMPONENT_DEFINE ("ndn.Name");
-
-namespace ns3 {
-namespace ndn {
-
-ATTRIBUTE_HELPER_CPP (Name);
-
-Name::Name (/* root */)
-{
-}
-
-// Name::Name (const std::list<boost::reference_wrapper<const std::string> > &components)
-// {
-// BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components)
-// {
-// Add (component.get ());
-// }
-// }
-
-Name::Name (const std::list<std::string> &components)
-{
- BOOST_FOREACH (const std::string &component, components)
- {
- Add (component);
- }
-}
-
-Name::Name (const std::string &prefix)
-{
- istringstream is (prefix);
- is >> *this;
-}
-
-Name::Name (const char *prefix)
-{
- NS_ASSERT (prefix != 0);
-
- istringstream is (prefix);
- is >> *this;
-}
-
-const std::list<std::string> &
-Name::GetComponents () const
-{
- return m_prefix;
-}
-
-std::string
-Name::GetLastComponent () const
-{
- if (m_prefix.size () == 0)
- {
- return "";
- }
-
- return m_prefix.back ();
-}
-
-std::list<boost::reference_wrapper<const std::string> >
-Name::GetSubComponents (size_t num) const
-{
- NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
-
- std::list<boost::reference_wrapper<const std::string> > subComponents;
- std::list<std::string>::const_iterator component = m_prefix.begin();
- for (size_t i=0; i<num; i++, component++)
- {
- subComponents.push_back (boost::ref (*component));
- }
-
- return subComponents;
-}
-
-Name
-Name::cut (size_t minusComponents) const
-{
- Name retval;
- std::list<std::string>::const_iterator component = m_prefix.begin ();
- for (uint32_t i = 0; i < m_prefix.size () - minusComponents; i++, component++)
- {
- retval.Add (*component);
- }
-
- return retval;
-}
-
-void
-Name::Print (std::ostream &os) const
-{
- for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
- {
- os << "/" << *i;
- }
- if (m_prefix.size ()==0) os << "/";
-}
-
-std::ostream &
-operator << (std::ostream &os, const Name &components)
-{
- components.Print (os);
- return os;
-}
-
-std::istream &
-operator >> (std::istream &is, Name &components)
-{
- istream_iterator<char> eos; // end of stream
-
- std::string component = "";
- istream_iterator<char> it (is);
- for (; it != eos; it++)
- {
- if (*it == '/')
- {
- if (component != "")
- components.Add (component);
- component = "";
- }
- else
- component.push_back (*it);
- }
- if (component != "")
- components.Add (component);
-
- is.clear ();
- // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ());
-
- return is;
-}
-
-} // ndn
-} // ns3
diff --git a/model/ndn-name.h b/model/ndn-name.h
index 3eb2021..797de98 100644
--- a/model/ndn-name.h
+++ b/model/ndn-name.h
@@ -1,315 +1,2 @@
-/* -*- 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>
- * Ilya Moiseenko <iliamo@cs.ucla.edu>
- */
-
-#ifndef _NDN_NAME_H_
-#define _NDN_NAME_H_
-
-#include "ns3/simple-ref-count.h"
-#include "ns3/attribute.h"
-#include "ns3/attribute-helper.h"
-
-#include <string>
-#include <algorithm>
-#include <list>
-#include "ns3/object.h"
-#include "ns3/buffer.h"
-
-#include <boost/ref.hpp>
-
-namespace ns3 {
-namespace ndn {
-
-/**
- * \ingroup ndn
- * \brief Hierarchical NDN name
- * A Name element represents a hierarchical name for Ndn content.
- * It simply contains a sequence of Component elements.
- * Each Component element contains a sequence of zero or more bytes.
- * There are no restrictions on what byte sequences may be used.
- * The Name element in an Interest is often referred to with the term name prefix or simply prefix.
- */
-class Name : public SimpleRefCount<Name>
-{
-public:
- typedef std::list<std::string>::iterator iterator;
- typedef std::list<std::string>::const_iterator const_iterator;
-
- /**
- * \brief Constructor
- * Creates a prefix with zero components (can be looked as root "/")
- */
- Name ();
-
- // /**
- // * \brief Constructor
- // * Creates a prefix from a list of strings where every string represents a prefix component
- // * @param[in] components A list of strings
- // */
- // Name (const std::list<boost::reference_wrapper<const std::string> > &components);
-
- /**
- * \brief Constructor
- * Creates a prefix from a list of strings where every string represents a prefix component
- * @param[in] components A list of strings
- */
- Name (const std::list<std::string> &components);
-
- /**
- * @brief Constructor
- * Creates a prefix from the string (string is parsed using operator>>)
- * @param[in] prefix A string representation of a prefix
- */
- Name (const std::string &prefix);
-
- /**
- * @brief Constructor
- * Creates a prefix from the string (string is parsed using operator>>)
- * @param[in] prefix A string representation of a prefix
- */
- Name (const char *prefix);
-
- /**
- * \brief Generic Add method
- * Appends object of type T to the list of components
- * @param[in] value The object to be appended
- */
- template<class T>
- inline Name&
- Add (const T &value);
-
- /**
- * \brief Append components from another name
- * @param otherName Name to add at the end
- */
- inline Name&
- Append (const Name &otherName);
-
- /**
- * \brief Generic constructor operator
- * The object of type T will be appended to the list of components
- */
- template<class T>
- inline Name&
- operator () (const T &value);
-
- /**
- * \brief Get a name
- * Returns a list of components (strings)
- */
- const std::list<std::string> &
- GetComponents () const;
-
- /**
- * @brief Helper call to get the last component of the name
- */
- std::string
- GetLastComponent () const;
-
- /**
- * \brief Get subcomponents of the name, starting with first component
- * @param[in] num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
- */
- std::list<boost::reference_wrapper<const std::string> >
- GetSubComponents (size_t num) const;
-
- /**
- * @brief Get prefix of the name, containing less minusComponents right components
- */
- Name
- cut (size_t minusComponents) const;
-
- /**
- * \brief Print name
- * @param[in] os Stream to print
- */
- void Print (std::ostream &os) const;
-
- /**
- * \brief Returns the size of Name
- */
- inline size_t
- size () const;
-
- /**
- * @brief Get read-write begin() iterator
- */
- inline iterator
- begin ();
-
- /**
- * @brief Get read-only begin() iterator
- */
- inline const_iterator
- begin () const;
-
- /**
- * @brief Get read-write end() iterator
- */
- inline iterator
- end ();
-
- /**
- * @brief Get read-only end() iterator
- */
- inline const_iterator
- end () const;
-
- /**
- * \brief Equality operator for Name
- */
- inline bool
- operator== (const Name &prefix) const;
-
- /**
- * \brief Less than operator for Name
- */
- inline bool
- operator< (const Name &prefix) const;
-
- typedef std::string partial_type;
-
-private:
- std::list<std::string> m_prefix; ///< \brief a list of strings (components)
-};
-
-/**
- * \brief Print out name components separated by slashes, e.g., /first/second/third
- */
-std::ostream &
-operator << (std::ostream &os, const Name &components);
-
-/**
- * \brief Read components from input and add them to components. Will read input stream till eof
- * Substrings separated by slashes will become separate components
- */
-std::istream &
-operator >> (std::istream &is, Name &components);
-
-/**
- * \brief Returns the size of Name object
- */
-size_t
-Name::size () const
-{
- return m_prefix.size ();
-}
-
-Name::iterator
-Name::begin ()
-{
- return m_prefix.begin ();
-}
-
-/**
- * @brief Get read-only begin() iterator
- */
-Name::const_iterator
-Name::begin () const
-{
- return m_prefix.begin ();
-}
-
-/**
- * @brief Get read-write end() iterator
- */
-Name::iterator
-Name::end ()
-{
- return m_prefix.end ();
-}
-
-/**
- * @brief Get read-only end() iterator
- */
-Name::const_iterator
-Name::end () const
-{
- return m_prefix.end ();
-}
-
-
-/**
- * \brief Generic constructor operator
- * The object of type T will be appended to the list of components
- */
-template<class T>
-Name&
-Name::operator () (const T &value)
-{
- return Add (value);
-}
-
-/**
- * \brief Generic Add method
- * Appends object of type T to the list of components
- * @param[in] value The object to be appended
- */
-template<class T>
-Name&
-Name::Add (const T &value)
-{
- std::ostringstream os;
- os << value;
- m_prefix.push_back (os.str ());
-
- return *this;
-}
-
-inline Name&
-Name::Append (const Name &otherName)
-{
- std::copy (otherName.begin (), otherName.end (), std::back_inserter (m_prefix));
- return *this;
-}
-
-
-/**
- * \brief Equality operator for Name
- */
-bool
-Name::operator== (const Name &prefix) const
-{
- if (m_prefix.size () != prefix.m_prefix.size ())
- return false;
-
- return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
-}
-
-/**
- * \brief Less than operator for Name
- */
-bool
-Name::operator< (const Name &prefix) const
-{
- return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
- prefix.m_prefix.begin (), prefix.m_prefix.end ());
-}
-
-ATTRIBUTE_HELPER_HEADER (Name);
-
-// for backwards compatibility
-typedef Name NameComponents;
-
-} // namespace ndn
-} // namespace ns3
-
-#endif // _NDN_NAME_H_
-
+// backwards compatibility header
+#include "ns3/ndnSIM/ndn.cxx/name.h"
diff --git a/model/wire/ccnb/ccnb-parser/visitors/name-visitor.cc b/model/wire/ccnb/ccnb-parser/visitors/name-visitor.cc
index 965b320..998e4e2 100644
--- a/model/wire/ccnb/ccnb-parser/visitors/name-visitor.cc
+++ b/model/wire/ccnb/ccnb-parser/visitors/name-visitor.cc
@@ -44,7 +44,7 @@
case CCN_DTAG_Component:
if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
throw CcnbDecodingException ();
- components.Add (
+ components.append (
boost::any_cast<std::string> ((*n.m_nestedTags.begin())->accept(
stringVisitor
)));
diff --git a/model/wire/ccnb/wire-ccnb.cc b/model/wire/ccnb/wire-ccnb.cc
index 9033d3f..e3a9ef1 100644
--- a/model/wire/ccnb/wire-ccnb.cc
+++ b/model/wire/ccnb/wire-ccnb.cc
@@ -191,10 +191,10 @@
Ccnb::SerializeName (Buffer::Iterator &start, const Name &name)
{
size_t written = 0;
- BOOST_FOREACH (const std::string &component, name.GetComponents())
+ BOOST_FOREACH (const name::Component &component, name)
{
written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component,
- reinterpret_cast<const uint8_t*>(component.c_str()), component.size());
+ reinterpret_cast<const uint8_t*>(component.buf ()), component.size());
}
return written;
}
@@ -203,9 +203,9 @@
Ccnb::SerializedSizeName (const Name &name)
{
size_t written = 0;
- BOOST_FOREACH (const std::string &component, name.GetComponents())
+ BOOST_FOREACH (const name::Component &component, name)
{
- written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size());
+ written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size ());
}
return written;
}
diff --git a/model/wire/ndnsim/wire-ndnsim.cc b/model/wire/ndnsim/wire-ndnsim.cc
index 7d383a4..8b4d81d 100644
--- a/model/wire/ndnsim/wire-ndnsim.cc
+++ b/model/wire/ndnsim/wire-ndnsim.cc
@@ -26,12 +26,12 @@
i.WriteU16 (static_cast<uint16_t> (SerializedSizeName (name)-2));
- for (std::list<std::string>::const_iterator item = name.begin ();
+ for (Name::const_iterator item = name.begin ();
item != name.end ();
item++)
{
i.WriteU16 (static_cast<uint16_t> (item->size ()));
- i.Write (reinterpret_cast<const uint8_t*> (item->c_str ()), item->size ());
+ i.Write (reinterpret_cast<const uint8_t*> (item->buf ()), item->size ());
}
return i.GetDistanceFrom (start);
@@ -42,7 +42,7 @@
{
size_t nameSerializedSize = 2;
- for (std::list<std::string>::const_iterator i = name.begin ();
+ for (Name::const_iterator i = name.begin ();
i != name.end ();
i++)
{
@@ -67,7 +67,7 @@
uint8_t tmp[length];
i.Read (tmp, length);
- name->Add (std::string (reinterpret_cast<const char*> (tmp), length));
+ name->append (std::string (reinterpret_cast<const char*> (tmp), length));
}
return name;