forked from named-data-ndnSIM/ndnSIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndn-app.cpp
151 lines (121 loc) · 3.99 KB
/
ndn-app.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2011-2015 Regents of the University of California.
*
* This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
* contributors.
*
* ndnSIM is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
#include "ndn-app.hpp"
#include "ns3/log.h"
#include "ns3/assert.h"
#include "ns3/packet.h"
#include "model/ndn-l3-protocol.hpp"
#include "model/ndn-app-face.hpp"
NS_LOG_COMPONENT_DEFINE("ndn.App");
namespace ns3 {
namespace ndn {
NS_OBJECT_ENSURE_REGISTERED(App);
TypeId
App::GetTypeId(void)
{
static TypeId tid = TypeId("ns3::ndn::App")
.SetGroupName("Ndn")
.SetParent<Application>()
.AddConstructor<App>()
.AddTraceSource("ReceivedInterests", "ReceivedInterests",
MakeTraceSourceAccessor(&App::m_receivedInterests),
"ns3::ndn::App::InterestTraceCallback")
.AddTraceSource("ReceivedDatas", "ReceivedDatas",
MakeTraceSourceAccessor(&App::m_receivedDatas),
"ns3::ndn::App::DataTraceCallback")
.AddTraceSource("TransmittedInterests", "TransmittedInterests",
MakeTraceSourceAccessor(&App::m_transmittedInterests),
"ns3::ndn::App::InterestTraceCallback")
.AddTraceSource("TransmittedDatas", "TransmittedDatas",
MakeTraceSourceAccessor(&App::m_transmittedDatas),
"ns3::ndn::App::DataTraceCallback");
return tid;
}
App::App()
: m_active(false)
, m_face(0)
, m_appId(std::numeric_limits<uint32_t>::max())
{
}
App::~App()
{
}
void
App::DoInitialize()
{
NS_LOG_FUNCTION_NOARGS();
// find out what is application id on the node
for (uint32_t id = 0; id < GetNode()->GetNApplications(); ++id) {
if (GetNode()->GetApplication(id) == this) {
m_appId = id;
}
}
Application::DoInitialize();
}
void
App::DoDispose(void)
{
NS_LOG_FUNCTION_NOARGS();
// Unfortunately, this causes SEGFAULT
// The best reason I see is that apps are freed after ndn stack is removed
// StopApplication ();
Application::DoDispose();
}
uint32_t
App::GetId() const
{
return m_appId;
}
void
App::OnInterest(shared_ptr<const Interest> interest)
{
NS_LOG_FUNCTION(this << interest);
m_receivedInterests(interest, this, m_face);
}
void
App::OnData(shared_ptr<const Data> data)
{
NS_LOG_FUNCTION(this << data);
m_receivedDatas(data, this, m_face);
}
// Application Methods
void
App::StartApplication() // Called at time specified by Start
{
NS_LOG_FUNCTION_NOARGS();
NS_ASSERT(m_active != true);
m_active = true;
NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0,
"Ndn stack should be installed on the node " << GetNode());
// step 1. Create a face
m_face = std::make_shared<AppFace>(this);
// step 2. Add face to the Ndn stack
GetNode()->GetObject<L3Protocol>()->addFace(m_face);
}
void
App::StopApplication() // Called at time specified by Stop
{
NS_LOG_FUNCTION_NOARGS();
if (!m_active)
return; // don't assert here, just return
m_active = false;
m_face->close();
}
} // namespace ndn
} // namespace ns3