blob: 35cd5fd7ed3d9bb8cebb47fd2500b8337b9788b8 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080019
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080020// dumb-requester.cpp
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080021
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080022#include "one-interest-requester.hpp"
23
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"
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070028#include "ns3/random-variable-stream.h"
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080029#include "ns3/string.h"
30
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080031NS_LOG_COMPONENT_DEFINE("OneInterestRequester");
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080032
33namespace ns3 {
34
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080035NS_OBJECT_ENSURE_REGISTERED(OneInterestRequester);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080036
37// register NS-3 type
38TypeId
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080039OneInterestRequester::GetTypeId()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080040{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041 static TypeId tid =
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080042 TypeId("OneInterestRequester")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 .SetParent<ndn::App>()
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080044 .AddConstructor<OneInterestRequester>()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080045
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046 .AddAttribute("Prefix", "Requested name", StringValue("/dumb-interest"),
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080047 ndn::MakeNameAccessor(&OneInterestRequester::m_name), ndn::MakeNameChecker());
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080048 return tid;
49}
50
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080051OneInterestRequester::OneInterestRequester()
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052 : m_isRunning(false)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080053{
54}
55
56// Processing upon start of the application
57void
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080058OneInterestRequester::StartApplication()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080059{
60 // initialize ndn::App
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 ndn::App::StartApplication();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080062
63 m_isRunning = true;
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080064 Simulator::ScheduleNow(&OneInterestRequester::SendInterest, this);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080065}
66
67// Processing when application is stopped
68void
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080069OneInterestRequester::StopApplication()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080070{
71 m_isRunning = false;
72 // cleanup ndn::App
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 ndn::App::StopApplication();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080074}
75
76void
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080077OneInterestRequester::SendInterest()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080078{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 if (!m_isRunning)
80 return;
81
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080082 /////////////////////////////////////
83 // Sending one Interest packet out //
84 /////////////////////////////////////
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070086 // Create and configure ndn::Interest
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080087 auto interest = std::make_shared<ndn::Interest>(m_name);
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070088 Ptr<UniformRandomVariable> rand = CreateObject<UniformRandomVariable>();
89 interest->setNonce(rand->GetValue(0, std::numeric_limits<uint32_t>::max()));
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080090 interest->setInterestLifetime(ndn::time::seconds(1));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080091
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080092 NS_LOG_DEBUG("Sending Interest packet for " << m_name);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080093
94 // Call trace (for logging purposes)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 m_transmittedInterests(interest, this, m_face);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070096
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080097 NS_LOG_DEBUG(">> I: " << m_name);
98
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070099 // Forward packet to lower (network) layer
Alexander Afanasyev50ea1a32016-09-08 15:44:08 -0700100 m_appLink->onReceiveInterest(*interest);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800101}
102
103void
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -0800104OneInterestRequester::OnData(std::shared_ptr<const ndn::Data> data)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800105{
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -0800106 NS_LOG_DEBUG("<< D: " << data->getName());
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800107}
108
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800109} // namespace ns3