Another set of refactoring
diff --git a/model/cs/content-store-impl.cc b/model/cs/content-store-impl.cc
new file mode 100644
index 0000000..fe6346b
--- /dev/null
+++ b/model/cs/content-store-impl.cc
@@ -0,0 +1,188 @@
+/* -*-  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 "content-store-impl.h"
+#include "ns3/log.h"
+#include "ns3/uinteger.h"
+#include "ns3/string.h"
+
+#include "../../utils/random-policy.h"
+#include "../../utils/lru-policy.h"
+#include "../../utils/fifo-policy.h"
+
+NS_LOG_COMPONENT_DEFINE ("ndn.cs.ContentStoreImpl");
+
+#define NS_OBJECT_ENSURE_REGISTERED_TEMPL(type, templ)  \
+  static struct X ## type ## templ ## RegistrationClass \
+  {                                                     \
+    X ## type ## templ ## RegistrationClass () {        \
+      ns3::TypeId tid = type<templ>::GetTypeId ();      \
+      tid.GetParent ();                                 \
+    }                                                   \
+  } x_ ## type ## templ ## RegistrationVariable
+
+namespace ns3 {
+namespace ndn {
+
+using namespace ndnSIM;
+
+namespace cs {
+
+
+template<>
+TypeId
+ContentStoreImpl< lru_policy_traits >::GetTypeId ()
+{
+  static TypeId tid = TypeId ("ns3::ndn::cs::Lru")
+    .SetGroupName ("Ndn")
+    .SetParent<ContentStore> ()
+    .AddConstructor< ContentStoreImpl< lru_policy_traits > > ()
+    .AddAttribute ("MaxSize",
+                   "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
+                   StringValue ("100"),
+                   MakeUintegerAccessor (&ContentStoreImpl< lru_policy_traits >::GetMaxSize,
+                                         &ContentStoreImpl< lru_policy_traits >::SetMaxSize),
+                   MakeUintegerChecker<uint32_t> ())
+    ;
+
+  return tid;
+}
+
+template<>
+TypeId
+ContentStoreImpl< random_policy_traits >::GetTypeId ()
+{
+  static TypeId tid = TypeId ("ns3::ndn::cs::Random")
+    .SetGroupName ("Ndn")
+    .SetParent<ContentStore> ()
+    .AddConstructor< ContentStoreImpl< random_policy_traits > > ()
+    .AddAttribute ("MaxSize",
+                   "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
+                   StringValue ("100"),
+                   MakeUintegerAccessor (&ContentStoreImpl< random_policy_traits >::GetMaxSize,
+                                         &ContentStoreImpl< random_policy_traits >::SetMaxSize),
+                   MakeUintegerChecker<uint32_t> ())
+    ;
+
+  return tid;
+}
+
+template<>
+TypeId
+ContentStoreImpl< fifo_policy_traits >::GetTypeId ()
+{
+  static TypeId tid = TypeId ("ns3::ndn::cs::Fifo")
+    .SetGroupName ("Ndn")
+    .SetParent<ContentStore> ()
+    .AddConstructor< ContentStoreImpl< fifo_policy_traits > > ()
+    .AddAttribute ("MaxSize",
+                   "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
+                   StringValue ("100"),
+                   MakeUintegerAccessor (&ContentStoreImpl< fifo_policy_traits >::GetMaxSize,
+                                         &ContentStoreImpl< fifo_policy_traits >::SetMaxSize),
+                   MakeUintegerChecker<uint32_t> ())
+    ;
+
+  return tid;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////////////
+/////////////////////////////////////////////////////////////////////////////////////////////////
+
+template<class Policy>
+boost::tuple<Ptr<Packet>, Ptr<const ContentObjectHeader>, Ptr<const Packet> >
+ContentStoreImpl<Policy>::Lookup (Ptr<const InterestHeader> interest)
+{
+  // NS_LOG_FUNCTION (this << interest->GetName ());
+
+  /// @todo Change to search with predicate
+  typename super::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 ()->GetFullyFormedNdnPacket (),
+                                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<ContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
+    }
+}   
+    
+template<class Policy>
+bool 
+ContentStoreImpl<Policy>::Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet)
+{
+  // NS_LOG_FUNCTION (this << header->GetName ());
+
+  return
+    this->insert (header->GetName (), Create<Entry> (header, packet))
+    .second;
+}
+    
+template<class Policy>
+void 
+ContentStoreImpl<Policy>::Print (std::ostream &os) const
+{
+  for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
+       item != this->getPolicy ().end ();
+       item++)
+  // BOOST_FOREACH (const typename super::parent_trie &item, this->getPolicy ())
+    {
+      os << item->payload ()->GetName () << std::endl;
+    }
+}
+
+template<class Policy>
+void 
+ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
+{
+  this->getPolicy ().set_max_size (maxSize);
+}
+
+template<class Policy>
+uint32_t
+ContentStoreImpl<Policy>::GetMaxSize () const
+{
+  return this->getPolicy ().get_max_size ();
+}
+
+////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////
+
+// explicit instantiation and registering
+template class ContentStoreImpl<lru_policy_traits>;
+template class ContentStoreImpl<random_policy_traits>;
+template class ContentStoreImpl<fifo_policy_traits>;
+
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, lru_policy_traits);
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, random_policy_traits);
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, fifo_policy_traits);
+
+
+} // namespace cs
+} // namespace ndn
+} // namespace ns3
diff --git a/model/cs/content-store-impl.h b/model/cs/content-store-impl.h
new file mode 100644
index 0000000..9ba1af5
--- /dev/null
+++ b/model/cs/content-store-impl.h
@@ -0,0 +1,81 @@
+/* -*-  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 NDN_CONTENT_STORE_IMPL_H_
+#define NDN_CONTENT_STORE_IMPL_H_
+
+#include "ndn-content-store.h"
+#include "ns3/packet.h"
+#include "ns3/ndn-interest-header.h"
+#include "ns3/ndn-content-object-header.h"
+#include <boost/foreach.hpp>
+
+#include "../../utils/trie-with-policy.h"
+
+namespace ns3 {
+namespace ndn {
+namespace cs {
+
+
+template<class Policy>
+class ContentStoreImpl : public ContentStore,
+                         protected ndnSIM::trie_with_policy< NameComponents,
+                                                             ndnSIM::smart_pointer_payload_traits< Entry >,
+                                                             Policy >
+{
+private:
+  typedef ndnSIM::trie_with_policy< NameComponents,
+                                    ndnSIM::smart_pointer_payload_traits< Entry >,
+                                    Policy > super;
+  
+public:
+  static TypeId
+  GetTypeId ();
+  
+  ContentStoreImpl () { };
+  virtual ~ContentStoreImpl () { };
+  
+  // from ContentStore
+  
+  virtual inline boost::tuple<Ptr<Packet>, Ptr<const ContentObjectHeader>, Ptr<const Packet> >
+  Lookup (Ptr<const InterestHeader> interest);
+            
+  virtual inline bool
+  Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet);
+
+  // virtual bool
+  // Remove (Ptr<InterestHeader> header);
+  
+  virtual inline void
+  Print (std::ostream &os) const;  
+
+private:
+  void
+  SetMaxSize (uint32_t maxSize);
+
+  uint32_t
+  GetMaxSize () const;
+};
+
+} // namespace cs
+} // namespace ndn
+} // namespace ns3
+
+#endif // NDN_CONTENT_STORE_IMPL_H_
diff --git a/model/cs/ndn-content-store.cc b/model/cs/ndn-content-store.cc
new file mode 100644
index 0000000..bfd4926
--- /dev/null
+++ b/model/cs/ndn-content-store.cc
@@ -0,0 +1,100 @@
+/* -*-  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 "ndn-content-store.h"
+#include "ns3/log.h"
+#include "ns3/packet.h"
+#include "ns3/ndn-name-components.h"
+#include "ns3/ndn-interest-header.h"
+#include "ns3/ndn-content-object-header.h"
+
+NS_LOG_COMPONENT_DEFINE ("ndn.cs.ContentStore");
+
+namespace ns3 {
+namespace ndn {
+
+NS_OBJECT_ENSURE_REGISTERED (ContentStore);
+
+TypeId 
+ContentStore::GetTypeId (void)
+{
+  static TypeId tid = TypeId ("ns3::ndn::ContentStore")
+    .SetGroupName ("Ndn")
+    .SetParent<Object> ()
+
+    .AddTraceSource ("CacheHits", "Trace called every time there is a cache hit",
+                     MakeTraceSourceAccessor (&ContentStore::m_cacheHitsTrace))
+
+    .AddTraceSource ("CacheMisses", "Trace called every time there is a cache miss",
+                     MakeTraceSourceAccessor (&ContentStore::m_cacheMissesTrace))
+    ;
+
+  return tid;
+}
+
+
+ContentStore::~ContentStore () 
+{
+}
+
+namespace cs {
+
+//////////////////////////////////////////////////////////////////////
+
+Entry::Entry (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet)
+  : m_header (header)
+  , m_packet (packet->Copy ())
+{
+}
+
+Ptr<Packet>
+Entry::GetFullyFormedNdnPacket () const
+{
+  static ContentObjectTail tail; ///< \internal for optimization purposes
+
+  Ptr<Packet> packet = m_packet->Copy ();
+  packet->AddHeader (*m_header);
+  packet->AddTrailer (tail);
+  return packet;
+}
+
+const NameComponents&
+Entry::GetName () const
+{
+  return m_header->GetName ();
+}
+
+Ptr<const ContentObjectHeader>
+Entry::GetHeader () const
+{
+  return m_header;
+}
+
+Ptr<const Packet>
+Entry::GetPacket () const
+{
+  return m_packet;
+}
+
+} // namespace cs
+} // namespace ndn
+} // namespace ns3
diff --git a/model/cs/ndn-content-store.h b/model/cs/ndn-content-store.h
new file mode 100644
index 0000000..963c7bc
--- /dev/null
+++ b/model/cs/ndn-content-store.h
@@ -0,0 +1,182 @@
+/* -*-  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 NDN_CONTENT_STORE_H
+#define	NDN_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;
+
+namespace ndn {
+
+class ContentObjectHeader;
+class InterestHeader;
+class NameComponents;
+
+namespace cs {
+
+/**
+ * \ingroup ndn
+ * \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 NDN Packet by calling Copy(), AddHeader(),
+ * AddTail() on the packet received by GetPacket() method.
+ *
+ * GetFullyFormedNdnPacket method provided as a convenience
+ */
+class Entry : public SimpleRefCount<Entry>
+{
+public:
+  /**
+   * \brief Construct content store entry
+   *
+   * \param header Parsed ContentObject header
+   * \param packet Original Ndn packet
+   *
+   * The constructor will make a copy of the supplied packet and calls
+   * RemoveHeader and RemoveTail on the copy.
+   */
+  Entry (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet);
+
+  /**
+   * \brief Get prefix of the stored entry
+   * \returns prefix of the stored entry
+   */
+  const NameComponents&
+  GetName () const;
+
+  /**
+   * \brief Get ContentObjectHeader of the stored entry
+   * \returns ContentObjectHeader of the stored entry
+   */
+  Ptr<const ContentObjectHeader>
+  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 Ndn packet from stored header and content
+   * \returns A read-write copy of the packet with ContentObjectHeader and ContentObjectTail
+   */
+  Ptr<Packet>
+  GetFullyFormedNdnPacket () const;
+
+private:
+  Ptr<const ContentObjectHeader> m_header; ///< \brief non-modifiable ContentObjectHeader
+  Ptr<Packet> m_packet; ///< \brief non-modifiable content of the ContentObject packet
+};
+
+} // namespace cs
+
+
+/**
+ * \ingroup ndn
+ * \brief Base class for NDN content store
+ *
+ * Particular implementations should implement Lookup, Add, and Print methods
+ */
+class ContentStore : public Object
+{
+public:
+  /**
+   * \brief Interface ID
+   *
+   * \return interface ID
+   */
+  static
+  TypeId GetTypeId ();
+
+  /**
+   * @brief Virtual destructor
+   */
+  virtual
+  ~ContentStore ();
+            
+  /**
+   * \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 ContentObjectHeader>, Ptr<const Packet> >
+  Lookup (Ptr<const InterestHeader> interest) = 0;
+            
+  /**
+   * \brief Add a new content to the content store.
+   *
+   * \param header Fully parsed ContentObjectHeader
+   * \param packet Fully formed Ndn 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<const ContentObjectHeader> 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<InterestHeader> header) = 0;
+  
+  /**
+   * \brief Print out content store entries
+   */
+  virtual void
+  Print (std::ostream &os) const = 0;
+
+protected:
+  TracedCallback<Ptr<const InterestHeader>,
+                 Ptr<const ContentObjectHeader> > m_cacheHitsTrace; ///< @brief trace of cache hits
+    
+  TracedCallback<Ptr<const InterestHeader> > m_cacheMissesTrace; ///< @brief trace of cache misses
+};
+
+inline std::ostream&
+operator<< (std::ostream &os, const ContentStore &cs)
+{
+  cs.Print (os);
+  return os;
+}
+
+} // namespace ndn
+} // namespace ns3
+
+#endif // NDN_CONTENT_STORE_H