blob: 3a37efd7896dacda0da63028caf2eec35c88b0ba [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>
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070034
35NS_LOG_COMPONENT_DEFINE ("CcnxProducer");
36
37namespace ns3
38{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080039
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070040NS_OBJECT_ENSURE_REGISTERED (CcnxProducer);
41
42TypeId
43CcnxProducer::GetTypeId (void)
44{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080045 static TypeId tid = TypeId ("ns3::CcnxProducer")
46 .SetParent<CcnxApp> ()
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070047 .AddConstructor<CcnxProducer> ()
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070048 .AddAttribute ("Prefix","Prefix, for which producer has the data",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080049 StringValue ("/"),
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070050 MakeCcnxNameComponentsAccessor (&CcnxProducer::m_prefix),
51 MakeCcnxNameComponentsChecker ())
52 .AddAttribute ("PayloadSize", "Virtual payload size for Content packets",
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080053 UintegerValue (1024),
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070054 MakeUintegerAccessor(&CcnxProducer::m_virtualPayloadSize),
55 MakeUintegerChecker<uint32_t>())
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080056
57 .AddTraceSource ("TransmittedContentObjects", "TransmittedContentObjects",
58 MakeTraceSourceAccessor (&CcnxProducer::m_transmittedContentObjects))
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070059 ;
60
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080061 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070062}
63
64CcnxProducer::CcnxProducer ()
65{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080066 // NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070067}
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080068
69// inherited from Application base class.
70void
71CcnxProducer::StartApplication ()
72{
73 NS_LOG_FUNCTION_NOARGS ();
74 NS_ASSERT (GetNode ()->GetObject<CcnxFib> () != 0);
75
76 CcnxApp::StartApplication ();
77
78 GetNode ()->GetObject<CcnxFib> ()->Add (m_prefix, m_face, 0);
79}
80
81void
82CcnxProducer::StopApplication ()
83{
84 NS_LOG_FUNCTION_NOARGS ();
85 NS_ASSERT (GetNode ()->GetObject<CcnxFib> () != 0);
86
87 CcnxApp::StopApplication ();
88}
89
90
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070091void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080092CcnxProducer::OnInterest (const Ptr<const CcnxInterestHeader> &interest)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070093{
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080094 CcnxApp::OnInterest (interest); // tracing inside
95
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080096 NS_LOG_FUNCTION (this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070097
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080098 if (!m_active) return;
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070099
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800100 static CcnxContentObjectTail tail;
101 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
102 header->SetName (Create<CcnxNameComponents> (interest->GetName ()));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800103
104 NS_LOG_INFO ("Respodning with ContentObject:\n" << boost::cref(*header));
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700105
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800106 Ptr<Packet> packet = Create<Packet> (m_virtualPayloadSize);
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800107
108 m_transmittedContentObjects (header, packet, this, m_face);
109
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800110 packet->AddHeader (*header);
111 packet->AddTrailer (tail);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800112
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800113 m_protocolHandler (packet);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700114}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800115
116} // namespace ns3