blob: 06bae2390073ff67bb4529421633f7a5b33431e1 [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
Davide Pesaventobe40fb12015-02-23 21:09:34 +010028#include <ndn-cxx/management/nfd-face-event-notification.hpp>
29
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080030namespace nfd {
Junxiao Shi32bfeb32014-01-25 18:22:02 -070031
Junxiao Shi79494162014-04-02 18:25:11 -070032Face::Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal)
Junxiao Shi8c8d2182014-01-30 22:33:00 -070033 : m_id(INVALID_FACEID)
Davide Pesavento0ff10db2014-02-28 03:12:27 +010034 , m_isLocal(isLocal)
Junxiao Shi79494162014-04-02 18:25:11 -070035 , m_remoteUri(remoteUri)
36 , m_localUri(localUri)
Alexander Afanasyev355c0662014-03-20 18:08:17 -070037 , m_isOnDemand(false)
Junxiao Shi08d07a72014-06-09 23:17:57 -070038 , m_isFailed(false)
Junxiao Shi32bfeb32014-01-25 18:22:02 -070039{
Junxiao Shic099ddb2014-12-25 20:53:20 -070040 onReceiveInterest.connect([this] (const ndn::Interest&) { ++m_counters.getNInInterests(); });
41 onReceiveData .connect([this] (const ndn::Data&) { ++m_counters.getNInDatas(); });
42 onSendInterest .connect([this] (const ndn::Interest&) { ++m_counters.getNOutInterests(); });
43 onSendData .connect([this] (const ndn::Data&) { ++m_counters.getNOutDatas(); });
Junxiao Shi32bfeb32014-01-25 18:22:02 -070044}
45
46Face::~Face()
47{
48}
49
Junxiao Shi8c8d2182014-01-30 22:33:00 -070050FaceId
51Face::getId() const
52{
53 return m_id;
54}
55
Junxiao Shi33152f12014-07-16 19:54:32 -070056// this method is private and should be used only by the FaceTable
Alexander Afanasyev46f66472014-01-31 16:50:58 -080057void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070058Face::setId(FaceId faceId)
59{
60 m_id = faceId;
61}
62
Junxiao Shi32bfeb32014-01-25 18:22:02 -070063void
64Face::setDescription(const std::string& description)
65{
66 m_description = description;
67}
68
69const std::string&
70Face::getDescription() const
71{
72 return m_description;
73}
74
75bool
76Face::isMultiAccess() const
77{
78 return false;
79}
80
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080081bool
Davide Pesavento0ff10db2014-02-28 03:12:27 +010082Face::isUp() const
83{
84 return true;
85}
86
87bool
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080088Face::decodeAndDispatchInput(const Block& element)
Junxiao Shi32bfeb32014-01-25 18:22:02 -070089{
Alexander Afanasyev650028d2014-04-25 18:39:10 -070090 try {
91 /// \todo Ensure lazy field decoding process
Alexander Afanasyev355c0662014-03-20 18:08:17 -070092
Alexander Afanasyev650028d2014-04-25 18:39:10 -070093 if (element.type() == tlv::Interest)
94 {
95 shared_ptr<Interest> i = make_shared<Interest>();
96 i->wireDecode(element);
97 this->onReceiveInterest(*i);
98 }
99 else if (element.type() == tlv::Data)
100 {
101 shared_ptr<Data> d = make_shared<Data>();
102 d->wireDecode(element);
103 this->onReceiveData(*d);
104 }
105 else
106 return false;
107
108 return true;
109 }
Davide Pesavento66ff0982015-01-29 22:39:00 +0100110 catch (const tlv::Error&) {
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800111 return false;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700112 }
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700113}
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700114
Junxiao Shi08d07a72014-06-09 23:17:57 -0700115void
116Face::fail(const std::string& reason)
117{
118 if (m_isFailed) {
119 return;
120 }
121
122 m_isFailed = true;
123 this->onFail(reason);
124}
125
Chengyu Fanf9c2bb12014-10-06 11:52:44 -0600126template<typename FaceTraits>
127void
128Face::copyStatusTo(FaceTraits& traits) const
129{
130 traits.setFaceId(getId())
Davide Pesavento13b76f02015-02-26 03:59:54 +0100131 .setRemoteUri(getRemoteUri().toString())
132 .setLocalUri(getLocalUri().toString())
133 .setFaceScope(isLocal() ? ndn::nfd::FACE_SCOPE_LOCAL
134 : ndn::nfd::FACE_SCOPE_NON_LOCAL)
135 .setFacePersistency(isOnDemand() ? ndn::nfd::FACE_PERSISTENCY_ON_DEMAND
136 : ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
137 .setLinkType(isMultiAccess() ? ndn::nfd::LINK_TYPE_MULTI_ACCESS
138 : ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Chengyu Fanf9c2bb12014-10-06 11:52:44 -0600139}
140
141template void
142Face::copyStatusTo<ndn::nfd::FaceStatus>(ndn::nfd::FaceStatus&) const;
143
144template void
145Face::copyStatusTo<ndn::nfd::FaceEventNotification>(ndn::nfd::FaceEventNotification&) const;
146
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700147ndn::nfd::FaceStatus
148Face::getFaceStatus() const
149{
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700150 ndn::nfd::FaceStatus status;
Junxiao Shi632a6202014-07-20 01:14:30 -0700151
Davide Pesavento13b76f02015-02-26 03:59:54 +0100152 copyStatusTo(status);
Junxiao Shi632a6202014-07-20 01:14:30 -0700153 this->getCounters().copyTo(status);
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700154
155 return status;
156}
157
Davide Pesavento13b76f02015-02-26 03:59:54 +0100158} // namespace nfd