blob: 4f7afc4cc05d6c3691e421b7e73ebc35b0bb49b4 [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"
33// #include "../model/ndn-fib-impl.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080034
35#include <boost/ref.hpp>
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080036#include <boost/lambda/lambda.hpp>
37#include <boost/lambda/bind.hpp>
38namespace ll = boost::lambda;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070039
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040NS_LOG_COMPONENT_DEFINE ("ndn.Producer");
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070041
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042namespace ns3 {
43namespace ndn {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080044
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045NS_OBJECT_ENSURE_REGISTERED (Producer);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070046
47TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048Producer::GetTypeId (void)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070049{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070050 static TypeId tid = TypeId ("ns3::ndn::Producer")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070051 .SetGroupName ("Ndn")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070052 .SetParent<App> ()
53 .AddConstructor<Producer> ()
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070054 .AddAttribute ("Prefix","Prefix, for which producer has the data",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080055 StringValue ("/"),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070056 MakeNameComponentsAccessor (&Producer::m_prefix),
57 MakeNameComponentsChecker ())
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070058 .AddAttribute ("PayloadSize", "Virtual payload size for Content packets",
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080059 UintegerValue (1024),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070060 MakeUintegerAccessor(&Producer::m_virtualPayloadSize),
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070061 MakeUintegerChecker<uint32_t>())
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080062 .AddAttribute ("Freshness", "Freshness of data packets, if 0, then unlimited freshness",
63 TimeValue (Seconds (0)),
64 MakeTimeAccessor (&Producer::m_freshness),
65 MakeTimeChecker ())
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070066 ;
67
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080068 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070069}
70
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070071Producer::Producer ()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070072{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -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 Afanasyev2b4c9472012-08-09 15:00:38 -070078Producer::StartApplication ()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080079{
80 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070081 NS_ASSERT (GetNode ()->GetObject<Fib> () != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080082
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070083 App::StartApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080084
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -070085 NS_LOG_DEBUG ("NodeID: " << GetNode ()->GetId ());
86
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070087 Ptr<Fib> fib = GetNode ()->GetObject<Fib> ();
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -070088
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089 Ptr<fib::Entry> fibEntry = fib->Add (m_prefix, m_face, 0);
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -070090
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070091 fibEntry->UpdateStatus (m_face, fib::FaceMetric::NDN_FIB_GREEN);
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -070092
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -070093 // // make face green, so it will be used primarily
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070094 // StaticCast<fib::FibImpl> (fib)->modify (fibEntry,
95 // ll::bind (&fib::Entry::UpdateStatus,
96 // ll::_1, m_face, fib::FaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080097}
98
99void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700100Producer::StopApplication ()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800101{
102 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700103 NS_ASSERT (GetNode ()->GetObject<Fib> () != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800104
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700105 App::StopApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106}
107
108
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700109void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700110Producer::OnInterest (const Ptr<const InterestHeader> &interest, Ptr<Packet> origPacket)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700111{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700112 App::OnInterest (interest, origPacket); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800113
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800114 NS_LOG_FUNCTION (this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700115
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116 if (!m_active) return;
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700117
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700118 static ContentObjectTail tail;
119 Ptr<ContentObjectHeader> header = Create<ContentObjectHeader> ();
120 header->SetName (Create<NameComponents> (interest->GetName ()));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800121 header->SetFreshness (m_freshness);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800122
Lucasa3295ab2012-02-04 13:36:31 -0800123 NS_LOG_INFO ("node("<< GetNode()->GetId() <<") respodning with ContentObject:\n" << boost::cref(*header));
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700124
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800125 Ptr<Packet> packet = Create<Packet> (m_virtualPayloadSize);
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800126 // Ptr<const WeightsPathStretchTag> tag = origPacket->RemovePacketTag<WeightsPathStretchTag> ();
127 // if (tag != 0)
128 // {
129 // // std::cout << Simulator::Now () << ", " << m_app->GetInstanceTypeId ().GetName () << "\n";
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800130
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800131 // // echo back WeightsPathStretchTag
132 // packet->AddPacketTag (CreateObject<WeightsPathStretchTag> (*tag));
Alexander Afanasyev6bff0df2012-01-19 17:51:52 -0800133
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800134 // // \todo
135 // // packet->AddPacketTag should actually accept Ptr<const WeightsPathStretchTag> instead of
136 // // Ptr<WeightsPathStretchTag>. Echoing will be simplified after change is done
137 // }
Alexander Afanasyev6bff0df2012-01-19 17:51:52 -0800138
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800139 packet->AddHeader (*header);
140 packet->AddTrailer (tail);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800141
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800142 m_protocolHandler (packet);
Alexander Afanasyev15f92992012-04-09 14:56:56 -0700143
144 m_transmittedContentObjects (header, packet, this, m_face);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700145}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800146
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700147} // namespace ndn
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148} // namespace ns3