Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, 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/>. |
| 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 | 7f02c1e | 2014-01-29 22:57:01 -0700 | [diff] [blame] | 30 | #include "core/event-emitter.hpp" |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 31 | #include "core/face-uri.hpp" |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 32 | #include "face-counter.hpp" |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 34 | namespace nfd { |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 35 | |
| 36 | /** \class FaceId |
| 37 | * \brief identifies a face |
| 38 | */ |
| 39 | typedef int FaceId; |
| 40 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 41 | const FaceId INVALID_FACEID = -1; |
| 42 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 43 | const size_t MAX_NDN_PACKET_SIZE = 8800; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 44 | |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 45 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 46 | /** \brief represents a face |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 47 | */ |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 48 | class Face : noncopyable, public enable_shared_from_this<Face> |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 49 | { |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 50 | public: |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 51 | /** |
| 52 | * \brief Face-related error |
| 53 | */ |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 54 | class Error : public std::runtime_error |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 55 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 56 | public: |
| 57 | explicit |
| 58 | Error(const std::string& what) |
| 59 | : std::runtime_error(what) |
| 60 | { |
| 61 | } |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 64 | Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal = false); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 65 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 66 | virtual |
| 67 | ~Face(); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 68 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 69 | /// fires when an Interest is received |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 70 | EventEmitter<Interest> onReceiveInterest; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 71 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 72 | /// fires when a Data is received |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 73 | EventEmitter<Data> onReceiveData; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 75 | /// fires when an Interest is sent out |
| 76 | EventEmitter<Interest> onSendInterest; |
| 77 | |
| 78 | /// fires when a Data is sent out |
| 79 | EventEmitter<Data> onSendData; |
| 80 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 81 | /// fires when face disconnects or fails to perform properly |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 82 | EventEmitter<std::string/*reason*/> onFail; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 83 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 84 | /// send an Interest |
| 85 | virtual void |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 86 | sendInterest(const Interest& interest) = 0; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 87 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 88 | /// send a Data |
| 89 | virtual void |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 90 | sendData(const Data& data) = 0; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 92 | /** \brief Close the face |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 93 | * |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 94 | * This terminates all communication on the face and cause |
| 95 | * onFail() method event to be invoked |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 96 | */ |
| 97 | virtual void |
| 98 | close() = 0; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 99 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 100 | public: // attributes |
| 101 | FaceId |
| 102 | getId() const; |
| 103 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 104 | /** \brief Set the description |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 105 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 106 | * This is typically invoked by mgmt on set description command |
| 107 | */ |
| 108 | virtual void |
| 109 | setDescription(const std::string& description); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 110 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 111 | /// Get the description |
| 112 | virtual const std::string& |
| 113 | getDescription() const; |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 114 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 115 | /** \brief Get whether face is connected to a local app |
| 116 | */ |
| 117 | bool |
| 118 | isLocal() const; |
| 119 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 120 | /** \brief Get whether packets sent this Face may reach multiple peers |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 121 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 122 | * In this base class this property is always false. |
| 123 | */ |
| 124 | virtual bool |
| 125 | isMultiAccess() const; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 126 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 127 | /** \brief Get whether underlying communication is up |
| 128 | * |
| 129 | * In this base class this property is always true. |
| 130 | */ |
| 131 | virtual bool |
| 132 | isUp() const; |
| 133 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 134 | /** \brief Get whether face is created on demand or explicitly via FaceManagement protocol |
| 135 | */ |
| 136 | bool |
| 137 | isOnDemand() const; |
| 138 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 139 | const FaceCounters& |
| 140 | getCounters() const; |
| 141 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 142 | /** \return a FaceUri that represents the remote endpoint |
| 143 | */ |
| 144 | const FaceUri& |
| 145 | getRemoteUri() const; |
| 146 | |
| 147 | /** \return a FaceUri that represents the local endpoint (NFD side) |
| 148 | */ |
| 149 | const FaceUri& |
| 150 | getLocalUri() const; |
| 151 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 152 | protected: |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 153 | // this is a non-virtual method |
| 154 | bool |
| 155 | decodeAndDispatchInput(const Block& element); |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 156 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 157 | FaceCounters& |
| 158 | getMutableCounters(); |
| 159 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 160 | void |
| 161 | setOnDemand(bool isOnDemand); |
| 162 | |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 163 | /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing |
| 164 | */ |
| 165 | void |
| 166 | fail(const std::string& reason); |
| 167 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 168 | private: |
Alexander Afanasyev | 46f6647 | 2014-01-31 16:50:58 -0800 | [diff] [blame] | 169 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 170 | setId(FaceId faceId); |
| 171 | |
| 172 | private: |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 173 | FaceId m_id; |
| 174 | std::string m_description; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 175 | bool m_isLocal; // for scoping purposes |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 176 | FaceCounters m_counters; |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 177 | FaceUri m_remoteUri; |
| 178 | FaceUri m_localUri; |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 179 | bool m_isOnDemand; |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 180 | bool m_isFailed; |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 181 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 182 | // allow setting FaceId |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 183 | friend class FaceTable; |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 186 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 187 | inline bool |
| 188 | Face::isLocal() const |
| 189 | { |
| 190 | return m_isLocal; |
| 191 | } |
| 192 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 193 | inline const FaceCounters& |
| 194 | Face::getCounters() const |
| 195 | { |
| 196 | return m_counters; |
| 197 | } |
| 198 | |
| 199 | inline FaceCounters& |
| 200 | Face::getMutableCounters() |
| 201 | { |
| 202 | return m_counters; |
| 203 | } |
| 204 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 205 | inline const FaceUri& |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 206 | Face::getRemoteUri() const |
| 207 | { |
| 208 | return m_remoteUri; |
| 209 | } |
| 210 | |
| 211 | inline const FaceUri& |
| 212 | Face::getLocalUri() const |
| 213 | { |
| 214 | return m_localUri; |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 217 | inline void |
| 218 | Face::setOnDemand(bool isOnDemand) |
| 219 | { |
| 220 | m_isOnDemand = isOnDemand; |
| 221 | } |
| 222 | |
| 223 | inline bool |
| 224 | Face::isOnDemand() const |
| 225 | { |
| 226 | return m_isOnDemand; |
| 227 | } |
| 228 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 229 | } // namespace nfd |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 230 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 231 | #endif // NFD_DAEMON_FACE_FACE_HPP |