/* ndn-content-store-impl-prob.h * * Created: 05-06-2013 * Copyright (c) 2013 Trinity College fo Dublin, Ireland * Author: Andriana Ioannou * * This is the implementation of the probabilistic cahing. Additions to the original content object packet are the inclusion of the double probability variable, the SetCacheProbability, GetCacheProbability functions and the .AddAttribute "CacheProbability".(search for //(+) ) * Note: The main difference before and now is that i am trying to make it accepting a fixed probabilistic value and do the caching based on that. Not sure how i can use my constructor for doing this, so can always ask the user to input a value for it and then use this in the caching/ adding function of my content store. */ #ifndef NDN_CONTENT_STORE_IMPL_H #define NDN_CONTENT_STORE_IMPL_H #include #include "ndn-content-store.h" #include "ns3/packet.h" #include "ns3/ndn-interest.h" #include "ns3/ndn-content-object.h" #include #include "ns3/log.h" #include "ns3/object.h" #include "ns3/uinteger.h" #include "ns3/double.h" #include "ns3/string.h" #include "../../utils/trie/trie-with-policy.h" #include "ns3/simulator.h" #include "ns3/nstime.h" #include "ns3/command-line.h" #include "ns3/random-variable.h" #include #include #include #include namespace ns3 { namespace ndn { namespace cs { template class EntryImpl : public Entry { public: typedef Entry base_type; public: EntryImpl (Ptr cs, Ptr header, Ptr packet) : Entry (cs, header, packet) , item_ (0) { } void SetTrie (typename CS::super::iterator item) { item_ = item; } typename CS::super::iterator to_iterator () { return item_; } typename CS::super::const_iterator to_iterator () const { return item_; } private: typename CS::super::iterator item_; }; template class ContentStoreImpl : public ContentStore, protected ndnSIM::trie_with_policy< Name, ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > >, Entry >, Policy > { public: typedef ndnSIM::trie_with_policy< Name, ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl < Policy > >, Entry >, Policy > super; typedef EntryImpl< ContentStoreImpl< Policy > > entry; static TypeId GetTypeId (); ContentStoreImpl () { this->probability=0.0; }; /* instead of using this constructor i will use the input so as the user to define the probability of cahing each time. */ //ContentStoreImplProb2 (double num) { this->probability=num;}; // (+) virtual ~ContentStoreImpl () { }; // from ContentStore virtual inline boost::tuple, Ptr, Ptr > Lookup (Ptr interest); virtual inline bool Add (Ptr header, Ptr packet); // virtual bool // Remove (Ptr header); virtual inline void Print (std::ostream &os) const; virtual uint32_t GetSize () const; virtual Ptr Begin (); virtual Ptr End (); virtual Ptr Next (Ptr); private: //----------------------------------------------------------------------- double probability; //(+) void SetCacheProbability(double probability); //(+) double GetCacheProbability () const;//(+) //---------------------------------------------------------------------- void SetMaxSize (uint32_t maxSize); uint32_t GetMaxSize () const; private: static LogComponent g_log; ///< @brief Logging variable /// @brief trace of for entry additions (fired every time entry is successfully added to the cache): first parameter is pointer to the CS entry TracedCallback< Ptr > m_didAddEntry; }; ////////////////////////////////////////// ////////// Implementation //////////////// ////////////////////////////////////////// template LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ()); /* TypeId TypeId::AddAttribute (std::string name, std::string help, const AttributeValue &initialValue, Ptr accessor, Ptr checker) */ template TypeId ContentStoreImpl< Policy >::GetTypeId () { static TypeId tid = TypeId (("ns3::ndn::cs::"+Policy::GetName ()).c_str ()) .SetGroupName ("Ndn") .SetParent () .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 ()) //-------------------------------------------------------------------------- .AddAttribute ("CacheProbability", //(+) "Set probability of caching in ContentStore. If 1, every content is cached. If 0, no content is cached.",//(+) StringValue ("1.0"),//(+) MakeDoubleAccessor (&ContentStoreImpl< Policy >::GetCacheProbability,//(+) &ContentStoreImpl< Policy >::SetCacheProbability),MakeDoubleChecker ()) //(+) //-------------------------------------------------------------------------- .AddTraceSource ("DidAddEntry", "Trace fired every time entry is successfully added to the cache", MakeTraceSourceAccessor (&ContentStoreImpl< Policy >::m_didAddEntry)); return tid; } template boost::tuple, Ptr, Ptr > ContentStoreImpl::Lookup (Ptr 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, Ptr > (0, 0, 0); } } template//(~) bool ContentStoreImpl::Add (Ptr header, Ptr packet) { //-------------------------------------------------------------------------- bool to_cache; //(+) probability = 0.0; //(+) //ns3::UniformVariable u_rand(0,100); probability = this->GetCacheProbability();//(+) printf("i do want to print the cache probability: %lf \n", probability);//(+) NS_LOG_FUNCTION (this << header->GetName ()); //now will get a random probability so as to decide caching. to_cache = ((rand()%100)< (int)(probability*100)) ? true : false ;//(+) if (to_cache == false){//(+) printf("I don't cache\n");//(+) return false; // (+) } else { printf("I do cache\n");//(+) //-------------------------------------------------------------------------- Ptr< entry > newEntry = Create< entry > (this, 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); m_didAddEntry (newEntry); return true; } else { // should we do anything? // update payload? add new payload? return false; } } else return false; // cannot insert entry }//(+) } template void ContentStoreImpl::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//(+) void ContentStoreImpl::SetCacheProbability (double probability) { this->getPolicy ().set_probability (probability); } template//(+) double ContentStoreImpl::GetCacheProbability () const { return this->getPolicy ().get_probability (); } //-------------------------------------------------------------------------- template void ContentStoreImpl::SetMaxSize (uint32_t maxSize) { this->getPolicy ().set_max_size (maxSize); } template uint32_t ContentStoreImpl::GetMaxSize () const { return this->getPolicy ().get_max_size (); } template uint32_t ContentStoreImpl::GetSize () const { return this->getPolicy ().size (); } template Ptr ContentStoreImpl::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 Ptr ContentStoreImpl::End () { return 0; } template Ptr ContentStoreImpl::Next (Ptr 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 #endif // NDN_CONTENT_STORE_IMPL_H_