blob: 669f9a89f01ae664c13dd6998dac568f6649a475 [file] [log] [blame]
Alexander Afanasyev4aac5572012-08-09 10:49:55 -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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Ilya Moiseenko <iliamo@cs.ucla.edu>
20 *
21 */
22
23#include "ndn-app-face.h"
24
25#include "ns3/log.h"
26#include "ns3/packet.h"
27#include "ns3/node.h"
28#include "ns3/assert.h"
29#include "ns3/simulator.h"
30
31#include "ns3/ndn-header-helper.h"
32#include "ns3/ndn-app.h"
33
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080034#include "ndn-interest.h"
35#include "ndn-content-object.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070036
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037NS_LOG_COMPONENT_DEFINE ("ndn.AppFace");
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070038
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070039namespace ns3 {
40namespace ndn {
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070041
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042NS_OBJECT_ENSURE_REGISTERED (AppFace);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070043
44TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045AppFace::GetTypeId ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070046{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070047 static TypeId tid = TypeId ("ns3::ndn::AppFace")
48 .SetParent<Face> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070049 .SetGroupName ("Ndn")
50 ;
51 return tid;
52}
53
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070054AppFace::AppFace (Ptr<App> app)
55 : Face (app->GetNode ())
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070056 , m_app (app)
57{
58 NS_LOG_FUNCTION (this << app);
59
60 NS_ASSERT (m_app != 0);
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -070061 SetFlags (Face::APPLICATION);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070062}
63
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064AppFace::~AppFace ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065{
66 NS_LOG_FUNCTION_NOARGS ();
67}
68
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070069AppFace::AppFace ()
70 : Face (0)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070071{
72}
73
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070074AppFace::AppFace (const AppFace &)
75 : Face (0)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070076{
77}
78
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070079AppFace& AppFace::operator= (const AppFace &)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070080{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070081 return *((AppFace*)0);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070082}
83
84
85void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070086AppFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070087{
88 NS_LOG_FUNCTION (this);
89
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070090 Face::RegisterProtocolHandler (handler);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070091
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070092 m_app->RegisterProtocolHandler (MakeCallback (&Face::Receive, this));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070093}
94
95bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096AppFace::SendImpl (Ptr<Packet> p)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070097{
98 NS_LOG_FUNCTION (this << p);
99
100 try
101 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (p);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700103 switch (type)
104 {
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800105 case HeaderHelper::INTEREST_NDNSIM:
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700106 {
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700107 Ptr<Interest> header = Create<Interest> ();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700108 p->RemoveHeader (*header);
109
110 if (header->GetNack () > 0)
111 m_app->OnNack (header, p);
112 else
113 m_app->OnInterest (header, p);
114
115 break;
116 }
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800117 case HeaderHelper::CONTENT_OBJECT_NDNSIM:
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700118 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700119 static ContentObjectTail tail;
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700120 Ptr<ContentObject> header = Create<ContentObject> ();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700121 p->RemoveHeader (*header);
122 p->RemoveTrailer (tail);
123 m_app->OnContentObject (header, p/*payload*/);
124
125 break;
126 }
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800127 default:
128 NS_FATAL_ERROR ("ccnb support is currently broken");
129 break;
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700130 }
131
132 return true;
133 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700134 catch (UnknownHeaderException)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700135 {
136 NS_LOG_ERROR ("Unknown header type");
137 return false;
138 }
139}
140
141std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700142AppFace::Print (std::ostream& os) const
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700143{
144 os << "dev=local(" << GetId() << ")";
145 return os;
146}
147
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700148} // namespace ndn
149} // namespace ns3
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700150