Start of serious reorganization

The whole forwarding logic is (will be) moved to the Forwarding Strategy
class.
diff --git a/model/content-store/ccnx-content-store-impl.h b/model/content-store/ccnx-content-store-impl.h
new file mode 100644
index 0000000..9bdc58e
--- /dev/null
+++ b/model/content-store/ccnx-content-store-impl.h
@@ -0,0 +1,107 @@
+/* -*-  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 CCNX_CONTENT_STORE_IMPL_H_
+#define CCNX_CONTENT_STORE_IMPL_H_
+
+#include "ccnx-content-store.h"
+#include "ns3/packet.h"
+#include "ns3/ccnx-interest-header.h"
+#include "ns3/ccnx-content-object-header.h"
+#include <boost/foreach.hpp>
+
+namespace ns3
+{
+
+template<class Container>
+class CcnxContentStoreImpl : public CcnxContentStore,
+                             protected Container
+{
+public:
+  // from CcnxContentStore
+  
+  virtual inline boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
+  Lookup (Ptr<const CcnxInterestHeader> interest);
+            
+  virtual inline bool
+  Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet);
+
+  // virtual bool
+  // Remove (Ptr<CcnxInterestHeader> header);
+  
+  virtual inline void
+  Print (std::ostream &os) const;  
+};
+
+
+template<class Container>
+boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
+CcnxContentStoreImpl<Container>::Lookup (Ptr<const CcnxInterestHeader> interest)
+{
+  // NS_LOG_FUNCTION (this << interest->GetName ());
+
+  /// @todo Change to search with predicate
+  typename Container::const_iterator node = this->deepest_prefix_match (interest->GetName ());
+  
+  if (node != this->end ())
+    {
+      this->m_cacheHitsTrace (interest, node->payload ()->GetHeader ());
+
+      // NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ());
+      return boost::make_tuple (node->payload ()->GetFullyFormedCcnxPacket (),
+                                node->payload ()->GetHeader (),
+                                node->payload ()->GetPacket ());
+    }
+  else
+    {
+      // NS_LOG_DEBUG ("cache miss for " << interest->GetName ());
+      this->m_cacheMissesTrace (interest);
+      return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
+    }
+}   
+    
+template<class Container>
+bool 
+CcnxContentStoreImpl<Container>::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
+{
+  // NS_LOG_FUNCTION (this << header->GetName ());
+
+  return
+    this->insert (header->GetName (), Create<CcnxContentStoreEntry> (header, packet))
+    .second;
+}
+    
+template<class Container>
+void 
+CcnxContentStoreImpl<Container>::Print (std::ostream &os) const
+{
+  for (typename Container::policy_container::const_iterator item = this->getPolicy ().begin ();
+       item != this->getPolicy ().end ();
+       item++)
+  // BOOST_FOREACH (const typename Container::parent_trie &item, this->getPolicy ())
+    {
+      os << item->payload ()->GetName () << std::endl;
+    }
+}
+
+
+} // namespace ns3
+
+#endif // CCNX_CONTENT_STORE_IMPL_H_
diff --git a/model/content-store/ccnx-content-store-policies.cc b/model/content-store/ccnx-content-store-policies.cc
new file mode 100644
index 0000000..e9a23e1
--- /dev/null
+++ b/model/content-store/ccnx-content-store-policies.cc
@@ -0,0 +1,173 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "ccnx-content-store-policies.h"
+#include "ns3/log.h"
+#include "ns3/uinteger.h"
+
+NS_LOG_COMPONENT_DEFINE ("CcnxContentStorePolicies");
+
+namespace ns3
+{
+
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+// LRU policy
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+
+NS_OBJECT_ENSURE_REGISTERED (CcnxContentStoreLru);
+
+TypeId 
+CcnxContentStoreLru::GetTypeId (void)
+{
+  static TypeId tid = TypeId ("ns3::CcnxContentStoreLru")
+    .SetGroupName ("Ccnx")
+    .SetParent< CcnxContentStore > ()
+    .AddConstructor<CcnxContentStoreLru> ()
+    .AddAttribute ("Size",
+                   "Maximum number of packets that content storage can hold",
+                   UintegerValue (100),
+                   MakeUintegerAccessor (&CcnxContentStoreLru::SetMaxSize,
+                                         &CcnxContentStoreLru::GetMaxSize),
+                   MakeUintegerChecker<uint32_t> ())
+    ;
+
+  return tid;
+}
+
+void
+CcnxContentStoreLru::SetMaxSize (uint32_t maxSize)
+{
+  getPolicy ().set_max_size (maxSize);
+}
+
+uint32_t
+CcnxContentStoreLru::GetMaxSize () const
+{
+  return getPolicy ().get_max_size ();
+}
+
+CcnxContentStoreLru::CcnxContentStoreLru ()
+{
+}
+        
+CcnxContentStoreLru::~CcnxContentStoreLru () 
+{
+}
+
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+// RANDOM policy
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+
+NS_OBJECT_ENSURE_REGISTERED (CcnxContentStoreRandom);
+
+TypeId 
+CcnxContentStoreRandom::GetTypeId (void)
+{
+  static TypeId tid = TypeId ("ns3::CcnxContentStoreRandom")
+    .SetGroupName ("Ccnx")
+    .SetParent< CcnxContentStore > ()
+    .AddConstructor<CcnxContentStoreRandom> ()
+    
+    .AddAttribute ("Size",
+                   "Maximum number of packets that content storage can hold",
+                   UintegerValue (100),
+                   MakeUintegerAccessor (&CcnxContentStoreRandom::SetMaxSize,
+                                         &CcnxContentStoreRandom::GetMaxSize),
+                   MakeUintegerChecker<uint32_t> ())
+    ;
+
+  return tid;
+}
+
+void
+CcnxContentStoreRandom::SetMaxSize (uint32_t maxSize)
+{
+  getPolicy ().set_max_size (maxSize);
+}
+
+uint32_t
+CcnxContentStoreRandom::GetMaxSize () const
+{
+  return getPolicy ().get_max_size ();
+}
+
+CcnxContentStoreRandom::CcnxContentStoreRandom ()
+{
+}
+        
+CcnxContentStoreRandom::~CcnxContentStoreRandom () 
+{
+}
+
+
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+// FIFO policy
+////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////
+
+NS_OBJECT_ENSURE_REGISTERED (CcnxContentStoreFifo);
+
+TypeId 
+CcnxContentStoreFifo::GetTypeId (void)
+{
+  static TypeId tid = TypeId ("ns3::CcnxContentStoreFifo")
+    .SetGroupName ("Ccnx")
+    .SetParent< CcnxContentStore > ()
+    .AddConstructor<CcnxContentStoreFifo> ()
+    
+    .AddAttribute ("Size",
+                   "Maximum number of packets that content storage can hold",
+                   UintegerValue (100),
+                   MakeUintegerAccessor (&CcnxContentStoreFifo::SetMaxSize,
+                                         &CcnxContentStoreFifo::GetMaxSize),
+                   MakeUintegerChecker<uint32_t> ())
+    ;
+
+  return tid;
+}
+
+void
+CcnxContentStoreFifo::SetMaxSize (uint32_t maxSize)
+{
+  getPolicy ().set_max_size (maxSize);
+}
+
+uint32_t
+CcnxContentStoreFifo::GetMaxSize () const
+{
+  return getPolicy ().get_max_size ();
+}
+
+CcnxContentStoreFifo::CcnxContentStoreFifo ()
+{
+}
+        
+CcnxContentStoreFifo::~CcnxContentStoreFifo () 
+{
+}
+
+
+} // namespace ns3
+
diff --git a/model/content-store/ccnx-content-store-policies.h b/model/content-store/ccnx-content-store-policies.h
new file mode 100644
index 0000000..cb8ee10
--- /dev/null
+++ b/model/content-store/ccnx-content-store-policies.h
@@ -0,0 +1,142 @@
+/* -*-  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 CCNX_CONTENT_STORE_POLICIES_H
+#define	CCNX_CONTENT_STORE_POLICIES_H
+
+// #include "ns3/ccnx.h"
+
+#include "ccnx-content-store-impl.h"
+
+#include "../../utils/trie.h"
+#include "../../utils/trie-with-policy.h"
+
+#include "../../utils/lru-policy.h"
+#include "../../utils/random-policy.h"
+#include "../../utils/fifo-policy.h"
+
+namespace  ns3
+{
+
+/**
+ * \ingroup ccnx
+ * \brief Content Store with LRU replacement policy
+ */
+class CcnxContentStoreLru :
+    public CcnxContentStoreImpl<
+       ndnSIM::trie_with_policy<CcnxNameComponents,
+                                ndnSIM::smart_pointer_payload_traits<CcnxContentStoreEntry>,
+                                ndnSIM::lru_policy_traits >
+      >
+{
+public:
+  /**
+   * \brief Interface ID
+   *
+   * \return interface ID
+   */
+  static TypeId GetTypeId ();
+
+  /**
+   * Default constructor
+   */
+  CcnxContentStoreLru ();
+  virtual ~CcnxContentStoreLru ();
+
+private:
+  void
+  SetMaxSize (uint32_t maxSize);
+
+  uint32_t
+  GetMaxSize () const;
+};
+
+
+/**
+ * \ingroup ccnx
+ * \brief Content Store with RANDOM replacement policy
+ */
+class CcnxContentStoreRandom :
+    public CcnxContentStoreImpl<
+      ndnSIM::trie_with_policy<CcnxNameComponents,
+                       ndnSIM::smart_pointer_payload_traits<CcnxContentStoreEntry>,
+                       ndnSIM::random_policy_traits >
+      >
+{
+public:
+  /**
+   * \brief Interface ID
+   *
+   * \return interface ID
+   */
+  static TypeId GetTypeId ();
+
+  /**
+   * Default constructor
+   */
+  CcnxContentStoreRandom ();
+  virtual ~CcnxContentStoreRandom ();
+
+private:
+  void
+  SetMaxSize (uint32_t maxSize);
+
+  uint32_t
+  GetMaxSize () const;
+};
+
+/**
+ * \ingroup ccnx
+ * \brief Content Store with FIFO replacement policy
+ */
+class CcnxContentStoreFifo :
+    public CcnxContentStoreImpl<
+      ndnSIM::trie_with_policy<CcnxNameComponents,
+                       ndnSIM::smart_pointer_payload_traits<CcnxContentStoreEntry>,
+                       ndnSIM::fifo_policy_traits >
+      >
+{
+public:
+  /**
+   * \brief Interface ID
+   *
+   * \return interface ID
+   */
+  static TypeId GetTypeId ();
+
+  /**
+   * Default constructor
+   */
+  CcnxContentStoreFifo ();
+  virtual ~CcnxContentStoreFifo ();
+
+private:
+  void
+  SetMaxSize (uint32_t maxSize);
+
+  uint32_t
+  GetMaxSize () const;
+};
+
+
+} //namespace ns3
+
+#endif // CCNX_CONTENT_STORE_POLICIES_H
diff --git a/model/content-store/ccnx-content-store.cc b/model/content-store/ccnx-content-store.cc
new file mode 100644
index 0000000..ccbf200
--- /dev/null
+++ b/model/content-store/ccnx-content-store.cc
@@ -0,0 +1,96 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011,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>
+ *         Ilya Moiseenko <iliamo@cs.ucla.edu>
+ *         
+ */
+
+#include "ccnx-content-store.h"
+#include "ns3/log.h"
+#include "ns3/packet.h"
+#include "ns3/ccnx-name-components.h"
+#include "ns3/ccnx-interest-header.h"
+#include "ns3/ccnx-content-object-header.h"
+
+NS_LOG_COMPONENT_DEFINE ("CcnxContentStore");
+
+namespace ns3
+{
+
+NS_OBJECT_ENSURE_REGISTERED (CcnxContentStore);
+
+TypeId 
+CcnxContentStore::GetTypeId (void)
+{
+  static TypeId tid = TypeId ("ns3::CcnxContentStore")
+    .SetGroupName ("Ccnx")
+    .SetParent<Object> ()
+
+    .AddTraceSource ("CacheHits", "Trace called every time there is a cache hit",
+                     MakeTraceSourceAccessor (&CcnxContentStore::m_cacheHitsTrace))
+
+    .AddTraceSource ("CacheMisses", "Trace called every time there is a cache miss",
+                     MakeTraceSourceAccessor (&CcnxContentStore::m_cacheMissesTrace))
+    ;
+
+  return tid;
+}
+
+
+CcnxContentStore::~CcnxContentStore () 
+{
+}
+
+//////////////////////////////////////////////////////////////////////
+
+CcnxContentStoreEntry::CcnxContentStoreEntry (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
+  : m_header (header)
+  , m_packet (packet->Copy ())
+{
+}
+
+Ptr<Packet>
+CcnxContentStoreEntry::GetFullyFormedCcnxPacket () const
+{
+  static CcnxContentObjectTail tail; ///< \internal for optimization purposes
+
+  Ptr<Packet> packet = m_packet->Copy ();
+  packet->AddHeader (*m_header);
+  packet->AddTrailer (tail);
+  return packet;
+}
+
+const CcnxNameComponents&
+CcnxContentStoreEntry::GetName () const
+{
+  return m_header->GetName ();
+}
+
+Ptr<const CcnxContentObjectHeader>
+CcnxContentStoreEntry::GetHeader () const
+{
+  return m_header;
+}
+
+Ptr<const Packet>
+CcnxContentStoreEntry::GetPacket () const
+{
+  return m_packet;
+}
+
+} // namespace ns3
diff --git a/model/content-store/ccnx-content-store.h b/model/content-store/ccnx-content-store.h
new file mode 100644
index 0000000..34d209d
--- /dev/null
+++ b/model/content-store/ccnx-content-store.h
@@ -0,0 +1,174 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011,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>
+ *         Ilya Moiseenko <iliamo@cs.ucla.edu>
+ */
+
+#ifndef CCNX_CONTENT_STORE_H
+#define	CCNX_CONTENT_STORE_H
+
+#include "ns3/object.h"
+#include "ns3/ptr.h"
+#include "ns3/traced-callback.h"
+
+#include <boost/tuple/tuple.hpp>
+
+namespace  ns3
+{
+
+class Packet;
+class CcnxContentObjectHeader;
+class CcnxInterestHeader;
+class CcnxNameComponents;
+
+/**
+ * \ingroup ccnx
+ * \brief NDN content store entry
+ *
+ * Content store entry stores separately pseudo header and content of
+ * ContentObject packet.  It is responsibility of the caller to
+ * construct a fully formed CcnxPacket by calling Copy(), AddHeader(),
+ * AddTail() on the packet received by GetPacket() method.
+ *
+ * GetFullyFormedCcnxPacket method provided as a convenience
+ */
+class CcnxContentStoreEntry : public SimpleRefCount<CcnxContentStoreEntry>
+{
+public:
+  /**
+   * \brief Construct content store entry
+   *
+   * \param header Parsed CcnxContentObject header
+   * \param packet Original CCNx packet
+   *
+   * The constructor will make a copy of the supplied packet and calls
+   * RemoveHeader and RemoveTail on the copy.
+   */
+  CcnxContentStoreEntry (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet);
+
+  /**
+   * \brief Get prefix of the stored entry
+   * \returns prefix of the stored entry
+   */
+  const CcnxNameComponents&
+  GetName () const;
+
+  /**
+   * \brief Get CcnxContentObjectHeader of the stored entry
+   * \returns CcnxContentObjectHeader of the stored entry
+   */
+  Ptr<const CcnxContentObjectHeader>
+  GetHeader () const;
+
+  /**
+   * \brief Get content of the stored entry
+   * \returns content of the stored entry
+   */
+  Ptr<const Packet>
+  GetPacket () const;
+
+  /**
+   * \brief Convenience method to create a fully formed CCNx packet from stored header and content
+   * \returns A read-write copy of the packet with CcnxContentObjectHeader and CcxnContentObjectTail
+   */
+  Ptr<Packet>
+  GetFullyFormedCcnxPacket () const;
+
+private:
+  Ptr<CcnxContentObjectHeader> m_header; ///< \brief non-modifiable CcnxContentObjectHeader
+  Ptr<Packet> m_packet; ///< \brief non-modifiable content of the ContentObject packet
+};
+
+
+/**
+ * \ingroup ccnx
+ * \brief Base class for NDN content store
+ *
+ * Particular implementations should implement Lookup, Add, and Print methods
+ */
+class CcnxContentStore : public Object
+{
+public:
+  /**
+   * \brief Interface ID
+   *
+   * \return interface ID
+   */
+  static
+  TypeId GetTypeId ();
+
+  /**
+   * @brief Virtual destructor
+   */
+  virtual ~CcnxContentStore ();
+            
+  /**
+   * \brief Find corresponding CS entry for the given interest
+   *
+   * \param interest Interest for which matching content store entry
+   * will be searched
+   *
+   * If an entry is found, it is promoted to the top of most recent
+   * used entries index, \see m_contentStore
+   */
+  virtual boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
+  Lookup (Ptr<const CcnxInterestHeader> interest) = 0;
+            
+  /**
+   * \brief Add a new content to the content store.
+   *
+   * \param header Fully parsed CcnxContentObjectHeader
+   * \param packet Fully formed CCNx packet to add to content store
+   * (will be copied and stripped down of headers)
+   * @returns true if an existing entry was updated, false otherwise
+   */
+  virtual bool
+  Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet) = 0;
+
+  // /**
+  //  * \brief Add a new content to the content store.
+  //  *
+  //  * \param header Interest header for which an entry should be removed
+  //  * @returns true if an existing entry was removed, false otherwise
+  //  */
+  // virtual bool
+  // Remove (Ptr<CcnxInterestHeader> header) = 0;
+  
+  /**
+   * \brief Print out content store entries
+   */
+  virtual void
+  Print (std::ostream &os) const = 0;
+
+protected:
+  TracedCallback<Ptr<const CcnxInterestHeader>,
+                 Ptr<const CcnxContentObjectHeader> > m_cacheHitsTrace; ///< @brief trace of cache hits
+    
+  TracedCallback<Ptr<const CcnxInterestHeader> > m_cacheMissesTrace; ///< @brief trace of cache misses
+};
+
+inline std::ostream&
+operator<< (std::ostream &os, const CcnxContentStore &cs)
+{
+  cs.Print (os);
+  return os;
+}
+
+} // namespace ns3
+
+#endif // CCNX_CONTENT_STORE_H