blob: 130b450e086a11a8af7e0a89314c84ddca7e0df6 [file] [log] [blame]
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -07001/* -*- 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>
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080019 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070020 */
21
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ndn-producer.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080023#include "ns3/log.h"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080024#include "ns3/ndn-interest.h"
25#include "ns3/ndn-content-object.h"
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080026#include "ns3/string.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080027#include "ns3/uinteger.h"
28#include "ns3/packet.h"
Alexander Afanasyevfaf95812012-06-05 21:28:11 -070029#include "ns3/simulator.h"
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070030
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070031#include "ns3/ndn-app-face.h"
32#include "ns3/ndn-fib.h"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080033
34#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080035
36#include <boost/ref.hpp>
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080037#include <boost/lambda/lambda.hpp>
38#include <boost/lambda/bind.hpp>
39namespace ll = boost::lambda;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070040
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041NS_LOG_COMPONENT_DEFINE ("ndn.Producer");
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070042
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043namespace ns3 {
44namespace ndn {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080045
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046NS_OBJECT_ENSURE_REGISTERED (Producer);
Alexander Afanasyeve7221482013-07-15 18:24:37 -070047
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070048TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049Producer::GetTypeId (void)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070050{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070051 static TypeId tid = TypeId ("ns3::ndn::Producer")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070052 .SetGroupName ("Ndn")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070053 .SetParent<App> ()
54 .AddConstructor<Producer> ()
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070055 .AddAttribute ("Prefix","Prefix, for which producer has the data",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080056 StringValue ("/"),
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070057 MakeNameAccessor (&Producer::m_prefix),
58 MakeNameChecker ())
Alexander Afanasyev79606062013-07-11 00:57:28 -070059 .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 ())
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070063 .AddAttribute ("PayloadSize", "Virtual payload size for Content packets",
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080064 UintegerValue (1024),
Alexander Afanasyeve7221482013-07-15 18:24:37 -070065 MakeUintegerAccessor (&Producer::m_virtualPayloadSize),
66 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080067 .AddAttribute ("Freshness", "Freshness of data packets, if 0, then unlimited freshness",
68 TimeValue (Seconds (0)),
69 MakeTimeAccessor (&Producer::m_freshness),
70 MakeTimeChecker ())
Alexander Afanasyeve7221482013-07-15 18:24:37 -070071 .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 ())
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070079 ;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080080 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070081}
Alexander Afanasyeve7221482013-07-15 18:24:37 -070082
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070083Producer::Producer ()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070084{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080085 // NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070086}
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080087
88// inherited from Application base class.
89void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070090Producer::StartApplication ()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080091{
92 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070093 NS_ASSERT (GetNode ()->GetObject<Fib> () != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080094
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095 App::StartApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080096
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -070097 NS_LOG_DEBUG ("NodeID: " << GetNode ()->GetId ());
Alexander Afanasyeve7221482013-07-15 18:24:37 -070098
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070099 Ptr<Fib> fib = GetNode ()->GetObject<Fib> ();
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700100
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101 Ptr<fib::Entry> fibEntry = fib->Add (m_prefix, m_face, 0);
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -0700102
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700103 fibEntry->UpdateStatus (m_face, fib::FaceMetric::NDN_FIB_GREEN);
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700104
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -0700105 // // make face green, so it will be used primarily
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106 // StaticCast<fib::FibImpl> (fib)->modify (fibEntry,
107 // ll::bind (&fib::Entry::UpdateStatus,
108 // ll::_1, m_face, fib::FaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800109}
110
111void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700112Producer::StopApplication ()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800113{
114 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700115 NS_ASSERT (GetNode ()->GetObject<Fib> () != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800116
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700117 App::StopApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800118}
119
120
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700121void
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700122Producer::OnInterest (Ptr<const Interest> interest)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700123{
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700124 App::OnInterest (interest); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800125
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800126 NS_LOG_FUNCTION (this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700127
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800128 if (!m_active) return;
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700129
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700130 Ptr<ContentObject> data = Create<ContentObject> (Create<Packet> (m_virtualPayloadSize));
Alexander Afanasyev79606062013-07-11 00:57:28 -0700131 Ptr<Name> dataName = Create<Name> (interest->GetName ());
132 dataName->Append (m_postfix);
133 data->SetName (dataName);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700134 data->SetFreshness (m_freshness);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800135
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700136 data->SetSignature (m_signature);
137 if (m_keyLocator.size () > 0)
138 {
139 data->SetKeyLocator (Create<Name> (m_keyLocator));
140 }
141
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700142 NS_LOG_INFO ("node("<< GetNode()->GetId() <<") respodning with ContentObject:\n" << data->GetName ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800143
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800144 // Echo back FwHopCountTag if exists
145 FwHopCountTag hopCountTag;
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700146 if (interest->GetPayload ()->PeekPacketTag (hopCountTag))
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800147 {
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700148 data->GetPayload ()->AddPacketTag (hopCountTag);
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800149 }
150
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700151 m_face->ReceiveData (data);
152 m_transmittedContentObjects (data, this, m_face);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700153}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800154
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700155} // namespace ndn
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800156} // namespace ns3