blob: 59da17ce2187dbd5b0af38ada7991aaa5cf646fd [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 Afanasyev4a4ea602012-06-06 11:12:45 -070030#include "ns3/ccnx-app-face.h"
Alexander Afanasyev33364b62012-07-26 17:53:56 -070031#include "ns3/ccnx-forwarding-strategy.h"
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080032
33NS_LOG_COMPONENT_DEFINE ("CcnxApp");
34
35namespace ns3
36{
37
38NS_OBJECT_ENSURE_REGISTERED (CcnxApp);
39
40TypeId
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080041CcnxApp::GetTypeId (void)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080042{
43 static TypeId tid = TypeId ("ns3::CcnxApp")
Alexander Afanasyev6315ef72012-06-01 20:56:31 -070044 .SetGroupName ("Ccnx")
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080045 .SetParent<Application> ()
46 .AddConstructor<CcnxApp> ()
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080047
48 .AddTraceSource ("ReceivedInterests", "ReceivedInterests",
49 MakeTraceSourceAccessor (&CcnxApp::m_receivedInterests))
50
51 .AddTraceSource ("ReceivedNacks", "ReceivedNacks",
52 MakeTraceSourceAccessor (&CcnxApp::m_receivedNacks))
53
54 .AddTraceSource ("ReceivedContentObjects", "ReceivedContentObjects",
55 MakeTraceSourceAccessor (&CcnxApp::m_receivedContentObjects))
Alexander Afanasyev15f92992012-04-09 14:56:56 -070056
57 .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
58 MakeTraceSourceAccessor (&CcnxApp::m_transmittedInterests))
59
60 .AddTraceSource ("TransmittedContentObjects", "TransmittedContentObjects",
61 MakeTraceSourceAccessor (&CcnxApp::m_transmittedContentObjects))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080062 ;
63 return tid;
64}
65
66CcnxApp::CcnxApp ()
67 : m_protocolHandler (0)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080068 , m_active (false)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080069 , m_face (0)
70{
71}
72
73CcnxApp::~CcnxApp ()
74{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080075}
76
77void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080078CcnxApp::DoDispose (void)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080079{
80 NS_LOG_FUNCTION_NOARGS ();
81
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080082 // Unfortunately, this causes SEGFAULT
83 // The best reason I see is that apps are freed after ccnx stack is removed
84 // StopApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080085 Application::DoDispose ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080086}
87
88void
89CcnxApp::RegisterProtocolHandler (ProtocolHandler handler)
90{
91 m_protocolHandler = handler;
92}
93
94void
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080095CcnxApp::OnInterest (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080096{
97 NS_LOG_FUNCTION (this << interest);
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080098 m_receivedInterests (interest, this, m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099}
100
101void
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800102CcnxApp::OnNack (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800103{
104 NS_LOG_FUNCTION (this << interest);
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800105 m_receivedNacks (interest, this, m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800106}
107
108void
109CcnxApp::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800110 Ptr<Packet> payload)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800111{
112 NS_LOG_FUNCTION (this << contentObject << payload);
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800113 m_receivedContentObjects (contentObject, payload, this, m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800114}
115
116// Application Methods
117void
118CcnxApp::StartApplication () // Called at time specified by Start
119{
120 NS_LOG_FUNCTION_NOARGS ();
121
122 NS_ASSERT (m_active != true);
123 m_active = true;
124
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800125 NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () != 0,
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800126 "Ccnx stack should be installed on the node " << GetNode ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800127
128 // step 1. Create a face
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -0700129 m_face = CreateObject<CcnxAppFace> (/*Ptr<CcnxApp> (this)*/this);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800130
131 // step 2. Add face to the CCNx stack
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800132 GetNode ()->GetObject<Ccnx> ()->AddFace (m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800133
134 // step 3. Enable face
135 m_face->SetUp (true);
136}
137
138void
139CcnxApp::StopApplication () // Called at time specified by Stop
140{
141 NS_LOG_FUNCTION_NOARGS ();
142
143 if (!m_active) return; //don't assert here, just return
144
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800145 NS_ASSERT (GetNode ()->GetObject<Ccnx> () != 0);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800146
147 m_active = false;
148
149 // step 1. Disable face
150 m_face->SetUp (false);
151
152 // step 2. Remove face from CCNx stack
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800153 GetNode ()->GetObject<Ccnx> ()->RemoveFace (m_face);
154 GetNode ()->GetObject<CcnxFib> ()->RemoveFromAll (m_face);
Alexander Afanasyev33364b62012-07-26 17:53:56 -0700155 GetNode ()->GetObject<CcnxForwardingStrategy> ()->RemoveFace (m_face); // notify that face is removed
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800156
157 // step 3. Destroy face
158 NS_ASSERT_MSG (m_face->GetReferenceCount ()==1,
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800159 "At this point, nobody else should have referenced this face, but we have "
160 << m_face->GetReferenceCount () << " references");
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800161 m_face = 0;
162}
163
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164}