Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [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 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 21 | #include "ndn-consumer-cbr.h" |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 22 | #include "ns3/ptr.h" |
| 23 | #include "ns3/log.h" |
| 24 | #include "ns3/simulator.h" |
| 25 | #include "ns3/packet.h" |
| 26 | #include "ns3/callback.h" |
| 27 | #include "ns3/string.h" |
| 28 | #include "ns3/boolean.h" |
| 29 | #include "ns3/uinteger.h" |
| 30 | #include "ns3/double.h" |
| 31 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 32 | #include "ns3/ndn-app-face.h" |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 33 | #include "ns3/ndn-interest.h" |
| 34 | #include "ns3/ndn-content-object.h" |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 36 | NS_LOG_COMPONENT_DEFINE ("ndn.ConsumerCbr"); |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 38 | namespace ns3 { |
| 39 | namespace ndn { |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 40 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 41 | NS_OBJECT_ENSURE_REGISTERED (ConsumerCbr); |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 42 | |
| 43 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 44 | ConsumerCbr::GetTypeId (void) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 45 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 46 | static TypeId tid = TypeId ("ns3::ndn::ConsumerCbr") |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 47 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 48 | .SetParent<Consumer> () |
| 49 | .AddConstructor<ConsumerCbr> () |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 51 | .AddAttribute ("Frequency", "Frequency of interest packets", |
| 52 | StringValue ("1.0"), |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 53 | MakeDoubleAccessor (&ConsumerCbr::m_frequency), |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 54 | MakeDoubleChecker<double> ()) |
Alexander Afanasyev | f8b0f71 | 2012-12-28 19:25:12 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 56 | .AddAttribute ("Randomize", "Type of send time randomization: none (default), uniform, exponential", |
| 57 | StringValue ("none"), |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 58 | MakeStringAccessor (&ConsumerCbr::SetRandomize, &ConsumerCbr::GetRandomize), |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 59 | MakeStringChecker ()) |
Alexander Afanasyev | f8b0f71 | 2012-12-28 19:25:12 -0800 | [diff] [blame] | 60 | |
| 61 | .AddAttribute ("MaxSeq", |
| 62 | "Maximum sequence number to request", |
| 63 | IntegerValue (std::numeric_limits<uint32_t>::max ()), |
| 64 | MakeIntegerAccessor (&ConsumerCbr::m_seqMax), |
| 65 | MakeIntegerChecker<uint32_t> ()) |
| 66 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 67 | ; |
| 68 | |
| 69 | return tid; |
| 70 | } |
| 71 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 72 | ConsumerCbr::ConsumerCbr () |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 73 | : m_frequency (1.0) |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 74 | , m_firstTime (true) |
| 75 | , m_random (0) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 76 | { |
| 77 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | 18750bb | 2012-02-02 23:11:30 -0800 | [diff] [blame] | 78 | m_seqMax = std::numeric_limits<uint32_t>::max (); |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 81 | ConsumerCbr::~ConsumerCbr () |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 82 | { |
| 83 | if (m_random) |
| 84 | delete m_random; |
| 85 | } |
| 86 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 87 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 88 | ConsumerCbr::ScheduleNextPacket () |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 89 | { |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 90 | // double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate (); |
Alexander Afanasyev | 94cebd0 | 2012-01-16 12:22:34 -0800 | [diff] [blame] | 91 | // std::cout << "next: " << Simulator::Now().ToDouble(Time::S) + mean << "s\n"; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 93 | if (m_firstTime) |
| 94 | { |
| 95 | m_sendEvent = Simulator::Schedule (Seconds (0.0), |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 96 | &Consumer::SendPacket, this); |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 97 | m_firstTime = false; |
| 98 | } |
| 99 | else if (!m_sendEvent.IsRunning ()) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 100 | m_sendEvent = Simulator::Schedule ( |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 101 | (m_random == 0) ? |
| 102 | Seconds(1.0 / m_frequency) |
| 103 | : |
| 104 | Seconds(m_random->GetValue ()), |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 105 | &Consumer::SendPacket, this); |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 108 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 109 | ConsumerCbr::SetRandomize (const std::string &value) |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 110 | { |
| 111 | if (m_random) |
| 112 | delete m_random; |
| 113 | |
Alexander Afanasyev | 89e688a | 2013-05-04 15:17:49 -0700 | [diff] [blame] | 114 | if (value == "uniform") |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 115 | { |
| 116 | m_random = new UniformVariable (0.0, 2 * 1.0 / m_frequency); |
| 117 | } |
| 118 | else if (value == "exponential") |
| 119 | { |
| 120 | m_random = new ExponentialVariable (1.0 / m_frequency, 50 * 1.0 / m_frequency); |
| 121 | } |
| 122 | else |
| 123 | m_random = 0; |
| 124 | |
| 125 | m_randomType = value; |
| 126 | } |
| 127 | |
| 128 | std::string |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 129 | ConsumerCbr::GetRandomize () const |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 130 | { |
| 131 | return m_randomType; |
| 132 | } |
| 133 | |
| 134 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 135 | /////////////////////////////////////////////////// |
| 136 | // Process incoming packets // |
| 137 | /////////////////////////////////////////////////// |
| 138 | |
| 139 | // void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 140 | // Consumer::OnContentObject (const Ptr<const ContentObject> &contentObject, |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 141 | // const Ptr<const Packet> &payload) |
| 142 | // { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 143 | // Consumer::OnContentObject (contentObject, payload); // tracing inside |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 144 | // } |
| 145 | |
| 146 | // void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 147 | // Consumer::OnNack (const Ptr<const Interest> &interest) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 148 | // { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 149 | // Consumer::OnNack (interest); // tracing inside |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 150 | // } |
| 151 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 152 | } // namespace ndn |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 153 | } // namespace ns3 |