blob: 54ea5c67898d1c4a6efd848458096d79afdc4538 [file] [log] [blame]
Alexander Afanasyev4aac5572012-08-09 10:49:55 -07001/* -*- 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 "ndn-app.h"
22#include "ns3/log.h"
23#include "ns3/assert.h"
24#include "ns3/packet.h"
25
26#include "ns3/ndn-interest-header.h"
27#include "ns3/ndn-content-object-header.h"
28#include "ns3/ndn.h"
29#include "ns3/ndn-fib.h"
30#include "ns3/ndn-app-face.h"
31#include "ns3/ndn-forwarding-strategy.h"
32
33NS_LOG_COMPONENT_DEFINE ("NdnApp");
34
35namespace ns3
36{
37
38NS_OBJECT_ENSURE_REGISTERED (NdnApp);
39
40TypeId
41NdnApp::GetTypeId (void)
42{
43 static TypeId tid = TypeId ("ns3::NdnApp")
44 .SetGroupName ("Ndn")
45 .SetParent<Application> ()
46 .AddConstructor<NdnApp> ()
47
48 .AddTraceSource ("ReceivedInterests", "ReceivedInterests",
49 MakeTraceSourceAccessor (&NdnApp::m_receivedInterests))
50
51 .AddTraceSource ("ReceivedNacks", "ReceivedNacks",
52 MakeTraceSourceAccessor (&NdnApp::m_receivedNacks))
53
54 .AddTraceSource ("ReceivedContentObjects", "ReceivedContentObjects",
55 MakeTraceSourceAccessor (&NdnApp::m_receivedContentObjects))
56
57 .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
58 MakeTraceSourceAccessor (&NdnApp::m_transmittedInterests))
59
60 .AddTraceSource ("TransmittedContentObjects", "TransmittedContentObjects",
61 MakeTraceSourceAccessor (&NdnApp::m_transmittedContentObjects))
62 ;
63 return tid;
64}
65
66NdnApp::NdnApp ()
67 : m_protocolHandler (0)
68 , m_active (false)
69 , m_face (0)
70{
71}
72
73NdnApp::~NdnApp ()
74{
75}
76
77void
78NdnApp::DoDispose (void)
79{
80 NS_LOG_FUNCTION_NOARGS ();
81
82 // Unfortunately, this causes SEGFAULT
83 // The best reason I see is that apps are freed after ndn stack is removed
84 // StopApplication ();
85 Application::DoDispose ();
86}
87
88void
89NdnApp::RegisterProtocolHandler (ProtocolHandler handler)
90{
91 m_protocolHandler = handler;
92}
93
94void
95NdnApp::OnInterest (const Ptr<const NdnInterestHeader> &interest, Ptr<Packet> packet)
96{
97 NS_LOG_FUNCTION (this << interest);
98 m_receivedInterests (interest, this, m_face);
99}
100
101void
102NdnApp::OnNack (const Ptr<const NdnInterestHeader> &interest, Ptr<Packet> packet)
103{
104 NS_LOG_FUNCTION (this << interest);
105 m_receivedNacks (interest, this, m_face);
106}
107
108void
109NdnApp::OnContentObject (const Ptr<const NdnContentObjectHeader> &contentObject,
110 Ptr<Packet> payload)
111{
112 NS_LOG_FUNCTION (this << contentObject << payload);
113 m_receivedContentObjects (contentObject, payload, this, m_face);
114}
115
116// Application Methods
117void
118NdnApp::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
125 NS_ASSERT_MSG (GetNode ()->GetObject<Ndn> () != 0,
126 "Ndn stack should be installed on the node " << GetNode ());
127
128 // step 1. Create a face
129 m_face = CreateObject<NdnAppFace> (/*Ptr<NdnApp> (this)*/this);
130
131 // step 2. Add face to the Ndn stack
132 GetNode ()->GetObject<Ndn> ()->AddFace (m_face);
133
134 // step 3. Enable face
135 m_face->SetUp (true);
136}
137
138void
139NdnApp::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
145 NS_ASSERT (GetNode ()->GetObject<Ndn> () != 0);
146
147 m_active = false;
148
149 // step 1. Disable face
150 m_face->SetUp (false);
151
152 // step 2. Remove face from Ndn stack
153 GetNode ()->GetObject<Ndn> ()->RemoveFace (m_face);
154 GetNode ()->GetObject<NdnFib> ()->RemoveFromAll (m_face);
155 GetNode ()->GetObject<NdnForwardingStrategy> ()->RemoveFace (m_face); // notify that face is removed
156
157 // step 3. Destroy face
158 NS_ASSERT_MSG (m_face->GetReferenceCount ()==1,
159 "At this point, nobody else should have referenced this face, but we have "
160 << m_face->GetReferenceCount () << " references");
161 m_face = 0;
162}
163
164}