blob: 0472550bebad2fdfc4a5b8c49796f128403f85ad [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
52 .AddTraceSource("TransmittedInterests", "TransmittedInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070053 MakeTraceSourceAccessor(&App::m_transmittedInterests),
54 "ns3::ndn::App::InterestTraceCallback")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080055
56 .AddTraceSource("TransmittedDatas", "TransmittedDatas",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070057 MakeTraceSourceAccessor(&App::m_transmittedDatas),
58 "ns3::ndn::App::DataTraceCallback");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070059 return tid;
60}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061
62App::App()
63 : m_active(false)
64 , m_face(0)
Alexander Afanasyev6a720c12015-08-21 12:50:13 -070065 , m_appId(std::numeric_limits<uint32_t>::max())
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070066{
67}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068
69App::~App()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070070{
71}
72
73void
Alexander Afanasyev6a720c12015-08-21 12:50:13 -070074App::DoInitialize()
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
88void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089App::DoDispose(void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070090{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070092
93 // Unfortunately, this causes SEGFAULT
94 // The best reason I see is that apps are freed after ndn stack is removed
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 // StopApplication ();
96 Application::DoDispose();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070097}
98
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080099uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100App::GetId() const
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800101{
Alexander Afanasyev6a720c12015-08-21 12:50:13 -0700102 return m_appId;
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800103}
104
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700105void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700106App::OnInterest(shared_ptr<const Interest> interest)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700107{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108 NS_LOG_FUNCTION(this << interest);
109 m_receivedInterests(interest, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700110}
111
112void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700113App::OnData(shared_ptr<const Data> data)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700114{
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700115 NS_LOG_FUNCTION(this << data);
116 m_receivedDatas(data, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700117}
118
Alexander Afanasyevc018a562016-09-08 16:27:31 -0700119void
120App::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
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700128// Application Methods
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800129void
130App::StartApplication() // Called at time specified by Start
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700131{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132 NS_LOG_FUNCTION_NOARGS();
133
134 NS_ASSERT(m_active != true);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700135 m_active = true;
136
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0,
138 "Ndn stack should be installed on the node " << GetNode());
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700139
140 // step 1. Create a face
Alexander Afanasyevc018a562016-09-08 16:27:31 -0700141 auto appLink = make_unique<AppLinkService>(this);
142 auto transport = make_unique<NullTransport>("appFace://", "appFace://",
143 ::ndn::nfd::FACE_SCOPE_LOCAL);
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);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800148
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700149 // step 2. Add face to the Ndn stack
Mickey Sweatt89046c12014-11-16 20:32:27 -0800150 GetNode()->GetObject<L3Protocol>()->addFace(m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700151}
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700152
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800153void
154App::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
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700161 m_active = false;
162
Mickey Sweatt89046c12014-11-16 20:32:27 -0800163 m_face->close();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700164}
165
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700166} // namespace ndn
167} // namespace ns3