Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 3 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 25 | |
| 26 | #include "face.hpp" |
| 27 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 28 | namespace nfd { |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 29 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 30 | Face::Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal) |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 31 | : m_id(INVALID_FACEID) |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 32 | , m_isLocal(isLocal) |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 33 | , m_remoteUri(remoteUri) |
| 34 | , m_localUri(localUri) |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 35 | , m_isOnDemand(false) |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 36 | , m_isFailed(false) |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 37 | { |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 38 | 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 Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | Face::~Face() |
| 45 | { |
| 46 | } |
| 47 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 48 | FaceId |
| 49 | Face::getId() const |
| 50 | { |
| 51 | return m_id; |
| 52 | } |
| 53 | |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 54 | // this method is private and should be used only by the FaceTable |
Alexander Afanasyev | 46f6647 | 2014-01-31 16:50:58 -0800 | [diff] [blame] | 55 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 56 | Face::setId(FaceId faceId) |
| 57 | { |
| 58 | m_id = faceId; |
| 59 | } |
| 60 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 61 | void |
| 62 | Face::setDescription(const std::string& description) |
| 63 | { |
| 64 | m_description = description; |
| 65 | } |
| 66 | |
| 67 | const std::string& |
| 68 | Face::getDescription() const |
| 69 | { |
| 70 | return m_description; |
| 71 | } |
| 72 | |
| 73 | bool |
| 74 | Face::isMultiAccess() const |
| 75 | { |
| 76 | return false; |
| 77 | } |
| 78 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 79 | bool |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 80 | Face::isUp() const |
| 81 | { |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | bool |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 86 | Face::decodeAndDispatchInput(const Block& element) |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 87 | { |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 88 | try { |
| 89 | /// \todo Ensure lazy field decoding process |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 91 | 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 Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame^] | 108 | catch (const tlv::Error&) { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 109 | return false; |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 110 | } |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 111 | } |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 112 | |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 113 | void |
| 114 | Face::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 Fan | f9c2bb1 | 2014-10-06 11:52:44 -0600 | [diff] [blame] | 124 | template<typename FaceTraits> |
| 125 | void |
| 126 | Face::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 | |
| 147 | template void |
| 148 | Face::copyStatusTo<ndn::nfd::FaceStatus>(ndn::nfd::FaceStatus&) const; |
| 149 | |
| 150 | template void |
| 151 | Face::copyStatusTo<ndn::nfd::FaceEventNotification>(ndn::nfd::FaceEventNotification&) const; |
| 152 | |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 153 | ndn::nfd::FaceStatus |
| 154 | Face::getFaceStatus() const |
| 155 | { |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 156 | ndn::nfd::FaceStatus status; |
Chengyu Fan | f9c2bb1 | 2014-10-06 11:52:44 -0600 | [diff] [blame] | 157 | copyStatusTo(status); |
Junxiao Shi | 632a620 | 2014-07-20 01:14:30 -0700 | [diff] [blame] | 158 | |
| 159 | this->getCounters().copyTo(status); |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 160 | |
| 161 | return status; |
| 162 | } |
| 163 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 164 | } //namespace nfd |