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-consumer.h" |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 22 | #include "ns3/ptr.h" |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 23 | #include "ns3/log.h" |
| 24 | #include "ns3/simulator.h" |
| 25 | #include "ns3/packet.h" |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 26 | #include "ns3/callback.h" |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 27 | #include "ns3/string.h" |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 28 | #include "ns3/boolean.h" |
| 29 | #include "ns3/uinteger.h" |
| 30 | |
| 31 | #include "ns3/ccnx.h" |
| 32 | #include "ns3/ccnx-local-face.h" |
| 33 | #include "ns3/ccnx-interest-header.h" |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 34 | #include "ns3/ccnx-content-object-header.h" |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 36 | #include <boost/ref.hpp> |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame^] | 37 | #include <boost/lexical_cast.hpp> |
| 38 | #include <boost/lambda/lambda.hpp> |
| 39 | #include <boost/lambda/bind.hpp> |
| 40 | |
| 41 | namespace ll = boost::lambda; |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 42 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 43 | NS_LOG_COMPONENT_DEFINE ("CcnxConsumer"); |
| 44 | |
| 45 | namespace ns3 |
| 46 | { |
| 47 | |
| 48 | NS_OBJECT_ENSURE_REGISTERED (CcnxConsumer); |
| 49 | |
| 50 | TypeId |
| 51 | CcnxConsumer::GetTypeId (void) |
| 52 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 53 | static TypeId tid = TypeId ("ns3::CcnxConsumer") |
| 54 | .SetParent<Application> () |
| 55 | .AddConstructor<CcnxConsumer> () |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame^] | 56 | .AddAttribute ("StartSeq", "Initial sequence number", |
| 57 | IntegerValue(0), |
| 58 | MakeIntegerAccessor(&CcnxConsumer::m_seq), |
| 59 | MakeIntegerChecker<int32_t>()) |
| 60 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 61 | .AddAttribute ("OffTime", "Time interval between packets", |
| 62 | StringValue ("100ms"), |
| 63 | MakeTimeAccessor (&CcnxConsumer::m_offTime), |
| 64 | MakeTimeChecker ()) |
| 65 | .AddAttribute ("InterestName","CcnxName of the Interest (use CcnxNameComponents)", |
| 66 | StringValue ("/"), |
| 67 | MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName), |
| 68 | MakeCcnxNameComponentsChecker ()) |
| 69 | .AddAttribute ("LifeTime", "LifeTime fo interest packet", |
| 70 | StringValue ("2s"), |
| 71 | MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime), |
| 72 | MakeTimeChecker ()) |
| 73 | .AddAttribute ("MinSuffixComponents", "MinSuffixComponents", |
| 74 | IntegerValue(-1), |
| 75 | MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents), |
| 76 | MakeIntegerChecker<int32_t>()) |
| 77 | .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents", |
| 78 | IntegerValue(-1), |
| 79 | MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents), |
| 80 | MakeIntegerChecker<int32_t>()) |
| 81 | .AddAttribute ("ChildSelector", "ChildSelector", |
| 82 | BooleanValue(false), |
| 83 | MakeBooleanAccessor(&CcnxConsumer::m_childSelector), |
| 84 | MakeBooleanChecker()) |
| 85 | .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)", |
| 86 | CcnxNameComponentsValue (), |
| 87 | MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude), |
| 88 | MakeCcnxNameComponentsChecker ()) |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame^] | 89 | |
| 90 | .AddAttribute ("RTO", |
| 91 | "Initial retransmission timeout", |
| 92 | StringValue ("1s"), |
| 93 | MakeTimeAccessor (&CcnxConsumer::m_rto), |
| 94 | MakeTimeChecker ()) |
| 95 | .AddAttribute ("RetxTimer", |
| 96 | "Timeout defining how frequent retransmission timeouts should be checked", |
| 97 | StringValue ("1s"), |
| 98 | MakeTimeAccessor (&CcnxConsumer::GetRetxTimer, &CcnxConsumer::SetRetxTimer), |
| 99 | MakeTimeChecker ()) |
| 100 | |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 101 | // .AddTraceSource ("InterestTrace", "Interests that were sent", |
| 102 | // MakeTraceSourceAccessor (&CcnxConsumer::m_interestsTrace)) |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame^] | 103 | // .AddTraceSource ("NackTrace", "NACKs received", |
| 104 | // MakeTraceSourceAccessor (&CcnxConsumer::m_nackTrace)) |
| 105 | // .AddTraceSource ("ContentObjectTrace", "ContentObjects received", |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 106 | // MakeTraceSourceAccessor (&CcnxConsumer::m_contentObjectsTrace)) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 107 | ; |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 109 | return tid; |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | CcnxConsumer::CcnxConsumer () |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 113 | : m_rand (0, std::numeric_limits<uint32_t>::max ()) |
| 114 | , m_seq (0) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 115 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 116 | NS_LOG_FUNCTION_NOARGS (); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 117 | } |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame^] | 118 | |
| 119 | void |
| 120 | CcnxConsumer::SetRetxTimer (Time retxTimer) |
| 121 | { |
| 122 | m_retxTimer = retxTimer; |
| 123 | if (m_retxEvent.IsRunning ()) |
| 124 | m_retxEvent.Cancel (); // cancel any scheduled cleanup events |
| 125 | |
| 126 | // schedule even with new timeout |
| 127 | m_retxEvent = Simulator::Schedule (m_retxTimer, |
| 128 | &CcnxConsumer::CheckRetxTimeout, this); |
| 129 | } |
| 130 | |
| 131 | Time |
| 132 | CcnxConsumer::GetRetxTimer () const |
| 133 | { |
| 134 | return m_retxTimer; |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | CcnxConsumer::CheckRetxTimeout () |
| 139 | { |
| 140 | Time now = Simulator::Now (); |
| 141 | |
| 142 | while (!m_seqTimeouts.empty ()) |
| 143 | { |
| 144 | SeqTimeoutsContainer::index<i_timestamp>::type::iterator entry = |
| 145 | m_seqTimeouts.get<i_timestamp> ().begin (); |
| 146 | if (entry->time + m_rto <= now) // timeout expired? |
| 147 | { |
| 148 | m_retxSeqs.insert (entry->seq); |
| 149 | m_seqTimeouts.get<i_timestamp> ().modify (entry, |
| 150 | ll::bind(&SeqTimeout::time, ll::_1) = now); |
| 151 | } |
| 152 | else |
| 153 | break; // nothing else to do. All later packets need not be retransmitted |
| 154 | } |
| 155 | |
| 156 | m_retxEvent = Simulator::Schedule (m_retxTimer, |
| 157 | &CcnxConsumer::CheckRetxTimeout, this); |
| 158 | } |
| 159 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 160 | // Application Methods |
| 161 | void |
| 162 | CcnxConsumer::StartApplication () // Called at time specified by Start |
| 163 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 164 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 166 | // do base stuff |
| 167 | CcnxApp::StartApplication (); |
| 168 | |
| 169 | // schedule periodic packet generation |
| 170 | m_sendEvent = Simulator::Schedule (Seconds(0.0), &CcnxConsumer::SendPacket, this); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void |
| 174 | CcnxConsumer::StopApplication () // Called at time specified by Stop |
| 175 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 176 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 178 | // cancel periodic packet generation |
| 179 | Simulator::Cancel (m_sendEvent); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 180 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 181 | // cleanup base stuff |
| 182 | CcnxApp::StopApplication (); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 185 | void |
| 186 | CcnxConsumer::SendPacket () |
| 187 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 188 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame^] | 189 | |
| 190 | uint32_t seq; |
| 191 | |
| 192 | if (m_retxSeqs.size () != 0) |
| 193 | { |
| 194 | seq = *m_retxSeqs.begin (); |
| 195 | m_retxSeqs.erase (m_retxSeqs.begin ()); |
| 196 | } |
| 197 | else |
| 198 | seq = m_seq++; |
| 199 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 200 | // |
| 201 | Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName); |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame^] | 202 | (*nameWithSequence) (seq); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 203 | // |
| 204 | |
| 205 | CcnxInterestHeader interestHeader; |
| 206 | interestHeader.SetNonce (m_rand.GetValue ()); |
| 207 | interestHeader.SetName (nameWithSequence); |
| 208 | interestHeader.SetInterestLifetime (m_interestLifeTime); |
| 209 | interestHeader.SetChildSelector (m_childSelector); |
Alexander Afanasyev | c39f0b4 | 2011-11-28 12:51:12 -0800 | [diff] [blame] | 210 | if (m_exclude.size ()>0) |
| 211 | { |
| 212 | interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude)); |
| 213 | } |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 214 | interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents); |
| 215 | interestHeader.SetMinSuffixComponents (m_minSuffixComponents); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 216 | |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame^] | 217 | // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader); |
| 218 | NS_LOG_INFO ("> Interest for " << seq); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 219 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 220 | Ptr<Packet> packet = Create<Packet> (); |
| 221 | packet->AddHeader (interestHeader); |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 222 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 223 | m_protocolHandler (packet); |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame^] | 224 | |
| 225 | std::pair<SeqTimeoutsContainer::iterator, bool> |
| 226 | res = m_seqTimeouts.insert (SeqTimeout (seq, Simulator::Now ())); |
| 227 | if (!res.second) |
| 228 | m_seqTimeouts.modify (res.first, |
| 229 | ll::bind(&SeqTimeout::time, ll::_1) = Simulator::Now ()); |
| 230 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 231 | m_sendEvent = Simulator::Schedule (m_offTime, &CcnxConsumer::SendPacket, this); |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame^] | 232 | |
| 233 | // \todo Trace |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 234 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 235 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 236 | void |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 237 | CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject, |
| 238 | const Ptr<const Packet> &payload) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 239 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 240 | NS_LOG_FUNCTION (this << contentObject << payload); |
| 241 | |
Alexander Afanasyev | 781ea81 | 2011-12-15 22:42:09 -0800 | [diff] [blame^] | 242 | // NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject)); |
| 243 | |
| 244 | uint32_t seq = boost::lexical_cast<uint32_t> (contentObject->GetName ().GetComponents ().back ()); |
| 245 | NS_LOG_INFO ("< DATA for " << seq); |
| 246 | |
| 247 | SeqTimeoutsContainer::iterator entry = m_seqTimeouts.find (seq); |
| 248 | |
| 249 | NS_ASSERT_MSG (entry != m_seqTimeouts.end (), |
| 250 | "Comment out this assert, if it causes problems"); |
| 251 | |
| 252 | if (entry != m_seqTimeouts.end ()) |
| 253 | m_seqTimeouts.erase (entry); |
| 254 | |
| 255 | // \todo Trace |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest) |
| 260 | { |
| 261 | NS_LOG_FUNCTION (this << interest); |
| 262 | |
| 263 | // NS_LOG_INFO ("Received NACK: " << boost::cref(*interest)); |
| 264 | uint32_t seq = boost::lexical_cast<uint32_t> (interest->GetName ().GetComponents ().back ()); |
| 265 | NS_LOG_INFO ("< NACK for " << seq); |
| 266 | |
| 267 | // put in the queue of interests to be retransmitted |
| 268 | m_retxSeqs.insert (seq); |
| 269 | |
| 270 | // \todo Trace? |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 271 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 272 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 273 | } // namespace ns3 |