blob: 60bc1dc293633c2fceb2036e805c8aa29ecb2782 [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 Afanasyev09c7deb2011-11-23 14:50:10 -080024#include "ns3/string.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080025#include "ns3/uinteger.h"
26#include "ns3/packet.h"
Alexander Afanasyevfaf95812012-06-05 21:28:11 -070027#include "ns3/simulator.h"
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070028
Alexander Afanasyev0c395372014-12-20 15:54:02 -080029#include "ns3/ndn-app-face.hpp"
30#include "ns3/ndn-fib.hpp"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080031
Alexander Afanasyev0c395372014-12-20 15:54:02 -080032#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.hpp"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080033
34#include <boost/ref.hpp>
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080035#include <boost/lambda/lambda.hpp>
36#include <boost/lambda/bind.hpp>
37namespace ll = boost::lambda;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070038
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039NS_LOG_COMPONENT_DEFINE("ndn.Producer");
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070040
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041namespace ns3 {
42namespace ndn {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080043
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044NS_OBJECT_ENSURE_REGISTERED(Producer);
Alexander Afanasyeve7221482013-07-15 18:24:37 -070045
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070046TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047Producer::GetTypeId(void)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070048{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 static TypeId tid =
50 TypeId("ns3::ndn::Producer")
51 .SetGroupName("Ndn")
52 .SetParent<App>()
53 .AddConstructor<Producer>()
54 .AddAttribute("Prefix", "Prefix, for which producer has the data", StringValue("/"),
55 MakeNameAccessor(&Producer::m_prefix), MakeNameChecker())
56 .AddAttribute(
57 "Postfix",
58 "Postfix that is added to the output data (e.g., for adding producer-uniqueness)",
59 StringValue("/"), MakeNameAccessor(&Producer::m_postfix), MakeNameChecker())
60 .AddAttribute("PayloadSize", "Virtual payload size for Content packets", UintegerValue(1024),
61 MakeUintegerAccessor(&Producer::m_virtualPayloadSize),
62 MakeUintegerChecker<uint32_t>())
63 .AddAttribute("Freshness", "Freshness of data packets, if 0, then unlimited freshness",
64 TimeValue(Seconds(0)), MakeTimeAccessor(&Producer::m_freshness),
65 MakeTimeChecker())
66 .AddAttribute(
67 "Signature",
68 "Fake signature, 0 valid signature (default), other values application-specific",
69 UintegerValue(0), MakeUintegerAccessor(&Producer::m_signature),
70 MakeUintegerChecker<uint32_t>())
71 .AddAttribute("KeyLocator",
72 "Name to be used for key locator. If root, then key locator is not used",
73 NameValue(), MakeNameAccessor(&Producer::m_keyLocator), MakeNameChecker());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080074 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070075}
Alexander Afanasyeve7221482013-07-15 18:24:37 -070076
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077Producer::Producer()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070078{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080079 // NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070080}
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080081
82// inherited from Application base class.
83void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084Producer::StartApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080085{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 NS_LOG_FUNCTION_NOARGS();
87 NS_ASSERT(GetNode()->GetObject<Fib>() != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080088
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 App::StartApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080090
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 NS_LOG_DEBUG("NodeID: " << GetNode()->GetId());
Alexander Afanasyeve7221482013-07-15 18:24:37 -070092
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 Ptr<Fib> fib = GetNode()->GetObject<Fib>();
Alexander Afanasyeve7221482013-07-15 18:24:37 -070094
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 Ptr<fib::Entry> fibEntry = fib->Add(m_prefix, m_face, 0);
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -070096
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 fibEntry->UpdateStatus(m_face, fib::FaceMetric::NDN_FIB_GREEN);
Alexander Afanasyeve7221482013-07-15 18:24:37 -070098
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -070099 // // make face green, so it will be used primarily
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700100 // StaticCast<fib::FibImpl> (fib)->modify (fibEntry,
101 // ll::bind (&fib::Entry::UpdateStatus,
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 // ll::_1, m_face,
103 // fib::FaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800104}
105
106void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107Producer::StopApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800108{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 NS_LOG_FUNCTION_NOARGS();
110 NS_ASSERT(GetNode()->GetObject<Fib>() != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800111
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 App::StopApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800113}
114
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700115void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700116Producer::OnInterest(shared_ptr<const Interest> interest)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700117{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 App::OnInterest(interest); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800119
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 NS_LOG_FUNCTION(this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700121
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 if (!m_active)
123 return;
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700124
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700125 shared_ptr<Data> data = make_shared<Data>(Create<Packet>(m_virtualPayloadSize));
126 shared_ptr<Name> dataName = make_shared<Name>(interest->GetName());
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 dataName->append(m_postfix);
128 data->SetName(dataName);
129 data->SetFreshness(m_freshness);
130 data->SetTimestamp(Simulator::Now());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800131
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132 data->SetSignature(m_signature);
133 if (m_keyLocator.size() > 0) {
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700134 data->SetKeyLocator(make_shared<Name>(m_keyLocator));
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135 }
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700136
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 NS_LOG_INFO("node(" << GetNode()->GetId() << ") respodning with Data: " << data->GetName());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800138
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800139 // Echo back FwHopCountTag if exists
140 FwHopCountTag hopCountTag;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800141 if (interest->GetPayload()->PeekPacketTag(hopCountTag)) {
142 data->GetPayload()->AddPacketTag(hopCountTag);
143 }
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800144
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145 m_face->ReceiveData(data);
146 m_transmittedDatas(data, this, m_face);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700147}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700149} // namespace ndn
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800150} // namespace ns3