blob: dc54f92225aac7192bf2d7076ba495d335ec6024 [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"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080023#include "ns3/log.h"
24#include "ns3/simulator.h"
25#include "ns3/packet.h"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070026#include "ns3/callback.h"
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080027#include "ns3/string.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080028#include "ns3/boolean.h"
29#include "ns3/uinteger.h"
30
31#include "ns3/ccnx.h"
32#include "ns3/ccnx-local-face.h"
33#include "ns3/ccnx-interest-header.h"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070034#include "ns3/ccnx-content-object-header.h"
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070035
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080036#include <boost/ref.hpp>
37
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070038NS_LOG_COMPONENT_DEFINE ("CcnxConsumer");
39
40namespace ns3
41{
42
43NS_OBJECT_ENSURE_REGISTERED (CcnxConsumer);
44
45TypeId
46CcnxConsumer::GetTypeId (void)
47{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080048 static TypeId tid = TypeId ("ns3::CcnxConsumer")
49 .SetParent<Application> ()
50 .AddConstructor<CcnxConsumer> ()
51 .AddAttribute ("OffTime", "Time interval between packets",
52 StringValue ("100ms"),
53 MakeTimeAccessor (&CcnxConsumer::m_offTime),
54 MakeTimeChecker ())
55 .AddAttribute ("InterestName","CcnxName of the Interest (use CcnxNameComponents)",
56 StringValue ("/"),
57 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_interestName),
58 MakeCcnxNameComponentsChecker ())
59 .AddAttribute ("LifeTime", "LifeTime fo interest packet",
60 StringValue ("2s"),
61 MakeTimeAccessor (&CcnxConsumer::m_interestLifeTime),
62 MakeTimeChecker ())
63 .AddAttribute ("MinSuffixComponents", "MinSuffixComponents",
64 IntegerValue(-1),
65 MakeIntegerAccessor(&CcnxConsumer::m_minSuffixComponents),
66 MakeIntegerChecker<int32_t>())
67 .AddAttribute ("MaxSuffixComponents", "MaxSuffixComponents",
68 IntegerValue(-1),
69 MakeIntegerAccessor(&CcnxConsumer::m_maxSuffixComponents),
70 MakeIntegerChecker<int32_t>())
71 .AddAttribute ("ChildSelector", "ChildSelector",
72 BooleanValue(false),
73 MakeBooleanAccessor(&CcnxConsumer::m_childSelector),
74 MakeBooleanChecker())
75 .AddAttribute ("Exclude", "only simple name matching is supported (use CcnxNameComponents)",
76 CcnxNameComponentsValue (),
77 MakeCcnxNameComponentsAccessor (&CcnxConsumer::m_exclude),
78 MakeCcnxNameComponentsChecker ())
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080079 // .AddAttribute ("Initial Nonce", "If 0 then nonce is not used",
80 // UintegerValue(1),
81 // MakeUintegerAccessor(&CcnxConsumer::m_initialNonce),
82 // MakeUintegerChecker<uint32_t>())
83 // .AddTraceSource ("InterestTrace", "Interests that were sent",
84 // MakeTraceSourceAccessor (&CcnxConsumer::m_interestsTrace))
85 // .AddTraceSource ("ContentObjectTrace", "ContentObjects that were received",
86 // MakeTraceSourceAccessor (&CcnxConsumer::m_contentObjectsTrace))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080087 ;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070088
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080089 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070090}
91
92CcnxConsumer::CcnxConsumer ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080093 : m_rand (0, std::numeric_limits<uint32_t>::max ())
94 , m_seq (0)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070095{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080096 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070097}
98
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070099// Application Methods
100void
101CcnxConsumer::StartApplication () // Called at time specified by Start
102{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800103 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700104
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105 // do base stuff
106 CcnxApp::StartApplication ();
107
108 // schedule periodic packet generation
109 m_sendEvent = Simulator::Schedule (Seconds(0.0), &CcnxConsumer::SendPacket, this);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700110}
111
112void
113CcnxConsumer::StopApplication () // Called at time specified by Stop
114{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800115 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700116
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800117 // cancel periodic packet generation
118 Simulator::Cancel (m_sendEvent);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700119
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800120 // cleanup base stuff
121 CcnxApp::StopApplication ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700122}
123
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700124void
125CcnxConsumer::SendPacket ()
126{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800127 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700128
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800129 //
130 Ptr<CcnxNameComponents> nameWithSequence = Create<CcnxNameComponents> (m_interestName);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800131 (*nameWithSequence) (m_seq++);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800132 //
133
134 CcnxInterestHeader interestHeader;
135 interestHeader.SetNonce (m_rand.GetValue ());
136 interestHeader.SetName (nameWithSequence);
137 interestHeader.SetInterestLifetime (m_interestLifeTime);
138 interestHeader.SetChildSelector (m_childSelector);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800139 if (m_exclude.size ()>0)
140 {
141 interestHeader.SetExclude (Create<CcnxNameComponents> (m_exclude));
142 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800143 interestHeader.SetMaxSuffixComponents (m_maxSuffixComponents);
144 interestHeader.SetMinSuffixComponents (m_minSuffixComponents);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700145
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800146 NS_LOG_INFO ("Requesting Interest: \n" << interestHeader);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700147
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148 Ptr<Packet> packet = Create<Packet> ();
149 packet->AddHeader (interestHeader);
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800150
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800151 m_protocolHandler (packet);
Ilya Moiseenko0605d262011-10-28 13:00:07 -0700152
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800153 m_sendEvent = Simulator::Schedule (m_offTime, &CcnxConsumer::SendPacket, this);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700154}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700155
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700156void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700157CcnxConsumer::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
158 const Ptr<const Packet> &payload)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700159{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800160 NS_LOG_FUNCTION (this << contentObject << payload);
161
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800162 NS_LOG_INFO ("Received content object: " << boost::cref(*contentObject));
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700163}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700164
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800165} // namespace ns3