blob: 44ec2045f3dc52bdceb7e14d10d6ece2b3058871 [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
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070088 // Create and configure ndn::Interest
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -080089 auto interest = make_shared<ndn::Interest>(*m_name);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 UniformVariable rand(0, std::numeric_limits<uint32_t>::max());
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -080091 interest->setNonce(rand.GetValue());
92 interest->setName(*prefix);
93 interest->setInterestLifetime(time::seconds(1));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080094
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 NS_LOG_DEBUG("Sending Interest packet for " << *prefix);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080096
97 // Call trace (for logging purposes)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 m_transmittedInterests(interest, this, m_face);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070099
100 // Forward packet to lower (network) layer
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -0800101 m_face->onReceiveInterest(*interest);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700102
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 Simulator::Schedule(Seconds(1.0), &DumbRequester::SendInterest, this);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800104}
105
106void
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -0800107DumbRequester::OnData(shared_ptr<const ndn::Data> contentObject)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800108{
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -0800109 NS_LOG_DEBUG("Receiving Data packet for " << contentObject->getName());
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800110}
111
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800112} // namespace ns3