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 | |
| 21 | #include "ndn-app.h" |
| 22 | #include "ns3/log.h" |
| 23 | #include "ns3/assert.h" |
| 24 | #include "ns3/packet.h" |
| 25 | |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 26 | #include "ns3/ndn-interest.h" |
| 27 | #include "ns3/ndn-content-object.h" |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 28 | #include "ns3/ndn-l3-protocol.h" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 29 | #include "ns3/ndn-fib.h" |
| 30 | #include "ns3/ndn-app-face.h" |
| 31 | #include "ns3/ndn-forwarding-strategy.h" |
| 32 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 33 | NS_LOG_COMPONENT_DEFINE ("ndn.App"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 35 | namespace ns3 { |
| 36 | namespace ndn { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 37 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 38 | NS_OBJECT_ENSURE_REGISTERED (App); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 39 | |
| 40 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 41 | App::GetTypeId (void) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 42 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 43 | static TypeId tid = TypeId ("ns3::ndn::App") |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 44 | .SetGroupName ("Ndn") |
| 45 | .SetParent<Application> () |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 46 | .AddConstructor<App> () |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 47 | |
| 48 | .AddTraceSource ("ReceivedInterests", "ReceivedInterests", |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 49 | MakeTraceSourceAccessor (&App::m_receivedInterests)) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 50 | |
| 51 | .AddTraceSource ("ReceivedNacks", "ReceivedNacks", |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 52 | MakeTraceSourceAccessor (&App::m_receivedNacks)) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 53 | |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame^] | 54 | .AddTraceSource ("ReceivedDatas", "ReceivedDatas", |
| 55 | MakeTraceSourceAccessor (&App::m_receivedDatas)) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 56 | |
| 57 | .AddTraceSource ("TransmittedInterests", "TransmittedInterests", |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 58 | MakeTraceSourceAccessor (&App::m_transmittedInterests)) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame^] | 60 | .AddTraceSource ("TransmittedDatas", "TransmittedDatas", |
| 61 | MakeTraceSourceAccessor (&App::m_transmittedDatas)) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 62 | ; |
| 63 | return tid; |
| 64 | } |
| 65 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 66 | App::App () |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 67 | : m_active (false) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 68 | , m_face (0) |
| 69 | { |
| 70 | } |
| 71 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 72 | App::~App () |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 73 | { |
| 74 | } |
| 75 | |
| 76 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 77 | App::DoDispose (void) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 78 | { |
| 79 | NS_LOG_FUNCTION_NOARGS (); |
| 80 | |
| 81 | // Unfortunately, this causes SEGFAULT |
| 82 | // The best reason I see is that apps are freed after ndn stack is removed |
| 83 | // StopApplication (); |
| 84 | Application::DoDispose (); |
| 85 | } |
| 86 | |
Alexander Afanasyev | db64ff1 | 2013-01-18 16:37:31 -0800 | [diff] [blame] | 87 | uint32_t |
| 88 | App::GetId () const |
| 89 | { |
| 90 | if (m_face == 0) |
| 91 | return (uint32_t)-1; |
| 92 | else |
| 93 | return m_face->GetId (); |
| 94 | } |
| 95 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 96 | void |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 97 | App::OnInterest (Ptr<const Interest> interest) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 98 | { |
| 99 | NS_LOG_FUNCTION (this << interest); |
| 100 | m_receivedInterests (interest, this, m_face); |
| 101 | } |
| 102 | |
| 103 | void |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 104 | App::OnNack (Ptr<const Interest> interest) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 105 | { |
| 106 | NS_LOG_FUNCTION (this << interest); |
| 107 | m_receivedNacks (interest, this, m_face); |
| 108 | } |
| 109 | |
| 110 | void |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame^] | 111 | App::OnData (Ptr<const Data> contentObject) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 112 | { |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 113 | NS_LOG_FUNCTION (this << contentObject); |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame^] | 114 | m_receivedDatas (contentObject, this, m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Application Methods |
| 118 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 119 | App::StartApplication () // Called at time specified by Start |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 120 | { |
| 121 | NS_LOG_FUNCTION_NOARGS (); |
| 122 | |
| 123 | NS_ASSERT (m_active != true); |
| 124 | m_active = true; |
| 125 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 126 | NS_ASSERT_MSG (GetNode ()->GetObject<L3Protocol> () != 0, |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 127 | "Ndn stack should be installed on the node " << GetNode ()); |
| 128 | |
| 129 | // step 1. Create a face |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 130 | m_face = CreateObject<AppFace> (/*Ptr<App> (this)*/this); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 131 | |
| 132 | // step 2. Add face to the Ndn stack |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 133 | GetNode ()->GetObject<L3Protocol> ()->AddFace (m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 134 | |
| 135 | // step 3. Enable face |
| 136 | m_face->SetUp (true); |
| 137 | } |
| 138 | |
| 139 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 140 | App::StopApplication () // Called at time specified by Stop |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 141 | { |
| 142 | NS_LOG_FUNCTION_NOARGS (); |
| 143 | |
| 144 | if (!m_active) return; //don't assert here, just return |
| 145 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 146 | NS_ASSERT (GetNode ()->GetObject<L3Protocol> () != 0); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 147 | |
| 148 | m_active = false; |
| 149 | |
| 150 | // step 1. Disable face |
| 151 | m_face->SetUp (false); |
| 152 | |
| 153 | // step 2. Remove face from Ndn stack |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 154 | GetNode ()->GetObject<L3Protocol> ()->RemoveFace (m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 155 | |
| 156 | // step 3. Destroy face |
Alexander Afanasyev | f383047 | 2012-10-09 18:23:21 -0700 | [diff] [blame] | 157 | if (m_face->GetReferenceCount () != 1) |
| 158 | { |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 159 | NS_LOG_ERROR ("Please a bug report on https://github.com/NDN-Routing/ndnSIM/issues"); |
Alexander Afanasyev | f383047 | 2012-10-09 18:23:21 -0700 | [diff] [blame] | 160 | NS_LOG_ERROR ("At this point, nobody else should have referenced this face, but we have " |
| 161 | << m_face->GetReferenceCount () << " references"); |
| 162 | |
| 163 | } |
| 164 | // NS_ASSERT_MSG (m_face->GetReferenceCount ()==2, |
| 165 | // "At this point, nobody else should have referenced this face, but we have " |
| 166 | // << m_face->GetReferenceCount () << " references"); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 167 | m_face = 0; |
| 168 | } |
| 169 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 170 | } // namespace ndn |
| 171 | } // namespace ns3 |