blob: 2d2ad9ef7df89dcf3f816452062a03b2e8e4efd5 [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
Mickey Sweatt89046c12014-11-16 20:32:27 -080029#include "model/ndn-app-face.hpp"
30#include "model/ndn-ns3.hpp"
31#include "model/ndn-l3-protocol.hpp"
32#include "helper/ndn-fib-helper.hpp"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080033
Mickey Sweatt89046c12014-11-16 20:32:27 -080034#include <memory>
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070035
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080036NS_LOG_COMPONENT_DEFINE("ndn.Producer");
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070037
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070038namespace ns3 {
39namespace ndn {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080040
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041NS_OBJECT_ENSURE_REGISTERED(Producer);
Alexander Afanasyeve7221482013-07-15 18:24:37 -070042
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070043TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044Producer::GetTypeId(void)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070045{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046 static TypeId tid =
47 TypeId("ns3::ndn::Producer")
48 .SetGroupName("Ndn")
49 .SetParent<App>()
50 .AddConstructor<Producer>()
51 .AddAttribute("Prefix", "Prefix, for which producer has the data", StringValue("/"),
52 MakeNameAccessor(&Producer::m_prefix), MakeNameChecker())
53 .AddAttribute(
54 "Postfix",
55 "Postfix that is added to the output data (e.g., for adding producer-uniqueness)",
56 StringValue("/"), MakeNameAccessor(&Producer::m_postfix), MakeNameChecker())
57 .AddAttribute("PayloadSize", "Virtual payload size for Content packets", UintegerValue(1024),
58 MakeUintegerAccessor(&Producer::m_virtualPayloadSize),
59 MakeUintegerChecker<uint32_t>())
60 .AddAttribute("Freshness", "Freshness of data packets, if 0, then unlimited freshness",
61 TimeValue(Seconds(0)), MakeTimeAccessor(&Producer::m_freshness),
62 MakeTimeChecker())
63 .AddAttribute(
64 "Signature",
65 "Fake signature, 0 valid signature (default), other values application-specific",
66 UintegerValue(0), MakeUintegerAccessor(&Producer::m_signature),
67 MakeUintegerChecker<uint32_t>())
68 .AddAttribute("KeyLocator",
69 "Name to be used for key locator. If root, then key locator is not used",
70 NameValue(), MakeNameAccessor(&Producer::m_keyLocator), MakeNameChecker());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080071 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070072}
Alexander Afanasyeve7221482013-07-15 18:24:37 -070073
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074Producer::Producer()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070075{
Mickey Sweatt89046c12014-11-16 20:32:27 -080076 NS_LOG_FUNCTION_NOARGS();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070077}
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080078
79// inherited from Application base class.
80void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081Producer::StartApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080082{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084 App::StartApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080085
Mickey Sweatt89046c12014-11-16 20:32:27 -080086 FibHelper::AddRoute(GetNode(), m_prefix, m_face, 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080087}
88
89void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090Producer::StopApplication()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080091{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 NS_LOG_FUNCTION_NOARGS();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080093
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 App::StopApplication();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080095}
96
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070097void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070098Producer::OnInterest(shared_ptr<const Interest> interest)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070099{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 App::OnInterest(interest); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800101
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 NS_LOG_FUNCTION(this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700103
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 if (!m_active)
105 return;
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700106
Mickey Sweatt89046c12014-11-16 20:32:27 -0800107 Name dataName(interest->getName());
108 // dataName.append(m_postfix);
109 // dataName.appendVersion();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800110
Mickey Sweatt89046c12014-11-16 20:32:27 -0800111 auto data = make_shared<Data>();
112 data->setName(dataName);
113 data->setFreshnessPeriod(::ndn::time::milliseconds(m_freshness.GetMilliSeconds()));
114
115 data->setContent(make_shared<::ndn::Buffer>(m_virtualPayloadSize));
116
117 Signature signature;
118 SignatureInfo signatureInfo(static_cast<::ndn::tlv::SignatureTypeValue>(255));
119
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 if (m_keyLocator.size() > 0) {
Mickey Sweatt89046c12014-11-16 20:32:27 -0800121 signatureInfo.setKeyLocator(m_keyLocator);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 }
Alexander Afanasyeve7221482013-07-15 18:24:37 -0700123
Mickey Sweatt89046c12014-11-16 20:32:27 -0800124 signature.setInfo(signatureInfo);
125 signature.setValue(Block(&m_signature, sizeof(m_signature)));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800126
Mickey Sweatt89046c12014-11-16 20:32:27 -0800127 data->setSignature(signature);
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800128
Mickey Sweatt89046c12014-11-16 20:32:27 -0800129 NS_LOG_INFO("node(" << GetNode()->GetId() << ") respodning with Data: " << data->getName());
130
131 // to create real wire encoding
132 data->wireEncode();
133
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800134 m_transmittedDatas(data, this, m_face);
Mickey Sweatt89046c12014-11-16 20:32:27 -0800135 m_face->onReceiveData(*data);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700136}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800137
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700138} // namespace ndn
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800139} // namespace ns3