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 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 34 | // NS_OBJECT_ENSURE_REGISTERED (CcnxContentStore); |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 36 | using namespace __ccnx_private; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 37 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 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 | // ; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 52 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 53 | // return tid; |
| 54 | // } |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 55 | |
| 56 | CcnxContentObjectTail CcnxContentStoreEntry::m_tail; |
| 57 | |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 58 | ////////////////////////////////////////////////////////////////////// |
| 59 | // Helper classes |
| 60 | ////////////////////////////////////////////////////////////////////// |
| 61 | /** |
| 62 | * \ingroup ccnx |
| 63 | * \brief Typedef for hash index of content store container |
| 64 | */ |
| 65 | struct CcnxContentStoreByPrefix |
| 66 | { |
| 67 | typedef |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 68 | CcnxContentStoreContainer::type::index<i_prefix>::type |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 69 | type; |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * \ingroup ccnx |
| 74 | * \brief Typedef for MRU index of content store container |
| 75 | */ |
| 76 | struct CcnxContentStoreByMru |
| 77 | { |
| 78 | typedef |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 79 | CcnxContentStoreContainer::type::index<i_mru>::type |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 80 | type; |
| 81 | }; |
| 82 | |
| 83 | #ifdef _DEBUG |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 84 | #define DUMP_INDEX_TAG i_ordered |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 85 | #define DUMP_INDEX CcnxContentStoreOrderedPrefix |
| 86 | /** |
| 87 | * \ingroup ccnx |
| 88 | * \brief Typedef for ordered index of content store container |
| 89 | */ |
| 90 | struct CcnxContentStoreOrderedPrefix |
| 91 | { |
| 92 | typedef |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 93 | CcnxContentStoreContainer::type::index<i_ordered>::type |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 94 | type; |
| 95 | }; |
| 96 | #else |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 97 | #define DUMP_INDEX_TAG i_mru |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 98 | #define DUMP_INDEX CcnxContentStoreByMru |
| 99 | #endif |
| 100 | |
| 101 | ////////////////////////////////////////////////////////////////////// |
| 102 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 103 | CcnxContentStoreEntry::CcnxContentStoreEntry (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet) |
| 104 | : m_header (header) |
| 105 | { |
| 106 | m_packet = packet->Copy (); |
| 107 | m_packet->RemoveHeader (*header); |
| 108 | m_packet->RemoveTrailer (m_tail); |
| 109 | } |
| 110 | |
| 111 | Ptr<Packet> |
| 112 | CcnxContentStoreEntry::GetFullyFormedCcnxPacket () const |
| 113 | { |
| 114 | Ptr<Packet> packet = m_packet->Copy (); |
| 115 | packet->AddHeader (*m_header); |
| 116 | packet->AddTrailer (m_tail); |
| 117 | return packet; |
| 118 | } |
| 119 | |
| 120 | // /// Disabled copy constructor |
| 121 | // CcnxContentStoreEntry::CcnxContentStoreEntry (const CcnxContentStoreEntry &o) |
| 122 | // { |
| 123 | // } |
| 124 | |
| 125 | // /// Disables copy operator |
| 126 | // CcnxContentStoreEntry& CcnxContentStoreEntry::operator= (const CcnxContentStoreEntry &o) |
| 127 | // { |
| 128 | // return *this; |
| 129 | // } |
| 130 | |
| 131 | |
| 132 | |
| 133 | CcnxContentStore::CcnxContentStore( ) |
| 134 | : 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] | 135 | |
| 136 | CcnxContentStore::~CcnxContentStore( ) |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 137 | { } |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 138 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 139 | /// Disabled copy constructor |
| 140 | CcnxContentStore::CcnxContentStore (const CcnxContentStore &o) |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 141 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /// Disables copy operator |
| 145 | CcnxContentStore& CcnxContentStore::operator= (const CcnxContentStore &o) |
| 146 | { |
| 147 | return *this; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | Ptr<Packet> |
| 152 | CcnxContentStore::Lookup (Ptr<const CcnxInterestHeader> interest) |
| 153 | { |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 154 | CcnxContentStoreContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (interest->GetName ()); |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 155 | if (it != m_contentStore.end ()) |
| 156 | { |
| 157 | // promote entry to the top |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 158 | m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (), |
| 159 | m_contentStore.project<i_mru> (it)); |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 160 | |
| 161 | // return fully formed CCNx packet |
| 162 | return it->GetFullyFormedCcnxPacket (); |
| 163 | } |
| 164 | return 0; |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 167 | void |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 168 | CcnxContentStore::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet) |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 169 | { |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 170 | CcnxContentStoreContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (header->GetName ()); |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 171 | if (it == m_contentStore.end ()) |
| 172 | { // add entry to the top |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 173 | m_contentStore.get<i_mru> ().push_front (CcnxContentStoreEntry (header, packet)); |
Alexander Afanasyev | dd32de8 | 2011-08-20 00:47:28 -0700 | [diff] [blame] | 174 | if (m_contentStore.size () > m_maxSize) |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 175 | m_contentStore.get<i_mru> ().pop_back (); |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 176 | } |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 177 | else |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 178 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 179 | // promote entry to the top |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame^] | 180 | m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (), |
| 181 | m_contentStore.project<i_mru> (it)); |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 184 | |
| 185 | void |
| 186 | CcnxContentStore::Print() const |
| 187 | { |
| 188 | for( DUMP_INDEX::type::iterator it=m_contentStore.get<DUMP_INDEX_TAG> ().begin (); |
| 189 | it != m_contentStore.get<DUMP_INDEX_TAG> ().end (); |
| 190 | it++ |
| 191 | ) |
| 192 | { |
| 193 | NS_LOG_INFO (it->GetName ()); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | } // namespace ns3 |