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> |
| 37 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 38 | NS_LOG_COMPONENT_DEFINE ("CcnxConsumer"); |
| 39 | |
| 40 | namespace ns3 |
| 41 | { |
| 42 | |
| 43 | NS_OBJECT_ENSURE_REGISTERED (CcnxConsumer); |
| 44 | |
| 45 | TypeId |
| 46 | CcnxConsumer::GetTypeId (void) |
| 47 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 48 | static TypeId tid = TypeId ("ns3::CcnxConsumer") |
| 49 | .SetParent<Application> () |
| 50 | .AddConstructor<CcnxConsumer> () |
| 51 | .AddAttribute ("OffTime", "Time interval between packets", |
| 52 | StringValue ("100ms"), |
| 53 | MakeTimeAccessor (&CcnxConsumer::m_offTime), |
| 54 | MakeTimeChecker ()) |
| 55 | .AddAttribute ("InterestName","CcnxName of the Interest (use CcnxNameComponents)", |
| 56 | StringValue ("/"), |
| 57 | MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName), |
| 58 | MakeCcnxNameComponentsChecker ()) |
| 59 | .AddAttribute ("LifeTime", "LifeTime fo interest packet", |
| 60 | StringValue ("2s"), |
| 61 | MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime), |
| 62 | MakeTimeChecker ()) |
| 63 | .AddAttribute ("MinSuffixComponents", "MinSuffixComponents", |
| 64 | IntegerValue(-1), |
| 65 | MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents), |
| 66 | MakeIntegerChecker<int32_t>()) |
| 67 | .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents", |
| 68 | IntegerValue(-1), |
| 69 | MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents), |
| 70 | MakeIntegerChecker<int32_t>()) |
| 71 | .AddAttribute ("ChildSelector", "ChildSelector", |
| 72 | BooleanValue(false), |
| 73 | MakeBooleanAccessor(&CcnxConsumer::m_childSelector), |
| 74 | MakeBooleanChecker()) |
| 75 | .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)", |
| 76 | CcnxNameComponentsValue (), |
| 77 | MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude), |
| 78 | MakeCcnxNameComponentsChecker ()) |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 79 | // .AddAttribute ("Initial Nonce", "If 0 then nonce is not used", |
| 80 | // UintegerValue(1), |
| 81 | // MakeUintegerAccessor(&CcnxConsumer::m_initialNonce), |
| 82 | // MakeUintegerChecker<uint32_t>()) |
| 83 | // .AddTraceSource ("InterestTrace", "Interests that were sent", |
| 84 | // MakeTraceSourceAccessor (&CcnxConsumer::m_interestsTrace)) |
| 85 | // .AddTraceSource ("ContentObjectTrace", "ContentObjects that were received", |
| 86 | // MakeTraceSourceAccessor (&CcnxConsumer::m_contentObjectsTrace)) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 87 | ; |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 88 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 89 | return tid; |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | CcnxConsumer::CcnxConsumer () |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 93 | : m_rand (0, std::numeric_limits<uint32_t>::max ()) |
| 94 | , m_seq (0) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 95 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 96 | NS_LOG_FUNCTION_NOARGS (); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 99 | // Application Methods |
| 100 | void |
| 101 | CcnxConsumer::StartApplication () // Called at time specified by Start |
| 102 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 103 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 105 | // do base stuff |
| 106 | CcnxApp::StartApplication (); |
| 107 | |
| 108 | // schedule periodic packet generation |
| 109 | m_sendEvent = Simulator::Schedule (Seconds(0.0), &CcnxConsumer::SendPacket, this); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
| 113 | CcnxConsumer::StopApplication () // Called at time specified by Stop |
| 114 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 115 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 116 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 117 | // cancel periodic packet generation |
| 118 | Simulator::Cancel (m_sendEvent); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 120 | // cleanup base stuff |
| 121 | CcnxApp::StopApplication (); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 124 | void |
| 125 | CcnxConsumer::SendPacket () |
| 126 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 127 | NS_LOG_FUNCTION_NOARGS (); |
Ilya Moiseenko | 0605d26 | 2011-10-28 13:00:07 -0700 | [diff] [blame] | 128 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 129 | // |
| 130 | Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 131 | (*nameWithSequence) (m_seq++); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 132 | // |
| 133 | |
| 134 | CcnxInterestHeader interestHeader; |
| 135 | interestHeader.SetNonce (m_rand.GetValue ()); |
| 136 | interestHeader.SetName (nameWithSequence); |
| 137 | interestHeader.SetInterestLifetime (m_interestLifeTime); |
| 138 | interestHeader.SetChildSelector (m_childSelector); |
| 139 | interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude)); |
| 140 | interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents); |
| 141 | interestHeader.SetMinSuffixComponents (m_minSuffixComponents); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 142 | |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 143 | NS_LOG_INFO ("Requesting Interest: \n" << interestHeader); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 144 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 145 | Ptr<Packet> packet = Create<Packet> (); |
| 146 | packet->AddHeader (interestHeader); |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 147 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 148 | m_protocolHandler (packet); |
Ilya Moiseenko | 0605d26 | 2011-10-28 13:00:07 -0700 | [diff] [blame] | 149 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 150 | m_sendEvent = Simulator::Schedule (m_offTime, &CcnxConsumer::SendPacket, this); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 151 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 152 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 153 | void |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 154 | CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject, |
| 155 | const Ptr<const Packet> &payload) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 156 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 157 | NS_LOG_FUNCTION (this << contentObject << payload); |
| 158 | |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 159 | NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject)); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 160 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 162 | } // namespace ns3 |