Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * 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 Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * 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 Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * 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 Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 19 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 20 | #include "ndn-app.hpp" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 21 | #include "ns3/log.h" |
| 22 | #include "ns3/assert.h" |
| 23 | #include "ns3/packet.h" |
| 24 | |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 25 | #include "model/ndn-l3-protocol.hpp" |
Alexander Afanasyev | c018a56 | 2016-09-08 16:27:31 -0700 | [diff] [blame] | 26 | #include "model/ndn-app-link-service.hpp" |
| 27 | #include "model/null-transport.hpp" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 29 | NS_LOG_COMPONENT_DEFINE("ndn.App"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 31 | namespace ns3 { |
| 32 | namespace ndn { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 33 | |
| 34 | NS_OBJECT_ENSURE_REGISTERED(App); |
| 35 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 36 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 37 | App::GetTypeId(void) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 38 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 39 | static TypeId tid = TypeId("ns3::ndn::App") |
| 40 | .SetGroupName("Ndn") |
| 41 | .SetParent<Application>() |
| 42 | .AddConstructor<App>() |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 44 | .AddTraceSource("ReceivedInterests", "ReceivedInterests", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 45 | MakeTraceSourceAccessor(&App::m_receivedInterests), |
| 46 | "ns3::ndn::App::InterestTraceCallback") |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 48 | .AddTraceSource("ReceivedDatas", "ReceivedDatas", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 49 | MakeTraceSourceAccessor(&App::m_receivedDatas), |
| 50 | "ns3::ndn::App::DataTraceCallback") |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 51 | |
Spyridon Mastorakis | 7f2fd42 | 2016-12-07 14:33:59 -0800 | [diff] [blame] | 52 | .AddTraceSource("ReceivedNacks", "ReceivedNacks", |
| 53 | MakeTraceSourceAccessor(&App::m_receivedNacks), |
| 54 | "ns3::ndn::App::NackTraceCallback") |
| 55 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 56 | .AddTraceSource("TransmittedInterests", "TransmittedInterests", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 57 | MakeTraceSourceAccessor(&App::m_transmittedInterests), |
| 58 | "ns3::ndn::App::InterestTraceCallback") |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 59 | |
| 60 | .AddTraceSource("TransmittedDatas", "TransmittedDatas", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 61 | MakeTraceSourceAccessor(&App::m_transmittedDatas), |
Spyridon Mastorakis | 7f2fd42 | 2016-12-07 14:33:59 -0800 | [diff] [blame] | 62 | "ns3::ndn::App::DataTraceCallback") |
| 63 | |
| 64 | .AddTraceSource("TransmittedNacks", "TransmittedNacks", |
| 65 | MakeTraceSourceAccessor(&App::m_transmittedNacks), |
| 66 | "ns3::ndn::App::NackTraceCallback"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 67 | return tid; |
| 68 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 69 | |
| 70 | App::App() |
| 71 | : m_active(false) |
| 72 | , m_face(0) |
Alexander Afanasyev | 6a720c1 | 2015-08-21 12:50:13 -0700 | [diff] [blame] | 73 | , m_appId(std::numeric_limits<uint32_t>::max()) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 74 | { |
| 75 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 76 | |
| 77 | App::~App() |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 78 | { |
| 79 | } |
| 80 | |
| 81 | void |
Alexander Afanasyev | 6a720c1 | 2015-08-21 12:50:13 -0700 | [diff] [blame] | 82 | App::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 | |
| 96 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 97 | App::DoDispose(void) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 98 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 99 | NS_LOG_FUNCTION_NOARGS(); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 100 | |
| 101 | // Unfortunately, this causes SEGFAULT |
| 102 | // The best reason I see is that apps are freed after ndn stack is removed |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 103 | // StopApplication (); |
| 104 | Application::DoDispose(); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 107 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 108 | App::GetId() const |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 109 | { |
Alexander Afanasyev | 6a720c1 | 2015-08-21 12:50:13 -0700 | [diff] [blame] | 110 | return m_appId; |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 113 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 114 | App::OnInterest(shared_ptr<const Interest> interest) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 115 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 116 | NS_LOG_FUNCTION(this << interest); |
| 117 | m_receivedInterests(interest, this, m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 121 | App::OnData(shared_ptr<const Data> data) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 122 | { |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 123 | NS_LOG_FUNCTION(this << data); |
| 124 | m_receivedDatas(data, this, m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Alexander Afanasyev | c018a56 | 2016-09-08 16:27:31 -0700 | [diff] [blame] | 127 | void |
| 128 | App::OnNack(shared_ptr<const lp::Nack> nack) |
| 129 | { |
| 130 | NS_LOG_FUNCTION(this << nack); |
| 131 | |
| 132 | // @TODO Implement |
Spyridon Mastorakis | 7f2fd42 | 2016-12-07 14:33:59 -0800 | [diff] [blame] | 133 | m_receivedNacks(nack, this, m_face); |
Alexander Afanasyev | c018a56 | 2016-09-08 16:27:31 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 136 | // Application Methods |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 137 | void |
| 138 | App::StartApplication() // Called at time specified by Start |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 139 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 140 | NS_LOG_FUNCTION_NOARGS(); |
| 141 | |
| 142 | NS_ASSERT(m_active != true); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 143 | m_active = true; |
| 144 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 145 | NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0, |
| 146 | "Ndn stack should be installed on the node " << GetNode()); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 147 | |
| 148 | // step 1. Create a face |
Alexander Afanasyev | c018a56 | 2016-09-08 16:27:31 -0700 | [diff] [blame] | 149 | 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 Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 156 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 157 | // step 2. Add face to the Ndn stack |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 158 | GetNode()->GetObject<L3Protocol>()->addFace(m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 159 | } |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 160 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 161 | void |
| 162 | App::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 Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 169 | m_active = false; |
| 170 | |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 171 | m_face->close(); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 174 | } // namespace ndn |
| 175 | } // namespace ns3 |