blob: f92022bd9a228ccd0e88269385a3a0e910e37afb [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 Afanasyev0c395372014-12-20 15:54:02 -080022#include "dumb-requester.hpp"
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080023#include "ns3/ptr.h"
24#include "ns3/log.h"
25#include "ns3/simulator.h"
26#include "ns3/packet.h"
27#include "ns3/random-variable.h"
28#include "ns3/string.h"
29
Alexander Afanasyev0c395372014-12-20 15:54:02 -080030#include "ns3/ndn-app-face.hpp"
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080031
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080032NS_LOG_COMPONENT_DEFINE("DumbRequester");
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080033
34namespace ns3 {
35
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080036NS_OBJECT_ENSURE_REGISTERED(DumbRequester);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080037
38// register NS-3 type
39TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040DumbRequester::GetTypeId()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080041{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042 static TypeId tid =
43 TypeId("DumbRequester")
44 .SetParent<ndn::App>()
45 .AddConstructor<DumbRequester>()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080046
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047 .AddAttribute("Prefix", "Requested name", StringValue("/dumb-interest"),
48 ndn::MakeNameAccessor(&DumbRequester::m_name), ndn::MakeNameChecker());
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080049 return tid;
50}
51
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052DumbRequester::DumbRequester()
53 : m_isRunning(false)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080054{
55}
56
57// Processing upon start of the application
58void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059DumbRequester::StartApplication()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080060{
61 // initialize ndn::App
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062 ndn::App::StartApplication();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080063
64 m_isRunning = true;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 Simulator::ScheduleNow(&DumbRequester::SendInterest, this);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080066}
67
68// Processing when application is stopped
69void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070DumbRequester::StopApplication()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080071{
72 m_isRunning = false;
73 // cleanup ndn::App
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 ndn::App::StopApplication();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080075}
76
77void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078DumbRequester::SendInterest()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080079{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 if (!m_isRunning)
81 return;
82
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080083 /////////////////////////////////////
84 // Sending one Interest packet out //
85 /////////////////////////////////////
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070087 // Create and configure ndn::Interest
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -080088 auto interest = make_shared<ndn::Interest>(*m_name);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 UniformVariable rand(0, std::numeric_limits<uint32_t>::max());
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -080090 interest->setNonce(rand.GetValue());
91 interest->setName(*prefix);
92 interest->setInterestLifetime(time::seconds(1));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080093
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 NS_LOG_DEBUG("Sending Interest packet for " << *prefix);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080095
96 // Call trace (for logging purposes)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 m_transmittedInterests(interest, this, m_face);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070098
99 // Forward packet to lower (network) layer
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -0800100 m_face->onReceiveInterest(*interest);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700101
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 Simulator::Schedule(Seconds(1.0), &DumbRequester::SendInterest, this);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800103}
104
105void
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -0800106DumbRequester::OnData(shared_ptr<const ndn::Data> contentObject)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800107{
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -0800108 NS_LOG_DEBUG("Receiving Data packet for " << contentObject->getName());
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800109}
110
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800111} // namespace ns3