blob: b1a059270a1b22d2f682b04ed74326d2ec7d802b [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
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070093 // Create and configure ndn::Interest
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070094 Ptr<ndn::Interest> interest = Create<ndn::Interest> ();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080095 UniformVariable rand (0,std::numeric_limits<uint32_t>::max ());
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070096 interest->SetNonce (rand.GetValue ());
97 interest->SetName (prefix);
98 interest->SetInterestLifetime (Seconds (1.0));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080099
100 NS_LOG_DEBUG ("Sending Interest packet for " << *prefix);
101
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800102
103 // Call trace (for logging purposes)
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700104 m_transmittedInterests (interest, this, m_face);
105
106 // Forward packet to lower (network) layer
107 m_face->ReceiveInterest (interest);
108
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800109 Simulator::Schedule (Seconds (1.0), &DumbRequester::SendInterest, this);
110}
111
112void
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700113DumbRequester::OnContentObject (Ptr<const ndn::ContentObject> contentObject)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800114{
115 NS_LOG_DEBUG ("Receiving ContentObject packet for " << contentObject->GetName ());
116}
117
118
119} // namespace ns3