blob: 90ccce034fede8f92653d08aca1b7165cb84ca0e [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 Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ndn-producer.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080023#include "ns3/log.h"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080024#include "ns3/ndn-interest.h"
25#include "ns3/ndn-content-object.h"
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080026#include "ns3/string.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080027#include "ns3/uinteger.h"
28#include "ns3/packet.h"
Alexander Afanasyevfaf95812012-06-05 21:28:11 -070029#include "ns3/simulator.h"
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070030
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070031#include "ns3/ndn-app-face.h"
32#include "ns3/ndn-fib.h"
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -080033
34#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080035
36#include <boost/ref.hpp>
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080037#include <boost/lambda/lambda.hpp>
38#include <boost/lambda/bind.hpp>
39namespace ll = boost::lambda;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070040
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041NS_LOG_COMPONENT_DEFINE ("ndn.Producer");
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070042
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043namespace ns3 {
44namespace ndn {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080045
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046NS_OBJECT_ENSURE_REGISTERED (Producer);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070047
48TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049Producer::GetTypeId (void)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070050{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070051 static TypeId tid = TypeId ("ns3::ndn::Producer")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070052 .SetGroupName ("Ndn")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070053 .SetParent<App> ()
54 .AddConstructor<Producer> ()
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070055 .AddAttribute ("Prefix","Prefix, for which producer has the data",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080056 StringValue ("/"),
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070057 MakeNameAccessor (&Producer::m_prefix),
58 MakeNameChecker ())
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070059 .AddAttribute ("PayloadSize", "Virtual payload size for Content packets",
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080060 UintegerValue (1024),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070061 MakeUintegerAccessor(&Producer::m_virtualPayloadSize),
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070062 MakeUintegerChecker<uint32_t>())
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080063 .AddAttribute ("Freshness", "Freshness of data packets, if 0, then unlimited freshness",
64 TimeValue (Seconds (0)),
65 MakeTimeAccessor (&Producer::m_freshness),
66 MakeTimeChecker ())
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070067 ;
68
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080069 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070070}
71
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070072Producer::Producer ()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070073{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -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 Afanasyev2b4c9472012-08-09 15:00:38 -070079Producer::StartApplication ()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080080{
81 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070082 NS_ASSERT (GetNode ()->GetObject<Fib> () != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080083
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070084 App::StartApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080085
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -070086 NS_LOG_DEBUG ("NodeID: " << GetNode ()->GetId ());
87
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070088 Ptr<Fib> fib = GetNode ()->GetObject<Fib> ();
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -070089
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070090 Ptr<fib::Entry> fibEntry = fib->Add (m_prefix, m_face, 0);
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -070091
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070092 fibEntry->UpdateStatus (m_face, fib::FaceMetric::NDN_FIB_GREEN);
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -070093
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -070094 // // make face green, so it will be used primarily
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095 // StaticCast<fib::FibImpl> (fib)->modify (fibEntry,
96 // ll::bind (&fib::Entry::UpdateStatus,
97 // ll::_1, m_face, fib::FaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080098}
99
100void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101Producer::StopApplication ()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800102{
103 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700104 NS_ASSERT (GetNode ()->GetObject<Fib> () != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800105
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106 App::StopApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800107}
108
109
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700110void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700111Producer::OnInterest (const Ptr<const Interest> &interest, Ptr<Packet> origPacket)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700112{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700113 App::OnInterest (interest, origPacket); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800114
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800115 NS_LOG_FUNCTION (this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700116
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800117 if (!m_active) return;
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700118
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700119 static ContentObjectTail tail;
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700120 Ptr<ContentObject> header = Create<ContentObject> ();
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700121 header->SetName (Create<Name> (interest->GetName ()));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800122 header->SetFreshness (m_freshness);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800123
Lucasa3295ab2012-02-04 13:36:31 -0800124 NS_LOG_INFO ("node("<< GetNode()->GetId() <<") respodning with ContentObject:\n" << boost::cref(*header));
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700125
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800126 Ptr<Packet> packet = Create<Packet> (m_virtualPayloadSize);
Alexander Afanasyev6bff0df2012-01-19 17:51:52 -0800127
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800128 packet->AddHeader (*header);
129 packet->AddTrailer (tail);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800130
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800131 // Echo back FwHopCountTag if exists
132 FwHopCountTag hopCountTag;
133 if (origPacket->RemovePacketTag (hopCountTag))
134 {
135 packet->AddPacketTag (hopCountTag);
136 }
137
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800138 m_protocolHandler (packet);
Alexander Afanasyev15f92992012-04-09 14:56:56 -0700139
140 m_transmittedContentObjects (header, packet, this, m_face);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700141}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800142
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700143} // namespace ndn
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800144} // namespace ns3