NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndn-app.cc
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 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 #include "ndn-app.h"
22 #include "ns3/log.h"
23 #include "ns3/assert.h"
24 #include "ns3/packet.h"
25 
26 #include "ns3/ndn-interest.h"
27 #include "ns3/ndn-data.h"
28 #include "ns3/ndn-l3-protocol.h"
29 #include "ns3/ndn-fib.h"
30 #include "ns3/ndn-app-face.h"
31 #include "ns3/ndn-forwarding-strategy.h"
32 
33 NS_LOG_COMPONENT_DEFINE ("ndn.App");
34 
35 namespace ns3 {
36 namespace ndn {
37 
38 NS_OBJECT_ENSURE_REGISTERED (App);
39 
40 TypeId
41 App::GetTypeId (void)
42 {
43  static TypeId tid = TypeId ("ns3::ndn::App")
44  .SetGroupName ("Ndn")
45  .SetParent<Application> ()
46  .AddConstructor<App> ()
47 
48  .AddTraceSource ("ReceivedInterests", "ReceivedInterests",
49  MakeTraceSourceAccessor (&App::m_receivedInterests))
50 
51  .AddTraceSource ("ReceivedNacks", "ReceivedNacks",
52  MakeTraceSourceAccessor (&App::m_receivedNacks))
53 
54  .AddTraceSource ("ReceivedDatas", "ReceivedDatas",
55  MakeTraceSourceAccessor (&App::m_receivedDatas))
56 
57  .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
58  MakeTraceSourceAccessor (&App::m_transmittedInterests))
59 
60  .AddTraceSource ("TransmittedDatas", "TransmittedDatas",
61  MakeTraceSourceAccessor (&App::m_transmittedDatas))
62  ;
63  return tid;
64 }
65 
67  : m_active (false)
68  , m_face (0)
69 {
70 }
71 
72 App::~App ()
73 {
74 }
75 
76 void
78 {
79  NS_LOG_FUNCTION_NOARGS ();
80 
81  // Unfortunately, this causes SEGFAULT
82  // The best reason I see is that apps are freed after ndn stack is removed
83  // StopApplication ();
84  Application::DoDispose ();
85 }
86 
87 uint32_t
88 App::GetId () const
89 {
90  if (m_face == 0)
91  return (uint32_t)-1;
92  else
93  return m_face->GetId ();
94 }
95 
96 void
97 App::OnInterest (Ptr<const Interest> interest)
98 {
99  NS_LOG_FUNCTION (this << interest);
100  m_receivedInterests (interest, this, m_face);
101 }
102 
103 void
104 App::OnNack (Ptr<const Interest> interest)
105 {
106  NS_LOG_FUNCTION (this << interest);
107  m_receivedNacks (interest, this, m_face);
108 }
109 
110 void
111 App::OnData (Ptr<const Data> contentObject)
112 {
113  NS_LOG_FUNCTION (this << contentObject);
114  m_receivedDatas (contentObject, this, m_face);
115 }
116 
117 // Application Methods
118 void
119 App::StartApplication () // Called at time specified by Start
120 {
121  NS_LOG_FUNCTION_NOARGS ();
122 
123  NS_ASSERT (m_active != true);
124  m_active = true;
125 
126  NS_ASSERT_MSG (GetNode ()->GetObject<L3Protocol> () != 0,
127  "Ndn stack should be installed on the node " << GetNode ());
128 
129  // step 1. Create a face
130  m_face = CreateObject<AppFace> (/*Ptr<App> (this)*/this);
131 
132  // step 2. Add face to the Ndn stack
133  GetNode ()->GetObject<L3Protocol> ()->AddFace (m_face);
134 
135  // step 3. Enable face
136  m_face->SetUp (true);
137 }
138 
139 void
140 App::StopApplication () // Called at time specified by Stop
141 {
142  NS_LOG_FUNCTION_NOARGS ();
143 
144  if (!m_active) return; //don't assert here, just return
145 
146  NS_ASSERT (GetNode ()->GetObject<L3Protocol> () != 0);
147 
148  m_active = false;
149 
150  // step 1. Disable face
151  m_face->SetUp (false);
152 
153  // step 2. Remove face from Ndn stack
154  GetNode ()->GetObject<L3Protocol> ()->RemoveFace (m_face);
155 
156  // step 3. Destroy face
157  if (m_face->GetReferenceCount () != 1)
158  {
159  NS_LOG_ERROR ("Please a bug report on https://github.com/NDN-Routing/ndnSIM/issues");
160  NS_LOG_ERROR ("At this point, nobody else should have referenced this face, but we have "
161  << m_face->GetReferenceCount () << " references");
162 
163  }
164  // NS_ASSERT_MSG (m_face->GetReferenceCount ()==2,
165  // "At this point, nobody else should have referenced this face, but we have "
166  // << m_face->GetReferenceCount () << " references");
167  m_face = 0;
168 }
169 
170 } // namespace ndn
171 } // namespace ns3
bool m_active
Flag to indicate that application is active (set by StartApplication and StopApplication) ...
Definition: ndn-app.h:106
virtual void DoDispose()
Do cleanup when application is destroyed.
Definition: ndn-app.cc:77
TracedCallback< Ptr< const Interest >, Ptr< App >, Ptr< Face > > m_receivedNacks
App-level trace of received NACKs.
Definition: ndn-app.h:113
virtual void StartApplication()
Called at time specified by Start.
Definition: ndn-app.cc:119
uint32_t GetId() const
Get application ID (ID of applications face)
Definition: ndn-app.cc:88
TracedCallback< Ptr< const Interest >, Ptr< App >, Ptr< Face > > m_transmittedInterests
App-level trace of transmitted Interests.
Definition: ndn-app.h:120
Implementation network-layer of NDN stack.
TracedCallback< Ptr< const Data >, Ptr< App >, Ptr< Face > > m_receivedDatas
App-level trace of received Data.
Definition: ndn-app.h:116
virtual void OnNack(Ptr< const Interest > interest)
Method that will be called every time new NACK arrives.
Definition: ndn-app.cc:104
virtual void OnData(Ptr< const Data > contentObject)
Method that will be called every time new Data arrives.
Definition: ndn-app.cc:111
virtual void StopApplication()
Called at time specified by Stop.
Definition: ndn-app.cc:140
virtual void OnInterest(Ptr< const Interest > interest)
Method that will be called every time new Interest arrives.
Definition: ndn-app.cc:97
App()
Default constructor.
Definition: ndn-app.cc:66
Ptr< Face > m_face
automatically created application face through which application communicates
Definition: ndn-app.h:107
TracedCallback< Ptr< const Interest >, Ptr< App >, Ptr< Face > > m_receivedInterests
App-level trace of received Interests.
Definition: ndn-app.h:110
TracedCallback< Ptr< const Data >, Ptr< App >, Ptr< Face > > m_transmittedDatas
App-level trace of transmitted Data.
Definition: ndn-app.h:123