Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 21 | #include "ndn-app.hpp" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 22 | #include "ns3/log.h" |
| 23 | #include "ns3/assert.h" |
| 24 | #include "ns3/packet.h" |
| 25 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 26 | #include "ns3/ndn-l3-protocol.hpp" |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 27 | #include "ns3/ndn-app-face.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", |
| 45 | MakeTraceSourceAccessor(&App::m_receivedInterests)) |
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", |
| 48 | MakeTraceSourceAccessor(&App::m_receivedDatas)) |
| 49 | |
| 50 | .AddTraceSource("TransmittedInterests", "TransmittedInterests", |
| 51 | MakeTraceSourceAccessor(&App::m_transmittedInterests)) |
| 52 | |
| 53 | .AddTraceSource("TransmittedDatas", "TransmittedDatas", |
| 54 | MakeTraceSourceAccessor(&App::m_transmittedDatas)); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 55 | return tid; |
| 56 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 57 | |
| 58 | App::App() |
| 59 | : m_active(false) |
| 60 | , m_face(0) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 61 | { |
| 62 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 63 | |
| 64 | App::~App() |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 65 | { |
| 66 | } |
| 67 | |
| 68 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 69 | App::DoDispose(void) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 70 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 71 | NS_LOG_FUNCTION_NOARGS(); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 72 | |
| 73 | // Unfortunately, this causes SEGFAULT |
| 74 | // 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] | 75 | // StopApplication (); |
| 76 | Application::DoDispose(); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 79 | uint32_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 80 | App::GetId() const |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 81 | { |
| 82 | if (m_face == 0) |
| 83 | return (uint32_t)-1; |
| 84 | else |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 85 | return m_face->GetId(); |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 88 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 89 | App::OnInterest(shared_ptr<const Interest> interest) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 90 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 91 | NS_LOG_FUNCTION(this << interest); |
| 92 | m_receivedInterests(interest, this, m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 96 | App::OnData(shared_ptr<const Data> data) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 97 | { |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 98 | NS_LOG_FUNCTION(this << data); |
| 99 | m_receivedDatas(data, this, m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Application Methods |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 103 | void |
| 104 | App::StartApplication() // Called at time specified by Start |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 105 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 106 | NS_LOG_FUNCTION_NOARGS(); |
| 107 | |
| 108 | NS_ASSERT(m_active != true); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 109 | m_active = true; |
| 110 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 111 | NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0, |
| 112 | "Ndn stack should be installed on the node " << GetNode()); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 113 | |
| 114 | // step 1. Create a face |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 115 | m_face = CreateObject<AppFace>(/*Ptr<App> (this)*/ this); |
| 116 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 117 | // step 2. Add face to the Ndn stack |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 118 | GetNode()->GetObject<L3Protocol>()->AddFace(m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 119 | |
| 120 | // step 3. Enable face |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 121 | m_face->SetUp(true); |
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 | |
| 132 | NS_ASSERT(GetNode()->GetObject<L3Protocol>() != 0); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 133 | |
| 134 | m_active = false; |
| 135 | |
| 136 | // step 1. Disable face |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 137 | m_face->SetUp(false); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 138 | |
| 139 | // step 2. Remove face from Ndn stack |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 140 | GetNode()->GetObject<L3Protocol>()->RemoveFace(m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 141 | |
| 142 | // step 3. Destroy face |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 143 | if (m_face->GetReferenceCount() != 1) { |
| 144 | NS_LOG_ERROR("Please a bug report on https://github.com/NDN-Routing/ndnSIM/issues"); |
| 145 | NS_LOG_ERROR("At this point, nobody else should have referenced this face, but we have " |
| 146 | << m_face->GetReferenceCount() << " references"); |
| 147 | } |
Alexander Afanasyev | f383047 | 2012-10-09 18:23:21 -0700 | [diff] [blame] | 148 | // NS_ASSERT_MSG (m_face->GetReferenceCount ()==2, |
| 149 | // "At this point, nobody else should have referenced this face, but we have " |
| 150 | // << m_face->GetReferenceCount () << " references"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 151 | m_face = 0; |
| 152 | } |
| 153 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 154 | } // namespace ndn |
| 155 | } // namespace ns3 |