blob: b25d11c00796a3a7965578bfe4ff0b7c63e416a1 [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",
42 TimeValue (Seconds (0.1)),
43 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 ()
83{
84 NS_LOG_FUNCTION_NOARGS ();
85}
86
87CcnxConsumer::~CcnxConsumer()
88{
89 NS_LOG_FUNCTION_NOARGS ();
90}
91
92void
93CcnxConsumer::DoDispose (void)
94{
95 NS_LOG_FUNCTION_NOARGS ();
96
97 Application::DoDispose ();
98}
99
100// Application Methods
101void
102CcnxConsumer::StartApplication () // Called at time specified by Start
103{
104 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700105
106 NS_ASSERT_MSG (m_face == 0, "Face should not exist");
107 m_face = Create<CcnxLocalFace> ();
108
109 // step 1. Set up forwarding from face to application
110 m_face->SetNode (GetNode ());
111 m_face->SetContentObjectHandler (MakeCallback (&CcnxConsumer::OnContentObject, this));
112
113 // step 2. Set up forwarding to and from ccnx
114 NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () !=0,
115 "Ccnx stack should be installed on the node " << GetNode ());
116 GetNode ()->GetObject<Ccnx> ()->AddFace (m_face);
117
118 // step 3. Enable face
119 m_face->SetUp ();
120
121 // Send first packet immediately
122 m_sendEvent = Simulator::Schedule (Seconds(0.0), &CcnxConsumer::SendPacket, this);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700123}
124
125void
126CcnxConsumer::StopApplication () // Called at time specified by Stop
127{
128 NS_LOG_FUNCTION_NOARGS ();
129
130 CancelEvents ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700131
132 // step 1. Disable face
133 m_face->SetDown ();
134
135 // step 2. Remove face from ccnx stack
136 GetNode ()->GetObject<Ccnx> ()->RemoveFace (m_face);
137
138 // step 3. Disable callbacks
139 m_face->SetContentObjectHandler (MakeNullCallback<void,
140 const Ptr<const CcnxContentObjectHeader> &,
141 const Ptr<const Packet> &> ());
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700142}
143
144void
145CcnxConsumer::CancelEvents ()
146{
147 NS_LOG_FUNCTION_NOARGS ();
148
149 Simulator::Cancel (m_sendEvent);
150}
151
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700152void
153CcnxConsumer::SendPacket ()
154{
155 NS_LOG_FUNCTION_NOARGS ();
156 NS_LOG_INFO ("Sending Interest at " << Simulator::Now ());
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700157
158 UniformVariable rand(1, std::numeric_limits<uint32_t>::max ());
159 uint32_t randomNonce = rand.GetValue();
160
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700161 CcnxInterestHeader interestHeader;
162 interestHeader.SetNonce(randomNonce);
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700163
Alexander Afanasyev152cf112011-08-29 17:58:32 -0700164 interestHeader.SetName(Create<CcnxNameComponents> (m_interestName));
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700165 interestHeader.SetInterestLifetime(m_interestLifeTime);
166 interestHeader.SetChildSelector(m_childSelector);
Alexander Afanasyev152cf112011-08-29 17:58:32 -0700167 interestHeader.SetExclude(Create<CcnxNameComponents> (m_exclude));
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700168 interestHeader.SetMaxSuffixComponents(m_maxSuffixComponents);
169 interestHeader.SetMinSuffixComponents(m_minSuffixComponents);
170
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700171 NS_LOG_INFO ("Interest: \n" << interestHeader);
172
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700173 Ptr<Packet> packet = Create<Packet> ();
174 packet->AddHeader (interestHeader);
175
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700176 m_face->ReceiveFromApplication (packet);
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700177
178 m_interestsTrace (m_face,packet);
179
Ilya Moiseenkoe4aae552011-09-29 18:38:43 -0700180 NS_LOG_INFO("time = " << m_offTime);
181 m_sendEvent = Simulator::Schedule (m_offTime, &CcnxConsumer::SendPacket, this);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700182}
183
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700184// void
185// CcnxConsumer::OnInterest (const Ptr<const CcnxInterestHeader> &interest)
186// {
187// }
188
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700189void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700190CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
191 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700192{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700193 // do stuff
194 NS_LOG_FUNCTION ("Received contentObject " << contentObject );
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700195 NS_LOG_INFO ("Preved!");
196 m_contentObjectsTrace (m_face,payload);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700197}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700198
199
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700200}