blob: 0d711010b3ce1460abd2f23e85674b00f2c3c824 [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 Afanasyev4aac5572012-08-09 10:49:55 -070024#include "ns3/ndn-interest-header.h"
25#include "ns3/ndn-content-object-header.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 Afanasyevfaf95812012-06-05 21:28:11 -070062
63 // optional attributes
64 .AddAttribute ("SignatureBits", "SignatureBits field",
65 UintegerValue (0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070066 MakeUintegerAccessor(&Producer::m_signatureBits),
Alexander Afanasyevfaf95812012-06-05 21:28:11 -070067 MakeUintegerChecker<uint32_t> ())
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070068 ;
69
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080070 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070071}
72
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073Producer::Producer ()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070074{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080075 // NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070076}
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080077
78// inherited from Application base class.
79void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070080Producer::StartApplication ()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080081{
82 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070083 NS_ASSERT (GetNode ()->GetObject<Fib> () != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080084
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070085 App::StartApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080086
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -070087 NS_LOG_DEBUG ("NodeID: " << GetNode ()->GetId ());
88
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089 Ptr<Fib> fib = GetNode ()->GetObject<Fib> ();
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -070090
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070091 Ptr<fib::Entry> fibEntry = fib->Add (m_prefix, m_face, 0);
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -070092
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070093 fibEntry->UpdateStatus (m_face, fib::FaceMetric::NDN_FIB_GREEN);
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -070094
Alexander Afanasyev1aa4bbc2012-07-09 17:17:25 -070095 // // make face green, so it will be used primarily
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096 // StaticCast<fib::FibImpl> (fib)->modify (fibEntry,
97 // ll::bind (&fib::Entry::UpdateStatus,
98 // ll::_1, m_face, fib::FaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080099}
100
101void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102Producer::StopApplication ()
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800103{
104 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700105 NS_ASSERT (GetNode ()->GetObject<Fib> () != 0);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700107 App::StopApplication ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800108}
109
110
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700111void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700112Producer::OnInterest (const Ptr<const InterestHeader> &interest, Ptr<Packet> origPacket)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700113{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700114 App::OnInterest (interest, origPacket); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800115
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116 NS_LOG_FUNCTION (this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700117
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800118 if (!m_active) return;
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700119
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700120 static ContentObjectTail tail;
121 Ptr<ContentObjectHeader> header = Create<ContentObjectHeader> ();
122 header->SetName (Create<NameComponents> (interest->GetName ()));
Alexander Afanasyevfaf95812012-06-05 21:28:11 -0700123 header->GetSignedInfo ().SetTimestamp (Simulator::Now ());
124 header->GetSignature ().SetSignatureBits (m_signatureBits);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800125
Lucasa3295ab2012-02-04 13:36:31 -0800126 NS_LOG_INFO ("node("<< GetNode()->GetId() <<") respodning with ContentObject:\n" << boost::cref(*header));
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700127
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800128 Ptr<Packet> packet = Create<Packet> (m_virtualPayloadSize);
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800129 // Ptr<const WeightsPathStretchTag> tag = origPacket->RemovePacketTag<WeightsPathStretchTag> ();
130 // if (tag != 0)
131 // {
132 // // std::cout << Simulator::Now () << ", " << m_app->GetInstanceTypeId ().GetName () << "\n";
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800133
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800134 // // echo back WeightsPathStretchTag
135 // packet->AddPacketTag (CreateObject<WeightsPathStretchTag> (*tag));
Alexander Afanasyev6bff0df2012-01-19 17:51:52 -0800136
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800137 // // \todo
138 // // packet->AddPacketTag should actually accept Ptr<const WeightsPathStretchTag> instead of
139 // // Ptr<WeightsPathStretchTag>. Echoing will be simplified after change is done
140 // }
Alexander Afanasyev6bff0df2012-01-19 17:51:52 -0800141
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800142 packet->AddHeader (*header);
143 packet->AddTrailer (tail);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800144
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800145 m_protocolHandler (packet);
Alexander Afanasyev15f92992012-04-09 14:56:56 -0700146
147 m_transmittedContentObjects (header, packet, this, m_face);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700148}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800149
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700150} // namespace ndn
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800151} // namespace ns3