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