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" |
| 23 | #include "ns3/ccnx-local-face.h" |
| 24 | #include "ns3/ccnx.h" |
| 25 | #include "ns3/callback.h" |
| 26 | #include "ns3/ccnx-content-object-header.h" |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 27 | |
| 28 | NS_LOG_COMPONENT_DEFINE ("CcnxConsumer"); |
| 29 | |
| 30 | namespace ns3 |
| 31 | { |
| 32 | |
| 33 | NS_OBJECT_ENSURE_REGISTERED (CcnxConsumer); |
| 34 | |
| 35 | TypeId |
| 36 | CcnxConsumer::GetTypeId (void) |
| 37 | { |
| 38 | static TypeId tid = TypeId ("ns3::CcnxConsumer") |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 39 | .SetParent<Application> () |
| 40 | .AddConstructor<CcnxConsumer> () |
| 41 | .AddAttribute ("OffTime", "Time interval between packets", |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame^] | 42 | TimeValue (Seconds (0.1)), |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 43 | MakeTimeAccessor (&CcnxConsumer::m_offTime), |
| 44 | MakeTimeChecker ()) |
| 45 | .AddAttribute ("InterestName","CcnxName of the Interest (use CcnxNameComponents)", |
| 46 | CcnxNameComponentsValue (), |
| 47 | MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName), |
| 48 | MakeCcnxNameComponentsChecker ()) |
| 49 | .AddAttribute ("LifeTime", "LifeTime fo interest packet", |
| 50 | TimeValue (Seconds (0)), |
| 51 | MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime), |
| 52 | MakeTimeChecker ()) |
| 53 | .AddAttribute ("MinSuffixComponents", "MinSuffixComponents", |
| 54 | IntegerValue(-1), |
| 55 | MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents), |
| 56 | MakeIntegerChecker<int32_t>()) |
| 57 | .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents", |
| 58 | IntegerValue(-1), |
| 59 | MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents), |
| 60 | MakeIntegerChecker<int32_t>()) |
| 61 | .AddAttribute ("ChildSelector", "ChildSelector", |
| 62 | BooleanValue(false), |
| 63 | MakeBooleanAccessor(&CcnxConsumer::m_childSelector), |
| 64 | MakeBooleanChecker()) |
| 65 | .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)", |
| 66 | CcnxNameComponentsValue (), |
| 67 | MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude), |
| 68 | MakeCcnxNameComponentsChecker ()) |
| 69 | .AddAttribute ("Initial Nonce", "If 0 then nonce is not used", |
| 70 | UintegerValue(1), |
| 71 | MakeUintegerAccessor(&CcnxConsumer::m_initialNonce), |
| 72 | MakeUintegerChecker<uint32_t>()) |
| 73 | .AddTraceSource ("InterestTrace", "Interests that were sent", |
| 74 | MakeTraceSourceAccessor (&CcnxConsumer::m_interestsTrace)) |
| 75 | .AddTraceSource ("ContentObjectTrace", "ContentObjects that were received", |
| 76 | MakeTraceSourceAccessor (&CcnxConsumer::m_contentObjectsTrace)) |
| 77 | ; |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 78 | |
| 79 | return tid; |
| 80 | } |
| 81 | |
| 82 | CcnxConsumer::CcnxConsumer () |
Ilya Moiseenko | 82b8eea | 2011-11-02 23:08:47 -0700 | [diff] [blame] | 83 | : m_seq (0) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 84 | { |
| 85 | NS_LOG_FUNCTION_NOARGS (); |
| 86 | } |
| 87 | |
| 88 | CcnxConsumer::~CcnxConsumer() |
| 89 | { |
| 90 | NS_LOG_FUNCTION_NOARGS (); |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | CcnxConsumer::DoDispose (void) |
| 95 | { |
| 96 | NS_LOG_FUNCTION_NOARGS (); |
| 97 | |
| 98 | Application::DoDispose (); |
| 99 | } |
| 100 | |
| 101 | // Application Methods |
| 102 | void |
| 103 | CcnxConsumer::StartApplication () // Called at time specified by Start |
| 104 | { |
| 105 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 106 | |
| 107 | NS_ASSERT_MSG (m_face == 0, "Face should not exist"); |
| 108 | m_face = Create<CcnxLocalFace> (); |
| 109 | |
| 110 | // step 1. Set up forwarding from face to application |
| 111 | m_face->SetNode (GetNode ()); |
| 112 | m_face->SetContentObjectHandler (MakeCallback (&CcnxConsumer::OnContentObject, this)); |
| 113 | |
| 114 | // step 2. Set up forwarding to and from ccnx |
| 115 | NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () !=0, |
| 116 | "Ccnx stack should be installed on the node " << GetNode ()); |
| 117 | GetNode ()->GetObject<Ccnx> ()->AddFace (m_face); |
| 118 | |
| 119 | // step 3. Enable face |
| 120 | m_face->SetUp (); |
| 121 | |
| 122 | // Send first packet immediately |
| 123 | m_sendEvent = Simulator::Schedule (Seconds(0.0), &CcnxConsumer::SendPacket, this); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void |
| 127 | CcnxConsumer::StopApplication () // Called at time specified by Stop |
| 128 | { |
| 129 | NS_LOG_FUNCTION_NOARGS (); |
| 130 | |
| 131 | CancelEvents (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 132 | |
| 133 | // step 1. Disable face |
| 134 | m_face->SetDown (); |
| 135 | |
| 136 | // step 2. Remove face from ccnx stack |
| 137 | GetNode ()->GetObject<Ccnx> ()->RemoveFace (m_face); |
| 138 | |
| 139 | // step 3. Disable callbacks |
| 140 | m_face->SetContentObjectHandler (MakeNullCallback<void, |
| 141 | const Ptr<const CcnxContentObjectHeader> &, |
| 142 | const Ptr<const Packet> &> ()); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void |
| 146 | CcnxConsumer::CancelEvents () |
| 147 | { |
| 148 | NS_LOG_FUNCTION_NOARGS (); |
| 149 | |
| 150 | Simulator::Cancel (m_sendEvent); |
| 151 | } |
| 152 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 153 | void |
| 154 | CcnxConsumer::SendPacket () |
| 155 | { |
| 156 | NS_LOG_FUNCTION_NOARGS (); |
| 157 | NS_LOG_INFO ("Sending Interest at " << Simulator::Now ()); |
Ilya Moiseenko | 0605d26 | 2011-10-28 13:00:07 -0700 | [diff] [blame] | 158 | |
| 159 | UniformVariable rand(1, std::numeric_limits<uint32_t>::max ()); |
| 160 | uint32_t randomNonce = rand.GetValue(); |
| 161 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 162 | CcnxInterestHeader interestHeader; |
| 163 | interestHeader.SetNonce(randomNonce); |
Ilya Moiseenko | 0605d26 | 2011-10-28 13:00:07 -0700 | [diff] [blame] | 164 | |
Ilya Moiseenko | 82b8eea | 2011-11-02 23:08:47 -0700 | [diff] [blame] | 165 | Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> (m_interestName); |
| 166 | std::ostringstream os; |
| 167 | os << m_seq++; |
| 168 | (*name) (os.str ()); |
| 169 | |
| 170 | interestHeader.SetName (name); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 171 | interestHeader.SetInterestLifetime(m_interestLifeTime); |
| 172 | interestHeader.SetChildSelector(m_childSelector); |
Alexander Afanasyev | 152cf11 | 2011-08-29 17:58:32 -0700 | [diff] [blame] | 173 | interestHeader.SetExclude(Create<CcnxNameComponents> (m_exclude)); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 174 | interestHeader.SetMaxSuffixComponents(m_maxSuffixComponents); |
| 175 | interestHeader.SetMinSuffixComponents(m_minSuffixComponents); |
| 176 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 177 | NS_LOG_INFO ("Interest: \n" << interestHeader); |
| 178 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 179 | Ptr<Packet> packet = Create<Packet> (); |
| 180 | packet->AddHeader (interestHeader); |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame^] | 181 | |
| 182 | NS_LOG_INFO ("Packet: " << packet); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 183 | m_face->ReceiveFromApplication (packet); |
Ilya Moiseenko | 0605d26 | 2011-10-28 13:00:07 -0700 | [diff] [blame] | 184 | |
| 185 | m_interestsTrace (m_face,packet); |
| 186 | |
Ilya Moiseenko | e4aae55 | 2011-09-29 18:38:43 -0700 | [diff] [blame] | 187 | NS_LOG_INFO("time = " << m_offTime); |
| 188 | m_sendEvent = Simulator::Schedule (m_offTime, &CcnxConsumer::SendPacket, this); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 191 | // void |
| 192 | // CcnxConsumer::OnInterest (const Ptr<const CcnxInterestHeader> &interest) |
| 193 | // { |
| 194 | // } |
| 195 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 196 | void |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 197 | CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject, |
| 198 | const Ptr<const Packet> &payload) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 199 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 200 | // do stuff |
| 201 | NS_LOG_FUNCTION ("Received contentObject " << contentObject ); |
Ilya Moiseenko | 0605d26 | 2011-10-28 13:00:07 -0700 | [diff] [blame] | 202 | NS_LOG_INFO ("Preved!"); |
| 203 | m_contentObjectsTrace (m_face,payload); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 204 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 205 | |
| 206 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 207 | } |