blob: e735b9a2ae86bfb66d886c8139b761c360da5a37 [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);
61}
62
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070063AppFace::~AppFace ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070064{
65 NS_LOG_FUNCTION_NOARGS ();
66}
67
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070068AppFace::AppFace ()
69 : Face (0)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070070{
71}
72
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073AppFace::AppFace (const AppFace &)
74 : Face (0)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070075{
76}
77
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070078AppFace& AppFace::operator= (const AppFace &)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070079{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070080 return *((AppFace*)0);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070081}
82
83
84void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070085AppFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070086{
87 NS_LOG_FUNCTION (this);
88
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089 Face::RegisterProtocolHandler (handler);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070090
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070091 m_app->RegisterProtocolHandler (MakeCallback (&Face::Receive, this));
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070092}
93
94bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095AppFace::SendImpl (Ptr<Packet> p)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070096{
97 NS_LOG_FUNCTION (this << p);
98
99 try
100 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (p);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700102 switch (type)
103 {
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800104 case HeaderHelper::INTEREST_NDNSIM:
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700105 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106 Ptr<InterestHeader> header = Create<InterestHeader> ();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700107 p->RemoveHeader (*header);
108
109 if (header->GetNack () > 0)
110 m_app->OnNack (header, p);
111 else
112 m_app->OnInterest (header, p);
113
114 break;
115 }
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800116 case HeaderHelper::CONTENT_OBJECT_NDNSIM:
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700117 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700118 static ContentObjectTail tail;
119 Ptr<ContentObjectHeader> header = Create<ContentObjectHeader> ();
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700120 p->RemoveHeader (*header);
121 p->RemoveTrailer (tail);
122 m_app->OnContentObject (header, p/*payload*/);
123
124 break;
125 }
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -0800126 default:
127 NS_FATAL_ERROR ("ccnb support is currently broken");
128 break;
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700129 }
130
131 return true;
132 }
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700133 catch (UnknownHeaderException)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700134 {
135 NS_LOG_ERROR ("Unknown header type");
136 return false;
137 }
138}
139
140std::ostream&
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700141AppFace::Print (std::ostream& os) const
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700142{
143 os << "dev=local(" << GetId() << ")";
144 return os;
145}
146
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700147} // namespace ndn
148} // namespace ns3
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700149