Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 22 | #include "ndn-producer.h" |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 23 | #include "ns3/log.h" |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 24 | #include "ns3/ndn-interest.h" |
| 25 | #include "ns3/ndn-content-object.h" |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 26 | #include "ns3/string.h" |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 27 | #include "ns3/uinteger.h" |
| 28 | #include "ns3/packet.h" |
Alexander Afanasyev | faf9581 | 2012-06-05 21:28:11 -0700 | [diff] [blame] | 29 | #include "ns3/simulator.h" |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 31 | #include "ns3/ndn-app-face.h" |
| 32 | #include "ns3/ndn-fib.h" |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 33 | |
| 34 | #include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h" |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 35 | |
| 36 | #include <boost/ref.hpp> |
Alexander Afanasyev | b7ad232 | 2012-01-17 22:54:49 -0800 | [diff] [blame] | 37 | #include <boost/lambda/lambda.hpp> |
| 38 | #include <boost/lambda/bind.hpp> |
| 39 | namespace ll = boost::lambda; |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 40 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 41 | NS_LOG_COMPONENT_DEFINE ("ndn.Producer"); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 43 | namespace ns3 { |
| 44 | namespace ndn { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 46 | NS_OBJECT_ENSURE_REGISTERED (Producer); |
Alexander Afanasyev | e722148 | 2013-07-15 18:24:37 -0700 | [diff] [blame] | 47 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 48 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 49 | Producer::GetTypeId (void) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 50 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 51 | static TypeId tid = TypeId ("ns3::ndn::Producer") |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 52 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 53 | .SetParent<App> () |
| 54 | .AddConstructor<Producer> () |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 55 | .AddAttribute ("Prefix","Prefix, for which producer has the data", |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 56 | StringValue ("/"), |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 57 | MakeNameAccessor (&Producer::m_prefix), |
| 58 | MakeNameChecker ()) |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 59 | .AddAttribute ("Postfix", "Postfix that is added to the output data (e.g., for adding producer-uniqueness)", |
| 60 | StringValue ("/"), |
| 61 | MakeNameAccessor (&Producer::m_postfix), |
| 62 | MakeNameChecker ()) |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 63 | .AddAttribute ("PayloadSize", "Virtual payload size for Content packets", |
Alexander Afanasyev | c39f0b4 | 2011-11-28 12:51:12 -0800 | [diff] [blame] | 64 | UintegerValue (1024), |
Alexander Afanasyev | e722148 | 2013-07-15 18:24:37 -0700 | [diff] [blame] | 65 | MakeUintegerAccessor (&Producer::m_virtualPayloadSize), |
| 66 | MakeUintegerChecker<uint32_t> ()) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 67 | .AddAttribute ("Freshness", "Freshness of data packets, if 0, then unlimited freshness", |
| 68 | TimeValue (Seconds (0)), |
| 69 | MakeTimeAccessor (&Producer::m_freshness), |
| 70 | MakeTimeChecker ()) |
Alexander Afanasyev | e722148 | 2013-07-15 18:24:37 -0700 | [diff] [blame] | 71 | .AddAttribute ("Signature", "Fake signature, 0 valid signature (default), other values application-specific", |
| 72 | UintegerValue (0), |
| 73 | MakeUintegerAccessor (&Producer::m_signature), |
| 74 | MakeUintegerChecker<uint32_t> ()) |
| 75 | .AddAttribute ("KeyLocator", "Name to be used for key locator. If root, then key locator is not used", |
| 76 | NameValue (), |
| 77 | MakeNameAccessor (&Producer::m_keyLocator), |
| 78 | MakeNameChecker ()) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 79 | ; |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 80 | return tid; |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 81 | } |
Alexander Afanasyev | e722148 | 2013-07-15 18:24:37 -0700 | [diff] [blame] | 82 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 83 | Producer::Producer () |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 84 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 85 | // NS_LOG_FUNCTION_NOARGS (); |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 86 | } |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 87 | |
| 88 | // inherited from Application base class. |
| 89 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 90 | Producer::StartApplication () |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 91 | { |
| 92 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 93 | NS_ASSERT (GetNode ()->GetObject<Fib> () != 0); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 95 | App::StartApplication (); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 97 | NS_LOG_DEBUG ("NodeID: " << GetNode ()->GetId ()); |
Alexander Afanasyev | e722148 | 2013-07-15 18:24:37 -0700 | [diff] [blame] | 98 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 99 | Ptr<Fib> fib = GetNode ()->GetObject<Fib> (); |
Alexander Afanasyev | e722148 | 2013-07-15 18:24:37 -0700 | [diff] [blame] | 100 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 101 | Ptr<fib::Entry> fibEntry = fib->Add (m_prefix, m_face, 0); |
Alexander Afanasyev | 1aa4bbc | 2012-07-09 17:17:25 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 103 | fibEntry->UpdateStatus (m_face, fib::FaceMetric::NDN_FIB_GREEN); |
Alexander Afanasyev | e722148 | 2013-07-15 18:24:37 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | 1aa4bbc | 2012-07-09 17:17:25 -0700 | [diff] [blame] | 105 | // // make face green, so it will be used primarily |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 106 | // StaticCast<fib::FibImpl> (fib)->modify (fibEntry, |
| 107 | // ll::bind (&fib::Entry::UpdateStatus, |
| 108 | // ll::_1, m_face, fib::FaceMetric::NDN_FIB_GREEN)); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 112 | Producer::StopApplication () |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 113 | { |
| 114 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 115 | NS_ASSERT (GetNode ()->GetObject<Fib> () != 0); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 117 | App::StopApplication (); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 121 | void |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 122 | Producer::OnInterest (Ptr<const Interest> interest) |
Ilya Moiseenko | 8196d2e | 2011-08-29 13:03:22 -0700 | [diff] [blame] | 123 | { |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 124 | App::OnInterest (interest); // tracing inside |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame] | 125 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 126 | NS_LOG_FUNCTION (this << interest); |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 127 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 128 | if (!m_active) return; |
Alexander Afanasyev | e722148 | 2013-07-15 18:24:37 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 130 | Ptr<ContentObject> data = Create<ContentObject> (Create<Packet> (m_virtualPayloadSize)); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 131 | Ptr<Name> dataName = Create<Name> (interest->GetName ()); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 132 | dataName->append (m_postfix); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 133 | data->SetName (dataName); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 134 | data->SetFreshness (m_freshness); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 135 | |
Alexander Afanasyev | e722148 | 2013-07-15 18:24:37 -0700 | [diff] [blame] | 136 | data->SetSignature (m_signature); |
| 137 | if (m_keyLocator.size () > 0) |
| 138 | { |
| 139 | data->SetKeyLocator (Create<Name> (m_keyLocator)); |
| 140 | } |
| 141 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 142 | NS_LOG_INFO ("node("<< GetNode()->GetId() <<") respodning with ContentObject:\n" << data->GetName ()); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 143 | |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 144 | // Echo back FwHopCountTag if exists |
| 145 | FwHopCountTag hopCountTag; |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 146 | if (interest->GetPayload ()->PeekPacketTag (hopCountTag)) |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 147 | { |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 148 | data->GetPayload ()->AddPacketTag (hopCountTag); |
Alexander Afanasyev | 1a0fff6 | 2013-01-19 14:29:51 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 151 | m_face->ReceiveData (data); |
| 152 | m_transmittedContentObjects (data, this, m_face); |
Ilya Moiseenko | b62c740 | 2011-10-28 13:02:18 -0700 | [diff] [blame] | 153 | } |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 154 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 155 | } // namespace ndn |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 156 | } // namespace ns3 |