blob: cec8a1cc1cfbcbc9de9d41dd21b5abb262d8e446 [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
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070023#include "ccnx-app-face.h"
Alexander Afanasyev98256102011-08-14 01:00:02 -070024
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 Afanasyev98256102011-08-14 01:00:02 -070036
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070037NS_LOG_COMPONENT_DEFINE ("CcnxAppFace");
Alexander Afanasyev98256102011-08-14 01:00:02 -070038
Ilya Moiseenko1cf6b0a2011-08-29 13:02:34 -070039namespace ns3
40{
Alexander Afanasyev98256102011-08-14 01:00:02 -070041
Alexander Afanasyev3073da82012-06-19 14:57:43 -070042NS_OBJECT_ENSURE_REGISTERED (CcnxAppFace);
43
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080044TypeId
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070045CcnxAppFace::GetTypeId ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080046{
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070047 static TypeId tid = TypeId ("ns3::CcnxAppFace")
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080048 .SetParent<CcnxFace> ()
49 .SetGroupName ("Ccnx")
50 ;
51 return tid;
52}
53
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070054CcnxAppFace::CcnxAppFace (Ptr<CcnxApp> app)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080055 : CcnxFace (app->GetNode ())
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080056 , m_app (app)
Alexander Afanasyev98256102011-08-14 01:00:02 -070057{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080058 NS_LOG_FUNCTION (this << app);
59
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080060 NS_ASSERT (m_app != 0);
Alexander Afanasyev98256102011-08-14 01:00:02 -070061}
62
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070063CcnxAppFace::~CcnxAppFace ()
Alexander Afanasyev98256102011-08-14 01:00:02 -070064{
65 NS_LOG_FUNCTION_NOARGS ();
66}
67
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070068CcnxAppFace::CcnxAppFace ()
Alexander Afanasyev07827182011-12-13 01:07:32 -080069 : CcnxFace (0)
70{
71}
72
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070073CcnxAppFace::CcnxAppFace (const CcnxAppFace &)
Alexander Afanasyev07827182011-12-13 01:07:32 -080074 : CcnxFace (0)
75{
76}
77
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070078CcnxAppFace& CcnxAppFace::operator= (const CcnxAppFace &)
Alexander Afanasyev07827182011-12-13 01:07:32 -080079{
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070080 return *((CcnxAppFace*)0);
Alexander Afanasyev07827182011-12-13 01:07:32 -080081}
82
83
Alexander Afanasyev98256102011-08-14 01:00:02 -070084void
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070085CcnxAppFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev98256102011-08-14 01:00:02 -070086{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080087 NS_LOG_FUNCTION (this);
Alexander Afanasyev98256102011-08-14 01:00:02 -070088
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080089 CcnxFace::RegisterProtocolHandler (handler);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070090
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080091 m_app->RegisterProtocolHandler (MakeCallback (&CcnxFace::Receive, this));
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070092}
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080093
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -070094bool
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -070095CcnxAppFace::SendImpl (Ptr<Packet> p)
Alexander Afanasyev98256102011-08-14 01:00:02 -070096{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080097 NS_LOG_FUNCTION (this << p);
Alexander Afanasyev98256102011-08-14 01:00:02 -070098
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070099 try
100 {
101 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
102 switch (type)
103 {
104 case CcnxHeaderHelper::INTEREST:
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800105 {
106 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
107 p->RemoveHeader (*header);
108
109 if (header->GetNack () > 0)
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800110 m_app->OnNack (header, p);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800111 else
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -0800112 m_app->OnInterest (header, p);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800113
114 break;
115 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700116 case CcnxHeaderHelper::CONTENT_OBJECT:
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800117 {
118 static CcnxContentObjectTail tail;
119 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
120 p->RemoveHeader (*header);
121 p->RemoveTrailer (tail);
122 m_app->OnContentObject (header, p/*payload*/);
123
124 break;
125 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700126 }
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700127
128 return true;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700129 }
130 catch (CcnxUnknownHeaderException)
131 {
132 NS_LOG_ERROR ("Unknown header type");
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700133 return false;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700134 }
Ilya Moiseenko1cf6b0a2011-08-29 13:02:34 -0700135}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700136
Alexander Afanasyev4a4ea602012-06-06 11:12:45 -0700137std::ostream& CcnxAppFace::Print (std::ostream& os) const
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700138{
139 os << "dev=local(" << GetId() << ")";
Alexander Afanasyev98256102011-08-14 01:00:02 -0700140 return os;
141}
142
143}; // namespace ns3
144