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