blob: 73f5e72932abdf19e562ab8490c1f640dbfd8557 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-producer.hpp"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080021#include "ns3/log.h"
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080022#include "ns3/string.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080023#include "ns3/uinteger.h"
24#include "ns3/packet.h"
Alexander Afanasyevfaf95812012-06-05 21:28:11 -070025#include "ns3/simulator.h"
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070026
Mickey Sweatt89046c12014-11-16 20:32:27 -080027#include "model/ndn-l3-protocol.hpp"
28#include "helper/ndn-fib-helper.hpp"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080029
Mickey Sweatt89046c12014-11-16 20:32:27 -080030#include <memory>
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070031
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080032NS_LOG_COMPONENT_DEFINE("ndn.Producer");
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070033
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070034namespace ns3 {
35namespace ndn {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080036
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037NS_OBJECT_ENSURE_REGISTERED(Producer);
Alexander Afanasyeve7221482013-07-15 18:24:37 -070038
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070039TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040Producer::GetTypeId(void)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070041{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042 static TypeId tid =
43 TypeId("ns3::ndn::Producer")
44 .SetGroupName("Ndn")
45 .SetParent<App>()
46 .AddConstructor<Producer>()
47 .AddAttribute("Prefix", "Prefix, for which producer has the data", StringValue("/"),
48 MakeNameAccessor(&Producer::m_prefix), MakeNameChecker())
49 .AddAttribute(
50 "Postfix",
51 "Postfix that is added to the output data (e.g., for adding producer-uniqueness)",
52 StringValue("/"), MakeNameAccessor(&Producer::m_postfix), MakeNameChecker())
53 .AddAttribute("PayloadSize", "Virtual payload size for Content packets", UintegerValue(1024),
54 MakeUintegerAccessor(&Producer::m_virtualPayloadSize),
55 MakeUintegerChecker<uint32_t>())
56 .AddAttribute("Freshness", "Freshness of data packets, if 0, then unlimited freshness",
57 TimeValue(Seconds(0)), MakeTimeAccessor(&Producer::m_freshness),
58 MakeTimeChecker())
59 .AddAttribute(
60 "Signature",
61 "Fake signature, 0 valid signature (default), other values application-specific",
62 UintegerValue(0), MakeUintegerAccessor(&Producer::m_signature),
63 MakeUintegerChecker<uint32_t>())
64 .AddAttribute("KeyLocator",
65 "Name to be used for key locator. If root, then key locator is not used",
66 NameValue(), MakeNameAccessor(&Producer::m_keyLocator), MakeNameChecker());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080067 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070068}
Alexander Afanasyeve7221482013-07-15 18:24:37 -070069
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070Producer::Producer()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070071{
Mickey Sweatt89046c12014-11-16 20:32:27 -080072 NS_LOG_FUNCTION_NOARGS();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070073}
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080074
75// inherited from Application base class.
76void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077Producer::StartApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080078{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 App::StartApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080081
Mickey Sweatt89046c12014-11-16 20:32:27 -080082 FibHelper::AddRoute(GetNode(), m_prefix, m_face, 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080083}
84
85void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086Producer::StopApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080087{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080089
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 App::StopApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080091}
92
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070093void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070094Producer::OnInterest(shared_ptr<const Interest> interest)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070095{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096 App::OnInterest(interest); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080097
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 NS_LOG_FUNCTION(this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070099
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 if (!m_active)
101 return;
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700102
Mickey Sweatt89046c12014-11-16 20:32:27 -0800103 Name dataName(interest->getName());
104 // dataName.append(m_postfix);
105 // dataName.appendVersion();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106
Mickey Sweatt89046c12014-11-16 20:32:27 -0800107 auto data = make_shared<Data>();
108 data->setName(dataName);
109 data->setFreshnessPeriod(::ndn::time::milliseconds(m_freshness.GetMilliSeconds()));
110
Alexander Afanasyev8e60bcd2015-01-15 20:55:40 +0000111 data->setContent(make_shared< ::ndn::Buffer>(m_virtualPayloadSize));
Mickey Sweatt89046c12014-11-16 20:32:27 -0800112
Alexander Afanasyev8e60bcd2015-01-15 20:55:40 +0000113 SignatureInfo signatureInfo(static_cast< ::ndn::tlv::SignatureTypeValue>(255));
Mickey Sweatt89046c12014-11-16 20:32:27 -0800114
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 if (m_keyLocator.size() > 0) {
Mickey Sweatt89046c12014-11-16 20:32:27 -0800116 signatureInfo.setKeyLocator(m_keyLocator);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117 }
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700118
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400119 data->setSignatureInfo(signatureInfo);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800120
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400121 ::ndn::EncodingEstimator estimator;
122 ::ndn::EncodingBuffer encoder(estimator.appendVarNumber(m_signature), 0);
123 encoder.appendVarNumber(m_signature);
124 data->setSignatureValue(encoder.getBuffer());
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800125
Spyridon Mastorakisdb602302015-07-14 21:04:39 -0700126 NS_LOG_INFO("node(" << GetNode()->GetId() << ") responding with Data: " << data->getName());
Mickey Sweatt89046c12014-11-16 20:32:27 -0800127
128 // to create real wire encoding
129 data->wireEncode();
130
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131 m_transmittedDatas(data, this, m_face);
Alexander Afanasyev50ea1a32016-09-08 15:44:08 -0700132 m_appLink->onReceiveData(*data);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700133}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800134
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700135} // namespace ndn
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800136} // namespace ns3