NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
ndn-app.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #include "ndn-app.hpp"
21 #include "ns3/log.h"
22 #include "ns3/assert.h"
23 #include "ns3/packet.h"
24 
27 #include "model/null-transport.hpp"
28 
29 NS_LOG_COMPONENT_DEFINE("ndn.App");
30 
31 namespace ns3 {
32 namespace ndn {
33 
35 
36 TypeId
38 {
39  static TypeId tid = TypeId("ns3::ndn::App")
40  .SetGroupName("Ndn")
41  .SetParent<Application>()
42  .AddConstructor<App>()
43 
44  .AddTraceSource("ReceivedInterests", "ReceivedInterests",
45  MakeTraceSourceAccessor(&App::m_receivedInterests),
46  "ns3::ndn::App::InterestTraceCallback")
47 
48  .AddTraceSource("ReceivedDatas", "ReceivedDatas",
49  MakeTraceSourceAccessor(&App::m_receivedDatas),
50  "ns3::ndn::App::DataTraceCallback")
51 
52  .AddTraceSource("ReceivedNacks", "ReceivedNacks",
53  MakeTraceSourceAccessor(&App::m_receivedNacks),
54  "ns3::ndn::App::NackTraceCallback")
55 
56  .AddTraceSource("TransmittedInterests", "TransmittedInterests",
57  MakeTraceSourceAccessor(&App::m_transmittedInterests),
58  "ns3::ndn::App::InterestTraceCallback")
59 
60  .AddTraceSource("TransmittedDatas", "TransmittedDatas",
61  MakeTraceSourceAccessor(&App::m_transmittedDatas),
62  "ns3::ndn::App::DataTraceCallback")
63 
64  .AddTraceSource("TransmittedNacks", "TransmittedNacks",
65  MakeTraceSourceAccessor(&App::m_transmittedNacks),
66  "ns3::ndn::App::NackTraceCallback");
67  return tid;
68 }
69 
71  : m_active(false)
72  , m_face(0)
73  , m_appId(std::numeric_limits<uint32_t>::max())
74 {
75 }
76 
78 {
79 }
80 
81 void
83 {
84  NS_LOG_FUNCTION_NOARGS();
85 
86  // find out what is application id on the node
87  for (uint32_t id = 0; id < GetNode()->GetNApplications(); ++id) {
88  if (GetNode()->GetApplication(id) == this) {
89  m_appId = id;
90  }
91  }
92 
93  Application::DoInitialize();
94 }
95 
96 void
98 {
99  NS_LOG_FUNCTION_NOARGS();
100 
101  // Unfortunately, this causes SEGFAULT
102  // The best reason I see is that apps are freed after ndn stack is removed
103  // StopApplication ();
104  Application::DoDispose();
105 }
106 
107 uint32_t
108 App::GetId() const
109 {
110  return m_appId;
111 }
112 
113 void
114 App::OnInterest(shared_ptr<const Interest> interest)
115 {
116  NS_LOG_FUNCTION(this << interest);
117  m_receivedInterests(interest, this, m_face);
118 }
119 
120 void
121 App::OnData(shared_ptr<const Data> data)
122 {
123  NS_LOG_FUNCTION(this << data);
124  m_receivedDatas(data, this, m_face);
125 }
126 
127 void
128 App::OnNack(shared_ptr<const lp::Nack> nack)
129 {
130  NS_LOG_FUNCTION(this << nack);
131 
132  // @TODO Implement
133  m_receivedNacks(nack, this, m_face);
134 }
135 
136 // Application Methods
137 void
138 App::StartApplication() // Called at time specified by Start
139 {
140  NS_LOG_FUNCTION_NOARGS();
141 
142  NS_ASSERT(m_active != true);
143  m_active = true;
144 
145  NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0,
146  "Ndn stack should be installed on the node " << GetNode());
147 
148  // step 1. Create a face
149  auto appLink = make_unique<AppLinkService>(this);
150  auto transport = make_unique<NullTransport>("appFace://", "appFace://",
152  // @TODO Consider making AppTransport instead
153  m_face = std::make_shared<Face>(std::move(appLink), std::move(transport));
154  m_appLink = static_cast<AppLinkService*>(m_face->getLinkService());
155  m_face->setMetric(1);
156 
157  // step 2. Add face to the Ndn stack
158  GetNode()->GetObject<L3Protocol>()->addFace(m_face);
159 }
160 
161 void
162 App::StopApplication() // Called at time specified by Stop
163 {
164  NS_LOG_FUNCTION_NOARGS();
165 
166  if (!m_active)
167  return; // don't assert here, just return
168 
169  m_active = false;
170 
171  m_face->close();
172 }
173 
174 } // namespace ndn
175 } // namespace ns3
TracedCallback< shared_ptr< const lp::Nack >, Ptr< App >, shared_ptr< Face > > m_receivedNacks
App-level trace of received Nacks.
Definition: ndn-app.hpp:116
Copyright (c) 2011-2015 Regents of the University of California.
virtual void StopApplication()
Called at time specified by Stop.
Definition: ndn-app.cpp:162
Implementation of LinkService for ndnSIM application.
uint32_t m_appId
Definition: ndn-app.hpp:107
virtual ~App()
Definition: ndn-app.cpp:77
TracedCallback< shared_ptr< const lp::Nack >, Ptr< App >, shared_ptr< Face > > m_transmittedNacks
App-level trace of transmitted Nacks.
Definition: ndn-app.hpp:125
ndn App
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-app.cpp:29
NS_OBJECT_ENSURE_REGISTERED(ContentStore)
virtual void DoInitialize()
Definition: ndn-app.cpp:82
virtual void StartApplication()
Called at time specified by Start.
Definition: ndn-app.cpp:138
STL namespace.
TracedCallback< shared_ptr< const Interest >, Ptr< App >, shared_ptr< Face > > m_receivedInterests
App-level trace of received Interests.
Definition: ndn-app.hpp:110
static TypeId GetTypeId()
Definition: ndn-app.cpp:37
virtual void OnInterest(shared_ptr< const Interest > interest)
Method that will be called every time new Interest arrives.
Definition: ndn-app.cpp:114
virtual void OnNack(shared_ptr< const lp::Nack > nack)
Method that will be called every time new Nack arrives.
Definition: ndn-app.cpp:128
Copyright (c) 2011-2015 Regents of the University of California.
virtual void DoDispose()
Definition: ndn-app.cpp:97
TracedCallback< shared_ptr< const Interest >, Ptr< App >, shared_ptr< Face > > m_transmittedInterests
App-level trace of transmitted Interests.
Definition: ndn-app.hpp:119
shared_ptr< Face > m_face
Definition: ndn-app.hpp:104
TracedCallback< shared_ptr< const Data >, Ptr< App >, shared_ptr< Face > > m_transmittedDatas
App-level trace of transmitted Data.
Definition: ndn-app.hpp:122
Implementation network-layer of NDN stack.
AppLinkService * m_appLink
Definition: ndn-app.hpp:105
App()
Default constructor.
Definition: ndn-app.cpp:70
uint32_t GetId() const
Get application ID (ID of applications face)
Definition: ndn-app.cpp:108
TracedCallback< shared_ptr< const Data >, Ptr< App >, shared_ptr< Face > > m_receivedDatas
App-level trace of received Data.
Definition: ndn-app.hpp:113
bool m_active
Flag to indicate that application is active (set by StartApplication and StopApplication) ...
Definition: ndn-app.hpp:103
virtual void OnData(shared_ptr< const Data > data)
Method that will be called every time new Data arrives.
Definition: ndn-app.cpp:121