blob: ac3950c84fe041a23fa5af2c4a286712f2ee8d1e [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"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070022#include "ns3/ptr.h"
23#include "ns3/ccnx-local-face.h"
24#include "ns3/ccnx.h"
25#include "ns3/callback.h"
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080026#include "ns3/string.h"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070027#include "ns3/ccnx-content-object-header.h"
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070028
29NS_LOG_COMPONENT_DEFINE ("CcnxConsumer");
30
31namespace ns3
32{
33
34NS_OBJECT_ENSURE_REGISTERED (CcnxConsumer);
35
36TypeId
37CcnxConsumer::GetTypeId (void)
38{
39 static TypeId tid = TypeId ("ns3::CcnxConsumer")
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070040 .SetParent<Application> ()
41 .AddConstructor<CcnxConsumer> ()
42 .AddAttribute ("OffTime", "Time interval between packets",
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080043 StringValue ("100ms"),
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070044 MakeTimeAccessor (&CcnxConsumer::m_offTime),
45 MakeTimeChecker ())
46 .AddAttribute ("InterestName","CcnxName of the Interest (use CcnxNameComponents)",
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080047 StringValue ("/"),
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070048 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName),
49 MakeCcnxNameComponentsChecker ())
50 .AddAttribute ("LifeTime", "LifeTime fo interest packet",
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080051 StringValue ("2s"),
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070052 MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime),
53 MakeTimeChecker ())
54 .AddAttribute ("MinSuffixComponents", "MinSuffixComponents",
55 IntegerValue(-1),
56 MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents),
57 MakeIntegerChecker<int32_t>())
58 .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents",
59 IntegerValue(-1),
60 MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents),
61 MakeIntegerChecker<int32_t>())
62 .AddAttribute ("ChildSelector", "ChildSelector",
63 BooleanValue(false),
64 MakeBooleanAccessor(&CcnxConsumer::m_childSelector),
65 MakeBooleanChecker())
66 .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)",
67 CcnxNameComponentsValue (),
68 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude),
69 MakeCcnxNameComponentsChecker ())
70 .AddAttribute ("Initial Nonce", "If 0 then nonce is not used",
71 UintegerValue(1),
72 MakeUintegerAccessor(&CcnxConsumer::m_initialNonce),
73 MakeUintegerChecker<uint32_t>())
74 .AddTraceSource ("InterestTrace", "Interests that were sent",
75 MakeTraceSourceAccessor (&CcnxConsumer::m_interestsTrace))
76 .AddTraceSource ("ContentObjectTrace", "ContentObjects that were received",
77 MakeTraceSourceAccessor (&CcnxConsumer::m_contentObjectsTrace))
78 ;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070079
80 return tid;
81}
82
83CcnxConsumer::CcnxConsumer ()
Ilya Moiseenko82b8eea2011-11-02 23:08:47 -070084 : m_seq (0)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070085{
86 NS_LOG_FUNCTION_NOARGS ();
87}
88
89CcnxConsumer::~CcnxConsumer()
90{
91 NS_LOG_FUNCTION_NOARGS ();
92}
93
94void
95CcnxConsumer::DoDispose (void)
96{
97 NS_LOG_FUNCTION_NOARGS ();
98
99 Application::DoDispose ();
100}
101
102// Application Methods
103void
104CcnxConsumer::StartApplication () // Called at time specified by Start
105{
106 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700107
108 NS_ASSERT_MSG (m_face == 0, "Face should not exist");
109 m_face = Create<CcnxLocalFace> ();
110
111 // step 1. Set up forwarding from face to application
112 m_face->SetNode (GetNode ());
113 m_face->SetContentObjectHandler (MakeCallback (&CcnxConsumer::OnContentObject, this));
114
115 // step 2. Set up forwarding to and from ccnx
116 NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () !=0,
117 "Ccnx stack should be installed on the node " << GetNode ());
118 GetNode ()->GetObject<Ccnx> ()->AddFace (m_face);
119
120 // step 3. Enable face
121 m_face->SetUp ();
122
123 // Send first packet immediately
124 m_sendEvent = Simulator::Schedule (Seconds(0.0), &CcnxConsumer::SendPacket, this);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700125}
126
127void
128CcnxConsumer::StopApplication () // Called at time specified by Stop
129{
130 NS_LOG_FUNCTION_NOARGS ();
131
132 CancelEvents ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700133
134 // step 1. Disable face
135 m_face->SetDown ();
136
137 // step 2. Remove face from ccnx stack
138 GetNode ()->GetObject<Ccnx> ()->RemoveFace (m_face);
139
140 // step 3. Disable callbacks
141 m_face->SetContentObjectHandler (MakeNullCallback<void,
142 const Ptr<const CcnxContentObjectHeader> &,
143 const Ptr<const Packet> &> ());
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700144}
145
146void
147CcnxConsumer::CancelEvents ()
148{
149 NS_LOG_FUNCTION_NOARGS ();
150
151 Simulator::Cancel (m_sendEvent);
152}
153
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700154void
155CcnxConsumer::SendPacket ()
156{
157 NS_LOG_FUNCTION_NOARGS ();
158 NS_LOG_INFO ("Sending Interest at " << Simulator::Now ());
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700159
160 UniformVariable rand(1, std::numeric_limits<uint32_t>::max ());
161 uint32_t randomNonce = rand.GetValue();
162
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700163 CcnxInterestHeader interestHeader;
164 interestHeader.SetNonce(randomNonce);
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700165
Ilya Moiseenko82b8eea2011-11-02 23:08:47 -0700166 Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> (m_interestName);
167 std::ostringstream os;
168 os << m_seq++;
169 (*name) (os.str ());
170
171 interestHeader.SetName (name);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700172 interestHeader.SetInterestLifetime(m_interestLifeTime);
173 interestHeader.SetChildSelector(m_childSelector);
Alexander Afanasyev152cf112011-08-29 17:58:32 -0700174 interestHeader.SetExclude(Create<CcnxNameComponents> (m_exclude));
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700175 interestHeader.SetMaxSuffixComponents(m_maxSuffixComponents);
176 interestHeader.SetMinSuffixComponents(m_minSuffixComponents);
177
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700178 NS_LOG_INFO ("Interest: \n" << interestHeader);
179
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700180 Ptr<Packet> packet = Create<Packet> ();
181 packet->AddHeader (interestHeader);
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800182
183 NS_LOG_INFO ("Packet: " << packet);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700184 m_face->ReceiveFromApplication (packet);
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700185
186 m_interestsTrace (m_face,packet);
187
Ilya Moiseenkoe4aae552011-09-29 18:38:43 -0700188 NS_LOG_INFO("time = " << m_offTime);
189 m_sendEvent = Simulator::Schedule (m_offTime, &CcnxConsumer::SendPacket, this);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700190}
191
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700192// void
193// CcnxConsumer::OnInterest (const Ptr<const CcnxInterestHeader> &interest)
194// {
195// }
196
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700197void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700198CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
199 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700200{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700201 // do stuff
202 NS_LOG_FUNCTION ("Received contentObject " << contentObject );
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700203 NS_LOG_INFO ("Preved!");
204 m_contentObjectsTrace (m_face,payload);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700205}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700206
207
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700208}