blob: f353e6256bcc8b55f047fcffb7bfb8f697a079f0 [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
Alexander Afanasyev0c395372014-12-20 15:54:02 -080021#include "ndn-app.hpp"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ns3/log.h"
23#include "ns3/assert.h"
24#include "ns3/packet.h"
25
Alexander Afanasyev0c395372014-12-20 15:54:02 -080026#include "ns3/ndn-interest.hpp"
27#include "ns3/ndn-data.hpp"
28#include "ns3/ndn-l3-protocol.hpp"
29#include "ns3/ndn-fib.hpp"
30#include "ns3/ndn-app-face.hpp"
31#include "ns3/ndn-forwarding-strategy.hpp"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070032
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080033NS_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 Afanasyevbe55cf62014-12-20 17:51:09 -080037
38NS_OBJECT_ENSURE_REGISTERED(App);
39
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070040TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041App::GetTypeId(void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070042{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 static TypeId tid = TypeId("ns3::ndn::App")
44 .SetGroupName("Ndn")
45 .SetParent<Application>()
46 .AddConstructor<App>()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070047
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048 .AddTraceSource("ReceivedInterests", "ReceivedInterests",
49 MakeTraceSourceAccessor(&App::m_receivedInterests))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070050
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 .AddTraceSource("ReceivedNacks", "ReceivedNacks",
52 MakeTraceSourceAccessor(&App::m_receivedNacks))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070053
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054 .AddTraceSource("ReceivedDatas", "ReceivedDatas",
55 MakeTraceSourceAccessor(&App::m_receivedDatas))
56
57 .AddTraceSource("TransmittedInterests", "TransmittedInterests",
58 MakeTraceSourceAccessor(&App::m_transmittedInterests))
59
60 .AddTraceSource("TransmittedDatas", "TransmittedDatas",
61 MakeTraceSourceAccessor(&App::m_transmittedDatas));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070062 return tid;
63}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064
65App::App()
66 : m_active(false)
67 , m_face(0)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070068{
69}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070
71App::~App()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070072{
73}
74
75void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076App::DoDispose(void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070077{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070079
80 // Unfortunately, this causes SEGFAULT
81 // The best reason I see is that apps are freed after ndn stack is removed
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 // StopApplication ();
83 Application::DoDispose();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070084}
85
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080086uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087App::GetId() const
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080088{
89 if (m_face == 0)
90 return (uint32_t)-1;
91 else
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 return m_face->GetId();
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080093}
94
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070095void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096App::OnInterest(Ptr<const Interest> interest)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070097{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 NS_LOG_FUNCTION(this << interest);
99 m_receivedInterests(interest, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700100}
101
102void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103App::OnNack(Ptr<const Interest> interest)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700104{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 NS_LOG_FUNCTION(this << interest);
106 m_receivedNacks(interest, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700107}
108
109void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110App::OnData(Ptr<const Data> contentObject)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700111{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 NS_LOG_FUNCTION(this << contentObject);
113 m_receivedDatas(contentObject, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700114}
115
116// Application Methods
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117void
118App::StartApplication() // Called at time specified by Start
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700119{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 NS_LOG_FUNCTION_NOARGS();
121
122 NS_ASSERT(m_active != true);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700123 m_active = true;
124
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125 NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0,
126 "Ndn stack should be installed on the node " << GetNode());
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700127
128 // step 1. Create a face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800129 m_face = CreateObject<AppFace>(/*Ptr<App> (this)*/ this);
130
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700131 // step 2. Add face to the Ndn stack
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132 GetNode()->GetObject<L3Protocol>()->AddFace(m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700133
134 // step 3. Enable face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135 m_face->SetUp(true);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700136}
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700137
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800138void
139App::StopApplication() // Called at time specified by Stop
140{
141 NS_LOG_FUNCTION_NOARGS();
142
143 if (!m_active)
144 return; // don't assert here, just return
145
146 NS_ASSERT(GetNode()->GetObject<L3Protocol>() != 0);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700147
148 m_active = false;
149
150 // step 1. Disable face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800151 m_face->SetUp(false);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700152
153 // step 2. Remove face from Ndn stack
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800154 GetNode()->GetObject<L3Protocol>()->RemoveFace(m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700155
156 // step 3. Destroy face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800157 if (m_face->GetReferenceCount() != 1) {
158 NS_LOG_ERROR("Please a bug report on https://github.com/NDN-Routing/ndnSIM/issues");
159 NS_LOG_ERROR("At this point, nobody else should have referenced this face, but we have "
160 << m_face->GetReferenceCount() << " references");
161 }
Alexander Afanasyevf3830472012-10-09 18:23:21 -0700162 // NS_ASSERT_MSG (m_face->GetReferenceCount ()==2,
163 // "At this point, nobody else should have referenced this face, but we have "
164 // << m_face->GetReferenceCount () << " references");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700165 m_face = 0;
166}
167
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700168} // namespace ndn
169} // namespace ns3