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 | |
| 26 | #include "ns3/ndn-interest-header.h" |
| 27 | #include "ns3/ndn-content-object-header.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 | |
| 54 | .AddTraceSource ("ReceivedContentObjects", "ReceivedContentObjects", |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 55 | MakeTraceSourceAccessor (&App::m_receivedContentObjects)) |
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 | |
| 60 | .AddTraceSource ("TransmittedContentObjects", "TransmittedContentObjects", |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 61 | MakeTraceSourceAccessor (&App::m_transmittedContentObjects)) |
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 | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 67 | : m_protocolHandler (0) |
| 68 | , m_active (false) |
| 69 | , m_face (0) |
| 70 | { |
| 71 | } |
| 72 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 73 | App::~App () |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 74 | { |
| 75 | } |
| 76 | |
| 77 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 78 | App::DoDispose (void) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 79 | { |
| 80 | NS_LOG_FUNCTION_NOARGS (); |
| 81 | |
| 82 | // Unfortunately, this causes SEGFAULT |
| 83 | // The best reason I see is that apps are freed after ndn stack is removed |
| 84 | // StopApplication (); |
| 85 | Application::DoDispose (); |
| 86 | } |
| 87 | |
| 88 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 89 | App::RegisterProtocolHandler (ProtocolHandler handler) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 90 | { |
| 91 | m_protocolHandler = handler; |
| 92 | } |
| 93 | |
| 94 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 95 | App::OnInterest (const Ptr<const InterestHeader> &interest, Ptr<Packet> packet) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 96 | { |
| 97 | NS_LOG_FUNCTION (this << interest); |
| 98 | m_receivedInterests (interest, this, m_face); |
| 99 | } |
| 100 | |
| 101 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 102 | App::OnNack (const Ptr<const InterestHeader> &interest, Ptr<Packet> packet) |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 103 | { |
| 104 | NS_LOG_FUNCTION (this << interest); |
| 105 | m_receivedNacks (interest, this, m_face); |
| 106 | } |
| 107 | |
| 108 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 109 | App::OnContentObject (const Ptr<const ContentObjectHeader> &contentObject, |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 110 | Ptr<Packet> payload) |
| 111 | { |
| 112 | NS_LOG_FUNCTION (this << contentObject << payload); |
| 113 | m_receivedContentObjects (contentObject, payload, this, m_face); |
| 114 | } |
| 115 | |
| 116 | // Application Methods |
| 117 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 118 | App::StartApplication () // Called at time specified by Start |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 119 | { |
| 120 | NS_LOG_FUNCTION_NOARGS (); |
| 121 | |
| 122 | NS_ASSERT (m_active != true); |
| 123 | m_active = true; |
| 124 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 125 | NS_ASSERT_MSG (GetNode ()->GetObject<L3Protocol> () != 0, |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 126 | "Ndn stack should be installed on the node " << GetNode ()); |
| 127 | |
| 128 | // step 1. Create a face |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 129 | m_face = CreateObject<AppFace> (/*Ptr<App> (this)*/this); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 130 | |
| 131 | // step 2. Add face to the Ndn stack |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 132 | GetNode ()->GetObject<L3Protocol> ()->AddFace (m_face); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 133 | |
| 134 | // step 3. Enable face |
| 135 | m_face->SetUp (true); |
| 136 | } |
| 137 | |
| 138 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 139 | App::StopApplication () // Called at time specified by Stop |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 140 | { |
| 141 | NS_LOG_FUNCTION_NOARGS (); |
| 142 | |
| 143 | if (!m_active) return; //don't assert here, just return |
| 144 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 145 | NS_ASSERT (GetNode ()->GetObject<L3Protocol> () != 0); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 146 | |
| 147 | m_active = false; |
| 148 | |
| 149 | // step 1. Disable face |
| 150 | m_face->SetUp (false); |
| 151 | |
| 152 | // step 2. Remove face from Ndn stack |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 153 | GetNode ()->GetObject<L3Protocol> ()->RemoveFace (m_face); |
| 154 | GetNode ()->GetObject<Fib> ()->RemoveFromAll (m_face); |
| 155 | GetNode ()->GetObject<ForwardingStrategy> ()->RemoveFace (m_face); // notify that face is removed |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 156 | |
| 157 | // step 3. Destroy face |
Alexander Afanasyev | 08b7d9e | 2012-08-23 10:53:46 -0700 | [diff] [blame] | 158 | NS_ASSERT_MSG (m_face->GetReferenceCount ()==2, |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 159 | "At this point, nobody else should have referenced this face, but we have " |
| 160 | << m_face->GetReferenceCount () << " references"); |
| 161 | m_face = 0; |
| 162 | } |
| 163 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 164 | } // namespace ndn |
| 165 | } // namespace ns3 |