NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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("TransmittedInterests", "TransmittedInterests",
53  MakeTraceSourceAccessor(&App::m_transmittedInterests),
54  "ns3::ndn::App::InterestTraceCallback")
55 
56  .AddTraceSource("TransmittedDatas", "TransmittedDatas",
57  MakeTraceSourceAccessor(&App::m_transmittedDatas),
58  "ns3::ndn::App::DataTraceCallback");
59  return tid;
60 }
61 
63  : m_active(false)
64  , m_face(0)
65  , m_appId(std::numeric_limits<uint32_t>::max())
66 {
67 }
68 
70 {
71 }
72 
73 void
75 {
76  NS_LOG_FUNCTION_NOARGS();
77 
78  // find out what is application id on the node
79  for (uint32_t id = 0; id < GetNode()->GetNApplications(); ++id) {
80  if (GetNode()->GetApplication(id) == this) {
81  m_appId = id;
82  }
83  }
84 
85  Application::DoInitialize();
86 }
87 
88 void
90 {
91  NS_LOG_FUNCTION_NOARGS();
92 
93  // Unfortunately, this causes SEGFAULT
94  // The best reason I see is that apps are freed after ndn stack is removed
95  // StopApplication ();
96  Application::DoDispose();
97 }
98 
99 uint32_t
100 App::GetId() const
101 {
102  return m_appId;
103 }
104 
105 void
106 App::OnInterest(shared_ptr<const Interest> interest)
107 {
108  NS_LOG_FUNCTION(this << interest);
109  m_receivedInterests(interest, this, m_face);
110 }
111 
112 void
113 App::OnData(shared_ptr<const Data> data)
114 {
115  NS_LOG_FUNCTION(this << data);
116  m_receivedDatas(data, this, m_face);
117 }
118 
119 void
120 App::OnNack(shared_ptr<const lp::Nack> nack)
121 {
122  NS_LOG_FUNCTION(this << nack);
123 
124  // @TODO Implement
125  // m_receivedDatas(data, this, m_face);
126 }
127 
128 // Application Methods
129 void
130 App::StartApplication() // Called at time specified by Start
131 {
132  NS_LOG_FUNCTION_NOARGS();
133 
134  NS_ASSERT(m_active != true);
135  m_active = true;
136 
137  NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0,
138  "Ndn stack should be installed on the node " << GetNode());
139 
140  // step 1. Create a face
141  auto appLink = make_unique<AppLinkService>(this);
142  auto transport = make_unique<NullTransport>("appFace://", "appFace://",
144  // @TODO Consider making AppTransport instead
145  m_face = std::make_shared<Face>(std::move(appLink), std::move(transport));
146  m_appLink = static_cast<AppLinkService*>(m_face->getLinkService());
147  m_face->setMetric(1);
148 
149  // step 2. Add face to the Ndn stack
150  GetNode()->GetObject<L3Protocol>()->addFace(m_face);
151 }
152 
153 void
154 App::StopApplication() // Called at time specified by Stop
155 {
156  NS_LOG_FUNCTION_NOARGS();
157 
158  if (!m_active)
159  return; // don't assert here, just return
160 
161  m_active = false;
162 
163  m_face->close();
164 }
165 
166 } // namespace ndn
167 } // namespace ns3
Copyright (c) 2011-2015 Regents of the University of California.
virtual void StopApplication()
Called at time specified by Stop.
Definition: ndn-app.cpp:154
Implementation of LinkService for ndnSIM application.
uint32_t m_appId
Definition: ndn-app.hpp:107
virtual ~App()
Definition: ndn-app.cpp:69
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:74
virtual void StartApplication()
Called at time specified by Start.
Definition: ndn-app.cpp:130
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:106
virtual void OnNack(shared_ptr< const lp::Nack > nack)
Method that will be called every time new Nack arrives.
Definition: ndn-app.cpp:120
Copyright (c) 2011-2015 Regents of the University of California.
virtual void DoDispose()
Definition: ndn-app.cpp:89
TracedCallback< shared_ptr< const Interest >, Ptr< App >, shared_ptr< Face > > m_transmittedInterests
App-level trace of transmitted Interests.
Definition: ndn-app.hpp:118
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:121
Implementation network-layer of NDN stack.
AppLinkService * m_appLink
Definition: ndn-app.hpp:105
App()
Default constructor.
Definition: ndn-app.cpp:62
uint32_t GetId() const
Get application ID (ID of applications face)
Definition: ndn-app.cpp:100
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:113