blob: fe8e42f10ea151dc57575caffcf577a5aca69987 [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-app-face.hpp"
28#include "model/ndn-ns3.hpp"
29#include "model/ndn-l3-protocol.hpp"
30#include "helper/ndn-fib-helper.hpp"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080031
Mickey Sweatt89046c12014-11-16 20:32:27 -080032#include <memory>
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070033
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034NS_LOG_COMPONENT_DEFINE("ndn.Producer");
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070035
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036namespace ns3 {
37namespace ndn {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080038
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039NS_OBJECT_ENSURE_REGISTERED(Producer);
Alexander Afanasyeve7221482013-07-15 18:24:37 -070040
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070041TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042Producer::GetTypeId(void)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070043{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 static TypeId tid =
45 TypeId("ns3::ndn::Producer")
46 .SetGroupName("Ndn")
47 .SetParent<App>()
48 .AddConstructor<Producer>()
49 .AddAttribute("Prefix", "Prefix, for which producer has the data", StringValue("/"),
50 MakeNameAccessor(&Producer::m_prefix), MakeNameChecker())
51 .AddAttribute(
52 "Postfix",
53 "Postfix that is added to the output data (e.g., for adding producer-uniqueness)",
54 StringValue("/"), MakeNameAccessor(&Producer::m_postfix), MakeNameChecker())
55 .AddAttribute("PayloadSize", "Virtual payload size for Content packets", UintegerValue(1024),
56 MakeUintegerAccessor(&Producer::m_virtualPayloadSize),
57 MakeUintegerChecker<uint32_t>())
58 .AddAttribute("Freshness", "Freshness of data packets, if 0, then unlimited freshness",
59 TimeValue(Seconds(0)), MakeTimeAccessor(&Producer::m_freshness),
60 MakeTimeChecker())
61 .AddAttribute(
62 "Signature",
63 "Fake signature, 0 valid signature (default), other values application-specific",
64 UintegerValue(0), MakeUintegerAccessor(&Producer::m_signature),
65 MakeUintegerChecker<uint32_t>())
66 .AddAttribute("KeyLocator",
67 "Name to be used for key locator. If root, then key locator is not used",
68 NameValue(), MakeNameAccessor(&Producer::m_keyLocator), MakeNameChecker());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080069 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070070}
Alexander Afanasyeve7221482013-07-15 18:24:37 -070071
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072Producer::Producer()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070073{
Mickey Sweatt89046c12014-11-16 20:32:27 -080074 NS_LOG_FUNCTION_NOARGS();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070075}
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080076
77// inherited from Application base class.
78void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079Producer::StartApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080080{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 App::StartApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080083
Mickey Sweatt89046c12014-11-16 20:32:27 -080084 FibHelper::AddRoute(GetNode(), m_prefix, m_face, 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080085}
86
87void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088Producer::StopApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080089{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080091
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 App::StopApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080093}
94
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070095void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070096Producer::OnInterest(shared_ptr<const Interest> interest)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070097{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 App::OnInterest(interest); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080099
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 NS_LOG_FUNCTION(this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700101
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 if (!m_active)
103 return;
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700104
Mickey Sweatt89046c12014-11-16 20:32:27 -0800105 Name dataName(interest->getName());
106 // dataName.append(m_postfix);
107 // dataName.appendVersion();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800108
Mickey Sweatt89046c12014-11-16 20:32:27 -0800109 auto data = make_shared<Data>();
110 data->setName(dataName);
111 data->setFreshnessPeriod(::ndn::time::milliseconds(m_freshness.GetMilliSeconds()));
112
113 data->setContent(make_shared<::ndn::Buffer>(m_virtualPayloadSize));
114
115 Signature signature;
116 SignatureInfo signatureInfo(static_cast<::ndn::tlv::SignatureTypeValue>(255));
117
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 if (m_keyLocator.size() > 0) {
Mickey Sweatt89046c12014-11-16 20:32:27 -0800119 signatureInfo.setKeyLocator(m_keyLocator);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 }
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700121
Mickey Sweatt89046c12014-11-16 20:32:27 -0800122 signature.setInfo(signatureInfo);
123 signature.setValue(Block(&m_signature, sizeof(m_signature)));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800124
Mickey Sweatt89046c12014-11-16 20:32:27 -0800125 data->setSignature(signature);
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800126
Mickey Sweatt89046c12014-11-16 20:32:27 -0800127 NS_LOG_INFO("node(" << GetNode()->GetId() << ") respodning with Data: " << data->getName());
128
129 // to create real wire encoding
130 data->wireEncode();
131
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132 m_transmittedDatas(data, this, m_face);
Mickey Sweatt89046c12014-11-16 20:32:27 -0800133 m_face->onReceiveData(*data);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700134}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800135
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700136} // namespace ndn
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800137} // namespace ns3