blob: 62cacc448dbf0317f8eca5b7446febc50cab57a0 [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
Alexander Afanasyev0c395372014-12-20 15:54:02 -080029#include "ns3/ndn-app-face.hpp"
Alexander Afanasyev68de7952012-12-12 18:02:29 -080030
Alexander Afanasyev0c395372014-12-20 15:54:02 -080031#include "ns3/ndn-fib.hpp"
Alexander Afanasyev68de7952012-12-12 18:02:29 -080032#include "ns3/random-variable.h"
33
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034NS_LOG_COMPONENT_DEFINE("CustomApp");
Alexander Afanasyev68de7952012-12-12 18:02:29 -080035
36namespace ns3 {
37
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038NS_OBJECT_ENSURE_REGISTERED(CustomApp);
Alexander Afanasyev68de7952012-12-12 18:02:29 -080039
40// register NS-3 type
41TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042CustomApp::GetTypeId()
Alexander Afanasyev68de7952012-12-12 18:02:29 -080043{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 static TypeId tid = TypeId("CustomApp").SetParent<ndn::App>().AddConstructor<CustomApp>();
Alexander Afanasyev68de7952012-12-12 18:02:29 -080045 return tid;
46}
47
48// Processing upon start of the application
49void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050CustomApp::StartApplication()
Alexander Afanasyev68de7952012-12-12 18:02:29 -080051{
52 // initialize ndn::App
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053 ndn::App::StartApplication();
Alexander Afanasyev68de7952012-12-12 18:02:29 -080054
55 // Create a name components object for name ``/prefix/sub``
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080056 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 Afanasyev68de7952012-12-12 18:02:29 -080059
60 /////////////////////////////////////////////////////////////////////////////
61 // Creating FIB entry that ensures that we will receive incoming Interests //
62 /////////////////////////////////////////////////////////////////////////////
63
64 // Get FIB object
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 Ptr<ndn::Fib> fib = GetNode()->GetObject<ndn::Fib>();
Alexander Afanasyev68de7952012-12-12 18:02:29 -080066
67 // Add entry to FIB
68 // Note that ``m_face`` is cretaed by ndn::App
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 Ptr<ndn::fib::Entry> fibEntry = fib->Add(*prefix, m_face, 0);
Alexander Afanasyev68de7952012-12-12 18:02:29 -080070
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 Simulator::Schedule(Seconds(1.0), &CustomApp::SendInterest, this);
Alexander Afanasyev68de7952012-12-12 18:02:29 -080072}
73
74// Processing when application is stopped
75void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076CustomApp::StopApplication()
Alexander Afanasyev68de7952012-12-12 18:02:29 -080077{
78 // cleanup ndn::App
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 ndn::App::StopApplication();
Alexander Afanasyev68de7952012-12-12 18:02:29 -080080}
81
82void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083CustomApp::SendInterest()
Alexander Afanasyev68de7952012-12-12 18:02:29 -080084{
85 /////////////////////////////////////
86 // Sending one Interest packet out //
87 /////////////////////////////////////
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088
89 Ptr<ndn::Name> prefix = Create<ndn::Name>("/prefix/sub"); // another way to create name
Alexander Afanasyev68de7952012-12-12 18:02:29 -080090
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070091 // Create and configure ndn::Interest
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 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 Afanasyev68de7952012-12-12 18:02:29 -080097
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 NS_LOG_DEBUG("Sending Interest packet for " << *prefix);
99
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800100 // Call trace (for logging purposes)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 m_transmittedInterests(interest, this, m_face);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700102
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 m_face->ReceiveInterest(interest);
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800104}
105
106// Callback that will be called when Interest arrives
107void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108CustomApp::OnInterest(Ptr<const ndn::Interest> interest)
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800109{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 ndn::App::OnInterest(interest);
111
112 NS_LOG_DEBUG("Received Interest packet for " << interest->GetName());
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800113
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700114 // Create and configure ndn::Data and ndn::DataTail
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800115 // (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 Afanasyev68de7952012-12-12 18:02:29 -0800118
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 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 Afanasyev68de7952012-12-12 18:02:29 -0800124
125 // Call trace (for logging purposes)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126 m_transmittedDatas(data, this, m_face);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700127
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 m_face->ReceiveData(data);
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800129}
130
131// Callback that will be called when Data arrives
132void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800133CustomApp::OnData(Ptr<const ndn::Data> contentObject)
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800134{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135 NS_LOG_DEBUG("Receiving Data packet for " << contentObject->GetName());
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800136
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 std::cout << "DATA received for name " << contentObject->GetName() << std::endl;
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800138}
139
Alexander Afanasyev68de7952012-12-12 18:02:29 -0800140} // namespace ns3