blob: dcf164d5ac790b097b477c2f30632712903eeb7d [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
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080026#include "ns3/ndn-interest.h"
27#include "ns3/ndn-content-object.h"
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070028#include "ns3/ndn-l3-protocol.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070029#include "ns3/ndn-fib.h"
30#include "ns3/ndn-app-face.h"
31#include "ns3/ndn-forwarding-strategy.h"
32
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033NS_LOG_COMPONENT_DEFINE ("ndn.App");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070034
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070035namespace ns3 {
36namespace ndn {
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070037
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070038NS_OBJECT_ENSURE_REGISTERED (App);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070039
40TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041App::GetTypeId (void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070042{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043 static TypeId tid = TypeId ("ns3::ndn::App")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070044 .SetGroupName ("Ndn")
45 .SetParent<Application> ()
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046 .AddConstructor<App> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070047
48 .AddTraceSource ("ReceivedInterests", "ReceivedInterests",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049 MakeTraceSourceAccessor (&App::m_receivedInterests))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070050
51 .AddTraceSource ("ReceivedNacks", "ReceivedNacks",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070052 MakeTraceSourceAccessor (&App::m_receivedNacks))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070053
54 .AddTraceSource ("ReceivedContentObjects", "ReceivedContentObjects",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070055 MakeTraceSourceAccessor (&App::m_receivedContentObjects))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070056
57 .AddTraceSource ("TransmittedInterests", "TransmittedInterests",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058 MakeTraceSourceAccessor (&App::m_transmittedInterests))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070059
60 .AddTraceSource ("TransmittedContentObjects", "TransmittedContentObjects",
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070061 MakeTraceSourceAccessor (&App::m_transmittedContentObjects))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070062 ;
63 return tid;
64}
65
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070066App::App ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070067 : m_protocolHandler (0)
68 , m_active (false)
69 , m_face (0)
70{
71}
72
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073App::~App ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070074{
75}
76
77void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070078App::DoDispose (void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070079{
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
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089App::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070090{
91 m_protocolHandler = handler;
92}
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080093
94uint32_t
95App::GetId () const
96{
97 if (m_face == 0)
98 return (uint32_t)-1;
99 else
100 return m_face->GetId ();
101}
102
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700103void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700104App::OnInterest (const Ptr<const InterestHeader> &interest, Ptr<Packet> packet)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700105{
106 NS_LOG_FUNCTION (this << interest);
107 m_receivedInterests (interest, this, m_face);
108}
109
110void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700111App::OnNack (const Ptr<const InterestHeader> &interest, Ptr<Packet> packet)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700112{
113 NS_LOG_FUNCTION (this << interest);
114 m_receivedNacks (interest, this, m_face);
115}
116
117void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700118App::OnContentObject (const Ptr<const ContentObjectHeader> &contentObject,
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700119 Ptr<Packet> payload)
120{
121 NS_LOG_FUNCTION (this << contentObject << payload);
122 m_receivedContentObjects (contentObject, payload, this, m_face);
123}
124
125// Application Methods
126void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700127App::StartApplication () // Called at time specified by Start
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700128{
129 NS_LOG_FUNCTION_NOARGS ();
130
131 NS_ASSERT (m_active != true);
132 m_active = true;
133
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700134 NS_ASSERT_MSG (GetNode ()->GetObject<L3Protocol> () != 0,
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700135 "Ndn stack should be installed on the node " << GetNode ());
136
137 // step 1. Create a face
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700138 m_face = CreateObject<AppFace> (/*Ptr<App> (this)*/this);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700139
140 // step 2. Add face to the Ndn stack
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700141 GetNode ()->GetObject<L3Protocol> ()->AddFace (m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700142
143 // step 3. Enable face
144 m_face->SetUp (true);
145}
146
147void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700148App::StopApplication () // Called at time specified by Stop
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700149{
150 NS_LOG_FUNCTION_NOARGS ();
151
152 if (!m_active) return; //don't assert here, just return
153
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700154 NS_ASSERT (GetNode ()->GetObject<L3Protocol> () != 0);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700155
156 m_active = false;
157
158 // step 1. Disable face
159 m_face->SetUp (false);
160
161 // step 2. Remove face from Ndn stack
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700162 GetNode ()->GetObject<L3Protocol> ()->RemoveFace (m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700163
164 // step 3. Destroy face
Alexander Afanasyevf3830472012-10-09 18:23:21 -0700165 if (m_face->GetReferenceCount () != 1)
166 {
167 NS_LOG_ERROR ("Please a bug report on https://github.com/NDN-Routing/ndnSIM/issues:");
168 NS_LOG_ERROR ("At this point, nobody else should have referenced this face, but we have "
169 << m_face->GetReferenceCount () << " references");
170
171 }
172 // NS_ASSERT_MSG (m_face->GetReferenceCount ()==2,
173 // "At this point, nobody else should have referenced this face, but we have "
174 // << m_face->GetReferenceCount () << " references");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700175 m_face = 0;
176}
177
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700178} // namespace ndn
179} // namespace ns3