Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -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> |
| 19 | */ |
| 20 | |
| 21 | #include "ccnx-producer.h" |
| 22 | |
| 23 | |
| 24 | NS_LOG_COMPONENT_DEFINE ("CcnxProducer"); |
| 25 | |
| 26 | namespace ns3 |
| 27 | { |
| 28 | |
| 29 | NS_OBJECT_ENSURE_REGISTERED (CcnxProducer); |
| 30 | |
| 31 | TypeId |
| 32 | CcnxProducer::GetTypeId (void) |
| 33 | { |
| 34 | static TypeId tid = TypeId ("ns3::CcnxProducer") |
| 35 | .SetParent<Application> () |
| 36 | .AddConstructor<CcnxProducer> () |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 37 | /*.AddAttribute ("Capacity", "Capacity of the ContentStore", |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 38 | UintegerValue(100), |
| 39 | MakeUintegerAccessor(&CcnxProducer::m_storeCapacity), |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 40 | MakeUintegerChecker<uint32_t>())*/ |
| 41 | .AddAttribute ("Prefix","Prefix, for which producer has the data", |
| 42 | CcnxNameComponentsValue (), |
| 43 | MakeCcnxNameComponentsAccessor (&CcnxProducer::m_prefix), |
| 44 | MakeCcnxNameComponentsChecker ()) |
| 45 | .AddAttribute ("PayloadSize", "Virtual payload size for Content packets", |
| 46 | UintegerValue(100), |
| 47 | MakeUintegerAccessor(&CcnxProducer::m_virtualPayloadSize), |
| 48 | MakeUintegerChecker<uint32_t>()) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 49 | .AddTraceSource ("InterestTrace", "Interests that were received", |
| 50 | MakeTraceSourceAccessor (&CcnxProducer::m_interestsTrace)) |
| 51 | .AddTraceSource ("ContentObjectTrace", "ContentObjects that were sent", |
| 52 | MakeTraceSourceAccessor (&CcnxProducer::m_contentObjectsTrace)) |
| 53 | ; |
| 54 | |
| 55 | return tid; |
| 56 | } |
| 57 | |
| 58 | CcnxProducer::CcnxProducer () |
| 59 | { |
| 60 | NS_LOG_FUNCTION_NOARGS (); |
| 61 | } |
| 62 | |
| 63 | CcnxProducer::~CcnxProducer() |
| 64 | { |
| 65 | NS_LOG_FUNCTION_NOARGS (); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | CcnxProducer::DoDispose (void) |
| 70 | { |
| 71 | NS_LOG_FUNCTION_NOARGS (); |
| 72 | |
| 73 | Application::DoDispose (); |
| 74 | } |
| 75 | |
| 76 | // Application Methods |
| 77 | void |
| 78 | CcnxProducer::StartApplication () // Called at time specified by Start |
| 79 | { |
| 80 | NS_LOG_FUNCTION_NOARGS (); |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 81 | |
| 82 | NS_ASSERT_MSG (m_face == 0, "Face should not exist"); |
| 83 | m_face = Create<CcnxLocalFace> (); |
| 84 | |
| 85 | // step 1. Set up forwarding from face to application |
| 86 | m_face->SetNode (GetNode ()); |
| 87 | m_face->SetInterestHandler (MakeCallback (&CcnxProducer::OnInterest, this)); |
| 88 | |
| 89 | // step 2. Set up forwarding to and from ccnx |
| 90 | NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () !=0, |
| 91 | "Ccnx stack should be installed on the node " << GetNode ()); |
| 92 | GetNode ()->GetObject<Ccnx> ()->AddFace (m_face); |
| 93 | //Add (const CcnxNameComponents &prefix, Ptr<CcnxFace> face, int32_t metric); |
| 94 | GetNode ()->GetObject<Ccnx> ()->GetObject<CcnxFib> ()->Add(m_prefix, m_face, 0); |
| 95 | // step 3. Enable face |
| 96 | m_face->SetUp (); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
| 100 | CcnxProducer::StopApplication () // Called at time specified by Stop |
| 101 | { |
| 102 | NS_LOG_FUNCTION_NOARGS (); |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void |
| 106 | CcnxProducer::OnInterest(const Ptr<const CcnxInterestHeader> &interest) |
| 107 | { |
| 108 | NS_LOG_FUNCTION (this); |
| 109 | |
| 110 | |
| 111 | |
| 112 | //Ptr<Packet> data = Lookup (interest); |
| 113 | |
| 114 | |
| 115 | |
| 116 | Ptr<Packet> incomingPacket = Create<Packet>(m_virtualPayloadSize); |
| 117 | incomingPacket->AddHeader (*interest); |
| 118 | m_interestsTrace(m_face,incomingPacket); |
| 119 | |
| 120 | |
| 121 | |
| 122 | static CcnxContentObjectTail tail; ///< \internal for optimization purposes |
| 123 | Ptr<Packet> outgoingPacket = Create<Packet> (m_virtualPayloadSize); |
| 124 | Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader>(); |
| 125 | header->SetName(Create<CcnxNameComponents>(interest->GetName())); |
| 126 | outgoingPacket->AddHeader(*header); |
| 127 | outgoingPacket->AddTrailer (tail); |
| 128 | |
| 129 | m_contentObjectsTrace(m_face,outgoingPacket); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 130 | |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 131 | m_face->ReceiveFromApplication(outgoingPacket); |
| 132 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void |
| 136 | CcnxProducer::CancelEvents () |
| 137 | { |
| 138 | NS_LOG_FUNCTION_NOARGS (); |
| 139 | |
Alexander Afanasyev | 152cf11 | 2011-08-29 17:58:32 -0700 | [diff] [blame] | 140 | // Simulator::Cancel (m_sendEvent); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 141 | } |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 142 | |
| 143 | CcnxNameComponents |
| 144 | CcnxProducer::GetPrefix() const |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 145 | { |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 146 | return m_prefix; |
| 147 | } |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 148 | |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 149 | /*uint32_t |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 150 | CcnxProducer::GetStoreCapacity() |
| 151 | { |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 152 | return m_storeCapacity; |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 156 | CcnxProducer::SetStoreCapacity(uint32_t capacity) |
| 157 | { |
| 158 | m_storeCapacity = capacity; |
| 159 | } |
| 160 | */ |
| 161 | /*void |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 162 | CcnxProducer::HandlePacket(const Ptr<CcnxFace> &face, const Ptr<const Packet> &packet) |
| 163 | { |
| 164 | uint8_t type[2]; |
| 165 | uint32_t read = packet->CopyData (type,2); |
| 166 | if (read!=2) |
| 167 | { |
| 168 | NS_LOG_INFO ("Unknown CcnxPacket"); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (type[0] == INTEREST_BYTE0 && type[1] == INTEREST_BYTE1) |
| 173 | { |
| 174 | m_interestsTrace(face,packet); |
| 175 | } |
| 176 | else if (type[0] == CONTENT_OBJECT_BYTE0 && type[1] == CONTENT_OBJECT_BYTE1) |
| 177 | { |
| 178 | m_contentObjectsTrace(face,packet); |
| 179 | } |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 180 | }*/ |
| 181 | |
| 182 | /*Ptr<Packet> |
| 183 | CcnxProducer::Lookup (Ptr<const CcnxInterestHeader> interest) |
| 184 | { |
| 185 | NS_LOG_FUNCTION_NOARGS (); |
| 186 | DataStoreContainer::type::iterator it = m_availableData.get<i_prefix> ().find (interest->GetName ()); |
| 187 | |
| 188 | if (it != m_availableData.end ()) |
| 189 | { |
| 190 | // return fully formed CCNx packet |
| 191 | return it->GetFullyFormedCcnxPacket (); |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 196 | |
| 197 | void |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 198 | CcnxProducer::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 199 | { |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 200 | NS_LOG_FUNCTION_NOARGS (); |
| 201 | DataStoreContainer::type::iterator it = m_availableData.get<i_prefix> ().find (header->GetName ()); |
| 202 | |
| 203 | if (it == m_availableData.end ()) |
| 204 | { // add entry to the top |
| 205 | m_availableData.get<i_mru> ().push_front (DataStoreEntry (header, packet)); |
| 206 | |
| 207 | if (m_availableData.size () > m_storeCapacity) |
| 208 | m_availableData.get<i_mru> ().pop_back (); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | // promote entry to the top |
| 213 | //m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (), |
| 214 | // m_contentStore.project<i_mru> (it)); |
| 215 | } |
| 216 | }*/ |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 217 | } |