Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * 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 Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * 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 Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * 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 Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 19 | |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 20 | // custom-app.cpp |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 22 | #include "custom-app.hpp" |
| 23 | |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 24 | #include "ns3/ptr.h" |
| 25 | #include "ns3/log.h" |
| 26 | #include "ns3/simulator.h" |
| 27 | #include "ns3/packet.h" |
| 28 | |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 29 | #include "ns3/ndnSIM/helper/ndn-stack-helper.hpp" |
Spyridon Mastorakis | db8280f | 2014-11-21 20:00:17 -0800 | [diff] [blame] | 30 | #include "ns3/ndnSIM/helper/ndn-fib-helper.hpp" |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 32 | #include "ns3/random-variable.h" |
| 33 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 34 | NS_LOG_COMPONENT_DEFINE("CustomApp"); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 35 | |
| 36 | namespace ns3 { |
| 37 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 38 | NS_OBJECT_ENSURE_REGISTERED(CustomApp); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 39 | |
| 40 | // register NS-3 type |
| 41 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 42 | CustomApp::GetTypeId() |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 43 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 44 | static TypeId tid = TypeId("CustomApp").SetParent<ndn::App>().AddConstructor<CustomApp>(); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 45 | return tid; |
| 46 | } |
| 47 | |
| 48 | // Processing upon start of the application |
| 49 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 50 | CustomApp::StartApplication() |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 51 | { |
| 52 | // initialize ndn::App |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 53 | ndn::App::StartApplication(); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 55 | // Add entry to FIB for `/prefix/sub` |
| 56 | ndn::FibHelper::AddRoute(GetNode(), "/prefix/sub", m_face, 0); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 57 | |
Spyridon Mastorakis | db8280f | 2014-11-21 20:00:17 -0800 | [diff] [blame] | 58 | // Schedule send of first interest |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 59 | Simulator::Schedule(Seconds(1.0), &CustomApp::SendInterest, this); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Processing when application is stopped |
| 63 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 64 | CustomApp::StopApplication() |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 65 | { |
| 66 | // cleanup ndn::App |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 67 | ndn::App::StopApplication(); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 71 | CustomApp::SendInterest() |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 72 | { |
| 73 | ///////////////////////////////////// |
| 74 | // Sending one Interest packet out // |
| 75 | ///////////////////////////////////// |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 77 | // Create and configure ndn::Interest |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 78 | auto interest = std::make_shared<ndn::Interest>("/prefix/sub"); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 79 | UniformVariable rand(0, std::numeric_limits<uint32_t>::max()); |
Spyridon Mastorakis | db8280f | 2014-11-21 20:00:17 -0800 | [diff] [blame] | 80 | interest->setNonce(rand.GetValue()); |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 81 | interest->setInterestLifetime(ndn::time::seconds(1)); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 83 | NS_LOG_DEBUG("Sending Interest packet for " << *interest); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 85 | // Call trace (for logging purposes) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 86 | m_transmittedInterests(interest, this, m_face); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 87 | |
Spyridon Mastorakis | db8280f | 2014-11-21 20:00:17 -0800 | [diff] [blame] | 88 | m_face->onReceiveInterest(*interest); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Callback that will be called when Interest arrives |
| 92 | void |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 93 | CustomApp::OnInterest(std::shared_ptr<const ndn::Interest> interest) |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 94 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 95 | ndn::App::OnInterest(interest); |
| 96 | |
Spyridon Mastorakis | db8280f | 2014-11-21 20:00:17 -0800 | [diff] [blame] | 97 | NS_LOG_DEBUG("Received Interest packet for " << interest->getName()); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 98 | |
| 99 | // Note that Interests send out by the app will not be sent back to the app ! |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 101 | auto data = std::make_shared<ndn::Data>(interest->getName()); |
| 102 | data->setFreshnessPeriod(ndn::time::milliseconds(1000)); |
Alexander Afanasyev | 8e60bcd | 2015-01-15 20:55:40 +0000 | [diff] [blame^] | 103 | data->setContent(std::make_shared< ::ndn::Buffer>(1024)); |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 104 | ndn::StackHelper::getKeyChain().sign(*data); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 105 | |
Spyridon Mastorakis | db8280f | 2014-11-21 20:00:17 -0800 | [diff] [blame] | 106 | NS_LOG_DEBUG("Sending Data packet for " << data->getName()); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 107 | |
| 108 | // Call trace (for logging purposes) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 109 | m_transmittedDatas(data, this, m_face); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 110 | |
Spyridon Mastorakis | db8280f | 2014-11-21 20:00:17 -0800 | [diff] [blame] | 111 | m_face->onReceiveData(*data); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Callback that will be called when Data arrives |
| 115 | void |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 116 | CustomApp::OnData(std::shared_ptr<const ndn::Data> data) |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 117 | { |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 118 | NS_LOG_DEBUG("Receiving Data packet for " << data->getName()); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | afe47fe | 2015-01-06 18:29:39 -0800 | [diff] [blame] | 120 | std::cout << "DATA received for name " << data->getName() << std::endl; |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 123 | } // namespace ns3 |