blob: a1c9e80f1bdb07f25f1db31de1505db5095672df [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-app.hpp"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ns3/log.h"
22#include "ns3/assert.h"
23#include "ns3/packet.h"
24
Mickey Sweatt89046c12014-11-16 20:32:27 -080025#include "model/ndn-l3-protocol.hpp"
26#include "model/ndn-app-face.hpp"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070027
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080028NS_LOG_COMPONENT_DEFINE("ndn.App");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070029
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070030namespace ns3 {
31namespace ndn {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080032
33NS_OBJECT_ENSURE_REGISTERED(App);
34
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070035TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080036App::GetTypeId(void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070037{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038 static TypeId tid = TypeId("ns3::ndn::App")
39 .SetGroupName("Ndn")
40 .SetParent<Application>()
41 .AddConstructor<App>()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070042
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 .AddTraceSource("ReceivedInterests", "ReceivedInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070044 MakeTraceSourceAccessor(&App::m_receivedInterests),
45 "ns3::ndn::App::InterestTraceCallback")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070046
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047 .AddTraceSource("ReceivedDatas", "ReceivedDatas",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070048 MakeTraceSourceAccessor(&App::m_receivedDatas),
49 "ns3::ndn::App::DataTraceCallback")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050
51 .AddTraceSource("TransmittedInterests", "TransmittedInterests",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070052 MakeTraceSourceAccessor(&App::m_transmittedInterests),
53 "ns3::ndn::App::InterestTraceCallback")
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054
55 .AddTraceSource("TransmittedDatas", "TransmittedDatas",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070056 MakeTraceSourceAccessor(&App::m_transmittedDatas),
57 "ns3::ndn::App::DataTraceCallback");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070058 return tid;
59}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060
61App::App()
62 : m_active(false)
63 , m_face(0)
Alexander Afanasyev6a720c12015-08-21 12:50:13 -070064 , m_appId(std::numeric_limits<uint32_t>::max())
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065{
66}
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067
68App::~App()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070069{
70}
71
72void
Alexander Afanasyev6a720c12015-08-21 12:50:13 -070073App::DoInitialize()
74{
75 NS_LOG_FUNCTION_NOARGS();
76
77 // find out what is application id on the node
78 for (uint32_t id = 0; id < GetNode()->GetNApplications(); ++id) {
79 if (GetNode()->GetApplication(id) == this) {
80 m_appId = id;
81 }
82 }
83
84 Application::DoInitialize();
85}
86
87void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088App::DoDispose(void)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070089{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070091
92 // Unfortunately, this causes SEGFAULT
93 // The best reason I see is that apps are freed after ndn stack is removed
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 // StopApplication ();
95 Application::DoDispose();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070096}
97
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -080098uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099App::GetId() const
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800100{
Alexander Afanasyev6a720c12015-08-21 12:50:13 -0700101 return m_appId;
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800102}
103
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700104void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700105App::OnInterest(shared_ptr<const Interest> interest)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700106{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 NS_LOG_FUNCTION(this << interest);
108 m_receivedInterests(interest, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700109}
110
111void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700112App::OnData(shared_ptr<const Data> data)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700113{
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700114 NS_LOG_FUNCTION(this << data);
115 m_receivedDatas(data, this, m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700116}
117
118// Application Methods
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119void
120App::StartApplication() // Called at time specified by Start
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700121{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 NS_LOG_FUNCTION_NOARGS();
123
124 NS_ASSERT(m_active != true);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700125 m_active = true;
126
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 NS_ASSERT_MSG(GetNode()->GetObject<L3Protocol>() != 0,
128 "Ndn stack should be installed on the node " << GetNode());
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700129
130 // step 1. Create a face
Mickey Sweatt89046c12014-11-16 20:32:27 -0800131 m_face = std::make_shared<AppFace>(this);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700133 // step 2. Add face to the Ndn stack
Mickey Sweatt89046c12014-11-16 20:32:27 -0800134 GetNode()->GetObject<L3Protocol>()->addFace(m_face);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700135}
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700136
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137void
138App::StopApplication() // Called at time specified by Stop
139{
140 NS_LOG_FUNCTION_NOARGS();
141
142 if (!m_active)
143 return; // don't assert here, just return
144
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700145 m_active = false;
146
Mickey Sweatt89046c12014-11-16 20:32:27 -0800147 m_face->close();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700148}
149
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700150} // namespace ndn
151} // namespace ns3