blob: 55cee9b1e6e4127e8fde6281d4e7b8ddd9a1f808 [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
34#include "ndn-interest-header.h"
35#include "ndn-content-object-header.h"
36
37NS_LOG_COMPONENT_DEFINE ("NdnAppFace");
38
39namespace ns3
40{
41
42NS_OBJECT_ENSURE_REGISTERED (NdnAppFace);
43
44TypeId
45NdnAppFace::GetTypeId ()
46{
47 static TypeId tid = TypeId ("ns3::NdnAppFace")
48 .SetParent<NdnFace> ()
49 .SetGroupName ("Ndn")
50 ;
51 return tid;
52}
53
54NdnAppFace::NdnAppFace (Ptr<NdnApp> app)
55 : NdnFace (app->GetNode ())
56 , m_app (app)
57{
58 NS_LOG_FUNCTION (this << app);
59
60 NS_ASSERT (m_app != 0);
61}
62
63NdnAppFace::~NdnAppFace ()
64{
65 NS_LOG_FUNCTION_NOARGS ();
66}
67
68NdnAppFace::NdnAppFace ()
69 : NdnFace (0)
70{
71}
72
73NdnAppFace::NdnAppFace (const NdnAppFace &)
74 : NdnFace (0)
75{
76}
77
78NdnAppFace& NdnAppFace::operator= (const NdnAppFace &)
79{
80 return *((NdnAppFace*)0);
81}
82
83
84void
85NdnAppFace::RegisterProtocolHandler (ProtocolHandler handler)
86{
87 NS_LOG_FUNCTION (this);
88
89 NdnFace::RegisterProtocolHandler (handler);
90
91 m_app->RegisterProtocolHandler (MakeCallback (&NdnFace::Receive, this));
92}
93
94bool
95NdnAppFace::SendImpl (Ptr<Packet> p)
96{
97 NS_LOG_FUNCTION (this << p);
98
99 try
100 {
101 NdnHeaderHelper::Type type = NdnHeaderHelper::GetNdnHeaderType (p);
102 switch (type)
103 {
104 case NdnHeaderHelper::INTEREST:
105 {
106 Ptr<NdnInterestHeader> header = Create<NdnInterestHeader> ();
107 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 }
116 case NdnHeaderHelper::CONTENT_OBJECT:
117 {
118 static NdnContentObjectTail tail;
119 Ptr<NdnContentObjectHeader> header = Create<NdnContentObjectHeader> ();
120 p->RemoveHeader (*header);
121 p->RemoveTrailer (tail);
122 m_app->OnContentObject (header, p/*payload*/);
123
124 break;
125 }
126 }
127
128 return true;
129 }
130 catch (NdnUnknownHeaderException)
131 {
132 NS_LOG_ERROR ("Unknown header type");
133 return false;
134 }
135}
136
137std::ostream&
138NdnAppFace::Print (std::ostream& os) const
139{
140 os << "dev=local(" << GetId() << ")";
141 return os;
142}
143
144}; // namespace ns3
145