blob: 0e6ec9e3b1ae4e615c187f77e4642f23a2f59ccc [file] [log] [blame]
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -07001/* -*- 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"
22
23NS_LOG_COMPONENT_DEFINE ("CcnxConsumer");
24
25namespace ns3
26{
27
28NS_OBJECT_ENSURE_REGISTERED (CcnxConsumer);
29
30TypeId
31CcnxConsumer::GetTypeId (void)
32{
33 static TypeId tid = TypeId ("ns3::CcnxConsumer")
34 .SetParent<Application> ()
35 .AddConstructor<CcnxConsumer> ()
36 .AddAttribute ("OffTime", "Time interval between packets",
37 TimeValue (Seconds (0.1)),
38 MakeTimeAccessor (&CcnxConsumer::m_offTime),
39 MakeTimeChecker ())
40 .AddAttribute ("Face","Local face to be used",
41 PointerValue (CreateObject<CcnxLocalFace> ()),
42 MakePointerAccessor (&CcnxConsumer::m_face),
43 MakePointerChecker<CcnxLocalFace> ())
44 .AddAttribute ("InterestName","CcnxName of the Interest (use CcnxNameComponents)",
Alexander Afanasyev152cf112011-08-29 17:58:32 -070045 CcnxNameComponentsValue (CcnxNameComponents ()),
46 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName),
47 MakeCcnxNameComponentsChecker ())
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070048 .AddAttribute ("LifeTime", "LifeTime fo interest packet",
49 TimeValue (Seconds (4.0)),
50 MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime),
51 MakeTimeChecker ())
52 .AddAttribute ("MinSuffixComponents", "MinSuffixComponents",
53 IntegerValue(-1),
54 MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents),
55 MakeIntegerChecker<int32_t>())
56 .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents",
57 IntegerValue(-1),
58 MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents),
59 MakeIntegerChecker<int32_t>())
60 .AddAttribute ("ChildSelector", "ChildSelector",
61 BooleanValue(false),
62 MakeBooleanAccessor(&CcnxConsumer::m_childSelector),
63 MakeBooleanChecker())
64 .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)",
Alexander Afanasyev152cf112011-08-29 17:58:32 -070065 CcnxNameComponentsValue (CcnxNameComponents ()),
66 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude),
67 MakeCcnxNameComponentsChecker ())
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070068 .AddAttribute ("Initial Nonce", "If 0 then nonce is not used",
69 UintegerValue(1),
70 MakeUintegerAccessor(&CcnxConsumer::m_initialNonce),
71 MakeUintegerChecker<uint32_t>())
72 .AddTraceSource ("InterestTrace", "Interests that were sent",
73 MakeTraceSourceAccessor (&CcnxConsumer::m_interestsTrace))
74 .AddTraceSource ("ContentObjectTrace", "ContentObjects that were received",
75 MakeTraceSourceAccessor (&CcnxConsumer::m_contentObjectsTrace))
76 ;
77
78 return tid;
79}
80
81CcnxConsumer::CcnxConsumer ()
82{
83 NS_LOG_FUNCTION_NOARGS ();
84}
85
86CcnxConsumer::~CcnxConsumer()
87{
88 NS_LOG_FUNCTION_NOARGS ();
89}
90
91void
92CcnxConsumer::DoDispose (void)
93{
94 NS_LOG_FUNCTION_NOARGS ();
95
96 Application::DoDispose ();
97}
98
99// Application Methods
100void
101CcnxConsumer::StartApplication () // Called at time specified by Start
102{
103 NS_LOG_FUNCTION_NOARGS ();
104 ScheduleNextTx();
105}
106
107void
108CcnxConsumer::StopApplication () // Called at time specified by Stop
109{
110 NS_LOG_FUNCTION_NOARGS ();
111
112 CancelEvents ();
113}
114
115void
116CcnxConsumer::CancelEvents ()
117{
118 NS_LOG_FUNCTION_NOARGS ();
119
120 Simulator::Cancel (m_sendEvent);
121}
122
123void
124CcnxConsumer::ScheduleNextTx ()
125{
126 NS_LOG_FUNCTION_NOARGS ();
127
128 Time nextTime = Seconds(m_offTime);
129 m_sendEvent = Simulator::Schedule (nextTime, &CcnxConsumer::SendPacket, this);
130}
131
132void
133CcnxConsumer::SendPacket ()
134{
135 NS_LOG_FUNCTION_NOARGS ();
136 NS_LOG_INFO ("Sending Interest at " << Simulator::Now ());
137
138 uint32_t randomNonce = UniformVariable().GetInteger(1, std::numeric_limits<uint32_t>::max ());
139 CcnxInterestHeader interestHeader;
140 interestHeader.SetNonce(randomNonce);
Alexander Afanasyev152cf112011-08-29 17:58:32 -0700141 interestHeader.SetName(Create<CcnxNameComponents> (m_interestName));
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700142 interestHeader.SetInterestLifetime(m_interestLifeTime);
143 interestHeader.SetChildSelector(m_childSelector);
Alexander Afanasyev152cf112011-08-29 17:58:32 -0700144 interestHeader.SetExclude(Create<CcnxNameComponents> (m_exclude));
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700145 interestHeader.SetMaxSuffixComponents(m_maxSuffixComponents);
146 interestHeader.SetMinSuffixComponents(m_minSuffixComponents);
147
148 Ptr<Packet> packet = Create<Packet> ();
149 packet->AddHeader (interestHeader);
150
151 m_face->Receive(packet);
152
153 ScheduleNextTx();
154}
155
156void
157CcnxConsumer::HandlePacket(const Ptr<CcnxFace> &face, const Ptr<const Packet> &packet)
158{
159 uint8_t type[2];
160 uint32_t read = packet->CopyData (type,2);
161 if (read!=2)
162 {
163 NS_LOG_INFO ("Unknown CcnxPacket");
164 return;
165 }
166
167 if (type[0] == INTEREST_BYTE0 && type[1] == INTEREST_BYTE1)
168 {
169 m_interestsTrace(face,packet);
170 }
171 else if (type[0] == CONTENT_OBJECT_BYTE0 && type[1] == CONTENT_OBJECT_BYTE1)
172 {
173 m_contentObjectsTrace(face,packet);
174 }
175}
176}