blob: e45fb1b0e3c8d49ddc61b060a38417941c704a2a [file] [log] [blame]
Junxiao Shi32bfeb32014-01-25 18:22:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi5dd26c32014-07-20 23:15:14 -070024 */
Junxiao Shi32bfeb32014-01-25 18:22:02 -070025
26#include "face.hpp"
27
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080028namespace nfd {
Junxiao Shi32bfeb32014-01-25 18:22:02 -070029
Junxiao Shi79494162014-04-02 18:25:11 -070030Face::Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal)
Junxiao Shi8c8d2182014-01-30 22:33:00 -070031 : m_id(INVALID_FACEID)
Davide Pesavento0ff10db2014-02-28 03:12:27 +010032 , m_isLocal(isLocal)
Junxiao Shi79494162014-04-02 18:25:11 -070033 , m_remoteUri(remoteUri)
34 , m_localUri(localUri)
Alexander Afanasyev355c0662014-03-20 18:08:17 -070035 , m_isOnDemand(false)
Junxiao Shi08d07a72014-06-09 23:17:57 -070036 , m_isFailed(false)
Junxiao Shi32bfeb32014-01-25 18:22:02 -070037{
Junxiao Shic099ddb2014-12-25 20:53:20 -070038 onReceiveInterest.connect([this] (const ndn::Interest&) { ++m_counters.getNInInterests(); });
39 onReceiveData .connect([this] (const ndn::Data&) { ++m_counters.getNInDatas(); });
40 onSendInterest .connect([this] (const ndn::Interest&) { ++m_counters.getNOutInterests(); });
41 onSendData .connect([this] (const ndn::Data&) { ++m_counters.getNOutDatas(); });
Junxiao Shi32bfeb32014-01-25 18:22:02 -070042}
43
44Face::~Face()
45{
46}
47
Junxiao Shi8c8d2182014-01-30 22:33:00 -070048FaceId
49Face::getId() const
50{
51 return m_id;
52}
53
Junxiao Shi33152f12014-07-16 19:54:32 -070054// this method is private and should be used only by the FaceTable
Alexander Afanasyev46f66472014-01-31 16:50:58 -080055void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070056Face::setId(FaceId faceId)
57{
58 m_id = faceId;
59}
60
Junxiao Shi32bfeb32014-01-25 18:22:02 -070061void
62Face::setDescription(const std::string& description)
63{
64 m_description = description;
65}
66
67const std::string&
68Face::getDescription() const
69{
70 return m_description;
71}
72
73bool
74Face::isMultiAccess() const
75{
76 return false;
77}
78
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080079bool
Davide Pesavento0ff10db2014-02-28 03:12:27 +010080Face::isUp() const
81{
82 return true;
83}
84
85bool
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080086Face::decodeAndDispatchInput(const Block& element)
Junxiao Shi32bfeb32014-01-25 18:22:02 -070087{
Alexander Afanasyev650028d2014-04-25 18:39:10 -070088 try {
89 /// \todo Ensure lazy field decoding process
Alexander Afanasyev355c0662014-03-20 18:08:17 -070090
Alexander Afanasyev650028d2014-04-25 18:39:10 -070091 if (element.type() == tlv::Interest)
92 {
93 shared_ptr<Interest> i = make_shared<Interest>();
94 i->wireDecode(element);
95 this->onReceiveInterest(*i);
96 }
97 else if (element.type() == tlv::Data)
98 {
99 shared_ptr<Data> d = make_shared<Data>();
100 d->wireDecode(element);
101 this->onReceiveData(*d);
102 }
103 else
104 return false;
105
106 return true;
107 }
Davide Pesavento66ff0982015-01-29 22:39:00 +0100108 catch (const tlv::Error&) {
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800109 return false;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700110 }
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700111}
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700112
Junxiao Shi08d07a72014-06-09 23:17:57 -0700113void
114Face::fail(const std::string& reason)
115{
116 if (m_isFailed) {
117 return;
118 }
119
120 m_isFailed = true;
121 this->onFail(reason);
122}
123
Chengyu Fanf9c2bb12014-10-06 11:52:44 -0600124template<typename FaceTraits>
125void
126Face::copyStatusTo(FaceTraits& traits) const
127{
128 traits.setFaceId(getId())
129 .setRemoteUri(getRemoteUri().toString())
130 .setLocalUri(getLocalUri().toString());
131
132 if (isLocal()) {
133 traits.setFaceScope(ndn::nfd::FACE_SCOPE_LOCAL);
134 }
135 else {
136 traits.setFaceScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
137 }
138
139 if (isOnDemand()) {
140 traits.setFacePersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
141 }
142 else {
143 traits.setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
144 }
145}
146
147template void
148Face::copyStatusTo<ndn::nfd::FaceStatus>(ndn::nfd::FaceStatus&) const;
149
150template void
151Face::copyStatusTo<ndn::nfd::FaceEventNotification>(ndn::nfd::FaceEventNotification&) const;
152
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700153ndn::nfd::FaceStatus
154Face::getFaceStatus() const
155{
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700156 ndn::nfd::FaceStatus status;
Chengyu Fanf9c2bb12014-10-06 11:52:44 -0600157 copyStatusTo(status);
Junxiao Shi632a6202014-07-20 01:14:30 -0700158
159 this->getCounters().copyTo(status);
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700160
161 return status;
162}
163
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800164} //namespace nfd