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 | |
| 29 | #include "ns3/ndn-app-face.h" |
| 30 | #include "ns3/ndn-interest.h" |
| 31 | #include "ns3/ndn-content-object.h" |
| 32 | |
| 33 | #include "ns3/ndn-fib.h" |
| 34 | #include "ns3/random-variable.h" |
| 35 | |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 36 | NS_LOG_COMPONENT_DEFINE ("CustomApp"); |
| 37 | |
| 38 | namespace ns3 { |
| 39 | |
| 40 | NS_OBJECT_ENSURE_REGISTERED (CustomApp); |
| 41 | |
| 42 | // register NS-3 type |
| 43 | TypeId |
| 44 | CustomApp::GetTypeId () |
| 45 | { |
| 46 | static TypeId tid = TypeId ("CustomApp") |
| 47 | .SetParent<ndn::App> () |
| 48 | .AddConstructor<CustomApp> () |
| 49 | ; |
| 50 | return tid; |
| 51 | } |
| 52 | |
| 53 | // Processing upon start of the application |
| 54 | void |
| 55 | CustomApp::StartApplication () |
| 56 | { |
| 57 | // initialize ndn::App |
| 58 | ndn::App::StartApplication (); |
| 59 | |
| 60 | // Create a name components object for name ``/prefix/sub`` |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 61 | Ptr<ndn::Name> prefix = Create<ndn::Name> (); // now prefix contains ``/`` |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 62 | prefix->Add ("prefix"); // now prefix contains ``/prefix`` |
| 63 | prefix->Add ("sub"); // now prefix contains ``/prefix/sub`` |
| 64 | |
| 65 | ///////////////////////////////////////////////////////////////////////////// |
| 66 | // Creating FIB entry that ensures that we will receive incoming Interests // |
| 67 | ///////////////////////////////////////////////////////////////////////////// |
| 68 | |
| 69 | // Get FIB object |
| 70 | Ptr<ndn::Fib> fib = GetNode ()->GetObject<ndn::Fib> (); |
| 71 | |
| 72 | // Add entry to FIB |
| 73 | // Note that ``m_face`` is cretaed by ndn::App |
| 74 | Ptr<ndn::fib::Entry> fibEntry = fib->Add (*prefix, m_face, 0); |
| 75 | |
| 76 | Simulator::Schedule (Seconds (1.0), &CustomApp::SendInterest, this); |
| 77 | } |
| 78 | |
| 79 | // Processing when application is stopped |
| 80 | void |
| 81 | CustomApp::StopApplication () |
| 82 | { |
| 83 | // cleanup ndn::App |
| 84 | ndn::App::StopApplication (); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | CustomApp::SendInterest () |
| 89 | { |
| 90 | ///////////////////////////////////// |
| 91 | // Sending one Interest packet out // |
| 92 | ///////////////////////////////////// |
| 93 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 94 | 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] | 95 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 96 | // Create and configure ndn::Interest |
| 97 | ndn::Interest interestHeader; |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 98 | UniformVariable rand (0,std::numeric_limits<uint32_t>::max ()); |
| 99 | interestHeader.SetNonce (rand.GetValue ()); |
| 100 | interestHeader.SetName (prefix); |
| 101 | interestHeader.SetInterestLifetime (Seconds (1.0)); |
| 102 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 103 | // Create packet and add ndn::Interest |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 104 | Ptr<Packet> packet = Create<Packet> (); |
| 105 | packet->AddHeader (interestHeader); |
| 106 | |
| 107 | NS_LOG_DEBUG ("Sending Interest packet for " << *prefix); |
| 108 | |
| 109 | // Forward packet to lower (network) layer |
| 110 | m_protocolHandler (packet); |
| 111 | |
| 112 | // Call trace (for logging purposes) |
| 113 | m_transmittedInterests (&interestHeader, this, m_face); |
| 114 | } |
| 115 | |
| 116 | // Callback that will be called when Interest arrives |
| 117 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 118 | CustomApp::OnInterest (const Ptr<const ndn::Interest> &interest, Ptr<Packet> origPacket) |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 119 | { |
| 120 | NS_LOG_DEBUG ("Received Interest packet for " << interest->GetName ()); |
| 121 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 122 | // Create and configure ndn::ContentObject and ndn::ContentObjectTail |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 123 | // (header is added in front of the packet, tail is added at the end of the packet) |
| 124 | |
| 125 | // Note that Interests send out by the app will not be sent back to the app ! |
| 126 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 127 | ndn::ContentObject data; |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 128 | data.SetName (Create<ndn::Name> (interest->GetName ())); // data will have the same name as Interests |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 129 | |
| 130 | ndn::ContentObjectTail trailer; // doesn't require any configuration |
| 131 | |
| 132 | // Create packet and add header and trailer |
| 133 | Ptr<Packet> packet = Create<Packet> (1024); |
| 134 | packet->AddHeader (data); |
| 135 | packet->AddTrailer (trailer); |
| 136 | |
| 137 | NS_LOG_DEBUG ("Sending ContentObject packet for " << data.GetName ()); |
| 138 | |
| 139 | // Forward packet to lower (network) layer |
| 140 | m_protocolHandler (packet); |
| 141 | |
| 142 | // Call trace (for logging purposes) |
| 143 | m_transmittedContentObjects (&data, packet, this, m_face); |
| 144 | } |
| 145 | |
| 146 | // Callback that will be called when Data arrives |
| 147 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 148 | CustomApp::OnContentObject (const Ptr<const ndn::ContentObject> &contentObject, |
Alexander Afanasyev | 68de795 | 2012-12-12 18:02:29 -0800 | [diff] [blame] | 149 | Ptr<Packet> payload) |
| 150 | { |
| 151 | NS_LOG_DEBUG ("Receiving ContentObject packet for " << contentObject->GetName ()); |
| 152 | |
| 153 | std::cout << "DATA received for name " << contentObject->GetName () << std::endl; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | } // namespace ns3 |