Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 77911cc | 2017-04-08 22:12:30 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 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 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_HPP |
| 27 | #define NFD_DAEMON_FACE_HPP |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 28 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 29 | #include "transport.hpp" |
| 30 | #include "link-service.hpp" |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 31 | #include "face-counters.hpp" |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 32 | #include "face-log.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 | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 35 | namespace face { |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 37 | /** \brief identifies a face |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 38 | */ |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 39 | typedef uint64_t FaceId; |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 40 | |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 41 | /// indicates an invalid FaceId |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 42 | const FaceId INVALID_FACEID = 0; |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 43 | /// identifies the InternalFace used in management |
| 44 | const FaceId FACEID_INTERNAL_FACE = 1; |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 45 | /// identifies a packet comes from the ContentStore |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 46 | const FaceId FACEID_CONTENT_STORE = 254; |
| 47 | /// identifies the NullFace that drops every packet |
| 48 | const FaceId FACEID_NULL = 255; |
| 49 | /// upper bound of reserved FaceIds |
| 50 | const FaceId FACEID_RESERVED_MAX = 255; |
| 51 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 52 | /** \brief indicates the state of a face |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 53 | */ |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 54 | typedef TransportState FaceState; |
| 55 | |
| 56 | /** \brief generalization of a network interface |
| 57 | * |
| 58 | * A face generalizes a network interface. |
| 59 | * It provides best-effort network-layer packet delivery services |
| 60 | * on a physical interface, an overlay tunnel, or a link to a local application. |
| 61 | * |
| 62 | * A face combines two parts: LinkService and Transport. |
| 63 | * Transport is the lower part, which provides best-effort TLV block deliveries. |
| 64 | * LinkService is the upper part, which translates between network-layer packets |
| 65 | * and TLV blocks, and may provide additional services such as fragmentation and reassembly. |
| 66 | */ |
| 67 | class Face |
| 68 | #ifndef WITH_TESTS |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 69 | final |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 70 | #endif |
| 71 | : public enable_shared_from_this<Face>, noncopyable |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 72 | { |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 73 | public: |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 74 | Face(unique_ptr<LinkService> service, unique_ptr<Transport> transport); |
| 75 | |
| 76 | LinkService* |
Eric Newberry | c05918c | 2016-09-29 10:38:13 -0700 | [diff] [blame] | 77 | getLinkService() const; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 78 | |
| 79 | Transport* |
Eric Newberry | c05918c | 2016-09-29 10:38:13 -0700 | [diff] [blame] | 80 | getTransport() const; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 81 | |
| 82 | public: // upper interface connected to forwarding |
| 83 | /** \brief sends Interest on Face |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 84 | */ |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 85 | void |
| 86 | sendInterest(const Interest& interest); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 87 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 88 | /** \brief sends Data on Face |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 89 | */ |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 90 | void |
| 91 | sendData(const Data& data); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 93 | /** \brief sends Nack on Face |
| 94 | */ |
| 95 | void |
| 96 | sendNack(const lp::Nack& nack); |
| 97 | |
| 98 | /** \brief signals on Interest received |
| 99 | */ |
| 100 | signal::Signal<LinkService, Interest>& afterReceiveInterest; |
| 101 | |
| 102 | /** \brief signals on Data received |
| 103 | */ |
| 104 | signal::Signal<LinkService, Data>& afterReceiveData; |
| 105 | |
| 106 | /** \brief signals on Nack received |
| 107 | */ |
| 108 | signal::Signal<LinkService, lp::Nack>& afterReceiveNack; |
| 109 | |
| 110 | public: // static properties |
| 111 | /** \return face ID |
| 112 | */ |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 113 | FaceId |
| 114 | getId() const; |
| 115 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 116 | /** \brief sets face ID |
| 117 | * \note Normally, this should only be invoked by FaceTable. |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 118 | */ |
| 119 | void |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 120 | setId(FaceId id); |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 121 | |
Alexander Afanasyev | 8f2e483 | 2014-12-31 00:11:43 -0800 | [diff] [blame] | 122 | void |
| 123 | setMetric(uint64_t metric); |
| 124 | |
| 125 | uint64_t |
| 126 | getMetric() const; |
| 127 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 128 | /** \return a FaceUri representing local endpoint |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 129 | */ |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 130 | FaceUri |
| 131 | getLocalUri() const; |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 132 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 133 | /** \return a FaceUri representing remote endpoint |
| 134 | */ |
| 135 | FaceUri |
| 136 | getRemoteUri() const; |
| 137 | |
| 138 | /** \return whether face is local or non-local for scope control purpose |
| 139 | */ |
| 140 | ndn::nfd::FaceScope |
| 141 | getScope() const; |
| 142 | |
| 143 | /** \return face persistency setting |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 144 | */ |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 145 | ndn::nfd::FacePersistency |
| 146 | getPersistency() const; |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 147 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 148 | /** \brief changes face persistency setting |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 149 | */ |
| 150 | void |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 151 | setPersistency(ndn::nfd::FacePersistency persistency); |
Junxiao Shi | 08d07a7 | 2014-06-09 23:17:57 -0700 | [diff] [blame] | 152 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 153 | /** \return whether face is point-to-point or multi-access |
| 154 | */ |
| 155 | ndn::nfd::LinkType |
| 156 | getLinkType() const; |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 157 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 158 | public: // dynamic properties |
| 159 | /** \return face state |
| 160 | */ |
| 161 | FaceState |
| 162 | getState() const; |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 163 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 164 | /** \brief signals after face state changed |
| 165 | */ |
| 166 | signal::Signal<Transport, FaceState/*old*/, FaceState/*new*/>& afterStateChange; |
| 167 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 168 | /** \return expiration time of the face |
| 169 | * \retval time::steady_clock::TimePoint::max() the face has an indefinite lifetime |
| 170 | */ |
| 171 | time::steady_clock::TimePoint |
| 172 | getExpirationTime() const; |
| 173 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 174 | /** \brief request the face to be closed |
| 175 | * |
| 176 | * This operation is effective only if face is in UP or DOWN state, |
| 177 | * otherwise it has no effect. |
| 178 | * The face changes state to CLOSING, and performs cleanup procedure. |
| 179 | * The state will be changed to CLOSED when cleanup is complete, which may |
| 180 | * happen synchronously or asynchronously. |
| 181 | * |
| 182 | * \warning the face must not be deallocated until its state changes to CLOSED |
| 183 | */ |
| 184 | void |
| 185 | close(); |
| 186 | |
| 187 | const FaceCounters& |
| 188 | getCounters() const; |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 189 | |
| 190 | private: |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 191 | FaceId m_id; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 192 | unique_ptr<LinkService> m_service; |
| 193 | unique_ptr<Transport> m_transport; |
| 194 | FaceCounters m_counters; |
Alexander Afanasyev | 8f2e483 | 2014-12-31 00:11:43 -0800 | [diff] [blame] | 195 | uint64_t m_metric; |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 198 | inline LinkService* |
Eric Newberry | c05918c | 2016-09-29 10:38:13 -0700 | [diff] [blame] | 199 | Face::getLinkService() const |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 200 | { |
| 201 | return m_service.get(); |
| 202 | } |
| 203 | |
| 204 | inline Transport* |
Eric Newberry | c05918c | 2016-09-29 10:38:13 -0700 | [diff] [blame] | 205 | Face::getTransport() const |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 206 | { |
| 207 | return m_transport.get(); |
| 208 | } |
| 209 | |
| 210 | inline void |
| 211 | Face::sendInterest(const Interest& interest) |
| 212 | { |
| 213 | m_service->sendInterest(interest); |
| 214 | } |
| 215 | |
| 216 | inline void |
| 217 | Face::sendData(const Data& data) |
| 218 | { |
| 219 | m_service->sendData(data); |
| 220 | } |
| 221 | |
| 222 | inline void |
| 223 | Face::sendNack(const lp::Nack& nack) |
| 224 | { |
| 225 | m_service->sendNack(nack); |
| 226 | } |
| 227 | |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 228 | inline FaceId |
| 229 | Face::getId() const |
| 230 | { |
| 231 | return m_id; |
| 232 | } |
| 233 | |
| 234 | inline void |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 235 | Face::setId(FaceId id) |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 236 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 237 | m_id = id; |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Alexander Afanasyev | 8f2e483 | 2014-12-31 00:11:43 -0800 | [diff] [blame] | 240 | inline void |
| 241 | Face::setMetric(uint64_t metric) |
| 242 | { |
| 243 | m_metric = metric; |
| 244 | } |
| 245 | |
| 246 | inline uint64_t |
| 247 | Face::getMetric() const |
| 248 | { |
| 249 | return m_metric; |
| 250 | } |
| 251 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 252 | inline FaceUri |
| 253 | Face::getLocalUri() const |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 254 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 255 | return m_transport->getLocalUri(); |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 258 | inline FaceUri |
| 259 | Face::getRemoteUri() const |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 260 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 261 | return m_transport->getRemoteUri(); |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 264 | inline ndn::nfd::FaceScope |
| 265 | Face::getScope() const |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 266 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 267 | return m_transport->getScope(); |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 270 | inline ndn::nfd::FacePersistency |
| 271 | Face::getPersistency() const |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 272 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 273 | return m_transport->getPersistency(); |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | inline void |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 277 | Face::setPersistency(ndn::nfd::FacePersistency persistency) |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 278 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 279 | return m_transport->setPersistency(persistency); |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 282 | inline ndn::nfd::LinkType |
| 283 | Face::getLinkType() const |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 284 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 285 | return m_transport->getLinkType(); |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 288 | inline FaceState |
| 289 | Face::getState() const |
| 290 | { |
| 291 | return m_transport->getState(); |
| 292 | } |
| 293 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 294 | inline time::steady_clock::TimePoint |
| 295 | Face::getExpirationTime() const |
| 296 | { |
| 297 | return m_transport->getExpirationTime(); |
| 298 | } |
| 299 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 300 | inline void |
| 301 | Face::close() |
| 302 | { |
| 303 | m_transport->close(); |
| 304 | } |
| 305 | |
| 306 | inline const FaceCounters& |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 307 | Face::getCounters() const |
| 308 | { |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 309 | return m_counters; |
| 310 | } |
| 311 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 312 | std::ostream& |
| 313 | operator<<(std::ostream& os, const FaceLogHelper<Face>& flh); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 314 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 315 | } // namespace face |
| 316 | |
| 317 | using face::FaceId; |
| 318 | using face::Face; |
| 319 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 320 | } // namespace nfd |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 321 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 322 | #endif // NFD_DAEMON_FACE_HPP |