blob: 68f65e229b8de2c20cb2e4df9c128dbda80480d5 [file] [log] [blame]
Alexander Afanasyev68de7952012-12-12 18:02:29 -08001/* -*- 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"
Alexander Afanasyev6eba36f2013-08-07 17:42:54 -070031#include "ns3/ndn-data.h"
Alexander Afanasyev68de7952012-12-12 18:02:29 -080032
33#include "ns3/ndn-fib.h"
34#include "ns3/random-variable.h"
35
Alexander Afanasyev68de7952012-12-12 18:02:29 -080036NS_LOG_COMPONENT_DEFINE ("CustomApp");
37
38namespace ns3 {
39
40NS_OBJECT_ENSURE_REGISTERED (CustomApp);
41
42// register NS-3 type
43TypeId
44CustomApp::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
54void
55CustomApp::StartApplication ()
56{
57 // initialize ndn::App
58 ndn::App::StartApplication ();
59
60 // Create a name components object for name ``/prefix/sub``
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070061 Ptr<ndn::Name> prefix = Create<ndn::Name> (); // now prefix contains ``/``
Alexander Afanasyev92136012013-07-16 20:36:30 -070062 prefix->append ("prefix"); // now prefix contains ``/prefix``
63 prefix->append ("sub"); // now prefix contains ``/prefix/sub``
Alexander Afanasyev68de7952012-12-12 18:02:29 -080064
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
80void
81CustomApp::StopApplication ()
82{
83 // cleanup ndn::App
84 ndn::App::StopApplication ();
85}
86
87void
88CustomApp::SendInterest ()
89{
90 /////////////////////////////////////
91 // Sending one Interest packet out //
92 /////////////////////////////////////
93
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070094 Ptr<ndn::Name> prefix = Create<ndn::Name> ("/prefix/sub"); // another way to create name
Alexander Afanasyev68de7952012-12-12 18:02:29 -080095
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070096 // Create and configure ndn::Interest
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070097 Ptr<ndn::Interest> interest = Create<ndn::Interest> ();
Alexander Afanasyev68de7952012-12-12 18:02:29 -080098 UniformVariable rand (0,std::numeric_limits<uint32_t>::max ());
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070099 interest->SetNonce (rand.GetValue ());
100 interest->SetName (prefix);
101 interest->SetInterestLifetime (Seconds (1.0));
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800102
103 NS_LOG_DEBUG ("Sending Interest packet for " << *prefix);
104
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800105 // Call trace (for logging purposes)
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700106 m_transmittedInterests (interest, this, m_face);
107
108 m_face->ReceiveInterest (interest);
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800109}
110
111// Callback that will be called when Interest arrives
112void
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700113CustomApp::OnInterest (Ptr<const ndn::Interest> interest)
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800114{
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700115 ndn::App::OnInterest (interest);
116
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800117 NS_LOG_DEBUG ("Received Interest packet for " << interest->GetName ());
118
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700119 // Create and configure ndn::Data and ndn::DataTail
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800120 // (header is added in front of the packet, tail is added at the end of the packet)
121
122 // Note that Interests send out by the app will not be sent back to the app !
123
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700124 Ptr<ndn::Data> data = Create<ndn::Data> (Create<Packet> (1024));
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700125 data->SetName (Create<ndn::Name> (interest->GetName ())); // data will have the same name as Interests
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800126
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700127 NS_LOG_DEBUG ("Sending Data packet for " << data->GetName ());
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800128
129 // Call trace (for logging purposes)
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700130 m_transmittedDatas (data, this, m_face);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700131
132 m_face->ReceiveData (data);
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800133}
134
135// Callback that will be called when Data arrives
136void
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700137CustomApp::OnData (Ptr<const ndn::Data> contentObject)
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800138{
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700139 NS_LOG_DEBUG ("Receiving Data packet for " << contentObject->GetName ());
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800140
141 std::cout << "DATA received for name " << contentObject->GetName () << std::endl;
142}
143
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800144} // namespace ns3