blob: 7d3c1b7158a59b52fe69a098c38580955eba0037 [file] [log] [blame]
Junxiao Shi32bfeb32014-01-25 18:22:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi08d07a72014-06-09 23:17:57 -07003 * Copyright (c) 2014, 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"
Junxiao Shi16b8bc92014-02-17 22:24:55 -070027#include "core/logger.hpp"
Junxiao Shi32bfeb32014-01-25 18:22:02 -070028
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080029namespace nfd {
Junxiao Shi32bfeb32014-01-25 18:22:02 -070030
Junxiao Shi79494162014-04-02 18:25:11 -070031Face::Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal)
Junxiao Shi8c8d2182014-01-30 22:33:00 -070032 : m_id(INVALID_FACEID)
Davide Pesavento0ff10db2014-02-28 03:12:27 +010033 , m_isLocal(isLocal)
Junxiao Shi79494162014-04-02 18:25:11 -070034 , m_remoteUri(remoteUri)
35 , m_localUri(localUri)
Alexander Afanasyev355c0662014-03-20 18:08:17 -070036 , m_isOnDemand(false)
Junxiao Shi08d07a72014-06-09 23:17:57 -070037 , m_isFailed(false)
Junxiao Shi32bfeb32014-01-25 18:22:02 -070038{
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020039 onReceiveInterest += [this](const ndn::Interest&) { ++m_counters.getNInInterests(); };
40 onReceiveData += [this](const ndn::Data&) { ++m_counters.getNInDatas(); };
41 onSendInterest += [this](const ndn::Interest&) { ++m_counters.getNOutInterests(); };
42 onSendData += [this](const ndn::Data&) { ++m_counters.getNOutDatas(); };
Junxiao Shi32bfeb32014-01-25 18:22:02 -070043}
44
45Face::~Face()
46{
47}
48
Junxiao Shi8c8d2182014-01-30 22:33:00 -070049FaceId
50Face::getId() const
51{
52 return m_id;
53}
54
Junxiao Shi33152f12014-07-16 19:54:32 -070055// this method is private and should be used only by the FaceTable
Alexander Afanasyev46f66472014-01-31 16:50:58 -080056void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070057Face::setId(FaceId faceId)
58{
59 m_id = faceId;
60}
61
Junxiao Shi32bfeb32014-01-25 18:22:02 -070062void
63Face::setDescription(const std::string& description)
64{
65 m_description = description;
66}
67
68const std::string&
69Face::getDescription() const
70{
71 return m_description;
72}
73
74bool
75Face::isMultiAccess() const
76{
77 return false;
78}
79
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080080bool
Davide Pesavento0ff10db2014-02-28 03:12:27 +010081Face::isUp() const
82{
83 return true;
84}
85
86bool
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080087Face::decodeAndDispatchInput(const Block& element)
Junxiao Shi32bfeb32014-01-25 18:22:02 -070088{
Alexander Afanasyev650028d2014-04-25 18:39:10 -070089 try {
90 /// \todo Ensure lazy field decoding process
Alexander Afanasyev355c0662014-03-20 18:08:17 -070091
Alexander Afanasyev650028d2014-04-25 18:39:10 -070092 if (element.type() == tlv::Interest)
93 {
94 shared_ptr<Interest> i = make_shared<Interest>();
95 i->wireDecode(element);
96 this->onReceiveInterest(*i);
97 }
98 else if (element.type() == tlv::Data)
99 {
100 shared_ptr<Data> d = make_shared<Data>();
101 d->wireDecode(element);
102 this->onReceiveData(*d);
103 }
104 else
105 return false;
106
107 return true;
108 }
109 catch (tlv::Error&) {
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800110 return false;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700111 }
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700112}
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700113
Junxiao Shi08d07a72014-06-09 23:17:57 -0700114void
115Face::fail(const std::string& reason)
116{
117 if (m_isFailed) {
118 return;
119 }
120
121 m_isFailed = true;
122 this->onFail(reason);
Alexander Afanasyev583760b2014-06-30 19:27:37 -0700123
124 this->onFail.clear();
Junxiao Shi08d07a72014-06-09 23:17:57 -0700125}
126
Chengyu Fanf9c2bb12014-10-06 11:52:44 -0600127template<typename FaceTraits>
128void
129Face::copyStatusTo(FaceTraits& traits) const
130{
131 traits.setFaceId(getId())
132 .setRemoteUri(getRemoteUri().toString())
133 .setLocalUri(getLocalUri().toString());
134
135 if (isLocal()) {
136 traits.setFaceScope(ndn::nfd::FACE_SCOPE_LOCAL);
137 }
138 else {
139 traits.setFaceScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
140 }
141
142 if (isOnDemand()) {
143 traits.setFacePersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
144 }
145 else {
146 traits.setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
147 }
148}
149
150template void
151Face::copyStatusTo<ndn::nfd::FaceStatus>(ndn::nfd::FaceStatus&) const;
152
153template void
154Face::copyStatusTo<ndn::nfd::FaceEventNotification>(ndn::nfd::FaceEventNotification&) const;
155
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700156ndn::nfd::FaceStatus
157Face::getFaceStatus() const
158{
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700159 ndn::nfd::FaceStatus status;
Chengyu Fanf9c2bb12014-10-06 11:52:44 -0600160 copyStatusTo(status);
Junxiao Shi632a6202014-07-20 01:14:30 -0700161
162 this->getCounters().copyTo(status);
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700163
164 return status;
165}
166
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800167} //namespace nfd