blob: ef8119746a8114e0af2ac774d1d4b723d0668f6a [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"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080030#include "../model/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> ()
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080045
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 Afanasyev15f92992012-04-09 14:56:56 -070054
55 .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
56 MakeTraceSourceAccessor (&CcnxApp::m_transmittedInterests))
57
58 .AddTraceSource ("TransmittedContentObjects", "TransmittedContentObjects",
59 MakeTraceSourceAccessor (&CcnxApp::m_transmittedContentObjects))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080060 ;
61 return tid;
62}
63
64CcnxApp::CcnxApp ()
65 : m_protocolHandler (0)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080066 , m_active (false)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080067 , m_face (0)
68{
69}
70
71CcnxApp::~CcnxApp ()
72{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080073}
74
75void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080076CcnxApp::DoDispose (void)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080077{
78 NS_LOG_FUNCTION_NOARGS ();
79
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080080 // Unfortunately, this causes SEGFAULT
81 // The best reason I see is that apps are freed after ccnx stack is removed
82 // StopApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080083 Application::DoDispose ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080084}
85
86void
87CcnxApp::RegisterProtocolHandler (ProtocolHandler handler)
88{
89 m_protocolHandler = handler;
90}
91
92void
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080093CcnxApp::OnInterest (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080094{
95 NS_LOG_FUNCTION (this << interest);
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080096 m_receivedInterests (interest, this, m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080097}
98
99void
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800100CcnxApp::OnNack (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800101{
102 NS_LOG_FUNCTION (this << interest);
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800103 m_receivedNacks (interest, this, m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800104}
105
106void
107CcnxApp::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800108 Ptr<Packet> payload)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109{
110 NS_LOG_FUNCTION (this << contentObject << payload);
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800111 m_receivedContentObjects (contentObject, payload, this, m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800112}
113
114// Application Methods
115void
116CcnxApp::StartApplication () // Called at time specified by Start
117{
118 NS_LOG_FUNCTION_NOARGS ();
119
120 NS_ASSERT (m_active != true);
121 m_active = true;
122
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800123 NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () != 0,
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800124 "Ccnx stack should be installed on the node " << GetNode ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800125
126 // step 1. Create a face
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800127 m_face = CreateObject<CcnxLocalFace> (/*Ptr<CcnxApp> (this)*/this);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800128
129 // step 2. Add face to the CCNx stack
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800130 GetNode ()->GetObject<Ccnx> ()->AddFace (m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800131
132 // step 3. Enable face
133 m_face->SetUp (true);
134}
135
136void
137CcnxApp::StopApplication () // Called at time specified by Stop
138{
139 NS_LOG_FUNCTION_NOARGS ();
140
141 if (!m_active) return; //don't assert here, just return
142
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800143 NS_ASSERT (GetNode ()->GetObject<Ccnx> () != 0);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800144
145 m_active = false;
146
147 // step 1. Disable face
148 m_face->SetUp (false);
149
150 // step 2. Remove face from CCNx stack
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800151 GetNode ()->GetObject<Ccnx> ()->RemoveFace (m_face);
152 GetNode ()->GetObject<CcnxFib> ()->RemoveFromAll (m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800153
154 // step 3. Destroy face
155 NS_ASSERT_MSG (m_face->GetReferenceCount ()==1,
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800156 "At this point, nobody else should have referenced this face, but we have "
157 << m_face->GetReferenceCount () << " references");
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800158 m_face = 0;
159}
160
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800161}