blob: 67a5043426f801f759e2d3a6355b67b732ddab20 [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 Afanasyev0c395372014-12-20 15:54:02 -080022#include "ndn-producer.hpp"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080023#include "ns3/log.h"
Alexander Afanasyev0c395372014-12-20 15:54:02 -080024#include "ns3/ndn-interest.hpp"
25#include "ns3/ndn-data.hpp"
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 Afanasyev0c395372014-12-20 15:54:02 -080031#include "ns3/ndn-app-face.hpp"
32#include "ns3/ndn-fib.hpp"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080033
Alexander Afanasyev0c395372014-12-20 15:54:02 -080034#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.hpp"
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 Afanasyevbe55cf62014-12-20 17:51:09 -080041NS_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 Afanasyevbe55cf62014-12-20 17:51:09 -080046NS_OBJECT_ENSURE_REGISTERED(Producer);
Alexander Afanasyeve7221482013-07-15 18:24:37 -070047
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070048TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049Producer::GetTypeId(void)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070050{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 static TypeId tid =
52 TypeId("ns3::ndn::Producer")
53 .SetGroupName("Ndn")
54 .SetParent<App>()
55 .AddConstructor<Producer>()
56 .AddAttribute("Prefix", "Prefix, for which producer has the data", StringValue("/"),
57 MakeNameAccessor(&Producer::m_prefix), MakeNameChecker())
58 .AddAttribute(
59 "Postfix",
60 "Postfix that is added to the output data (e.g., for adding producer-uniqueness)",
61 StringValue("/"), MakeNameAccessor(&Producer::m_postfix), MakeNameChecker())
62 .AddAttribute("PayloadSize", "Virtual payload size for Content packets", UintegerValue(1024),
63 MakeUintegerAccessor(&Producer::m_virtualPayloadSize),
64 MakeUintegerChecker<uint32_t>())
65 .AddAttribute("Freshness", "Freshness of data packets, if 0, then unlimited freshness",
66 TimeValue(Seconds(0)), MakeTimeAccessor(&Producer::m_freshness),
67 MakeTimeChecker())
68 .AddAttribute(
69 "Signature",
70 "Fake signature, 0 valid signature (default), other values application-specific",
71 UintegerValue(0), MakeUintegerAccessor(&Producer::m_signature),
72 MakeUintegerChecker<uint32_t>())
73 .AddAttribute("KeyLocator",
74 "Name to be used for key locator. If root, then key locator is not used",
75 NameValue(), MakeNameAccessor(&Producer::m_keyLocator), MakeNameChecker());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080076 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070077}
Alexander Afanasyeve7221482013-07-15 18:24:37 -070078
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079Producer::Producer()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070080{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080081 // NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070082}
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080083
84// inherited from Application base class.
85void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086Producer::StartApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080087{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 NS_LOG_FUNCTION_NOARGS();
89 NS_ASSERT(GetNode()->GetObject<Fib>() != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080090
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 App::StartApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080092
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 NS_LOG_DEBUG("NodeID: " << GetNode()->GetId());
Alexander Afanasyeve7221482013-07-15 18:24:37 -070094
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 Ptr<Fib> fib = GetNode()->GetObject<Fib>();
Alexander Afanasyeve7221482013-07-15 18:24:37 -070096
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 Ptr<fib::Entry> fibEntry = fib->Add(m_prefix, m_face, 0);
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -070098
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 fibEntry->UpdateStatus(m_face, fib::FaceMetric::NDN_FIB_GREEN);
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700100
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -0700101 // // make face green, so it will be used primarily
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102 // StaticCast<fib::FibImpl> (fib)->modify (fibEntry,
103 // ll::bind (&fib::Entry::UpdateStatus,
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 // ll::_1, m_face,
105 // fib::FaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106}
107
108void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109Producer::StopApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800110{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 NS_LOG_FUNCTION_NOARGS();
112 NS_ASSERT(GetNode()->GetObject<Fib>() != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800113
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800114 App::StopApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800115}
116
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700117void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118Producer::OnInterest(Ptr<const Interest> interest)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700119{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 App::OnInterest(interest); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800121
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 NS_LOG_FUNCTION(this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700123
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800124 if (!m_active)
125 return;
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700126
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 Ptr<Data> data = Create<Data>(Create<Packet>(m_virtualPayloadSize));
128 Ptr<Name> dataName = Create<Name>(interest->GetName());
129 dataName->append(m_postfix);
130 data->SetName(dataName);
131 data->SetFreshness(m_freshness);
132 data->SetTimestamp(Simulator::Now());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800133
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800134 data->SetSignature(m_signature);
135 if (m_keyLocator.size() > 0) {
136 data->SetKeyLocator(Create<Name>(m_keyLocator));
137 }
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700138
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800139 NS_LOG_INFO("node(" << GetNode()->GetId() << ") respodning with Data: " << data->GetName());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800140
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800141 // Echo back FwHopCountTag if exists
142 FwHopCountTag hopCountTag;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800143 if (interest->GetPayload()->PeekPacketTag(hopCountTag)) {
144 data->GetPayload()->AddPacketTag(hopCountTag);
145 }
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800146
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800147 m_face->ReceiveData(data);
148 m_transmittedDatas(data, this, m_face);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700149}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800150
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700151} // namespace ndn
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800152} // namespace ns3