Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [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 "ccnx-app.h" |
| 22 | #include "ns3/log.h" |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 23 | #include "ns3/assert.h" |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 24 | #include "ns3/packet.h" |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 26 | #include "ns3/ccnx-interest-header.h" |
| 27 | #include "ns3/ccnx-content-object-header.h" |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 28 | #include "ns3/ccnx.h" |
| 29 | #include "ns3/ccnx-fib.h" |
Alexander Afanasyev | f9f4eb0 | 2011-12-16 01:51:14 -0800 | [diff] [blame] | 30 | #include "../model/ccnx-local-face.h" |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 31 | |
| 32 | NS_LOG_COMPONENT_DEFINE ("CcnxApp"); |
| 33 | |
| 34 | namespace ns3 |
| 35 | { |
| 36 | |
| 37 | NS_OBJECT_ENSURE_REGISTERED (CcnxApp); |
| 38 | |
| 39 | TypeId |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 40 | CcnxApp::GetTypeId (void) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 41 | { |
| 42 | static TypeId tid = TypeId ("ns3::CcnxApp") |
| 43 | .SetParent<Application> () |
| 44 | .AddConstructor<CcnxApp> () |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 45 | |
| 46 | .AddTraceSource ("ReceivedInterests", "ReceivedInterests", |
| 47 | MakeTraceSourceAccessor (&CcnxApp::m_receivedInterests)) |
| 48 | |
| 49 | .AddTraceSource ("ReceivedNacks", "ReceivedNacks", |
| 50 | MakeTraceSourceAccessor (&CcnxApp::m_receivedNacks)) |
| 51 | |
| 52 | .AddTraceSource ("ReceivedContentObjects", "ReceivedContentObjects", |
| 53 | MakeTraceSourceAccessor (&CcnxApp::m_receivedContentObjects)) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 54 | ; |
| 55 | return tid; |
| 56 | } |
| 57 | |
| 58 | CcnxApp::CcnxApp () |
| 59 | : m_protocolHandler (0) |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 60 | , m_active (false) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 61 | , m_face (0) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | CcnxApp::~CcnxApp () |
| 66 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 70 | CcnxApp::DoDispose (void) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 71 | { |
| 72 | NS_LOG_FUNCTION_NOARGS (); |
| 73 | |
Alexander Afanasyev | 4a5c2c1 | 2011-12-12 18:50:57 -0800 | [diff] [blame] | 74 | // Unfortunately, this causes SEGFAULT |
| 75 | // The best reason I see is that apps are freed after ccnx stack is removed |
| 76 | // StopApplication (); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 77 | Application::DoDispose (); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
| 81 | CcnxApp::RegisterProtocolHandler (ProtocolHandler handler) |
| 82 | { |
| 83 | m_protocolHandler = handler; |
| 84 | } |
| 85 | |
| 86 | void |
Alexander Afanasyev | e9c9d72 | 2012-01-19 16:59:30 -0800 | [diff] [blame] | 87 | CcnxApp::OnInterest (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> packet) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 88 | { |
| 89 | NS_LOG_FUNCTION (this << interest); |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 90 | m_receivedInterests (interest, this, m_face); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void |
Alexander Afanasyev | e9c9d72 | 2012-01-19 16:59:30 -0800 | [diff] [blame] | 94 | CcnxApp::OnNack (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> packet) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 95 | { |
| 96 | NS_LOG_FUNCTION (this << interest); |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 97 | m_receivedNacks (interest, this, m_face); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
| 101 | CcnxApp::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject, |
Alexander Afanasyev | e9c9d72 | 2012-01-19 16:59:30 -0800 | [diff] [blame] | 102 | Ptr<Packet> payload) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 103 | { |
| 104 | NS_LOG_FUNCTION (this << contentObject << payload); |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 105 | m_receivedContentObjects (contentObject, payload, this, m_face); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Application Methods |
| 109 | void |
| 110 | CcnxApp::StartApplication () // Called at time specified by Start |
| 111 | { |
| 112 | NS_LOG_FUNCTION_NOARGS (); |
| 113 | |
| 114 | NS_ASSERT (m_active != true); |
| 115 | m_active = true; |
| 116 | |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 117 | NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () != 0, |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 118 | "Ccnx stack should be installed on the node " << GetNode ()); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 119 | |
| 120 | // step 1. Create a face |
Alexander Afanasyev | cbe92ae | 2011-12-16 13:06:18 -0800 | [diff] [blame] | 121 | m_face = CreateObject<CcnxLocalFace> (/*Ptr<CcnxApp> (this)*/this); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 122 | |
| 123 | // step 2. Add face to the CCNx stack |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 124 | GetNode ()->GetObject<Ccnx> ()->AddFace (m_face); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 125 | |
| 126 | // step 3. Enable face |
| 127 | m_face->SetUp (true); |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | CcnxApp::StopApplication () // Called at time specified by Stop |
| 132 | { |
| 133 | NS_LOG_FUNCTION_NOARGS (); |
| 134 | |
| 135 | if (!m_active) return; //don't assert here, just return |
| 136 | |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 137 | NS_ASSERT (GetNode ()->GetObject<Ccnx> () != 0); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 138 | |
| 139 | m_active = false; |
| 140 | |
| 141 | // step 1. Disable face |
| 142 | m_face->SetUp (false); |
| 143 | |
| 144 | // step 2. Remove face from CCNx stack |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 145 | GetNode ()->GetObject<Ccnx> ()->RemoveFace (m_face); |
| 146 | GetNode ()->GetObject<CcnxFib> ()->RemoveFromAll (m_face); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 147 | |
| 148 | // step 3. Destroy face |
| 149 | NS_ASSERT_MSG (m_face->GetReferenceCount ()==1, |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 150 | "At this point, nobody else should have referenced this face, but we have " |
| 151 | << m_face->GetReferenceCount () << " references"); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 152 | m_face = 0; |
| 153 | } |
| 154 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 155 | } |