blob: 8cdddac87b2109c4c15bcab289d0b244bb2c97e1 [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 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")
Alexander Afanasyev6315ef72012-06-01 20:56:31 -070043 .SetGroupName ("Ccnx")
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080044 .SetParent<Application> ()
45 .AddConstructor<CcnxApp> ()
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080046
47 .AddTraceSource ("ReceivedInterests", "ReceivedInterests",
48 MakeTraceSourceAccessor (&CcnxApp::m_receivedInterests))
49
50 .AddTraceSource ("ReceivedNacks", "ReceivedNacks",
51 MakeTraceSourceAccessor (&CcnxApp::m_receivedNacks))
52
53 .AddTraceSource ("ReceivedContentObjects", "ReceivedContentObjects",
54 MakeTraceSourceAccessor (&CcnxApp::m_receivedContentObjects))
Alexander Afanasyev15f92992012-04-09 14:56:56 -070055
56 .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
57 MakeTraceSourceAccessor (&CcnxApp::m_transmittedInterests))
58
59 .AddTraceSource ("TransmittedContentObjects", "TransmittedContentObjects",
60 MakeTraceSourceAccessor (&CcnxApp::m_transmittedContentObjects))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080061 ;
62 return tid;
63}
64
65CcnxApp::CcnxApp ()
66 : m_protocolHandler (0)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080067 , m_active (false)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080068 , m_face (0)
69{
70}
71
72CcnxApp::~CcnxApp ()
73{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080074}
75
76void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080077CcnxApp::DoDispose (void)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080078{
79 NS_LOG_FUNCTION_NOARGS ();
80
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080081 // Unfortunately, this causes SEGFAULT
82 // The best reason I see is that apps are freed after ccnx stack is removed
83 // StopApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080084 Application::DoDispose ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080085}
86
87void
88CcnxApp::RegisterProtocolHandler (ProtocolHandler handler)
89{
90 m_protocolHandler = handler;
91}
92
93void
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080094CcnxApp::OnInterest (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080095{
96 NS_LOG_FUNCTION (this << interest);
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080097 m_receivedInterests (interest, this, m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080098}
99
100void
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800101CcnxApp::OnNack (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800102{
103 NS_LOG_FUNCTION (this << interest);
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800104 m_receivedNacks (interest, this, m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105}
106
107void
108CcnxApp::OnContentObject (const Ptr<const CcnxContentObjectHeader> &contentObject,
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800109 Ptr<Packet> payload)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800110{
111 NS_LOG_FUNCTION (this << contentObject << payload);
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800112 m_receivedContentObjects (contentObject, payload, this, m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800113}
114
115// Application Methods
116void
117CcnxApp::StartApplication () // Called at time specified by Start
118{
119 NS_LOG_FUNCTION_NOARGS ();
120
121 NS_ASSERT (m_active != true);
122 m_active = true;
123
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800124 NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () != 0,
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800125 "Ccnx stack should be installed on the node " << GetNode ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800126
127 // step 1. Create a face
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -0700128 m_face = CreateObject<CcnxAppFace> (/*Ptr<CcnxApp> (this)*/this);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800129
130 // step 2. Add face to the CCNx stack
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800131 GetNode ()->GetObject<Ccnx> ()->AddFace (m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800132
133 // step 3. Enable face
134 m_face->SetUp (true);
135}
136
137void
138CcnxApp::StopApplication () // Called at time specified by Stop
139{
140 NS_LOG_FUNCTION_NOARGS ();
141
142 if (!m_active) return; //don't assert here, just return
143
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800144 NS_ASSERT (GetNode ()->GetObject<Ccnx> () != 0);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800145
146 m_active = false;
147
148 // step 1. Disable face
149 m_face->SetUp (false);
150
151 // step 2. Remove face from CCNx stack
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800152 GetNode ()->GetObject<Ccnx> ()->RemoveFace (m_face);
153 GetNode ()->GetObject<CcnxFib> ()->RemoveFromAll (m_face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800154
155 // step 3. Destroy face
156 NS_ASSERT_MSG (m_face->GetReferenceCount ()==1,
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800157 "At this point, nobody else should have referenced this face, but we have "
158 << m_face->GetReferenceCount () << " references");
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800159 m_face = 0;
160}
161
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800162}