blob: afdf9dee1c5b5774434200f1d261f1fe0ecc63be [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{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080058}
59
60void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080061CcnxApp::DoDispose (void)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080062{
63 NS_LOG_FUNCTION_NOARGS ();
64
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080065 // Unfortunately, this causes SEGFAULT
66 // The best reason I see is that apps are freed after ccnx stack is removed
67 // StopApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080068 Application::DoDispose ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080069}
70
71void
72CcnxApp::RegisterProtocolHandler (ProtocolHandler handler)
73{
74 m_protocolHandler = handler;
75}
76
77void
78CcnxApp::OnInterest (const Ptr<const CcnxInterestHeader> &interest)
79{
80 NS_LOG_FUNCTION (this << interest);
81}
82
83void
84CcnxApp::OnNack (const Ptr<const CcnxInterestHeader> &interest)
85{
86 NS_LOG_FUNCTION (this << interest);
87}
88
89void
90CcnxApp::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
91 const Ptr<const Packet> &payload)
92{
93 NS_LOG_FUNCTION (this << contentObject << payload);
94}
95
96// Application Methods
97void
98CcnxApp::StartApplication () // Called at time specified by Start
99{
100 NS_LOG_FUNCTION_NOARGS ();
101
102 NS_ASSERT (m_active != true);
103 m_active = true;
104
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800105 NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () != 0,
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800106 "Ccnx stack should be installed on the node " << GetNode ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800107
108 // step 1. Create a face
109 m_face = Create<CcnxLocalFace> (/*Ptr<CcnxApp> (this)*/this);
110
111 // step 2. Add face to the CCNx stack
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800112 GetNode ()->GetObject<Ccnx> ()->AddFace (m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800113
114 // step 3. Enable face
115 m_face->SetUp (true);
116}
117
118void
119CcnxApp::StopApplication () // Called at time specified by Stop
120{
121 NS_LOG_FUNCTION_NOARGS ();
122
123 if (!m_active) return; //don't assert here, just return
124
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800125 NS_ASSERT (GetNode ()->GetObject<Ccnx> () != 0);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800126
127 m_active = false;
128
129 // step 1. Disable face
130 m_face->SetUp (false);
131
132 // step 2. Remove face from CCNx stack
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800133 GetNode ()->GetObject<Ccnx> ()->RemoveFace (m_face);
134 GetNode ()->GetObject<CcnxFib> ()->RemoveFromAll (m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800135
136 // step 3. Destroy face
137 NS_ASSERT_MSG (m_face->GetReferenceCount ()==1,
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800138 "At this point, nobody else should have referenced this face, but we have "
139 << m_face->GetReferenceCount () << " references");
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800140 m_face = 0;
141}
142
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800143}