Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #ifndef CCNX_CONTENT_STORE_IMPL_H_ |
| 22 | #define CCNX_CONTENT_STORE_IMPL_H_ |
| 23 | |
| 24 | #include "ccnx-content-store.h" |
| 25 | #include "ns3/packet.h" |
| 26 | #include "ns3/ccnx-interest-header.h" |
| 27 | #include "ns3/ccnx-content-object-header.h" |
| 28 | #include <boost/foreach.hpp> |
| 29 | |
| 30 | namespace ns3 |
| 31 | { |
| 32 | |
| 33 | template<class Container> |
| 34 | class CcnxContentStoreImpl : public CcnxContentStore, |
| 35 | protected Container |
| 36 | { |
| 37 | public: |
| 38 | // from CcnxContentStore |
| 39 | |
| 40 | virtual inline boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> > |
| 41 | Lookup (Ptr<const CcnxInterestHeader> interest); |
| 42 | |
| 43 | virtual inline bool |
| 44 | Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet); |
| 45 | |
| 46 | // virtual bool |
| 47 | // Remove (Ptr<CcnxInterestHeader> header); |
| 48 | |
| 49 | virtual inline void |
| 50 | Print (std::ostream &os) const; |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | template<class Container> |
| 55 | boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> > |
| 56 | CcnxContentStoreImpl<Container>::Lookup (Ptr<const CcnxInterestHeader> interest) |
| 57 | { |
| 58 | // NS_LOG_FUNCTION (this << interest->GetName ()); |
| 59 | |
| 60 | /// @todo Change to search with predicate |
| 61 | typename Container::iterator node = this->deepest_prefix_match (interest->GetName ()); |
| 62 | |
| 63 | if (node != this->end ()) |
| 64 | { |
| 65 | this->m_cacheHitsTrace (interest, node->payload ()->GetHeader ()); |
| 66 | |
| 67 | // NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ()); |
| 68 | return boost::make_tuple (node->payload ()->GetFullyFormedCcnxPacket (), |
| 69 | node->payload ()->GetHeader (), |
| 70 | node->payload ()->GetPacket ()); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | // NS_LOG_DEBUG ("cache miss for " << interest->GetName ()); |
| 75 | this->m_cacheMissesTrace (interest); |
| 76 | return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | template<class Container> |
| 81 | bool |
| 82 | CcnxContentStoreImpl<Container>::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet) |
| 83 | { |
| 84 | // NS_LOG_FUNCTION (this << header->GetName ()); |
| 85 | |
| 86 | return |
| 87 | this->insert (header->GetName (), Create<CcnxContentStoreEntry> (header, packet)) |
| 88 | .second; |
| 89 | } |
| 90 | |
| 91 | template<class Container> |
| 92 | void |
| 93 | CcnxContentStoreImpl<Container>::Print (std::ostream &os) const |
| 94 | { |
Alexander Afanasyev | 051d378 | 2012-07-03 10:10:15 -0700 | [diff] [blame] | 95 | for (typename Container::policy_container::const_iterator item = this->getPolicy ().begin (); |
| 96 | item != this->getPolicy ().end (); |
| 97 | item++) |
| 98 | // BOOST_FOREACH (const typename Container::parent_trie &item, this->getPolicy ()) |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 99 | { |
Alexander Afanasyev | 051d378 | 2012-07-03 10:10:15 -0700 | [diff] [blame] | 100 | os << item->payload ()->GetName () << std::endl; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | } // namespace ns3 |
| 106 | |
| 107 | #endif // CCNX_CONTENT_STORE_IMPL_H_ |