blob: 404e38f2073dce4f27a903b03f43c998f6e8a5b9 [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/*
Ilya Moiseenko956d0542012-01-02 15:26:40 -08003 * Copyright (c) 2011 University of California, Los Angeles
Alexander Afanasyev98256102011-08-14 01:00:02 -07004 *
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"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070028#include "ns3/assert.h"
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080029#include "ns3/simulator.h"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070030
31#include "ns3/ccnx-header-helper.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080032#include "ns3/ccnx-app.h"
33
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070034#include "ccnx-interest-header.h"
35#include "ccnx-content-object-header.h"
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080036#include "ccnx-path-stretch-tag.h"
Alexander Afanasyev98256102011-08-14 01:00:02 -070037
38NS_LOG_COMPONENT_DEFINE ("CcnxLocalFace");
39
Ilya Moiseenko1cf6b0a2011-08-29 13:02:34 -070040namespace ns3
41{
Alexander Afanasyev98256102011-08-14 01:00:02 -070042
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080043TypeId
44CcnxLocalFace::GetTypeId ()
45{
46 static TypeId tid = TypeId ("ns3::CcnxLocalFace")
47 .SetParent<CcnxFace> ()
48 .SetGroupName ("Ccnx")
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080049 .AddTraceSource ("PathWeightsTrace", "PathWeightsTrace",
50 MakeTraceSourceAccessor (&CcnxLocalFace::m_pathWeightsTrace))
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080051 ;
52 return tid;
53}
54
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080055CcnxLocalFace::CcnxLocalFace (Ptr<CcnxApp> app)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080056 : CcnxFace (app->GetNode ())
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080057 , m_app (app)
Alexander Afanasyev98256102011-08-14 01:00:02 -070058{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080059 NS_LOG_FUNCTION (this << app);
60
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080061 NS_ASSERT (m_app != 0);
Alexander Afanasyev98256102011-08-14 01:00:02 -070062}
63
64CcnxLocalFace::~CcnxLocalFace ()
65{
66 NS_LOG_FUNCTION_NOARGS ();
67}
68
Alexander Afanasyev07827182011-12-13 01:07:32 -080069CcnxLocalFace::CcnxLocalFace ()
70 : CcnxFace (0)
71{
72}
73
74CcnxLocalFace::CcnxLocalFace (const CcnxLocalFace &)
75 : CcnxFace (0)
76{
77}
78
79CcnxLocalFace& CcnxLocalFace::operator= (const CcnxLocalFace &)
80{
81 return *((CcnxLocalFace*)0);
82}
83
84
Alexander Afanasyev98256102011-08-14 01:00:02 -070085void
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070086CcnxLocalFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev98256102011-08-14 01:00:02 -070087{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080088 NS_LOG_FUNCTION (this);
Alexander Afanasyev98256102011-08-14 01:00:02 -070089
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080090 CcnxFace::RegisterProtocolHandler (handler);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070091
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080092 m_app->RegisterProtocolHandler (MakeCallback (&CcnxFace::Receive, this));
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070093}
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080094
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070095void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080096CcnxLocalFace::SendImpl (Ptr<Packet> p)
Alexander Afanasyev98256102011-08-14 01:00:02 -070097{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080098 NS_LOG_FUNCTION (this << p);
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080099 std::cout << Simulator::Now () << ", " << m_app->GetInstanceTypeId ().GetName () << "\n";
100
101 // Notify trace about path weights vector (e.g., for path-stretch calculation)
102 Ptr<const WeightsPathStretchTag> tag = p->RemovePacketTag<WeightsPathStretchTag> ();
103 if (tag != 0)
104 {
105 m_pathWeightsTrace (tag->GetTotalWeight (), tag->GetSourceNode (), m_app->GetNode ());
106 std::cout << boost::cref(*tag) << "\n";
107 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700108
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700109 try
110 {
111 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
112 switch (type)
113 {
114 case CcnxHeaderHelper::INTEREST:
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800115 {
116 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
117 p->RemoveHeader (*header);
118
119 if (header->GetNack () > 0)
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800120 m_app->OnNack (header, p);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800121 else
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800122 m_app->OnInterest (header, p);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800123
124 break;
125 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700126 case CcnxHeaderHelper::CONTENT_OBJECT:
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800127 {
128 static CcnxContentObjectTail tail;
129 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
130 p->RemoveHeader (*header);
131 p->RemoveTrailer (tail);
132 m_app->OnContentObject (header, p/*payload*/);
133
134 break;
135 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700136 }
137 }
138 catch (CcnxUnknownHeaderException)
139 {
140 NS_LOG_ERROR ("Unknown header type");
141 }
Ilya Moiseenko1cf6b0a2011-08-29 13:02:34 -0700142}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700143
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700144std::ostream& CcnxLocalFace::Print (std::ostream& os) const
145{
146 os << "dev=local(" << GetId() << ")";
Alexander Afanasyev98256102011-08-14 01:00:02 -0700147 return os;
148}
149
150}; // namespace ns3
151