Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -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 | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_FACE_HPP |
| 27 | #define NFD_DAEMON_FACE_FACE_HPP |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 28 | |
| 29 | #include "common.hpp" |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 30 | #include "core/logger.hpp" |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 31 | #include "face-counters.hpp" |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 33 | #include <ndn-cxx/management/nfd-face-status.hpp> |
| 34 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 35 | namespace nfd { |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 36 | |
| 37 | /** \class FaceId |
| 38 | * \brief identifies a face |
| 39 | */ |
| 40 | typedef int FaceId; |
| 41 | |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 42 | /// indicates an invalid FaceId |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 43 | const FaceId INVALID_FACEID = -1; |
| 44 | |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 45 | /// identifies the InternalFace used in management |
| 46 | const FaceId FACEID_INTERNAL_FACE = 1; |
| 47 | /// identifies a packet comes from the ContentStore, in LocalControlHeader incomingFaceId |
| 48 | const FaceId FACEID_CONTENT_STORE = 254; |
| 49 | /// identifies the NullFace that drops every packet |
| 50 | const FaceId FACEID_NULL = 255; |
| 51 | /// upper bound of reserved FaceIds |
| 52 | const FaceId FACEID_RESERVED_MAX = 255; |
| 53 | |
| 54 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 55 | /** \brief represents a face |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 56 | */ |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 57 | class Face : noncopyable, public enable_shared_from_this<Face> |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 58 | { |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 59 | public: |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 60 | /** |
| 61 | * \brief Face-related error |
| 62 | */ |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 63 | class Error : public std::runtime_error |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 64 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 65 | public: |
| 66 | explicit |
| 67 | Error(const std::string& what) |
| 68 | : std::runtime_error(what) |
| 69 | { |
| 70 | } |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 73 | Face(const FaceUri& remoteUri, const FaceUri& localUri, |
| 74 | bool isLocal = false, bool isMultiAccess = false); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 75 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 76 | virtual |
| 77 | ~Face(); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 79 | /// fires when an Interest is received |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 80 | signal::Signal<Face, Interest> onReceiveInterest; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 81 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 82 | /// fires when a Data is received |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 83 | signal::Signal<Face, Data> onReceiveData; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 85 | /// fires when an Interest is sent out |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 86 | signal::Signal<Face, Interest> onSendInterest; |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 87 | |
| 88 | /// fires when a Data is sent out |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 89 | signal::Signal<Face, Data> onSendData; |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 90 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 91 | /// fires when face disconnects or fails to perform properly |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 92 | signal::Signal<Face, std::string/*reason*/> onFail; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 94 | /// send an Interest |
| 95 | virtual void |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 96 | sendInterest(const Interest& interest) = 0; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 97 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 98 | /// send a Data |
| 99 | virtual void |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 100 | sendData(const Data& data) = 0; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 102 | /** \brief Close the face |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 103 | * |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 104 | * This terminates all communication on the face and cause |
| 105 | * onFail() method event to be invoked |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 106 | */ |
| 107 | virtual void |
| 108 | close() = 0; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 109 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 110 | public: // attributes |
| 111 | FaceId |
| 112 | getId() const; |
| 113 | |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 114 | /** \brief Get the description |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 115 | */ |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 116 | const std::string& |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 117 | getDescription() const; |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 118 | |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 119 | /** \brief Set the face description |
| 120 | * |
| 121 | * This is typically invoked by management on set description command |
| 122 | */ |
| 123 | void |
| 124 | setDescription(const std::string& description); |
| 125 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 126 | /** \brief Get whether face is connected to a local app |
| 127 | */ |
| 128 | bool |
| 129 | isLocal() const; |
| 130 | |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 131 | /** \brief Get the persistency setting |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 132 | */ |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 133 | ndn::nfd::FacePersistency |
| 134 | getPersistency() const; |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 135 | |
| 136 | /** \brief Get whether packets sent by this face may reach multiple peers |
| 137 | */ |
| 138 | bool |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 139 | isMultiAccess() const; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 140 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 141 | /** \brief Get whether underlying communication is up |
| 142 | * |
| 143 | * In this base class this property is always true. |
| 144 | */ |
| 145 | virtual bool |
| 146 | isUp() const; |
| 147 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 148 | const FaceCounters& |
| 149 | getCounters() const; |
| 150 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 151 | /** \return a FaceUri that represents the remote endpoint |
| 152 | */ |
| 153 | const FaceUri& |
| 154 | getRemoteUri() const; |
| 155 | |
| 156 | /** \return a FaceUri that represents the local endpoint (NFD side) |
| 157 | */ |
| 158 | const FaceUri& |
| 159 | getLocalUri() const; |
| 160 | |
Chengyu Fan | f9c2bb1 | 2014-10-06 11:52:44 -0600 | [diff] [blame] | 161 | /** \return FaceTraits data structure filled with the current FaceTraits status |
| 162 | */ |
| 163 | template<typename FaceTraits> |
| 164 | void |
| 165 | copyStatusTo(FaceTraits& traits) const; |
| 166 | |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 167 | /** \return FaceStatus data structure filled with the current Face status |
| 168 | */ |
| 169 | virtual ndn::nfd::FaceStatus |
| 170 | getFaceStatus() const; |
| 171 | |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 172 | PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 173 | void |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 174 | setPersistency(ndn::nfd::FacePersistency persistency); |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 175 | |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 176 | protected: |
| 177 | bool |
| 178 | decodeAndDispatchInput(const Block& element); |
| 179 | |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 180 | /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing |
| 181 | */ |
| 182 | void |
| 183 | fail(const std::string& reason); |
| 184 | |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 185 | FaceCounters& |
| 186 | getMutableCounters(); |
| 187 | |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 188 | DECLARE_SIGNAL_EMIT(onReceiveInterest) |
| 189 | DECLARE_SIGNAL_EMIT(onReceiveData) |
| 190 | DECLARE_SIGNAL_EMIT(onSendInterest) |
| 191 | DECLARE_SIGNAL_EMIT(onSendData) |
| 192 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 193 | private: |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 194 | // this method should be used only by the FaceTable |
Alexander Afanasyev | 46f6647 | 2014-01-31 16:50:58 -0800 | [diff] [blame] | 195 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 196 | setId(FaceId faceId); |
| 197 | |
| 198 | private: |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 199 | FaceId m_id; |
| 200 | std::string m_description; |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 201 | FaceCounters m_counters; |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 202 | const FaceUri m_remoteUri; |
| 203 | const FaceUri m_localUri; |
| 204 | const bool m_isLocal; |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 205 | ndn::nfd::FacePersistency m_persistency; |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 206 | const bool m_isMultiAccess; |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 207 | bool m_isFailed; |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 208 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 209 | // allow setting FaceId |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 210 | friend class FaceTable; |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 213 | inline FaceId |
| 214 | Face::getId() const |
| 215 | { |
| 216 | return m_id; |
| 217 | } |
| 218 | |
| 219 | inline void |
| 220 | Face::setId(FaceId faceId) |
| 221 | { |
| 222 | m_id = faceId; |
| 223 | } |
| 224 | |
| 225 | inline const std::string& |
| 226 | Face::getDescription() const |
| 227 | { |
| 228 | return m_description; |
| 229 | } |
| 230 | |
| 231 | inline void |
| 232 | Face::setDescription(const std::string& description) |
| 233 | { |
| 234 | m_description = description; |
| 235 | } |
| 236 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 237 | inline bool |
| 238 | Face::isLocal() const |
| 239 | { |
| 240 | return m_isLocal; |
| 241 | } |
| 242 | |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 243 | inline ndn::nfd::FacePersistency |
| 244 | Face::getPersistency() const |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 245 | { |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 246 | return m_persistency; |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | inline void |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 250 | Face::setPersistency(ndn::nfd::FacePersistency persistency) |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 251 | { |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 252 | m_persistency = persistency; |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | inline bool |
| 256 | Face::isMultiAccess() const |
| 257 | { |
| 258 | return m_isMultiAccess; |
| 259 | } |
| 260 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 261 | inline const FaceCounters& |
| 262 | Face::getCounters() const |
| 263 | { |
| 264 | return m_counters; |
| 265 | } |
| 266 | |
| 267 | inline FaceCounters& |
| 268 | Face::getMutableCounters() |
| 269 | { |
| 270 | return m_counters; |
| 271 | } |
| 272 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 273 | inline const FaceUri& |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 274 | Face::getRemoteUri() const |
| 275 | { |
| 276 | return m_remoteUri; |
| 277 | } |
| 278 | |
| 279 | inline const FaceUri& |
| 280 | Face::getLocalUri() const |
| 281 | { |
| 282 | return m_localUri; |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Davide Pesavento | be40fb1 | 2015-02-23 21:09:34 +0100 | [diff] [blame] | 285 | |
| 286 | /** \defgroup FaceLogging Face logging macros |
| 287 | * |
| 288 | * These macros augment the log message with some face-specific information, |
| 289 | * such as the face ID, that are useful to distinguish which face produced the |
| 290 | * message. It is strongly recommended to use these macros instead of the |
| 291 | * generic ones for all logging inside Face subclasses. |
| 292 | * @{ |
| 293 | */ |
| 294 | |
| 295 | #define NFD_LOG_FACE(level, msg) \ |
| 296 | NFD_LOG_##level("[id=" << this->getId() << \ |
| 297 | ",local=" << this->getLocalUri() << \ |
| 298 | ",remote=" << this->getRemoteUri() << \ |
| 299 | "] " << msg) |
| 300 | |
| 301 | /** \brief Log a message at TRACE level */ |
| 302 | #define NFD_LOG_FACE_TRACE(msg) NFD_LOG_FACE(TRACE, msg) |
| 303 | |
| 304 | /** \brief Log a message at DEBUG level */ |
| 305 | #define NFD_LOG_FACE_DEBUG(msg) NFD_LOG_FACE(DEBUG, msg) |
| 306 | |
| 307 | /** \brief Log a message at INFO level */ |
| 308 | #define NFD_LOG_FACE_INFO(msg) NFD_LOG_FACE(INFO, msg) |
| 309 | |
| 310 | /** \brief Log a message at WARN level */ |
| 311 | #define NFD_LOG_FACE_WARN(msg) NFD_LOG_FACE(WARN, msg) |
| 312 | |
| 313 | /** \brief Log a message at ERROR level */ |
| 314 | #define NFD_LOG_FACE_ERROR(msg) NFD_LOG_FACE(ERROR, msg) |
| 315 | |
| 316 | /** @} */ |
| 317 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 318 | } // namespace nfd |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 319 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 320 | #endif // NFD_DAEMON_FACE_FACE_HPP |