blob: 81d44b85f81dcf6dc30c7f50e3874e6e68eddf18 [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"
26#include "ns3/ccnx-content-object-header.h"
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070027
28NS_LOG_COMPONENT_DEFINE ("CcnxConsumer");
29
30namespace ns3
31{
32
33NS_OBJECT_ENSURE_REGISTERED (CcnxConsumer);
34
35TypeId
36CcnxConsumer::GetTypeId (void)
37{
38 static TypeId tid = TypeId ("ns3::CcnxConsumer")
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070039 .SetParent<Application> ()
40 .AddConstructor<CcnxConsumer> ()
41 .AddAttribute ("OffTime", "Time interval between packets",
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080042 TimeValue (Seconds (0.1)),
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070043 MakeTimeAccessor (&CcnxConsumer::m_offTime),
44 MakeTimeChecker ())
45 .AddAttribute ("InterestName","CcnxName of the Interest (use CcnxNameComponents)",
46 CcnxNameComponentsValue (),
47 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName),
48 MakeCcnxNameComponentsChecker ())
49 .AddAttribute ("LifeTime", "LifeTime fo interest packet",
50 TimeValue (Seconds (0)),
51 MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime),
52 MakeTimeChecker ())
53 .AddAttribute ("MinSuffixComponents", "MinSuffixComponents",
54 IntegerValue(-1),
55 MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents),
56 MakeIntegerChecker<int32_t>())
57 .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents",
58 IntegerValue(-1),
59 MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents),
60 MakeIntegerChecker<int32_t>())
61 .AddAttribute ("ChildSelector", "ChildSelector",
62 BooleanValue(false),
63 MakeBooleanAccessor(&CcnxConsumer::m_childSelector),
64 MakeBooleanChecker())
65 .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)",
66 CcnxNameComponentsValue (),
67 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude),
68 MakeCcnxNameComponentsChecker ())
69 .AddAttribute ("Initial Nonce", "If 0 then nonce is not used",
70 UintegerValue(1),
71 MakeUintegerAccessor(&CcnxConsumer::m_initialNonce),
72 MakeUintegerChecker<uint32_t>())
73 .AddTraceSource ("InterestTrace", "Interests that were sent",
74 MakeTraceSourceAccessor (&CcnxConsumer::m_interestsTrace))
75 .AddTraceSource ("ContentObjectTrace", "ContentObjects that were received",
76 MakeTraceSourceAccessor (&CcnxConsumer::m_contentObjectsTrace))
77 ;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070078
79 return tid;
80}
81
82CcnxConsumer::CcnxConsumer ()
Ilya Moiseenko82b8eea2011-11-02 23:08:47 -070083 : m_seq (0)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070084{
85 NS_LOG_FUNCTION_NOARGS ();
86}
87
88CcnxConsumer::~CcnxConsumer()
89{
90 NS_LOG_FUNCTION_NOARGS ();
91}
92
93void
94CcnxConsumer::DoDispose (void)
95{
96 NS_LOG_FUNCTION_NOARGS ();
97
98 Application::DoDispose ();
99}
100
101// Application Methods
102void
103CcnxConsumer::StartApplication () // Called at time specified by Start
104{
105 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700106
107 NS_ASSERT_MSG (m_face == 0, "Face should not exist");
108 m_face = Create<CcnxLocalFace> ();
109
110 // step 1. Set up forwarding from face to application
111 m_face->SetNode (GetNode ());
112 m_face->SetContentObjectHandler (MakeCallback (&CcnxConsumer::OnContentObject, this));
113
114 // step 2. Set up forwarding to and from ccnx
115 NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () !=0,
116 "Ccnx stack should be installed on the node " << GetNode ());
117 GetNode ()->GetObject<Ccnx> ()->AddFace (m_face);
118
119 // step 3. Enable face
120 m_face->SetUp ();
121
122 // Send first packet immediately
123 m_sendEvent = Simulator::Schedule (Seconds(0.0), &CcnxConsumer::SendPacket, this);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700124}
125
126void
127CcnxConsumer::StopApplication () // Called at time specified by Stop
128{
129 NS_LOG_FUNCTION_NOARGS ();
130
131 CancelEvents ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700132
133 // step 1. Disable face
134 m_face->SetDown ();
135
136 // step 2. Remove face from ccnx stack
137 GetNode ()->GetObject<Ccnx> ()->RemoveFace (m_face);
138
139 // step 3. Disable callbacks
140 m_face->SetContentObjectHandler (MakeNullCallback<void,
141 const Ptr<const CcnxContentObjectHeader> &,
142 const Ptr<const Packet> &> ());
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700143}
144
145void
146CcnxConsumer::CancelEvents ()
147{
148 NS_LOG_FUNCTION_NOARGS ();
149
150 Simulator::Cancel (m_sendEvent);
151}
152
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700153void
154CcnxConsumer::SendPacket ()
155{
156 NS_LOG_FUNCTION_NOARGS ();
157 NS_LOG_INFO ("Sending Interest at " << Simulator::Now ());
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700158
159 UniformVariable rand(1, std::numeric_limits<uint32_t>::max ());
160 uint32_t randomNonce = rand.GetValue();
161
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700162 CcnxInterestHeader interestHeader;
163 interestHeader.SetNonce(randomNonce);
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700164
Ilya Moiseenko82b8eea2011-11-02 23:08:47 -0700165 Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> (m_interestName);
166 std::ostringstream os;
167 os << m_seq++;
168 (*name) (os.str ());
169
170 interestHeader.SetName (name);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700171 interestHeader.SetInterestLifetime(m_interestLifeTime);
172 interestHeader.SetChildSelector(m_childSelector);
Alexander Afanasyev152cf112011-08-29 17:58:32 -0700173 interestHeader.SetExclude(Create<CcnxNameComponents> (m_exclude));
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700174 interestHeader.SetMaxSuffixComponents(m_maxSuffixComponents);
175 interestHeader.SetMinSuffixComponents(m_minSuffixComponents);
176
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700177 NS_LOG_INFO ("Interest: \n" << interestHeader);
178
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700179 Ptr<Packet> packet = Create<Packet> ();
180 packet->AddHeader (interestHeader);
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800181
182 NS_LOG_INFO ("Packet: " << packet);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700183 m_face->ReceiveFromApplication (packet);
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700184
185 m_interestsTrace (m_face,packet);
186
Ilya Moiseenkoe4aae552011-09-29 18:38:43 -0700187 NS_LOG_INFO("time = " << m_offTime);
188 m_sendEvent = Simulator::Schedule (m_offTime, &CcnxConsumer::SendPacket, this);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700189}
190
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700191// void
192// CcnxConsumer::OnInterest (const Ptr<const CcnxInterestHeader> &interest)
193// {
194// }
195
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700196void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700197CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
198 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700199{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700200 // do stuff
201 NS_LOG_FUNCTION ("Received contentObject " << contentObject );
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700202 NS_LOG_INFO ("Preved!");
203 m_contentObjectsTrace (m_face,payload);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700204}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700205
206
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700207}