blob: 42021115902b55b42360f706e47e9712968e0713 [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-l3-protocol.hpp"
Alexander Afanasyev0c395372014-12-20 15:54:02 -080027#include "ns3/ndn-app-face.hpp"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070028
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080029NS_LOG_COMPONENT_DEFINE("ndn.App");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070030
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070031namespace ns3 {
32namespace ndn {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080033
34NS_OBJECT_ENSURE_REGISTERED(App);
35
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070036TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037App::GetTypeId(void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070038{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039 static TypeId tid = TypeId("ns3::ndn::App")
40 .SetGroupName("Ndn")
41 .SetParent<Application>()
42 .AddConstructor<App>()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070043
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 .AddTraceSource("ReceivedInterests", "ReceivedInterests",
45 MakeTraceSourceAccessor(&App::m_receivedInterests))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070046
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047 .AddTraceSource("ReceivedDatas", "ReceivedDatas",
48 MakeTraceSourceAccessor(&App::m_receivedDatas))
49
50 .AddTraceSource("TransmittedInterests", "TransmittedInterests",
51 MakeTraceSourceAccessor(&App::m_transmittedInterests))
52
53 .AddTraceSource("TransmittedDatas", "TransmittedDatas",
54 MakeTraceSourceAccessor(&App::m_transmittedDatas));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070055 return tid;
56}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057
58App::App()
59 : m_active(false)
60 , m_face(0)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070061{
62}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063
64App::~App()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065{
66}
67
68void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069App::DoDispose(void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070070{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070072
73 // Unfortunately, this causes SEGFAULT
74 // The best reason I see is that apps are freed after ndn stack is removed
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 // StopApplication ();
76 Application::DoDispose();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070077}
78
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080079uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080App::GetId() const
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080081{
82 if (m_face == 0)
83 return (uint32_t)-1;
84 else
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085 return m_face->GetId();
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080086}
87
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070088void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070089App::OnInterest(shared_ptr<const Interest> interest)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070090{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 NS_LOG_FUNCTION(this << interest);
92 m_receivedInterests(interest, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070093}
94
95void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070096App::OnData(shared_ptr<const Data> data)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070097{
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070098 NS_LOG_FUNCTION(this << data);
99 m_receivedDatas(data, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700100}
101
102// Application Methods
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103void
104App::StartApplication() // Called at time specified by Start
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700105{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 NS_LOG_FUNCTION_NOARGS();
107
108 NS_ASSERT(m_active != true);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700109 m_active = true;
110
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0,
112 "Ndn stack should be installed on the node " << GetNode());
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700113
114 // step 1. Create a face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 m_face = CreateObject<AppFace>(/*Ptr<App> (this)*/ this);
116
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700117 // step 2. Add face to the Ndn stack
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 GetNode()->GetObject<L3Protocol>()->AddFace(m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700119
120 // step 3. Enable face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121 m_face->SetUp(true);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700122}
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700123
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800124void
125App::StopApplication() // Called at time specified by Stop
126{
127 NS_LOG_FUNCTION_NOARGS();
128
129 if (!m_active)
130 return; // don't assert here, just return
131
132 NS_ASSERT(GetNode()->GetObject<L3Protocol>() != 0);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700133
134 m_active = false;
135
136 // step 1. Disable face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 m_face->SetUp(false);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700138
139 // step 2. Remove face from Ndn stack
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800140 GetNode()->GetObject<L3Protocol>()->RemoveFace(m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700141
142 // step 3. Destroy face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800143 if (m_face->GetReferenceCount() != 1) {
144 NS_LOG_ERROR("Please a bug report on https://github.com/NDN-Routing/ndnSIM/issues");
145 NS_LOG_ERROR("At this point, nobody else should have referenced this face, but we have "
146 << m_face->GetReferenceCount() << " references");
147 }
Alexander Afanasyevf3830472012-10-09 18:23:21 -0700148 // NS_ASSERT_MSG (m_face->GetReferenceCount ()==2,
149 // "At this point, nobody else should have referenced this face, but we have "
150 // << m_face->GetReferenceCount () << " references");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700151 m_face = 0;
152}
153
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700154} // namespace ndn
155} // namespace ns3