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" |
| 23 | #include "ns3/packet.h" |
| 24 | #include "ns3/ccnx-interest-header.h" |
| 25 | #include "ns3/ccnx-content-object-header.h" |
| 26 | |
| 27 | NS_LOG_COMPONENT_DEFINE ("CcnxApp"); |
| 28 | |
| 29 | namespace ns3 |
| 30 | { |
| 31 | |
| 32 | NS_OBJECT_ENSURE_REGISTERED (CcnxApp); |
| 33 | |
| 34 | TypeId |
| 35 | CcnxConsumer::GetTypeId (void) |
| 36 | { |
| 37 | static TypeId tid = TypeId ("ns3::CcnxApp") |
| 38 | .SetParent<Application> () |
| 39 | .AddConstructor<CcnxApp> () |
| 40 | ; |
| 41 | return tid; |
| 42 | } |
| 43 | |
| 44 | CcnxApp::CcnxApp () |
| 45 | : m_protocolHandler (0) |
| 46 | , m_active (true) |
| 47 | , m_face (0) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | CcnxApp::~CcnxApp () |
| 52 | { |
| 53 | StopApplication (); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | CcnxProducer::DoDispose (void) |
| 58 | { |
| 59 | NS_LOG_FUNCTION_NOARGS (); |
| 60 | |
| 61 | StopApplication (); |
| 62 | CcnxApp::DoDispose (); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | CcnxApp::RegisterProtocolHandler (ProtocolHandler handler) |
| 67 | { |
| 68 | m_protocolHandler = handler; |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | CcnxApp::OnInterest (const Ptr<const CcnxInterestHeader> &interest) |
| 73 | { |
| 74 | NS_LOG_FUNCTION (this << interest); |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | CcnxApp::OnNack (const Ptr<const CcnxInterestHeader> &interest) |
| 79 | { |
| 80 | NS_LOG_FUNCTION (this << interest); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | CcnxApp::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject, |
| 85 | const Ptr<const Packet> &payload) |
| 86 | { |
| 87 | NS_LOG_FUNCTION (this << contentObject << payload); |
| 88 | } |
| 89 | |
| 90 | // Application Methods |
| 91 | void |
| 92 | CcnxApp::StartApplication () // Called at time specified by Start |
| 93 | { |
| 94 | NS_LOG_FUNCTION_NOARGS (); |
| 95 | |
| 96 | NS_ASSERT (m_active != true); |
| 97 | m_active = true; |
| 98 | |
| 99 | NS_ASSERT_MSG (GetObject<Ccnx> () != 0, |
| 100 | "Ccnx stack should be installed on the node " << GetNode ()); |
| 101 | NS_ASSERT_MSG (GetObject<CcnxFib> () != 0); |
| 102 | |
| 103 | // step 1. Create a face |
| 104 | m_face = Create<CcnxLocalFace> (/*Ptr<CcnxApp> (this)*/this); |
| 105 | |
| 106 | // step 2. Add face to the CCNx stack |
| 107 | GetObject<CcnxFib> ()->Add (m_prefix, m_face, 0); |
| 108 | GetObject<Ccnx> ()->AddFace (m_face); |
| 109 | |
| 110 | // step 3. Enable face |
| 111 | m_face->SetUp (true); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | CcnxApp::StopApplication () // Called at time specified by Stop |
| 116 | { |
| 117 | NS_LOG_FUNCTION_NOARGS (); |
| 118 | |
| 119 | if (!m_active) return; //don't assert here, just return |
| 120 | |
| 121 | NS_ASSERT (GetObject<Ccnx> () != 0); |
| 122 | NS_ASSERT (GetObject<CcnxFib> () != 0); |
| 123 | |
| 124 | m_active = false; |
| 125 | |
| 126 | // step 1. Disable face |
| 127 | m_face->SetUp (false); |
| 128 | |
| 129 | // step 2. Remove face from CCNx stack |
| 130 | GetObject<Ccnx> ()->RemoveFace (m_face); |
| 131 | GetObject<CcnxFib> ()->Add (m_prefix, m_face, 0); |
| 132 | |
| 133 | // step 3. Destroy face |
| 134 | NS_ASSERT_MSG (m_face->GetReferenceCount ()==1, |
| 135 | "At this point, nobody else should have referenced this face"); |
| 136 | m_face = 0; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | } |