blob: abf4bc2113a5d28a58e67100183a68dc38959e0e [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
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"
33#include "../model/ccnx-local-face.h"
34#include "ns3/ccnx-interest-header.h"
35#include "ns3/ccnx-content-object-header.h"
36
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080037NS_LOG_COMPONENT_DEFINE ("CcnxConsumerCbr");
38
39namespace ns3
40{
41
42NS_OBJECT_ENSURE_REGISTERED (CcnxConsumerCbr);
43
44TypeId
45CcnxConsumerCbr::GetTypeId (void)
46{
47 static TypeId tid = TypeId ("ns3::CcnxConsumerCbr")
48 .SetParent<CcnxConsumer> ()
49 .AddConstructor<CcnxConsumerCbr> ()
50
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080051 .AddAttribute ("Frequency", "Frequency of interest packets",
52 StringValue ("1.0"),
53 MakeDoubleAccessor (&CcnxConsumerCbr::m_frequency),
54 MakeDoubleChecker<double> ())
Alexander Afanasyev59e67712012-02-07 11:49:34 -080055 .AddAttribute ("Randomize", "Type of send time randomization: none (default), uniform, exponential",
56 StringValue ("none"),
57 MakeStringAccessor (&CcnxConsumerCbr::SetRandomize, &CcnxConsumerCbr::GetRandomize),
58 MakeStringChecker ())
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080059 ;
60
61 return tid;
62}
63
64CcnxConsumerCbr::CcnxConsumerCbr ()
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080065 : m_frequency (1.0)
Alexander Afanasyev59e67712012-02-07 11:49:34 -080066 , m_firstTime (true)
67 , m_random (0)
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080068{
69 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev18750bb2012-02-02 23:11:30 -080070 m_seqMax = std::numeric_limits<uint32_t>::max ();
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080071}
72
Alexander Afanasyev59e67712012-02-07 11:49:34 -080073CcnxConsumerCbr::~CcnxConsumerCbr ()
74{
75 if (m_random)
76 delete m_random;
77}
78
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080079void
80CcnxConsumerCbr::ScheduleNextPacket ()
81{
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080082 // double mean = 8.0 * m_payloadSize / m_desiredRate.GetBitRate ();
Alexander Afanasyev94cebd02012-01-16 12:22:34 -080083 // std::cout << "next: " << Simulator::Now().ToDouble(Time::S) + mean << "s\n";
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080084
Alexander Afanasyev59e67712012-02-07 11:49:34 -080085 if (m_firstTime)
86 {
87 m_sendEvent = Simulator::Schedule (Seconds (0.0),
88 &CcnxConsumer::SendPacket, this);
89 m_firstTime = false;
90 }
91 else if (!m_sendEvent.IsRunning ())
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080092 m_sendEvent = Simulator::Schedule (
Alexander Afanasyev59e67712012-02-07 11:49:34 -080093 (m_random == 0) ?
94 Seconds(1.0 / m_frequency)
95 :
96 Seconds(m_random->GetValue ()),
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080097 &CcnxConsumer::SendPacket, this);
98}
99
Alexander Afanasyev59e67712012-02-07 11:49:34 -0800100void
101CcnxConsumerCbr::SetRandomize (const std::string &value)
102{
103 if (m_random)
104 delete m_random;
105
106 else if (value == "uniform")
107 {
108 m_random = new UniformVariable (0.0, 2 * 1.0 / m_frequency);
109 }
110 else if (value == "exponential")
111 {
112 m_random = new ExponentialVariable (1.0 / m_frequency, 50 * 1.0 / m_frequency);
113 }
114 else
115 m_random = 0;
116
117 m_randomType = value;
118}
119
120std::string
121CcnxConsumerCbr::GetRandomize () const
122{
123 return m_randomType;
124}
125
126
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800127///////////////////////////////////////////////////
128// Process incoming packets //
129///////////////////////////////////////////////////
130
131// void
132// CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
133// const Ptr<const Packet> &payload)
134// {
135// CcnxConsumer::OnContentObject (contentObject, payload); // tracing inside
136// }
137
138// void
139// CcnxConsumer::OnNack (const Ptr<const CcnxInterestHeader> &interest)
140// {
141// CcnxConsumer::OnNack (interest); // tracing inside
142// }
143
144} // namespace ns3