Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -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: Ilya Moiseenko <iliamo@cs.ucla.edu> |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "ccnx-content-store.h" |
| 23 | #include "ns3/log.h" |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 24 | #include "ns3/packet.h" |
| 25 | #include "ns3/ccnx-interest-header.h" |
| 26 | #include "ns3/ccnx-content-object-header.h" |
| 27 | #include "ns3/uinteger.h" |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 28 | |
| 29 | NS_LOG_COMPONENT_DEFINE ("CcnxContentStore"); |
| 30 | |
| 31 | namespace ns3 |
| 32 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 33 | |
| 34 | NS_OBJECT_ENSURE_REGISTERED (CcnxContentStore); |
| 35 | |
| 36 | using namespace __ccnx_private; |
| 37 | |
| 38 | TypeId |
| 39 | CcnxContentStore::GetTypeId (void) |
| 40 | { |
| 41 | static TypeId tid = TypeId ("ns3::CcnxContentStore") |
| 42 | .SetGroupName ("Ccnx") |
| 43 | .SetParent<Object> () |
| 44 | .AddConstructor<CcnxContentStore> () |
| 45 | .AddAttribute ("Size", |
| 46 | "Maximum number of packets that content storage can hold", |
| 47 | UintegerValue (100), |
| 48 | MakeUintegerAccessor (&CcnxContentStore::SetMaxSize, |
| 49 | &CcnxContentStore::GetMaxSize), |
| 50 | MakeUintegerChecker<uint32_t> ()) |
| 51 | ; |
| 52 | |
| 53 | return tid; |
| 54 | } |
| 55 | |
| 56 | CcnxContentObjectTail CcnxContentStoreEntry::m_tail; |
| 57 | |
| 58 | CcnxContentStoreEntry::CcnxContentStoreEntry (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet) |
| 59 | : m_header (header) |
| 60 | { |
| 61 | m_packet = packet->Copy (); |
| 62 | m_packet->RemoveHeader (*header); |
| 63 | m_packet->RemoveTrailer (m_tail); |
| 64 | } |
| 65 | |
| 66 | Ptr<Packet> |
| 67 | CcnxContentStoreEntry::GetFullyFormedCcnxPacket () const |
| 68 | { |
| 69 | Ptr<Packet> packet = m_packet->Copy (); |
| 70 | packet->AddHeader (*m_header); |
| 71 | packet->AddTrailer (m_tail); |
| 72 | return packet; |
| 73 | } |
| 74 | |
| 75 | // /// Disabled copy constructor |
| 76 | // CcnxContentStoreEntry::CcnxContentStoreEntry (const CcnxContentStoreEntry &o) |
| 77 | // { |
| 78 | // } |
| 79 | |
| 80 | // /// Disables copy operator |
| 81 | // CcnxContentStoreEntry& CcnxContentStoreEntry::operator= (const CcnxContentStoreEntry &o) |
| 82 | // { |
| 83 | // return *this; |
| 84 | // } |
| 85 | |
| 86 | |
| 87 | |
| 88 | CcnxContentStore::CcnxContentStore( ) |
| 89 | : m_maxSize(100) { } // this value shouldn't matter, NS-3 should call SetSize with default value specified in AddAttribute earlier |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 90 | |
| 91 | CcnxContentStore::~CcnxContentStore( ) |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 92 | { } |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 94 | /// Disabled copy constructor |
| 95 | CcnxContentStore::CcnxContentStore (const CcnxContentStore &o) |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 96 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /// Disables copy operator |
| 100 | CcnxContentStore& CcnxContentStore::operator= (const CcnxContentStore &o) |
| 101 | { |
| 102 | return *this; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | Ptr<Packet> |
| 107 | CcnxContentStore::Lookup (Ptr<const CcnxInterestHeader> interest) |
| 108 | { |
| 109 | CcnxContentStoreContainer::type::iterator it = m_contentStore.get<hash> ().find (interest->GetName ()); |
| 110 | if (it != m_contentStore.end ()) |
| 111 | { |
| 112 | // promote entry to the top |
| 113 | m_contentStore.get<mru> ().relocate (m_contentStore.get<mru> ().begin (), |
| 114 | m_contentStore.project<mru> (it)); |
| 115 | |
| 116 | // return fully formed CCNx packet |
| 117 | return it->GetFullyFormedCcnxPacket (); |
| 118 | } |
| 119 | return 0; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 122 | void |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 123 | CcnxContentStore::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet) |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 124 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 125 | CcnxContentStoreContainer::type::iterator it = m_contentStore.get<hash> ().find (header->GetName ()); |
| 126 | if (it == m_contentStore.end ()) |
| 127 | { // add entry to the top |
| 128 | m_contentStore.get<mru> ().push_front (CcnxContentStoreEntry (header, packet)); |
Alexander Afanasyev | dd32de8 | 2011-08-20 00:47:28 -0700 | [diff] [blame] | 129 | if (m_contentStore.size () > m_maxSize) |
| 130 | m_contentStore.get<mru> ().pop_back (); |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 131 | } |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 132 | else |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 133 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 134 | // promote entry to the top |
| 135 | m_contentStore.get<mru> ().relocate (m_contentStore.get<mru> ().begin (), |
| 136 | m_contentStore.project<mru> (it)); |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 139 | |
| 140 | void |
| 141 | CcnxContentStore::Print() const |
| 142 | { |
| 143 | for( DUMP_INDEX::type::iterator it=m_contentStore.get<DUMP_INDEX_TAG> ().begin (); |
| 144 | it != m_contentStore.get<DUMP_INDEX_TAG> ().end (); |
| 145 | it++ |
| 146 | ) |
| 147 | { |
| 148 | NS_LOG_INFO (it->GetName ()); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | } // namespace ns3 |