blob: 6eee63c7a3884ce08d0ff78bd5714be309ea8cac [file] [log] [blame]
Mohammad Sabet532990d2016-10-17 20:23:56 +03301/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2016 Regents of the University of California.
4 *
5 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
7 *
8 * 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.
11 *
12 * 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.
15 *
16 * 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 **/
19
20#include "ns3/core-module.h"
21#include "ns3/ndnSIM/helper/ndn-stack-helper.hpp"
22#include "ns3/ndnSIM/apps/ndn-app.hpp"
23#include "ns3/ndnSIM/ndn-cxx/link.hpp"
24#include "ns3/ndnSIM/ndn-cxx/interest.hpp"
25
26NS_LOG_COMPONENT_DEFINE("RequesterApp");
27
28namespace ns3 {
29namespace ndn {
30
31class RequesterApp : public App
32{
33public:
34 static TypeId
35 GetTypeId()
36 {
37 static TypeId tid = TypeId("RequesterApp")
38 .SetParent<App>()
39 .AddConstructor<RequesterApp>()
40 .AddAttribute("Name", "Name of data to request",
41 StringValue("/data/name"),
42 MakeNameAccessor(&RequesterApp::m_name), MakeNameChecker())
43 .AddAttribute("Delegation", "Delegation name to attach to Interest",
44 StringValue("/"),
45 MakeNameAccessor(&RequesterApp::getDelegation, &RequesterApp::setDelegation), MakeNameChecker())
46 ;
47
48 return tid;
49 }
50
51protected:
52 virtual void
53 StartApplication() override
54 {
55 App::StartApplication();
56
57 Simulator::Schedule(Seconds(1.0), &RequesterApp::sendInterest, this);
58 }
59
60 virtual void
61 StopApplication() override
62 {
63 // do cleanup
64 App::StopApplication();
65 m_face->close();
66 }
67
68private:
69 void
70 setDelegation(const Name& delegation)
71 {
72 m_delegation = delegation;
73
74 m_link = ::ndn::Link(Name(m_name).append("/LINK"));
75 m_link.addDelegation(1, m_delegation);
76 ndn::StackHelper::getKeyChain().sign(m_link, ::ndn::security::SigningInfo(::ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
77
78 NS_LOG_DEBUG("Created Link Object "<< m_link);
79 }
80
81 Name
82 getDelegation() const
83 {
84 return m_delegation;
85 }
86
87 void
88 sendInterest()
89 {
90 auto interest = make_shared<Interest>(m_name);
91 interest->setInterestLifetime(time::seconds(1));
92 if (m_delegation.size() > 0) {
93 interest->setLink(m_link.wireEncode());
94 }
95
96 NS_LOG_DEBUG("Sending an Interest for "<< *interest);
97
98 m_transmittedInterests(interest, this, m_face);
99 m_appLink->onReceiveInterest(*interest);
100 }
101
102private:
103 Name m_name;
104 Name m_delegation;
105 ::ndn::Link m_link;
106};
107
108NS_OBJECT_ENSURE_REGISTERED(RequesterApp);
109
110} // namespace ndn
111} // namespace ns3