model: Generalizing content store implementation
diff --git a/model/cs/content-store-impl.h b/model/cs/content-store-impl.h
index 1281495..a168636 100644
--- a/model/cs/content-store-impl.h
+++ b/model/cs/content-store-impl.h
@@ -27,6 +27,10 @@
 #include "ns3/ndn-content-object.h"
 #include <boost/foreach.hpp>
 
+#include "ns3/log.h"
+#include "ns3/uinteger.h"
+#include "ns3/string.h"
+
 #include "../../utils/trie/trie-with-policy.h"
 
 namespace ns3 {
@@ -109,8 +113,172 @@
 
   uint32_t
   GetMaxSize () const;
+
+private:
+  static LogComponent g_log; ///< @brief Logging variable
 };
 
+//////////////////////////////////////////
+////////// Implementation ////////////////
+//////////////////////////////////////////
+
+
+template<class Policy>
+LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ());
+
+
+template<class Policy>
+TypeId
+ContentStoreImpl< Policy >::GetTypeId ()
+{
+  static TypeId tid = TypeId (("ns3::ndn::cs::"+Policy::GetName ()).c_str ())
+    .SetGroupName ("Ndn")
+    .SetParent<ContentStore> ()
+    .AddConstructor< ContentStoreImpl< Policy > > ()
+    .AddAttribute ("MaxSize",
+                   "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
+                   StringValue ("100"),
+                   MakeUintegerAccessor (&ContentStoreImpl< Policy >::GetMaxSize,
+                                         &ContentStoreImpl< Policy >::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 ());
+
+  Ptr< entry > newEntry = Create< entry > (header, packet);
+  std::pair< typename super::iterator, bool > result = super::insert (header->GetName (), newEntry);
+
+  if (result.first != super::end ())
+    {
+      if (result.second)
+        {
+          newEntry->SetTrie (result.first);
+          return newEntry;
+        }
+      else
+        {
+          // should we do anything?
+          // update payload? add new payload?
+          return false;
+        }
+    }
+  else
+    return false; // cannot insert entry
+}
+
+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++)
+    {
+      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 ();
+}
+
+template<class Policy>
+uint32_t
+ContentStoreImpl<Policy>::GetSize () const
+{
+  return this->getPolicy ().size ();
+}
+
+template<class Policy>
+Ptr<Entry>
+ContentStoreImpl<Policy>::Begin ()
+{
+  typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
+  for (; item != end; item++)
+    {
+      if (item->payload () == 0) continue;
+      break;
+    }
+
+  if (item == end)
+    return End ();
+  else
+    return item->payload ();
+}
+
+template<class Policy>
+Ptr<Entry>
+ContentStoreImpl<Policy>::End ()
+{
+  return 0;
+}
+
+template<class Policy>
+Ptr<Entry>
+ContentStoreImpl<Policy>::Next (Ptr<Entry> from)
+{
+  if (from == 0) return 0;
+  
+  typename super::parent_trie::recursive_iterator
+    item (*StaticCast< entry > (from)->to_iterator ()),
+    end (0);
+  
+  for (item++; item != end; item++)
+    {
+      if (item->payload () == 0) continue;
+      break;
+    }
+
+  if (item == end)
+    return End ();
+  else
+    return item->payload ();
+}
+
+
 } // namespace cs
 } // namespace ndn
 } // namespace ns3