blob: 9aad4853e6097b52675c79f135ab1b8af3980497 [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 Afanasyev68de7952012-12-12 18:02:29 -08004 *
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 Afanasyev68de7952012-12-12 18:02:29 -08007 *
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 Afanasyev68de7952012-12-12 18:02:29 -080011 *
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 Afanasyev68de7952012-12-12 18:02:29 -080015 *
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 Afanasyev68de7952012-12-12 18:02:29 -080019
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080020// custom-app.cpp
Alexander Afanasyev68de7952012-12-12 18:02:29 -080021
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080022#include "custom-app.hpp"
23
Alexander Afanasyev68de7952012-12-12 18:02:29 -080024#include "ns3/ptr.h"
25#include "ns3/log.h"
26#include "ns3/simulator.h"
27#include "ns3/packet.h"
28
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080029#include "ns3/ndnSIM/helper/ndn-stack-helper.hpp"
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -080030#include "ns3/ndnSIM/helper/ndn-fib-helper.hpp"
Alexander Afanasyev68de7952012-12-12 18:02:29 -080031
Alexander Afanasyev68de7952012-12-12 18:02:29 -080032#include "ns3/random-variable.h"
33
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034NS_LOG_COMPONENT_DEFINE("CustomApp");
Alexander Afanasyev68de7952012-12-12 18:02:29 -080035
36namespace ns3 {
37
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038NS_OBJECT_ENSURE_REGISTERED(CustomApp);
Alexander Afanasyev68de7952012-12-12 18:02:29 -080039
40// register NS-3 type
41TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042CustomApp::GetTypeId()
Alexander Afanasyev68de7952012-12-12 18:02:29 -080043{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 static TypeId tid = TypeId("CustomApp").SetParent<ndn::App>().AddConstructor<CustomApp>();
Alexander Afanasyev68de7952012-12-12 18:02:29 -080045 return tid;
46}
47
48// Processing upon start of the application
49void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050CustomApp::StartApplication()
Alexander Afanasyev68de7952012-12-12 18:02:29 -080051{
52 // initialize ndn::App
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053 ndn::App::StartApplication();
Alexander Afanasyev68de7952012-12-12 18:02:29 -080054
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080055 // Add entry to FIB for `/prefix/sub`
56 ndn::FibHelper::AddRoute(GetNode(), "/prefix/sub", m_face, 0);
Alexander Afanasyev68de7952012-12-12 18:02:29 -080057
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -080058 // Schedule send of first interest
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 Simulator::Schedule(Seconds(1.0), &CustomApp::SendInterest, this);
Alexander Afanasyev68de7952012-12-12 18:02:29 -080060}
61
62// Processing when application is stopped
63void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064CustomApp::StopApplication()
Alexander Afanasyev68de7952012-12-12 18:02:29 -080065{
66 // cleanup ndn::App
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 ndn::App::StopApplication();
Alexander Afanasyev68de7952012-12-12 18:02:29 -080068}
69
70void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071CustomApp::SendInterest()
Alexander Afanasyev68de7952012-12-12 18:02:29 -080072{
73 /////////////////////////////////////
74 // Sending one Interest packet out //
75 /////////////////////////////////////
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070077 // Create and configure ndn::Interest
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080078 auto interest = std::make_shared<ndn::Interest>("/prefix/sub");
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 UniformVariable rand(0, std::numeric_limits<uint32_t>::max());
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -080080 interest->setNonce(rand.GetValue());
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080081 interest->setInterestLifetime(ndn::time::seconds(1));
Alexander Afanasyev68de7952012-12-12 18:02:29 -080082
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080083 NS_LOG_DEBUG("Sending Interest packet for " << *interest);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084
Alexander Afanasyev68de7952012-12-12 18:02:29 -080085 // Call trace (for logging purposes)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 m_transmittedInterests(interest, this, m_face);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070087
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -080088 m_face->onReceiveInterest(*interest);
Alexander Afanasyev68de7952012-12-12 18:02:29 -080089}
90
91// Callback that will be called when Interest arrives
92void
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -080093CustomApp::OnInterest(std::shared_ptr<const ndn::Interest> interest)
Alexander Afanasyev68de7952012-12-12 18:02:29 -080094{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 ndn::App::OnInterest(interest);
96
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -080097 NS_LOG_DEBUG("Received Interest packet for " << interest->getName());
Alexander Afanasyev68de7952012-12-12 18:02:29 -080098
99 // Note that Interests send out by the app will not be sent back to the app !
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800100
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -0800101 auto data = std::make_shared<ndn::Data>(interest->getName());
102 data->setFreshnessPeriod(ndn::time::milliseconds(1000));
Alexander Afanasyev8e60bcd2015-01-15 20:55:40 +0000103 data->setContent(std::make_shared< ::ndn::Buffer>(1024));
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -0800104 ndn::StackHelper::getKeyChain().sign(*data);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -0800106 NS_LOG_DEBUG("Sending Data packet for " << data->getName());
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800107
108 // Call trace (for logging purposes)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 m_transmittedDatas(data, this, m_face);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700110
Spyridon Mastorakisdb8280f2014-11-21 20:00:17 -0800111 m_face->onReceiveData(*data);
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800112}
113
114// Callback that will be called when Data arrives
115void
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -0800116CustomApp::OnData(std::shared_ptr<const ndn::Data> data)
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800117{
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -0800118 NS_LOG_DEBUG("Receiving Data packet for " << data->getName());
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800119
Alexander Afanasyevafe47fe2015-01-06 18:29:39 -0800120 std::cout << "DATA received for name " << data->getName() << std::endl;
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800121}
122
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800123} // namespace ns3