blob: 04895da665ef85baa272c957cd898e44547ff22b [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
Davide Pesavento94279412015-02-27 01:29:32 +010032Face::Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal, bool isMultiAccess)
Junxiao Shi8c8d2182014-01-30 22:33:00 -070033 : m_id(INVALID_FACEID)
Junxiao Shi79494162014-04-02 18:25:11 -070034 , m_remoteUri(remoteUri)
35 , m_localUri(localUri)
Davide Pesavento94279412015-02-27 01:29:32 +010036 , m_isLocal(isLocal)
Yukai Tu731f0d72015-07-04 11:14:44 +080037 , m_persistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
Davide Pesavento94279412015-02-27 01:29:32 +010038 , m_isMultiAccess(isMultiAccess)
Junxiao Shi08d07a72014-06-09 23:17:57 -070039 , m_isFailed(false)
Alexander Afanasyev752a0382014-12-31 00:11:43 -080040 , m_metric(0)
Junxiao Shi32bfeb32014-01-25 18:22:02 -070041{
Junxiao Shic099ddb2014-12-25 20:53:20 -070042 onReceiveInterest.connect([this] (const ndn::Interest&) { ++m_counters.getNInInterests(); });
43 onReceiveData .connect([this] (const ndn::Data&) { ++m_counters.getNInDatas(); });
44 onSendInterest .connect([this] (const ndn::Interest&) { ++m_counters.getNOutInterests(); });
45 onSendData .connect([this] (const ndn::Data&) { ++m_counters.getNOutDatas(); });
Junxiao Shi32bfeb32014-01-25 18:22:02 -070046}
47
48Face::~Face()
49{
50}
51
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080052bool
Davide Pesavento0ff10db2014-02-28 03:12:27 +010053Face::isUp() const
54{
55 return true;
56}
57
58bool
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080059Face::decodeAndDispatchInput(const Block& element)
Junxiao Shi32bfeb32014-01-25 18:22:02 -070060{
Alexander Afanasyev650028d2014-04-25 18:39:10 -070061 try {
62 /// \todo Ensure lazy field decoding process
Alexander Afanasyev355c0662014-03-20 18:08:17 -070063
Alexander Afanasyev650028d2014-04-25 18:39:10 -070064 if (element.type() == tlv::Interest)
65 {
66 shared_ptr<Interest> i = make_shared<Interest>();
67 i->wireDecode(element);
68 this->onReceiveInterest(*i);
69 }
70 else if (element.type() == tlv::Data)
71 {
72 shared_ptr<Data> d = make_shared<Data>();
73 d->wireDecode(element);
74 this->onReceiveData(*d);
75 }
76 else
77 return false;
78
79 return true;
80 }
Davide Pesavento66ff0982015-01-29 22:39:00 +010081 catch (const tlv::Error&) {
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080082 return false;
Alexander Afanasyev650028d2014-04-25 18:39:10 -070083 }
Junxiao Shi16b8bc92014-02-17 22:24:55 -070084}
Junxiao Shi32bfeb32014-01-25 18:22:02 -070085
Junxiao Shi08d07a72014-06-09 23:17:57 -070086void
87Face::fail(const std::string& reason)
88{
89 if (m_isFailed) {
90 return;
91 }
92
93 m_isFailed = true;
94 this->onFail(reason);
95}
96
Chengyu Fanf9c2bb12014-10-06 11:52:44 -060097template<typename FaceTraits>
98void
99Face::copyStatusTo(FaceTraits& traits) const
100{
101 traits.setFaceId(getId())
Davide Pesavento13b76f02015-02-26 03:59:54 +0100102 .setRemoteUri(getRemoteUri().toString())
103 .setLocalUri(getLocalUri().toString())
104 .setFaceScope(isLocal() ? ndn::nfd::FACE_SCOPE_LOCAL
105 : ndn::nfd::FACE_SCOPE_NON_LOCAL)
Yukai Tu731f0d72015-07-04 11:14:44 +0800106 .setFacePersistency(getPersistency())
Davide Pesavento13b76f02015-02-26 03:59:54 +0100107 .setLinkType(isMultiAccess() ? ndn::nfd::LINK_TYPE_MULTI_ACCESS
108 : ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Chengyu Fanf9c2bb12014-10-06 11:52:44 -0600109}
110
111template void
112Face::copyStatusTo<ndn::nfd::FaceStatus>(ndn::nfd::FaceStatus&) const;
113
114template void
115Face::copyStatusTo<ndn::nfd::FaceEventNotification>(ndn::nfd::FaceEventNotification&) const;
116
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700117ndn::nfd::FaceStatus
118Face::getFaceStatus() const
119{
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700120 ndn::nfd::FaceStatus status;
Junxiao Shi632a6202014-07-20 01:14:30 -0700121
Davide Pesavento13b76f02015-02-26 03:59:54 +0100122 copyStatusTo(status);
Junxiao Shi632a6202014-07-20 01:14:30 -0700123 this->getCounters().copyTo(status);
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700124
125 return status;
126}
127
Davide Pesavento13b76f02015-02-26 03:59:54 +0100128} // namespace nfd