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 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 29 | #include "ns3/ndn-app-face.hpp" |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 31 | #include "ns3/ndn-fib.hpp" |
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 | |
| 55 | // Create a name components object for name ``/prefix/sub`` |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 56 | Ptr<ndn::Name> prefix = Create<ndn::Name>(); // now prefix contains ``/`` |
| 57 | prefix->append("prefix"); // now prefix contains ``/prefix`` |
| 58 | prefix->append("sub"); // now prefix contains ``/prefix/sub`` |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 59 | |
| 60 | ///////////////////////////////////////////////////////////////////////////// |
| 61 | // Creating FIB entry that ensures that we will receive incoming Interests // |
| 62 | ///////////////////////////////////////////////////////////////////////////// |
| 63 | |
| 64 | // Get FIB object |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 65 | Ptr<ndn::Fib> fib = GetNode()->GetObject<ndn::Fib>(); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 66 | |
| 67 | // Add entry to FIB |
| 68 | // Note that ``m_face`` is cretaed by ndn::App |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 69 | Ptr<ndn::fib::Entry> fibEntry = fib->Add(*prefix, m_face, 0); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 71 | Simulator::Schedule(Seconds(1.0), &CustomApp::SendInterest, this); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Processing when application is stopped |
| 75 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 76 | CustomApp::StopApplication() |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 77 | { |
| 78 | // cleanup ndn::App |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 79 | ndn::App::StopApplication(); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 83 | CustomApp::SendInterest() |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 84 | { |
| 85 | ///////////////////////////////////// |
| 86 | // Sending one Interest packet out // |
| 87 | ///////////////////////////////////// |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | |
| 89 | Ptr<ndn::Name> prefix = Create<ndn::Name>("/prefix/sub"); // another way to create name |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 91 | // Create and configure ndn::Interest |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 92 | Ptr<ndn::Interest> interest = Create<ndn::Interest>(); |
| 93 | UniformVariable rand(0, std::numeric_limits<uint32_t>::max()); |
| 94 | interest->SetNonce(rand.GetValue()); |
| 95 | interest->SetName(prefix); |
| 96 | interest->SetInterestLifetime(Seconds(1.0)); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 98 | NS_LOG_DEBUG("Sending Interest packet for " << *prefix); |
| 99 | |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 100 | // Call trace (for logging purposes) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 101 | m_transmittedInterests(interest, this, m_face); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 103 | m_face->ReceiveInterest(interest); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Callback that will be called when Interest arrives |
| 107 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 108 | CustomApp::OnInterest(Ptr<const ndn::Interest> interest) |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 109 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 110 | ndn::App::OnInterest(interest); |
| 111 | |
| 112 | NS_LOG_DEBUG("Received Interest packet for " << interest->GetName()); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 114 | // Create and configure ndn::Data and ndn::DataTail |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 115 | // (header is added in front of the packet, tail is added at the end of the packet) |
| 116 | |
| 117 | // 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] | 118 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 119 | Ptr<ndn::Data> data = Create<ndn::Data>(Create<Packet>(1024)); |
| 120 | data->SetName( |
| 121 | Create<ndn::Name>(interest->GetName())); // data will have the same name as Interests |
| 122 | |
| 123 | NS_LOG_DEBUG("Sending Data packet for " << data->GetName()); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 124 | |
| 125 | // Call trace (for logging purposes) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 126 | m_transmittedDatas(data, this, m_face); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 127 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 128 | m_face->ReceiveData(data); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Callback that will be called when Data arrives |
| 132 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 133 | CustomApp::OnData(Ptr<const ndn::Data> contentObject) |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 134 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 135 | NS_LOG_DEBUG("Receiving Data packet for " << contentObject->GetName()); |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 136 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 137 | std::cout << "DATA received for name " << contentObject->GetName() << std::endl; |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 140 | } // namespace ns3 |