blob: b5d3e35967298c0dc2523eb061e06e6b3d00f5ce [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"
27#include "ns3/ndn-fib.hpp"
28#include "ns3/ndn-app-face.hpp"
29#include "ns3/ndn-forwarding-strategy.hpp"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070030
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080031NS_LOG_COMPONENT_DEFINE("ndn.App");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070032
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033namespace ns3 {
34namespace ndn {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080035
36NS_OBJECT_ENSURE_REGISTERED(App);
37
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070038TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039App::GetTypeId(void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070040{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041 static TypeId tid = TypeId("ns3::ndn::App")
42 .SetGroupName("Ndn")
43 .SetParent<Application>()
44 .AddConstructor<App>()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070045
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046 .AddTraceSource("ReceivedInterests", "ReceivedInterests",
47 MakeTraceSourceAccessor(&App::m_receivedInterests))
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070048
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 .AddTraceSource("ReceivedDatas", "ReceivedDatas",
50 MakeTraceSourceAccessor(&App::m_receivedDatas))
51
52 .AddTraceSource("TransmittedInterests", "TransmittedInterests",
53 MakeTraceSourceAccessor(&App::m_transmittedInterests))
54
55 .AddTraceSource("TransmittedDatas", "TransmittedDatas",
56 MakeTraceSourceAccessor(&App::m_transmittedDatas));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070057 return tid;
58}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059
60App::App()
61 : m_active(false)
62 , m_face(0)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070063{
64}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065
66App::~App()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070067{
68}
69
70void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071App::DoDispose(void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070072{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070074
75 // Unfortunately, this causes SEGFAULT
76 // The best reason I see is that apps are freed after ndn stack is removed
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 // StopApplication ();
78 Application::DoDispose();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070079}
80
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080081uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082App::GetId() const
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080083{
84 if (m_face == 0)
85 return (uint32_t)-1;
86 else
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 return m_face->GetId();
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080088}
89
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070090void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070091App::OnInterest(shared_ptr<const Interest> interest)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070092{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 NS_LOG_FUNCTION(this << interest);
94 m_receivedInterests(interest, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070095}
96
97void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070098App::OnData(shared_ptr<const Data> data)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070099{
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700100 NS_LOG_FUNCTION(this << data);
101 m_receivedDatas(data, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700102}
103
104// Application Methods
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105void
106App::StartApplication() // Called at time specified by Start
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700107{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108 NS_LOG_FUNCTION_NOARGS();
109
110 NS_ASSERT(m_active != true);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700111 m_active = true;
112
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113 NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0,
114 "Ndn stack should be installed on the node " << GetNode());
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700115
116 // step 1. Create a face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117 m_face = CreateObject<AppFace>(/*Ptr<App> (this)*/ this);
118
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700119 // step 2. Add face to the Ndn stack
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 GetNode()->GetObject<L3Protocol>()->AddFace(m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700121
122 // step 3. Enable face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800123 m_face->SetUp(true);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700124}
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700125
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126void
127App::StopApplication() // Called at time specified by Stop
128{
129 NS_LOG_FUNCTION_NOARGS();
130
131 if (!m_active)
132 return; // don't assert here, just return
133
134 NS_ASSERT(GetNode()->GetObject<L3Protocol>() != 0);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700135
136 m_active = false;
137
138 // step 1. Disable face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800139 m_face->SetUp(false);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700140
141 // step 2. Remove face from Ndn stack
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142 GetNode()->GetObject<L3Protocol>()->RemoveFace(m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700143
144 // step 3. Destroy face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145 if (m_face->GetReferenceCount() != 1) {
146 NS_LOG_ERROR("Please a bug report on https://github.com/NDN-Routing/ndnSIM/issues");
147 NS_LOG_ERROR("At this point, nobody else should have referenced this face, but we have "
148 << m_face->GetReferenceCount() << " references");
149 }
Alexander Afanasyevf3830472012-10-09 18:23:21 -0700150 // NS_ASSERT_MSG (m_face->GetReferenceCount ()==2,
151 // "At this point, nobody else should have referenced this face, but we have "
152 // << m_face->GetReferenceCount () << " references");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700153 m_face = 0;
154}
155
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700156} // namespace ndn
157} // namespace ns3