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