blob: 5be9241ee3276a5674cce57796dcbd41e4b5922a [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-ns3.hpp"
28#include "model/ndn-l3-protocol.hpp"
29#include "helper/ndn-fib-helper.hpp"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080030
Mickey Sweatt89046c12014-11-16 20:32:27 -080031#include <memory>
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070032
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080033NS_LOG_COMPONENT_DEFINE("ndn.Producer");
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070034
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070035namespace ns3 {
36namespace ndn {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080037
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038NS_OBJECT_ENSURE_REGISTERED(Producer);
Alexander Afanasyeve7221482013-07-15 18:24:37 -070039
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070040TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041Producer::GetTypeId(void)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070042{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 static TypeId tid =
44 TypeId("ns3::ndn::Producer")
45 .SetGroupName("Ndn")
46 .SetParent<App>()
47 .AddConstructor<Producer>()
48 .AddAttribute("Prefix", "Prefix, for which producer has the data", StringValue("/"),
49 MakeNameAccessor(&Producer::m_prefix), MakeNameChecker())
50 .AddAttribute(
51 "Postfix",
52 "Postfix that is added to the output data (e.g., for adding producer-uniqueness)",
53 StringValue("/"), MakeNameAccessor(&Producer::m_postfix), MakeNameChecker())
54 .AddAttribute("PayloadSize", "Virtual payload size for Content packets", UintegerValue(1024),
55 MakeUintegerAccessor(&Producer::m_virtualPayloadSize),
56 MakeUintegerChecker<uint32_t>())
57 .AddAttribute("Freshness", "Freshness of data packets, if 0, then unlimited freshness",
58 TimeValue(Seconds(0)), MakeTimeAccessor(&Producer::m_freshness),
59 MakeTimeChecker())
60 .AddAttribute(
61 "Signature",
62 "Fake signature, 0 valid signature (default), other values application-specific",
63 UintegerValue(0), MakeUintegerAccessor(&Producer::m_signature),
64 MakeUintegerChecker<uint32_t>())
65 .AddAttribute("KeyLocator",
66 "Name to be used for key locator. If root, then key locator is not used",
67 NameValue(), MakeNameAccessor(&Producer::m_keyLocator), MakeNameChecker());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080068 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070069}
Alexander Afanasyeve7221482013-07-15 18:24:37 -070070
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071Producer::Producer()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070072{
Mickey Sweatt89046c12014-11-16 20:32:27 -080073 NS_LOG_FUNCTION_NOARGS();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070074}
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080075
76// inherited from Application base class.
77void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078Producer::StartApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080079{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 App::StartApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080082
Mickey Sweatt89046c12014-11-16 20:32:27 -080083 FibHelper::AddRoute(GetNode(), m_prefix, m_face, 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080084}
85
86void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087Producer::StopApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080088{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080090
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 App::StopApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080092}
93
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070094void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070095Producer::OnInterest(shared_ptr<const Interest> interest)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070096{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 App::OnInterest(interest); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080098
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 NS_LOG_FUNCTION(this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700100
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 if (!m_active)
102 return;
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700103
Mickey Sweatt89046c12014-11-16 20:32:27 -0800104 Name dataName(interest->getName());
105 // dataName.append(m_postfix);
106 // dataName.appendVersion();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800107
Mickey Sweatt89046c12014-11-16 20:32:27 -0800108 auto data = make_shared<Data>();
109 data->setName(dataName);
110 data->setFreshnessPeriod(::ndn::time::milliseconds(m_freshness.GetMilliSeconds()));
111
Alexander Afanasyev8e60bcd2015-01-15 20:55:40 +0000112 data->setContent(make_shared< ::ndn::Buffer>(m_virtualPayloadSize));
Mickey Sweatt89046c12014-11-16 20:32:27 -0800113
114 Signature signature;
Alexander Afanasyev8e60bcd2015-01-15 20:55:40 +0000115 SignatureInfo signatureInfo(static_cast< ::ndn::tlv::SignatureTypeValue>(255));
Mickey Sweatt89046c12014-11-16 20:32:27 -0800116
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117 if (m_keyLocator.size() > 0) {
Mickey Sweatt89046c12014-11-16 20:32:27 -0800118 signatureInfo.setKeyLocator(m_keyLocator);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 }
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700120
Mickey Sweatt89046c12014-11-16 20:32:27 -0800121 signature.setInfo(signatureInfo);
Alexander Afanasyevd2652352015-06-19 08:59:39 -0700122 signature.setValue(::ndn::nonNegativeIntegerBlock(::ndn::tlv::SignatureValue, m_signature));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800123
Mickey Sweatt89046c12014-11-16 20:32:27 -0800124 data->setSignature(signature);
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