blob: cff6af44e8757d5f326b506c383f8cf6647748d3 [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
22#include "ccnx-producer.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080023#include "ns3/log.h"
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080024#include "ns3/ccnx-interest-header.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080025#include "ns3/ccnx-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"
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070029
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080030#include "../model/ccnx-local-face.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080031#include "ns3/ccnx-fib.h"
32
33#include <boost/ref.hpp>
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080034#include <boost/lambda/lambda.hpp>
35#include <boost/lambda/bind.hpp>
36namespace ll = boost::lambda;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070037
38NS_LOG_COMPONENT_DEFINE ("CcnxProducer");
39
40namespace ns3
41{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080042
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070043NS_OBJECT_ENSURE_REGISTERED (CcnxProducer);
44
45TypeId
46CcnxProducer::GetTypeId (void)
47{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080048 static TypeId tid = TypeId ("ns3::CcnxProducer")
49 .SetParent<CcnxApp> ()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070050 .AddConstructor<CcnxProducer> ()
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070051 .AddAttribute ("Prefix","Prefix, for which producer has the data",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080052 StringValue ("/"),
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070053 MakeCcnxNameComponentsAccessor (&CcnxProducer::m_prefix),
54 MakeCcnxNameComponentsChecker ())
55 .AddAttribute ("PayloadSize", "Virtual payload size for Content packets",
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080056 UintegerValue (1024),
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070057 MakeUintegerAccessor(&CcnxProducer::m_virtualPayloadSize),
58 MakeUintegerChecker<uint32_t>())
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080059
60 .AddTraceSource ("TransmittedContentObjects", "TransmittedContentObjects",
61 MakeTraceSourceAccessor (&CcnxProducer::m_transmittedContentObjects))
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070062 ;
63
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080064 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070065}
66
67CcnxProducer::CcnxProducer ()
68{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080069 // NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070070}
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080071
72// inherited from Application base class.
73void
74CcnxProducer::StartApplication ()
75{
76 NS_LOG_FUNCTION_NOARGS ();
77 NS_ASSERT (GetNode ()->GetObject<CcnxFib> () != 0);
78
79 CcnxApp::StartApplication ();
80
Alexander Afanasyevb7ad2322012-01-17 22:54:49 -080081 Ptr<CcnxFib> fib = GetNode ()->GetObject<CcnxFib> ();
82 CcnxFibEntryContainer::type::iterator fibEntry = fib->Add (m_prefix, m_face, 0);
83
84 // make face green, so it will be used primarily
85 fib->m_fib.modify (fibEntry,
86 ll::bind (&CcnxFibEntry::UpdateStatus,
87 ll::_1, m_face, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080088}
89
90void
91CcnxProducer::StopApplication ()
92{
93 NS_LOG_FUNCTION_NOARGS ();
94 NS_ASSERT (GetNode ()->GetObject<CcnxFib> () != 0);
95
96 CcnxApp::StopApplication ();
97}
98
99
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700100void
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800101CcnxProducer::OnInterest (const Ptr<const CcnxInterestHeader> &interest, Ptr<Packet> origPacket)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700102{
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800103 CcnxApp::OnInterest (interest, origPacket); // tracing inside
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800104
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105 NS_LOG_FUNCTION (this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700106
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800107 if (!m_active) return;
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700108
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109 static CcnxContentObjectTail tail;
110 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
111 header->SetName (Create<CcnxNameComponents> (interest->GetName ()));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800112
113 NS_LOG_INFO ("Respodning with ContentObject:\n" << boost::cref(*header));
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700114
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800115 Ptr<Packet> packet = Create<Packet> (m_virtualPayloadSize);
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800116
117 m_transmittedContentObjects (header, packet, this, m_face);
118
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800119 packet->AddHeader (*header);
120 packet->AddTrailer (tail);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800121
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800122 m_protocolHandler (packet);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700123}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800124
125} // namespace ns3