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