blob: 8850e18e72f55373b68f0354427f80745870cfda [file] [log] [blame]
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011-2012 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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21// dumb-requester.cc
22
Alexander Afanasyev0c395372014-12-20 15:54:02 -080023#include "dumb-requester.hpp"
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080024#include "ns3/ptr.h"
25#include "ns3/log.h"
26#include "ns3/simulator.h"
27#include "ns3/packet.h"
28#include "ns3/random-variable.h"
29#include "ns3/string.h"
30
Alexander Afanasyev0c395372014-12-20 15:54:02 -080031#include "ns3/ndn-app-face.hpp"
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080032
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080033NS_LOG_COMPONENT_DEFINE("DumbRequester");
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080034
35namespace ns3 {
36
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037NS_OBJECT_ENSURE_REGISTERED(DumbRequester);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080038
39// register NS-3 type
40TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041DumbRequester::GetTypeId()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080042{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 static TypeId tid =
44 TypeId("DumbRequester")
45 .SetParent<ndn::App>()
46 .AddConstructor<DumbRequester>()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080047
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048 .AddAttribute("Prefix", "Requested name", StringValue("/dumb-interest"),
49 ndn::MakeNameAccessor(&DumbRequester::m_name), ndn::MakeNameChecker());
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080050 return tid;
51}
52
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053DumbRequester::DumbRequester()
54 : m_isRunning(false)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080055{
56}
57
58// Processing upon start of the application
59void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060DumbRequester::StartApplication()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080061{
62 // initialize ndn::App
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063 ndn::App::StartApplication();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080064
65 m_isRunning = true;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 Simulator::ScheduleNow(&DumbRequester::SendInterest, this);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080067}
68
69// Processing when application is stopped
70void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071DumbRequester::StopApplication()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080072{
73 m_isRunning = false;
74 // cleanup ndn::App
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 ndn::App::StopApplication();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080076}
77
78void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079DumbRequester::SendInterest()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080080{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 if (!m_isRunning)
82 return;
83
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080084 /////////////////////////////////////
85 // Sending one Interest packet out //
86 /////////////////////////////////////
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087
88 Ptr<ndn::Name> prefix = Create<ndn::Name>(m_name); // another way to create name
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080089
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070090 // Create and configure ndn::Interest
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 Ptr<ndn::Interest> interest = Create<ndn::Interest>();
92 UniformVariable rand(0, std::numeric_limits<uint32_t>::max());
93 interest->SetNonce(rand.GetValue());
94 interest->SetName(prefix);
95 interest->SetInterestLifetime(Seconds(1.0));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080096
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 NS_LOG_DEBUG("Sending Interest packet for " << *prefix);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080098
99 // Call trace (for logging purposes)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 m_transmittedInterests(interest, this, m_face);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700101
102 // Forward packet to lower (network) layer
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 m_face->ReceiveInterest(interest);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700104
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 Simulator::Schedule(Seconds(1.0), &DumbRequester::SendInterest, this);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800106}
107
108void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109DumbRequester::OnData(Ptr<const ndn::Data> contentObject)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800110{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 NS_LOG_DEBUG("Receiving Data packet for " << contentObject->GetName());
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800112}
113
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800114} // namespace ns3