blob: 4121eef5d14aaa4f455374fe9865695c9dc8af13 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-app.hpp"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ns3/log.h"
22#include "ns3/assert.h"
23#include "ns3/packet.h"
24
Mickey Sweatt89046c12014-11-16 20:32:27 -080025#include "model/ndn-l3-protocol.hpp"
Alexander Afanasyevc018a562016-09-08 16:27:31 -070026#include "model/ndn-app-link-service.hpp"
27#include "model/null-transport.hpp"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070028
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080029NS_LOG_COMPONENT_DEFINE("ndn.App");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070030
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070031namespace ns3 {
32namespace ndn {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080033
34NS_OBJECT_ENSURE_REGISTERED(App);
35
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070036TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037App::GetTypeId(void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070038{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039 static TypeId tid = TypeId("ns3::ndn::App")
40 .SetGroupName("Ndn")
41 .SetParent<Application>()
42 .AddConstructor<App>()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070043
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 .AddTraceSource("ReceivedInterests", "ReceivedInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070045 MakeTraceSourceAccessor(&App::m_receivedInterests),
46 "ns3::ndn::App::InterestTraceCallback")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070047
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048 .AddTraceSource("ReceivedDatas", "ReceivedDatas",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070049 MakeTraceSourceAccessor(&App::m_receivedDatas),
50 "ns3::ndn::App::DataTraceCallback")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051
Spyridon Mastorakis7f2fd422016-12-07 14:33:59 -080052 .AddTraceSource("ReceivedNacks", "ReceivedNacks",
53 MakeTraceSourceAccessor(&App::m_receivedNacks),
54 "ns3::ndn::App::NackTraceCallback")
55
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080056 .AddTraceSource("TransmittedInterests", "TransmittedInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070057 MakeTraceSourceAccessor(&App::m_transmittedInterests),
58 "ns3::ndn::App::InterestTraceCallback")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059
60 .AddTraceSource("TransmittedDatas", "TransmittedDatas",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070061 MakeTraceSourceAccessor(&App::m_transmittedDatas),
Spyridon Mastorakis7f2fd422016-12-07 14:33:59 -080062 "ns3::ndn::App::DataTraceCallback")
63
64 .AddTraceSource("TransmittedNacks", "TransmittedNacks",
65 MakeTraceSourceAccessor(&App::m_transmittedNacks),
66 "ns3::ndn::App::NackTraceCallback");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070067 return tid;
68}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069
70App::App()
71 : m_active(false)
72 , m_face(0)
Alexander Afanasyev6a720c12015-08-21 12:50:13 -070073 , m_appId(std::numeric_limits<uint32_t>::max())
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070074{
75}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076
77App::~App()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070078{
79}
80
81void
Alexander Afanasyev6a720c12015-08-21 12:50:13 -070082App::DoInitialize()
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
96void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097App::DoDispose(void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070098{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700100
101 // Unfortunately, this causes SEGFAULT
102 // The best reason I see is that apps are freed after ndn stack is removed
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 // StopApplication ();
104 Application::DoDispose();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700105}
106
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800107uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108App::GetId() const
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800109{
Alexander Afanasyev6a720c12015-08-21 12:50:13 -0700110 return m_appId;
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800111}
112
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700113void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700114App::OnInterest(shared_ptr<const Interest> interest)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700115{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 NS_LOG_FUNCTION(this << interest);
117 m_receivedInterests(interest, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700118}
119
120void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700121App::OnData(shared_ptr<const Data> data)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700122{
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700123 NS_LOG_FUNCTION(this << data);
124 m_receivedDatas(data, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700125}
126
Alexander Afanasyevc018a562016-09-08 16:27:31 -0700127void
128App::OnNack(shared_ptr<const lp::Nack> nack)
129{
130 NS_LOG_FUNCTION(this << nack);
131
132 // @TODO Implement
Spyridon Mastorakis7f2fd422016-12-07 14:33:59 -0800133 m_receivedNacks(nack, this, m_face);
Alexander Afanasyevc018a562016-09-08 16:27:31 -0700134}
135
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700136// Application Methods
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137void
138App::StartApplication() // Called at time specified by Start
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700139{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800140 NS_LOG_FUNCTION_NOARGS();
141
142 NS_ASSERT(m_active != true);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700143 m_active = true;
144
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145 NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0,
146 "Ndn stack should be installed on the node " << GetNode());
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700147
148 // step 1. Create a face
Alexander Afanasyevc018a562016-09-08 16:27:31 -0700149 auto appLink = make_unique<AppLinkService>(this);
150 auto transport = make_unique<NullTransport>("appFace://", "appFace://",
151 ::ndn::nfd::FACE_SCOPE_LOCAL);
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);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800156
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700157 // step 2. Add face to the Ndn stack
Mickey Sweatt89046c12014-11-16 20:32:27 -0800158 GetNode()->GetObject<L3Protocol>()->addFace(m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700159}
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700160
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800161void
162App::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
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700169 m_active = false;
170
Mickey Sweatt89046c12014-11-16 20:32:27 -0800171 m_face->close();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700172}
173
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700174} // namespace ndn
175} // namespace ns3