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 | |
| 21 | #include "ccnx-consumer-cbr.h" |
| 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 | |
| 32 | #include "ns3/ccnx.h" |
Alexander Afanasyev | 4a4ea60 | 2012-06-06 11:12:45 -0700 | [diff] [blame] | 33 | #include "ns3/ccnx-app-face.h" |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 34 | #include "ns3/ccnx-interest-header.h" |
| 35 | #include "ns3/ccnx-content-object-header.h" |
| 36 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 37 | NS_LOG_COMPONENT_DEFINE ("CcnxConsumerCbr"); |
| 38 | |
| 39 | namespace ns3 |
| 40 | { |
| 41 | |
| 42 | NS_OBJECT_ENSURE_REGISTERED (CcnxConsumerCbr); |
| 43 | |
| 44 | TypeId |
| 45 | CcnxConsumerCbr::GetTypeId (void) |
| 46 | { |
| 47 | static TypeId tid = TypeId ("ns3::CcnxConsumerCbr") |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 48 | .SetGroupName ("Ccnx") |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 49 | .SetParent<CcnxConsumer> () |
| 50 | .AddConstructor<CcnxConsumerCbr> () |
| 51 | |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 52 | .AddAttribute ("Frequency", "Frequency of interest packets", |
| 53 | StringValue ("1.0"), |
| 54 | MakeDoubleAccessor (&CcnxConsumerCbr::m_frequency), |
| 55 | MakeDoubleChecker<double> ()) |
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"), |
| 58 | MakeStringAccessor (&CcnxConsumerCbr::SetRandomize, &CcnxConsumerCbr::GetRandomize), |
| 59 | MakeStringChecker ()) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 60 | ; |
| 61 | |
| 62 | return tid; |
| 63 | } |
| 64 | |
| 65 | CcnxConsumerCbr::CcnxConsumerCbr () |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 66 | : m_frequency (1.0) |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 67 | , m_firstTime (true) |
| 68 | , m_random (0) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 69 | { |
| 70 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | 18750bb | 2012-02-02 23:11:30 -0800 | [diff] [blame] | 71 | m_seqMax = std::numeric_limits<uint32_t>::max (); |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 74 | CcnxConsumerCbr::~CcnxConsumerCbr () |
| 75 | { |
| 76 | if (m_random) |
| 77 | delete m_random; |
| 78 | } |
| 79 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 80 | void |
| 81 | CcnxConsumerCbr::ScheduleNextPacket () |
| 82 | { |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 83 | // double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate (); |
Alexander Afanasyev | 94cebd0 | 2012-01-16 12:22:34 -0800 | [diff] [blame] | 84 | // std::cout << "next: " << Simulator::Now().ToDouble(Time::S) + mean << "s\n"; |
Alexander Afanasyev | 359bfb7 | 2012-01-09 18:42:50 -0800 | [diff] [blame] | 85 | |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 86 | if (m_firstTime) |
| 87 | { |
| 88 | m_sendEvent = Simulator::Schedule (Seconds (0.0), |
| 89 | &CcnxConsumer::SendPacket, this); |
| 90 | m_firstTime = false; |
| 91 | } |
| 92 | else if (!m_sendEvent.IsRunning ()) |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 93 | m_sendEvent = Simulator::Schedule ( |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 94 | (m_random == 0) ? |
| 95 | Seconds(1.0 / m_frequency) |
| 96 | : |
| 97 | Seconds(m_random->GetValue ()), |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 98 | &CcnxConsumer::SendPacket, this); |
| 99 | } |
| 100 | |
Alexander Afanasyev | 59e6771 | 2012-02-07 11:49:34 -0800 | [diff] [blame] | 101 | void |
| 102 | CcnxConsumerCbr::SetRandomize (const std::string &value) |
| 103 | { |
| 104 | if (m_random) |
| 105 | delete m_random; |
| 106 | |
| 107 | else if (value == "uniform") |
| 108 | { |
| 109 | m_random = new UniformVariable (0.0, 2 * 1.0 / m_frequency); |
| 110 | } |
| 111 | else if (value == "exponential") |
| 112 | { |
| 113 | m_random = new ExponentialVariable (1.0 / m_frequency, 50 * 1.0 / m_frequency); |
| 114 | } |
| 115 | else |
| 116 | m_random = 0; |
| 117 | |
| 118 | m_randomType = value; |
| 119 | } |
| 120 | |
| 121 | std::string |
| 122 | CcnxConsumerCbr::GetRandomize () const |
| 123 | { |
| 124 | return m_randomType; |
| 125 | } |
| 126 | |
| 127 | |
Alexander Afanasyev | 029d38d | 2012-01-09 13:50:50 -0800 | [diff] [blame] | 128 | /////////////////////////////////////////////////// |
| 129 | // Process incoming packets // |
| 130 | /////////////////////////////////////////////////// |
| 131 | |
| 132 | // void |
| 133 | // CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject, |
| 134 | // const Ptr<const Packet> &payload) |
| 135 | // { |
| 136 | // CcnxConsumer::OnContentObject (contentObject, payload); // tracing inside |
| 137 | // } |
| 138 | |
| 139 | // void |
| 140 | // CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest) |
| 141 | // { |
| 142 | // CcnxConsumer::OnNack (interest); // tracing inside |
| 143 | // } |
| 144 | |
| 145 | } // namespace ns3 |