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" |
| 26 | #include "model/ndn-app-face.hpp" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 28 | NS_LOG_COMPONENT_DEFINE("ndn.App"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 30 | namespace ns3 { |
| 31 | namespace ndn { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 32 | |
| 33 | NS_OBJECT_ENSURE_REGISTERED(App); |
| 34 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 35 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 36 | App::GetTypeId(void) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 37 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 38 | static TypeId tid = TypeId("ns3::ndn::App") |
| 39 | .SetGroupName("Ndn") |
| 40 | .SetParent<Application>() |
| 41 | .AddConstructor<App>() |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 43 | .AddTraceSource("ReceivedInterests", "ReceivedInterests", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 44 | MakeTraceSourceAccessor(&App::m_receivedInterests), |
| 45 | "ns3::ndn::App::InterestTraceCallback") |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 46 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 47 | .AddTraceSource("ReceivedDatas", "ReceivedDatas", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 48 | MakeTraceSourceAccessor(&App::m_receivedDatas), |
| 49 | "ns3::ndn::App::DataTraceCallback") |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 50 | |
| 51 | .AddTraceSource("TransmittedInterests", "TransmittedInterests", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 52 | MakeTraceSourceAccessor(&App::m_transmittedInterests), |
| 53 | "ns3::ndn::App::InterestTraceCallback") |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 54 | |
| 55 | .AddTraceSource("TransmittedDatas", "TransmittedDatas", |
Alexander Afanasyev | d6453cd | 2015-08-20 21:45:36 -0700 | [diff] [blame] | 56 | MakeTraceSourceAccessor(&App::m_transmittedDatas), |
| 57 | "ns3::ndn::App::DataTraceCallback"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 58 | return tid; |
| 59 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 60 | |
| 61 | App::App() |
| 62 | : m_active(false) |
| 63 | , m_face(0) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 64 | { |
| 65 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | |
| 67 | App::~App() |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 68 | { |
| 69 | } |
| 70 | |
| 71 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 72 | App::DoDispose(void) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 73 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | NS_LOG_FUNCTION_NOARGS(); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 75 | |
| 76 | // Unfortunately, this causes SEGFAULT |
| 77 | // 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] | 78 | // StopApplication (); |
| 79 | Application::DoDispose(); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 82 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 83 | App::GetId() const |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 84 | { |
| 85 | if (m_face == 0) |
| 86 | return (uint32_t)-1; |
| 87 | else |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 88 | return m_face->getId(); |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 91 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 92 | App::OnInterest(shared_ptr<const Interest> interest) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 93 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 94 | NS_LOG_FUNCTION(this << interest); |
| 95 | m_receivedInterests(interest, this, m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 99 | App::OnData(shared_ptr<const Data> data) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 100 | { |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 101 | NS_LOG_FUNCTION(this << data); |
| 102 | m_receivedDatas(data, this, m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Application Methods |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 106 | void |
| 107 | App::StartApplication() // Called at time specified by Start |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 108 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 109 | NS_LOG_FUNCTION_NOARGS(); |
| 110 | |
| 111 | NS_ASSERT(m_active != true); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 112 | m_active = true; |
| 113 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 114 | NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0, |
| 115 | "Ndn stack should be installed on the node " << GetNode()); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 116 | |
| 117 | // step 1. Create a face |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 118 | m_face = std::make_shared<AppFace>(this); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 120 | // step 2. Add face to the Ndn stack |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 121 | GetNode()->GetObject<L3Protocol>()->addFace(m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 122 | } |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 123 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 124 | void |
| 125 | App::StopApplication() // Called at time specified by Stop |
| 126 | { |
| 127 | NS_LOG_FUNCTION_NOARGS(); |
| 128 | |
| 129 | if (!m_active) |
| 130 | return; // don't assert here, just return |
| 131 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 132 | m_active = false; |
| 133 | |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame] | 134 | m_face->close(); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 137 | } // namespace ndn |
| 138 | } // namespace ns3 |