blob: ef52ef53120a9b2d6432eeb4cfd1b8425226f875 [file] [log] [blame]
Alexander Afanasyev029d38d2012-01-09 13:50:50 -08001/* -*- 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 Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ndn-consumer-cbr.h"
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080022#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 Afanasyev4aac5572012-08-09 10:49:55 -070032#include "ns3/ndn.h"
33#include "ns3/ndn-app-face.h"
34#include "ns3/ndn-interest-header.h"
35#include "ns3/ndn-content-object-header.h"
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080036
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070037NS_LOG_COMPONENT_DEFINE ("NdnConsumerCbr");
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080038
39namespace ns3
40{
41
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070042NS_OBJECT_ENSURE_REGISTERED (NdnConsumerCbr);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080043
44TypeId
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070045NdnConsumerCbr::GetTypeId (void)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080046{
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070047 static TypeId tid = TypeId ("ns3::NdnConsumerCbr")
48 .SetGroupName ("Ndn")
49 .SetParent<NdnConsumer> ()
50 .AddConstructor<NdnConsumerCbr> ()
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080051
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080052 .AddAttribute ("Frequency", "Frequency of interest packets",
53 StringValue ("1.0"),
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070054 MakeDoubleAccessor (&NdnConsumerCbr::m_frequency),
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080055 MakeDoubleChecker<double> ())
Alexander Afanasyev59e67712012-02-07 11:49:34 -080056 .AddAttribute ("Randomize", "Type of send time randomization: none (default), uniform, exponential",
57 StringValue ("none"),
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070058 MakeStringAccessor (&NdnConsumerCbr::SetRandomize, &NdnConsumerCbr::GetRandomize),
Alexander Afanasyev59e67712012-02-07 11:49:34 -080059 MakeStringChecker ())
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080060 ;
61
62 return tid;
63}
64
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065NdnConsumerCbr::NdnConsumerCbr ()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080066 : m_frequency (1.0)
Alexander Afanasyev59e67712012-02-07 11:49:34 -080067 , m_firstTime (true)
68 , m_random (0)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080069{
70 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev18750bb2012-02-02 23:11:30 -080071 m_seqMax = std::numeric_limits<uint32_t>::max ();
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080072}
73
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070074NdnConsumerCbr::~NdnConsumerCbr ()
Alexander Afanasyev59e67712012-02-07 11:49:34 -080075{
76 if (m_random)
77 delete m_random;
78}
79
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080080void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070081NdnConsumerCbr::ScheduleNextPacket ()
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080082{
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080083 // double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
Alexander Afanasyev94cebd02012-01-16 12:22:34 -080084 // std::cout << "next: " << Simulator::Now().ToDouble(Time::S) + mean << "s\n";
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080085
Alexander Afanasyev59e67712012-02-07 11:49:34 -080086 if (m_firstTime)
87 {
88 m_sendEvent = Simulator::Schedule (Seconds (0.0),
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070089 &NdnConsumer::SendPacket, this);
Alexander Afanasyev59e67712012-02-07 11:49:34 -080090 m_firstTime = false;
91 }
92 else if (!m_sendEvent.IsRunning ())
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080093 m_sendEvent = Simulator::Schedule (
Alexander Afanasyev59e67712012-02-07 11:49:34 -080094 (m_random == 0) ?
95 Seconds(1.0 / m_frequency)
96 :
97 Seconds(m_random->GetValue ()),
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070098 &NdnConsumer::SendPacket, this);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080099}
100
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800101void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700102NdnConsumerCbr::SetRandomize (const std::string &value)
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800103{
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
121std::string
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700122NdnConsumerCbr::GetRandomize () const
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800123{
124 return m_randomType;
125}
126
127
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800128///////////////////////////////////////////////////
129// Process incoming packets //
130///////////////////////////////////////////////////
131
132// void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700133// NdnConsumer::OnContentObject (const Ptr<const NdnContentObjectHeader> &contentObject,
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800134// const Ptr<const Packet> &payload)
135// {
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700136// NdnConsumer::OnContentObject (contentObject, payload); // tracing inside
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800137// }
138
139// void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700140// NdnConsumer::OnNack (const Ptr<const NdnInterestHeader> &interest)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800141// {
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700142// NdnConsumer::OnNack (interest); // tracing inside
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800143// }
144
145} // namespace ns3