blob: fc4dd119a2df2106be28fd119f2067063e9cd736 [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev98256102011-08-14 01:00:02 -07002/*
3 * Copyright (c) 2005,2006,2007 INRIA
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 *
Ilya Moiseenko1cf6b0a2011-08-29 13:02:34 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070019 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Alexander Afanasyev98256102011-08-14 01:00:02 -070020 *
21 */
22
23#include "ccnx-local-face.h"
24
Alexander Afanasyev98256102011-08-14 01:00:02 -070025#include "ns3/log.h"
26#include "ns3/packet.h"
27#include "ns3/node.h"
28#include "ns3/pointer.h"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070029#include "ns3/assert.h"
30
31#include "ns3/ccnx-header-helper.h"
32#include "ccnx-interest-header.h"
33#include "ccnx-content-object-header.h"
Alexander Afanasyev98256102011-08-14 01:00:02 -070034
35NS_LOG_COMPONENT_DEFINE ("CcnxLocalFace");
36
Ilya Moiseenko1cf6b0a2011-08-29 13:02:34 -070037namespace ns3
38{
Alexander Afanasyev98256102011-08-14 01:00:02 -070039
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070040// NS_OBJECT_ENSURE_REGISTERED (CcnxLocalFace);
Alexander Afanasyev98256102011-08-14 01:00:02 -070041
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070042// TypeId
43// CcnxLocalFace::GetTypeId (void)
44// {
45// static TypeId tid = TypeId ("ns3::CcnxLocalFace")
46// .SetGroupName ("Ccnx")
47// .SetParent<CcnxFace> ()
48// ;
49// return tid;
50// }
Alexander Afanasyev98256102011-08-14 01:00:02 -070051
Alexander Afanasyev98256102011-08-14 01:00:02 -070052CcnxLocalFace::CcnxLocalFace ()
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070053 : m_onInterest (0)
54 , m_onContentObject (0)
Alexander Afanasyev98256102011-08-14 01:00:02 -070055{
56 NS_LOG_FUNCTION (this);
57}
58
59CcnxLocalFace::~CcnxLocalFace ()
60{
61 NS_LOG_FUNCTION_NOARGS ();
62}
63
64void
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070065CcnxLocalFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev98256102011-08-14 01:00:02 -070066{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070067 m_protocolHandler = handler;
Alexander Afanasyev98256102011-08-14 01:00:02 -070068}
69
Alexander Afanasyev98256102011-08-14 01:00:02 -070070void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070071CcnxLocalFace::SetInterestHandler (InterestHandler onInterest)
72{
73 m_onInterest = onInterest;
74}
75
76void
77CcnxLocalFace::SetContentObjectHandler (ContentObjectHandler onContentObject)
78{
79 m_onContentObject = onContentObject;
80}
Ilya Moiseenkoacac1ea2011-10-28 13:16:53 -070081
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070082void
Alexander Afanasyev98256102011-08-14 01:00:02 -070083CcnxLocalFace::Send (Ptr<Packet> p)
84{
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080085 NS_LOG_FUNCTION("Local face send");
Alexander Afanasyev98256102011-08-14 01:00:02 -070086 NS_LOG_FUNCTION (*p);
87 if (!IsUp ())
88 {
89 return;
90 }
91
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070092 try
93 {
94 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
95 switch (type)
96 {
97 case CcnxHeaderHelper::INTEREST:
98 if (!m_onInterest.IsNull ())
99 {
100 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
101 p->RemoveHeader (*header);
102 m_onInterest (header);
103 }
104 break;
105 case CcnxHeaderHelper::CONTENT_OBJECT:
106 if (!m_onContentObject.IsNull ())
107 {
108 static CcnxContentObjectTail tail;
109 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
110 p->RemoveHeader (*header);
111 p->RemoveTrailer (tail);
112 m_onContentObject (header, p/*payload*/);
113 }
114 break;
115 }
116 }
117 catch (CcnxUnknownHeaderException)
118 {
119 NS_LOG_ERROR ("Unknown header type");
120 }
Ilya Moiseenko1cf6b0a2011-08-29 13:02:34 -0700121}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700122
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700123// propagate interest down to ccnx stack
124void
125CcnxLocalFace::ReceiveFromApplication (Ptr<Packet> p)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700126{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700127 m_protocolHandler (Ptr<CcnxFace>(this), p);
128}
129
130std::ostream& CcnxLocalFace::Print (std::ostream& os) const
131{
132 os << "dev=local(" << GetId() << ")";
Alexander Afanasyev98256102011-08-14 01:00:02 -0700133 return os;
134}
135
136}; // namespace ns3
137