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