blob: 3dbcadf6b85737c41b7087c6f359c15ab9835e08 [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
23#include "dumb-requester.h"
24#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
31#include "ns3/ndn-app-face.h"
32#include "ns3/ndn-interest.h"
33#include "ns3/ndn-content-object.h"
34
35NS_LOG_COMPONENT_DEFINE ("DumbRequester");
36
37namespace ns3 {
38
39NS_OBJECT_ENSURE_REGISTERED (DumbRequester);
40
41// register NS-3 type
42TypeId
43DumbRequester::GetTypeId ()
44{
45 static TypeId tid = TypeId ("DumbRequester")
46 .SetParent<ndn::App> ()
47 .AddConstructor<DumbRequester> ()
48
49 .AddAttribute ("Prefix", "Requested name",
50 StringValue ("/dumb-interest"),
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070051 ndn::MakeNameAccessor (&DumbRequester::m_name),
52 ndn::MakeNameChecker ())
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080053 ;
54 return tid;
55}
56
57DumbRequester::DumbRequester ()
58 : m_isRunning (false)
59{
60}
61
62// Processing upon start of the application
63void
64DumbRequester::StartApplication ()
65{
66 // initialize ndn::App
67 ndn::App::StartApplication ();
68
69 m_isRunning = true;
70 Simulator::ScheduleNow (&DumbRequester::SendInterest, this);
71}
72
73// Processing when application is stopped
74void
75DumbRequester::StopApplication ()
76{
77 m_isRunning = false;
78 // cleanup ndn::App
79 ndn::App::StopApplication ();
80}
81
82void
83DumbRequester::SendInterest ()
84{
85 if (!m_isRunning) return;
86
87 /////////////////////////////////////
88 // Sending one Interest packet out //
89 /////////////////////////////////////
90
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070091 Ptr<ndn::Name> prefix = Create<ndn::Name> (m_name); // another way to create name
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080092
93 // Create and configure ndn::InterestHeader
94 ndn::InterestHeader interestHeader;
95 UniformVariable rand (0,std::numeric_limits<uint32_t>::max ());
96 interestHeader.SetNonce (rand.GetValue ());
97 interestHeader.SetName (prefix);
98 interestHeader.SetInterestLifetime (Seconds (1.0));
99
100 // Create packet and add ndn::InterestHeader
101 Ptr<Packet> packet = Create<Packet> ();
102 packet->AddHeader (interestHeader);
103
104 NS_LOG_DEBUG ("Sending Interest packet for " << *prefix);
105
106 // Forward packet to lower (network) layer
107 m_protocolHandler (packet);
108
109 // Call trace (for logging purposes)
110 m_transmittedInterests (&interestHeader, this, m_face);
111 Simulator::Schedule (Seconds (1.0), &DumbRequester::SendInterest, this);
112}
113
114void
115DumbRequester::OnContentObject (const Ptr<const ndn::ContentObjectHeader> &contentObject,
116 Ptr<Packet> payload)
117{
118 NS_LOG_DEBUG ("Receiving ContentObject packet for " << contentObject->GetName ());
119}
120
121
122} // namespace ns3