NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndn-producer.cc
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 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: Ilya Moiseenko <iliamo@cs.ucla.edu>
19  * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20  */
21 
22 #include "ndn-producer.h"
23 #include "ns3/log.h"
24 #include "ns3/ndn-interest.h"
25 #include "ns3/ndn-data.h"
26 #include "ns3/string.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/packet.h"
29 #include "ns3/simulator.h"
30 
31 #include "ns3/ndn-app-face.h"
32 #include "ns3/ndn-fib.h"
33 
34 #include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
35 
36 #include <boost/ref.hpp>
37 #include <boost/lambda/lambda.hpp>
38 #include <boost/lambda/bind.hpp>
39 namespace ll = boost::lambda;
40 
41 NS_LOG_COMPONENT_DEFINE ("ndn.Producer");
42 
43 namespace ns3 {
44 namespace ndn {
45 
46 NS_OBJECT_ENSURE_REGISTERED (Producer);
47 
48 TypeId
49 Producer::GetTypeId (void)
50 {
51  static TypeId tid = TypeId ("ns3::ndn::Producer")
52  .SetGroupName ("Ndn")
53  .SetParent<App> ()
54  .AddConstructor<Producer> ()
55  .AddAttribute ("Prefix","Prefix, for which producer has the data",
56  StringValue ("/"),
57  MakeNameAccessor (&Producer::m_prefix),
58  MakeNameChecker ())
59  .AddAttribute ("Postfix", "Postfix that is added to the output data (e.g., for adding producer-uniqueness)",
60  StringValue ("/"),
61  MakeNameAccessor (&Producer::m_postfix),
62  MakeNameChecker ())
63  .AddAttribute ("PayloadSize", "Virtual payload size for Content packets",
64  UintegerValue (1024),
65  MakeUintegerAccessor (&Producer::m_virtualPayloadSize),
66  MakeUintegerChecker<uint32_t> ())
67  .AddAttribute ("Freshness", "Freshness of data packets, if 0, then unlimited freshness",
68  TimeValue (Seconds (0)),
69  MakeTimeAccessor (&Producer::m_freshness),
70  MakeTimeChecker ())
71  .AddAttribute ("Signature", "Fake signature, 0 valid signature (default), other values application-specific",
72  UintegerValue (0),
73  MakeUintegerAccessor (&Producer::m_signature),
74  MakeUintegerChecker<uint32_t> ())
75  .AddAttribute ("KeyLocator", "Name to be used for key locator. If root, then key locator is not used",
76  NameValue (),
77  MakeNameAccessor (&Producer::m_keyLocator),
78  MakeNameChecker ())
79  ;
80  return tid;
81 }
82 
83 Producer::Producer ()
84 {
85  // NS_LOG_FUNCTION_NOARGS ();
86 }
87 
88 // inherited from Application base class.
89 void
91 {
92  NS_LOG_FUNCTION_NOARGS ();
93  NS_ASSERT (GetNode ()->GetObject<Fib> () != 0);
94 
96 
97  NS_LOG_DEBUG ("NodeID: " << GetNode ()->GetId ());
98 
99  Ptr<Fib> fib = GetNode ()->GetObject<Fib> ();
100 
101  Ptr<fib::Entry> fibEntry = fib->Add (m_prefix, m_face, 0);
102 
103  fibEntry->UpdateStatus (m_face, fib::FaceMetric::NDN_FIB_GREEN);
104 
105  // // make face green, so it will be used primarily
106  // StaticCast<fib::FibImpl> (fib)->modify (fibEntry,
107  // ll::bind (&fib::Entry::UpdateStatus,
108  // ll::_1, m_face, fib::FaceMetric::NDN_FIB_GREEN));
109 }
110 
111 void
113 {
114  NS_LOG_FUNCTION_NOARGS ();
115  NS_ASSERT (GetNode ()->GetObject<Fib> () != 0);
116 
118 }
119 
120 
121 void
122 Producer::OnInterest (Ptr<const Interest> interest)
123 {
124  App::OnInterest (interest); // tracing inside
125 
126  NS_LOG_FUNCTION (this << interest);
127 
128  if (!m_active) return;
129 
130  Ptr<Data> data = Create<Data> (Create<Packet> (m_virtualPayloadSize));
131  Ptr<Name> dataName = Create<Name> (interest->GetName ());
132  dataName->append (m_postfix);
133  data->SetName (dataName);
134  data->SetFreshness (m_freshness);
135  data->SetTimestamp (Simulator::Now());
136 
137  data->SetSignature (m_signature);
138  if (m_keyLocator.size () > 0)
139  {
140  data->SetKeyLocator (Create<Name> (m_keyLocator));
141  }
142 
143  NS_LOG_INFO ("node("<< GetNode()->GetId() <<") respodning with Data: " << data->GetName ());
144 
145  // Echo back FwHopCountTag if exists
146  FwHopCountTag hopCountTag;
147  if (interest->GetPayload ()->PeekPacketTag (hopCountTag))
148  {
149  data->GetPayload ()->AddPacketTag (hopCountTag);
150  }
151 
152  m_face->ReceiveData (data);
153  m_transmittedDatas (data, this, m_face);
154 }
155 
156 } // namespace ndn
157 } // namespace ns3
bool m_active
Flag to indicate that application is active (set by StartApplication and StopApplication) ...
Definition: ndn-app.h:106
virtual Ptr< fib::Entry > Add(const Name &prefix, Ptr< Face > face, int32_t metric)=0
Add or update FIB entry.
virtual void StartApplication()
Called at time specified by Start.
Definition: ndn-producer.cc:90
virtual void StartApplication()
Called at time specified by Start.
Definition: ndn-app.cc:119
uint32_t GetId() const
Get application ID (ID of applications face)
Definition: ndn-app.cc:88
size_t size() const
Get number of the name components.
Definition: name.h:516
void OnInterest(Ptr< const Interest > interest)
Method that will be called every time new Interest arrives.
Class implementing FIB functionality.
Definition: ndn-fib.h:44
virtual void StopApplication()
Called at time specified by Stop.
Definition: ndn-app.cc:140
virtual void OnInterest(Ptr< const Interest > interest)
Method that will be called every time new Interest arrives.
Definition: ndn-app.cc:97
Packet tag that is used to track hop count for Interest-Data pairs.
App()
Default constructor.
Definition: ndn-app.cc:66
Ptr< Face > m_face
automatically created application face through which application communicates
Definition: ndn-app.h:107
virtual void StopApplication()
Called at time specified by Stop.
TracedCallback< Ptr< const Data >, Ptr< App >, Ptr< Face > > m_transmittedDatas
App-level trace of transmitted Data.
Definition: ndn-app.h:123