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 
26 #include "model/ndn-app-face.hpp"
27 
28 NS_LOG_COMPONENT_DEFINE("ndn.App");
29 
30 namespace ns3 {
31 namespace ndn {
32 
34 
35 TypeId
37 {
38  static TypeId tid = TypeId("ns3::ndn::App")
39  .SetGroupName("Ndn")
40  .SetParent<Application>()
41  .AddConstructor<App>()
42 
43  .AddTraceSource("ReceivedInterests", "ReceivedInterests",
44  MakeTraceSourceAccessor(&App::m_receivedInterests),
45  "ns3::ndn::App::InterestTraceCallback")
46 
47  .AddTraceSource("ReceivedDatas", "ReceivedDatas",
48  MakeTraceSourceAccessor(&App::m_receivedDatas),
49  "ns3::ndn::App::DataTraceCallback")
50 
51  .AddTraceSource("TransmittedInterests", "TransmittedInterests",
52  MakeTraceSourceAccessor(&App::m_transmittedInterests),
53  "ns3::ndn::App::InterestTraceCallback")
54 
55  .AddTraceSource("TransmittedDatas", "TransmittedDatas",
56  MakeTraceSourceAccessor(&App::m_transmittedDatas),
57  "ns3::ndn::App::DataTraceCallback");
58  return tid;
59 }
60 
62  : m_active(false)
63  , m_face(0)
64  , m_appId(std::numeric_limits<uint32_t>::max())
65 {
66 }
67 
69 {
70 }
71 
72 void
74 {
75  NS_LOG_FUNCTION_NOARGS();
76 
77  // find out what is application id on the node
78  for (uint32_t id = 0; id < GetNode()->GetNApplications(); ++id) {
79  if (GetNode()->GetApplication(id) == this) {
80  m_appId = id;
81  }
82  }
83 
84  Application::DoInitialize();
85 }
86 
87 void
89 {
90  NS_LOG_FUNCTION_NOARGS();
91 
92  // Unfortunately, this causes SEGFAULT
93  // The best reason I see is that apps are freed after ndn stack is removed
94  // StopApplication ();
95  Application::DoDispose();
96 }
97 
98 uint32_t
99 App::GetId() const
100 {
101  return m_appId;
102 }
103 
104 void
105 App::OnInterest(shared_ptr<const Interest> interest)
106 {
107  NS_LOG_FUNCTION(this << interest);
108  m_receivedInterests(interest, this, m_face);
109 }
110 
111 void
112 App::OnData(shared_ptr<const Data> data)
113 {
114  NS_LOG_FUNCTION(this << data);
115  m_receivedDatas(data, this, m_face);
116 }
117 
118 // Application Methods
119 void
120 App::StartApplication() // Called at time specified by Start
121 {
122  NS_LOG_FUNCTION_NOARGS();
123 
124  NS_ASSERT(m_active != true);
125  m_active = true;
126 
127  NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0,
128  "Ndn stack should be installed on the node " << GetNode());
129 
130  // step 1. Create a face
131  m_face = std::make_shared<AppFace>(this);
132 
133  // step 2. Add face to the Ndn stack
134  GetNode()->GetObject<L3Protocol>()->addFace(m_face);
135 }
136 
137 void
138 App::StopApplication() // Called at time specified by Stop
139 {
140  NS_LOG_FUNCTION_NOARGS();
141 
142  if (!m_active)
143  return; // don't assert here, just return
144 
145  m_active = false;
146 
147  m_face->close();
148 }
149 
150 } // namespace ndn
151 } // 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:138
uint32_t m_appId
Definition: ndn-app.hpp:104
virtual ~App()
Definition: ndn-app.cpp:68
ndn App
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-app.cpp:28
NS_OBJECT_ENSURE_REGISTERED(ContentStore)
virtual void DoInitialize()
Definition: ndn-app.cpp:73
virtual void StartApplication()
Called at time specified by Start.
Definition: ndn-app.cpp:120
STL namespace.
shared_ptr< AppFace > m_face
automatically created application face through which application communicates
Definition: ndn-app.hpp:103
TracedCallback< shared_ptr< const Interest >, Ptr< App >, shared_ptr< Face > > m_receivedInterests
App-level trace of received Interests.
Definition: ndn-app.hpp:107
static TypeId GetTypeId()
Definition: ndn-app.cpp:36
virtual void OnInterest(shared_ptr< const Interest > interest)
Method that will be called every time new Interest arrives.
Definition: ndn-app.cpp:105
Copyright (c) 2011-2015 Regents of the University of California.
virtual void DoDispose()
Definition: ndn-app.cpp:88
TracedCallback< shared_ptr< const Interest >, Ptr< App >, shared_ptr< Face > > m_transmittedInterests
App-level trace of transmitted Interests.
Definition: ndn-app.hpp:113
TracedCallback< shared_ptr< const Data >, Ptr< App >, shared_ptr< Face > > m_transmittedDatas
App-level trace of transmitted Data.
Definition: ndn-app.hpp:116
Implementation network-layer of NDN stack.
App()
Default constructor.
Definition: ndn-app.cpp:61
TracedCallback< shared_ptr< const Data >, Ptr< App >, shared_ptr< Face > > m_receivedDatas
App-level trace of received Data.
Definition: ndn-app.hpp:110
bool m_active
Flag to indicate that application is active (set by StartApplication and StopApplication) ...
Definition: ndn-app.hpp:102
virtual void OnData(shared_ptr< const Data > data)
Method that will be called every time new Data arrives.
Definition: ndn-app.cpp:112
uint32_t GetId() const
Get application ID (ID of applications face)
Definition: ndn-app.cpp:99