blob: 844acd4660d4b3fd2466e06db63a98b63adb42b5 [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 Afanasyev09c7deb2011-11-23 14:50:10 -080030#include "ns3/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 Afanasyev09c7deb2011-11-23 14:50:10 -080056 // .AddTraceSource ("InterestTrace", "Interests that were received",
57 // MakeTraceSourceAccessor (&CcnxProducer::m_interestsTrace))
58 // .AddTraceSource ("ContentObjectTrace", "ContentObjects that were sent",
59 // MakeTraceSourceAccessor (&CcnxProducer::m_contentObjectsTrace))
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070060 ;
61
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080062 return tid;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070063}
64
65CcnxProducer::CcnxProducer ()
66{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080067 // NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070068}
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080069
70// inherited from Application base class.
71void
72CcnxProducer::StartApplication ()
73{
74 NS_LOG_FUNCTION_NOARGS ();
75 NS_ASSERT (GetNode ()->GetObject<CcnxFib> () != 0);
76
77 CcnxApp::StartApplication ();
78
79 GetNode ()->GetObject<CcnxFib> ()->Add (m_prefix, m_face, 0);
80}
81
82void
83CcnxProducer::StopApplication ()
84{
85 NS_LOG_FUNCTION_NOARGS ();
86 NS_ASSERT (GetNode ()->GetObject<CcnxFib> () != 0);
87
88 CcnxApp::StopApplication ();
89}
90
91
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070092void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080093CcnxProducer::OnInterest (const Ptr<const CcnxInterestHeader> &interest)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070094{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080095 NS_LOG_FUNCTION (this << interest);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070096
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080097 if (!m_active) return;
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070098
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099 static CcnxContentObjectTail tail;
100 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
101 header->SetName (Create<CcnxNameComponents> (interest->GetName ()));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800102
103 NS_LOG_INFO ("Respodning with ContentObject:\n" << boost::cref(*header));
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700104
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105 Ptr<Packet> packet = Create<Packet> (m_virtualPayloadSize);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106 packet->AddHeader (*header);
107 packet->AddTrailer (tail);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800108
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800109 m_protocolHandler (packet);
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700110}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800111
112} // namespace ns3