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", |
| 42 | TimeValue (Seconds (0.1)), |
| 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 () |
| 83 | { |
| 84 | NS_LOG_FUNCTION_NOARGS (); |
| 85 | } |
| 86 | |
| 87 | CcnxConsumer::~CcnxConsumer() |
| 88 | { |
| 89 | NS_LOG_FUNCTION_NOARGS (); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | CcnxConsumer::DoDispose (void) |
| 94 | { |
| 95 | NS_LOG_FUNCTION_NOARGS (); |
| 96 | |
| 97 | Application::DoDispose (); |
| 98 | } |
| 99 | |
| 100 | // Application Methods |
| 101 | void |
| 102 | CcnxConsumer::StartApplication () // Called at time specified by Start |
| 103 | { |
| 104 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 105 | |
| 106 | NS_ASSERT_MSG (m_face == 0, "Face should not exist"); |
| 107 | m_face = Create<CcnxLocalFace> (); |
| 108 | |
| 109 | // step 1. Set up forwarding from face to application |
| 110 | m_face->SetNode (GetNode ()); |
| 111 | m_face->SetContentObjectHandler (MakeCallback (&CcnxConsumer::OnContentObject, this)); |
| 112 | |
| 113 | // step 2. Set up forwarding to and from ccnx |
| 114 | NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () !=0, |
| 115 | "Ccnx stack should be installed on the node " << GetNode ()); |
| 116 | GetNode ()->GetObject<Ccnx> ()->AddFace (m_face); |
| 117 | |
| 118 | // step 3. Enable face |
| 119 | m_face->SetUp (); |
| 120 | |
| 121 | // Send first packet immediately |
| 122 | m_sendEvent = Simulator::Schedule (Seconds(0.0), &CcnxConsumer::SendPacket, this); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
| 126 | CcnxConsumer::StopApplication () // Called at time specified by Stop |
| 127 | { |
| 128 | NS_LOG_FUNCTION_NOARGS (); |
| 129 | |
| 130 | CancelEvents (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 131 | |
| 132 | // step 1. Disable face |
| 133 | m_face->SetDown (); |
| 134 | |
| 135 | // step 2. Remove face from ccnx stack |
| 136 | GetNode ()->GetObject<Ccnx> ()->RemoveFace (m_face); |
| 137 | |
| 138 | // step 3. Disable callbacks |
| 139 | m_face->SetContentObjectHandler (MakeNullCallback<void, |
| 140 | const Ptr<const CcnxContentObjectHeader> &, |
| 141 | const Ptr<const Packet> &> ()); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void |
| 145 | CcnxConsumer::CancelEvents () |
| 146 | { |
| 147 | NS_LOG_FUNCTION_NOARGS (); |
| 148 | |
| 149 | Simulator::Cancel (m_sendEvent); |
| 150 | } |
| 151 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 152 | void |
| 153 | CcnxConsumer::SendPacket () |
| 154 | { |
| 155 | NS_LOG_FUNCTION_NOARGS (); |
| 156 | NS_LOG_INFO ("Sending Interest at " << Simulator::Now ()); |
| 157 | |
| 158 | uint32_t randomNonce = UniformVariable().GetInteger(1, std::numeric_limits<uint32_t>::max ()); |
| 159 | CcnxInterestHeader interestHeader; |
| 160 | interestHeader.SetNonce(randomNonce); |
Alexander Afanasyev | 152cf11 | 2011-08-29 17:58:32 -0700 | [diff] [blame] | 161 | interestHeader.SetName(Create<CcnxNameComponents> (m_interestName)); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 162 | interestHeader.SetInterestLifetime(m_interestLifeTime); |
| 163 | interestHeader.SetChildSelector(m_childSelector); |
Alexander Afanasyev | 152cf11 | 2011-08-29 17:58:32 -0700 | [diff] [blame] | 164 | interestHeader.SetExclude(Create<CcnxNameComponents> (m_exclude)); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 165 | interestHeader.SetMaxSuffixComponents(m_maxSuffixComponents); |
| 166 | interestHeader.SetMinSuffixComponents(m_minSuffixComponents); |
| 167 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 168 | NS_LOG_INFO ("Interest: \n" << interestHeader); |
| 169 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 170 | Ptr<Packet> packet = Create<Packet> (); |
| 171 | packet->AddHeader (interestHeader); |
| 172 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 173 | m_face->ReceiveFromApplication (packet); |
Ilya Moiseenko | e4aae55 | 2011-09-29 18:38:43 -0700 | [diff] [blame^] | 174 | NS_LOG_INFO("time = " << m_offTime); |
| 175 | m_sendEvent = Simulator::Schedule (m_offTime, &CcnxConsumer::SendPacket, this); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 178 | // void |
| 179 | // CcnxConsumer::OnInterest (const Ptr<const CcnxInterestHeader> &interest) |
| 180 | // { |
| 181 | // } |
| 182 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 183 | void |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 184 | CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject, |
| 185 | const Ptr<const Packet> &payload) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 186 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 187 | // do stuff |
| 188 | NS_LOG_FUNCTION ("Received contentObject " << contentObject ); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 189 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 190 | |
| 191 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 192 | } |