blob: 7b9c2b9ff3d06c971c8740195368e936c942efee [file] [log] [blame]
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -08001/* -*- 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 Afanasyev19426ef2011-11-23 20:55:28 -080023#include "ns3/assert.h"
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080024#include "ns3/packet.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080025
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080026#include "ns3/ccnx-interest-header.h"
27#include "ns3/ccnx-content-object-header.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080028#include "ns3/ccnx.h"
29#include "ns3/ccnx-fib.h"
30#include "ns3/ccnx-local-face.h"
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080031
32NS_LOG_COMPONENT_DEFINE ("CcnxApp");
33
34namespace ns3
35{
36
37NS_OBJECT_ENSURE_REGISTERED (CcnxApp);
38
39TypeId
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080040CcnxApp::GetTypeId (void)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080041{
42 static TypeId tid = TypeId ("ns3::CcnxApp")
43 .SetParent<Application> ()
44 .AddConstructor<CcnxApp> ()
45 ;
46 return tid;
47}
48
49CcnxApp::CcnxApp ()
50 : m_protocolHandler (0)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080051 , m_active (false)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080052 , m_face (0)
53{
54}
55
56CcnxApp::~CcnxApp ()
57{
58 StopApplication ();
59}
60
61void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080062CcnxApp::DoDispose (void)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080063{
64 NS_LOG_FUNCTION_NOARGS ();
65
66 StopApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080067 Application::DoDispose ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080068}
69
70void
71CcnxApp::RegisterProtocolHandler (ProtocolHandler handler)
72{
73 m_protocolHandler = handler;
74}
75
76void
77CcnxApp::OnInterest (const Ptr<const CcnxInterestHeader> &interest)
78{
79 NS_LOG_FUNCTION (this << interest);
80}
81
82void
83CcnxApp::OnNack (const Ptr<const CcnxInterestHeader> &interest)
84{
85 NS_LOG_FUNCTION (this << interest);
86}
87
88void
89CcnxApp::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
90 const Ptr<const Packet> &payload)
91{
92 NS_LOG_FUNCTION (this << contentObject << payload);
93}
94
95// Application Methods
96void
97CcnxApp::StartApplication () // Called at time specified by Start
98{
99 NS_LOG_FUNCTION_NOARGS ();
100
101 NS_ASSERT (m_active != true);
102 m_active = true;
103
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800104 NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () != 0,
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105 "Ccnx stack should be installed on the node " << GetNode ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800106
107 // step 1. Create a face
108 m_face = Create<CcnxLocalFace> (/*Ptr<CcnxApp> (this)*/this);
109
110 // step 2. Add face to the CCNx stack
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800111 GetNode ()->GetObject<Ccnx> ()->AddFace (m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800112
113 // step 3. Enable face
114 m_face->SetUp (true);
115}
116
117void
118CcnxApp::StopApplication () // Called at time specified by Stop
119{
120 NS_LOG_FUNCTION_NOARGS ();
121
122 if (!m_active) return; //don't assert here, just return
123
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800124 NS_ASSERT (GetNode ()->GetObject<Ccnx> () != 0);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800125
126 m_active = false;
127
128 // step 1. Disable face
129 m_face->SetUp (false);
130
131 // step 2. Remove face from CCNx stack
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800132 GetNode ()->GetObject<Ccnx> ()->RemoveFace (m_face);
133 GetNode ()->GetObject<CcnxFib> ()->RemoveFromAll (m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800134
135 // step 3. Destroy face
136 NS_ASSERT_MSG (m_face->GetReferenceCount ()==1,
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800137 "At this point, nobody else should have referenced this face, but we have "
138 << m_face->GetReferenceCount () << " references");
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800139 m_face = 0;
140}
141
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800142}