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" |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 30 | #include "face-counters.hpp" |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 31 | |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 32 | #include <ndn-cxx/util/face-uri.hpp> |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 33 | #include <ndn-cxx/management/nfd-face-status.hpp> |
Chengyu Fan | f9c2bb1 | 2014-10-06 11:52:44 -0600 | [diff] [blame] | 34 | #include <ndn-cxx/management/nfd-face-event-notification.hpp> |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 36 | namespace nfd { |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 37 | |
| 38 | /** \class FaceId |
| 39 | * \brief identifies a face |
| 40 | */ |
| 41 | typedef int FaceId; |
| 42 | |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 43 | /// indicates an invalid FaceId |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 44 | const FaceId INVALID_FACEID = -1; |
| 45 | |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 46 | /// identifies the InternalFace used in management |
| 47 | const FaceId FACEID_INTERNAL_FACE = 1; |
| 48 | /// identifies a packet comes from the ContentStore, in LocalControlHeader incomingFaceId |
| 49 | const FaceId FACEID_CONTENT_STORE = 254; |
| 50 | /// identifies the NullFace that drops every packet |
| 51 | const FaceId FACEID_NULL = 255; |
| 52 | /// upper bound of reserved FaceIds |
| 53 | const FaceId FACEID_RESERVED_MAX = 255; |
| 54 | |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame] | 55 | using ndn::util::FaceUri; |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 56 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 57 | /** \brief represents a face |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 58 | */ |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 59 | class Face : noncopyable, public enable_shared_from_this<Face> |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 60 | { |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 61 | public: |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 62 | /** |
| 63 | * \brief Face-related error |
| 64 | */ |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 65 | class Error : public std::runtime_error |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 66 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 67 | public: |
| 68 | explicit |
| 69 | Error(const std::string& what) |
| 70 | : std::runtime_error(what) |
| 71 | { |
| 72 | } |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 75 | Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal = false); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 76 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 77 | virtual |
| 78 | ~Face(); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 79 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 80 | /// fires when an Interest is received |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 81 | signal::Signal<Face, Interest> onReceiveInterest; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 82 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 83 | /// fires when a Data is received |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 84 | signal::Signal<Face, Data> onReceiveData; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 85 | |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 86 | /// fires when an Interest is sent out |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 87 | signal::Signal<Face, Interest> onSendInterest; |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 88 | |
| 89 | /// fires when a Data is sent out |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 90 | signal::Signal<Face, Data> onSendData; |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 91 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 92 | /// fires when face disconnects or fails to perform properly |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 93 | signal::Signal<Face, std::string/*reason*/> onFail; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 94 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 95 | /// send an Interest |
| 96 | virtual void |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 97 | sendInterest(const Interest& interest) = 0; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 98 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 99 | /// send a Data |
| 100 | virtual void |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 101 | sendData(const Data& data) = 0; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 102 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 103 | /** \brief Close the face |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 104 | * |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 105 | * This terminates all communication on the face and cause |
| 106 | * onFail() method event to be invoked |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 107 | */ |
| 108 | virtual void |
| 109 | close() = 0; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 110 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 111 | public: // attributes |
| 112 | FaceId |
| 113 | getId() const; |
| 114 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 115 | /** \brief Set the description |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 116 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 117 | * This is typically invoked by mgmt on set description command |
| 118 | */ |
| 119 | virtual void |
| 120 | setDescription(const std::string& description); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 121 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 122 | /// Get the description |
| 123 | virtual const std::string& |
| 124 | getDescription() const; |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 131 | /** \brief Get whether packets sent this Face may reach multiple peers |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 132 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 133 | * In this base class this property is always false. |
| 134 | */ |
| 135 | virtual bool |
| 136 | isMultiAccess() const; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 137 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 138 | /** \brief Get whether underlying communication is up |
| 139 | * |
| 140 | * In this base class this property is always true. |
| 141 | */ |
| 142 | virtual bool |
| 143 | isUp() const; |
| 144 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 145 | /** \brief Get whether face is created on demand or explicitly via FaceManagement protocol |
| 146 | */ |
| 147 | bool |
| 148 | isOnDemand() const; |
| 149 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 150 | const FaceCounters& |
| 151 | getCounters() const; |
| 152 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 153 | /** \return a FaceUri that represents the remote endpoint |
| 154 | */ |
| 155 | const FaceUri& |
| 156 | getRemoteUri() const; |
| 157 | |
| 158 | /** \return a FaceUri that represents the local endpoint (NFD side) |
| 159 | */ |
| 160 | const FaceUri& |
| 161 | getLocalUri() const; |
| 162 | |
Chengyu Fan | f9c2bb1 | 2014-10-06 11:52:44 -0600 | [diff] [blame] | 163 | /** \return FaceTraits data structure filled with the current FaceTraits status |
| 164 | */ |
| 165 | template<typename FaceTraits> |
| 166 | void |
| 167 | copyStatusTo(FaceTraits& traits) const; |
| 168 | |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 169 | /** \return FaceStatus data structure filled with the current Face status |
| 170 | */ |
| 171 | virtual ndn::nfd::FaceStatus |
| 172 | getFaceStatus() const; |
| 173 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 174 | protected: |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 175 | // this is a non-virtual method |
| 176 | bool |
| 177 | decodeAndDispatchInput(const Block& element); |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 178 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 179 | FaceCounters& |
| 180 | getMutableCounters(); |
| 181 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 182 | void |
| 183 | setOnDemand(bool isOnDemand); |
| 184 | |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 185 | /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing |
| 186 | */ |
| 187 | void |
| 188 | fail(const std::string& reason); |
| 189 | |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 190 | DECLARE_SIGNAL_EMIT(onReceiveInterest) |
| 191 | DECLARE_SIGNAL_EMIT(onReceiveData) |
| 192 | DECLARE_SIGNAL_EMIT(onSendInterest) |
| 193 | DECLARE_SIGNAL_EMIT(onSendData) |
| 194 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 195 | private: |
Alexander Afanasyev | 46f6647 | 2014-01-31 16:50:58 -0800 | [diff] [blame] | 196 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 197 | setId(FaceId faceId); |
| 198 | |
| 199 | private: |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 200 | FaceId m_id; |
| 201 | std::string m_description; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 202 | bool m_isLocal; // for scoping purposes |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 203 | FaceCounters m_counters; |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 204 | FaceUri m_remoteUri; |
| 205 | FaceUri m_localUri; |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 206 | bool m_isOnDemand; |
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 | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 213 | inline bool |
| 214 | Face::isLocal() const |
| 215 | { |
| 216 | return m_isLocal; |
| 217 | } |
| 218 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 219 | inline const FaceCounters& |
| 220 | Face::getCounters() const |
| 221 | { |
| 222 | return m_counters; |
| 223 | } |
| 224 | |
| 225 | inline FaceCounters& |
| 226 | Face::getMutableCounters() |
| 227 | { |
| 228 | return m_counters; |
| 229 | } |
| 230 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 231 | inline const FaceUri& |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 232 | Face::getRemoteUri() const |
| 233 | { |
| 234 | return m_remoteUri; |
| 235 | } |
| 236 | |
| 237 | inline const FaceUri& |
| 238 | Face::getLocalUri() const |
| 239 | { |
| 240 | return m_localUri; |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 243 | inline void |
| 244 | Face::setOnDemand(bool isOnDemand) |
| 245 | { |
| 246 | m_isOnDemand = isOnDemand; |
| 247 | } |
| 248 | |
| 249 | inline bool |
| 250 | Face::isOnDemand() const |
| 251 | { |
| 252 | return m_isOnDemand; |
| 253 | } |
| 254 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 255 | } // namespace nfd |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 256 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 257 | #endif // NFD_DAEMON_FACE_FACE_HPP |