Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 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. |
| 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 | */ |
| 25 | |
| 26 | #ifndef NFD_DAEMON_LP_FACE_HPP |
| 27 | #define NFD_DAEMON_LP_FACE_HPP |
| 28 | |
| 29 | #include "transport.hpp" |
| 30 | #include "link-service.hpp" |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -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" |
| 33 | |
| 34 | namespace nfd { |
| 35 | namespace face { |
| 36 | |
| 37 | /** \brief identifies a face |
| 38 | */ |
| 39 | typedef uint64_t FaceId; |
| 40 | |
| 41 | /// indicates an invalid FaceId |
| 42 | const FaceId INVALID_FACEID = 0; |
| 43 | /// identifies the InternalFace used in management |
| 44 | const FaceId FACEID_INTERNAL_FACE = 1; |
| 45 | /// identifies a packet comes from the ContentStore, in LocalControlHeader incomingFaceId |
| 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 | |
| 52 | /** \brief indicates the state of a face |
| 53 | */ |
| 54 | typedef TransportState FaceState; |
| 55 | |
| 56 | /** \brief generalization of a network interface |
| 57 | * |
| 58 | * A face generalizes a network interface. |
| 59 | * It provides network-layer packet delivery services on a physical interface, |
| 60 | * an overlay tunnel, or a link to a local application, with best-effort. |
| 61 | * |
| 62 | * A face combines two parts: LinkService and Transport. |
| 63 | * Transport is the lower part, which provides TLV block delivery services with best-effort. |
| 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 | * We are in the process of refactoring face system to use this LinkService+Transport |
| 68 | * architecture. During this process, the "face" is named LpFace, and we implement a |
| 69 | * LpFaceWrapper class as an adaptor to the old Face APIs. After the completion of refactoring, |
| 70 | * LpFace will be renamed to Face. |
| 71 | */ |
| 72 | class LpFace |
| 73 | #ifndef WITH_TESTS |
| 74 | DECL_CLASS_FINAL |
| 75 | #endif |
| 76 | : noncopyable |
| 77 | { |
| 78 | public: |
| 79 | LpFace(unique_ptr<LinkService> service, unique_ptr<Transport> transport); |
| 80 | |
| 81 | LinkService* |
| 82 | getLinkService(); |
| 83 | |
| 84 | Transport* |
| 85 | getTransport(); |
| 86 | |
| 87 | public: // upper interface connected to forwarding |
| 88 | /** \brief sends Interest on Face |
| 89 | */ |
| 90 | void |
| 91 | sendInterest(const Interest& interest); |
| 92 | |
| 93 | /** \brief sends Data on Face |
| 94 | */ |
| 95 | void |
| 96 | sendData(const Data& data); |
| 97 | |
| 98 | /** \brief sends Nack on Face |
| 99 | */ |
| 100 | void |
| 101 | sendNack(const lp::Nack& nack); |
| 102 | |
| 103 | /** \brief signals on Interest received |
| 104 | */ |
| 105 | signal::Signal<LinkService, Interest>& afterReceiveInterest; |
| 106 | |
| 107 | /** \brief signals on Data received |
| 108 | */ |
| 109 | signal::Signal<LinkService, Data>& afterReceiveData; |
| 110 | |
| 111 | /** \brief signals on Nack received |
| 112 | */ |
| 113 | signal::Signal<LinkService, lp::Nack>& afterReceiveNack; |
| 114 | |
| 115 | public: // static properties |
| 116 | /** \return face ID |
| 117 | */ |
| 118 | FaceId |
| 119 | getId() const; |
| 120 | |
| 121 | /** \brief sets face ID |
| 122 | * \note Normally, this should only be invoked by FaceTable. |
| 123 | */ |
| 124 | void |
| 125 | setId(FaceId id); |
| 126 | |
| 127 | /** \return a FaceUri representing local endpoint |
| 128 | */ |
| 129 | FaceUri |
| 130 | getLocalUri() const; |
| 131 | |
| 132 | /** \return a FaceUri representing remote endpoint |
| 133 | */ |
| 134 | FaceUri |
| 135 | getRemoteUri() const; |
| 136 | |
| 137 | /** \return whether face is local or non-local for scope control purpose |
| 138 | */ |
| 139 | ndn::nfd::FaceScope |
| 140 | getScope() const; |
| 141 | |
| 142 | /** \return face persistency setting |
| 143 | */ |
| 144 | ndn::nfd::FacePersistency |
| 145 | getPersistency() const; |
| 146 | |
| 147 | /** \brief changes face persistency setting |
| 148 | */ |
| 149 | void |
| 150 | setPersistency(ndn::nfd::FacePersistency persistency); |
| 151 | |
| 152 | /** \return whether face is point-to-point or multi-access |
| 153 | */ |
| 154 | ndn::nfd::LinkType |
| 155 | getLinkType() const; |
| 156 | |
| 157 | public: // dynamic properties |
| 158 | /** \return face state |
| 159 | */ |
| 160 | FaceState |
| 161 | getState() const; |
| 162 | |
| 163 | /** \brief signals after face state changed |
| 164 | */ |
| 165 | signal::Signal<Transport, FaceState/*old*/, FaceState/*new*/>& afterStateChange; |
| 166 | |
| 167 | /** \brief request the face to be closed |
| 168 | * |
| 169 | * This operation is effective only if face is in UP or DOWN state, |
| 170 | * otherwise it has no effect. |
| 171 | * The face changes state to CLOSING, and performs cleanup procedure. |
| 172 | * The state will be changed to CLOSED when cleanup is complete, which may |
| 173 | * happen synchronously or asynchronously. |
| 174 | * |
| 175 | * \warning the face must not be deallocated until its state changes to CLOSED |
| 176 | */ |
| 177 | void |
| 178 | close(); |
| 179 | |
| 180 | const FaceCounters& |
| 181 | getCounters() const; |
| 182 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 183 | private: |
| 184 | FaceId m_id; |
| 185 | unique_ptr<LinkService> m_service; |
| 186 | unique_ptr<Transport> m_transport; |
| 187 | FaceCounters m_counters; |
| 188 | }; |
| 189 | |
| 190 | inline LinkService* |
| 191 | LpFace::getLinkService() |
| 192 | { |
| 193 | return m_service.get(); |
| 194 | } |
| 195 | |
| 196 | inline Transport* |
| 197 | LpFace::getTransport() |
| 198 | { |
| 199 | return m_transport.get(); |
| 200 | } |
| 201 | |
| 202 | inline void |
| 203 | LpFace::sendInterest(const Interest& interest) |
| 204 | { |
| 205 | m_service->sendInterest(interest); |
| 206 | } |
| 207 | |
| 208 | inline void |
| 209 | LpFace::sendData(const Data& data) |
| 210 | { |
| 211 | m_service->sendData(data); |
| 212 | } |
| 213 | |
| 214 | inline void |
| 215 | LpFace::sendNack(const lp::Nack& nack) |
| 216 | { |
| 217 | m_service->sendNack(nack); |
| 218 | } |
| 219 | |
| 220 | inline FaceId |
| 221 | LpFace::getId() const |
| 222 | { |
| 223 | return m_id; |
| 224 | } |
| 225 | |
| 226 | inline void |
| 227 | LpFace::setId(FaceId id) |
| 228 | { |
| 229 | m_id = id; |
| 230 | } |
| 231 | |
| 232 | inline FaceUri |
| 233 | LpFace::getLocalUri() const |
| 234 | { |
| 235 | return m_transport->getLocalUri(); |
| 236 | } |
| 237 | |
| 238 | inline FaceUri |
| 239 | LpFace::getRemoteUri() const |
| 240 | { |
| 241 | return m_transport->getRemoteUri(); |
| 242 | } |
| 243 | |
| 244 | inline ndn::nfd::FaceScope |
| 245 | LpFace::getScope() const |
| 246 | { |
| 247 | return m_transport->getScope(); |
| 248 | } |
| 249 | |
| 250 | inline ndn::nfd::FacePersistency |
| 251 | LpFace::getPersistency() const |
| 252 | { |
| 253 | return m_transport->getPersistency(); |
| 254 | } |
| 255 | |
| 256 | inline void |
| 257 | LpFace::setPersistency(ndn::nfd::FacePersistency persistency) |
| 258 | { |
| 259 | return m_transport->setPersistency(persistency); |
| 260 | } |
| 261 | |
| 262 | inline ndn::nfd::LinkType |
| 263 | LpFace::getLinkType() const |
| 264 | { |
| 265 | return m_transport->getLinkType(); |
| 266 | } |
| 267 | |
| 268 | inline FaceState |
| 269 | LpFace::getState() const |
| 270 | { |
| 271 | return m_transport->getState(); |
| 272 | } |
| 273 | |
| 274 | inline void |
| 275 | LpFace::close() |
| 276 | { |
| 277 | m_transport->close(); |
| 278 | } |
| 279 | |
| 280 | inline const FaceCounters& |
| 281 | LpFace::getCounters() const |
| 282 | { |
| 283 | return m_counters; |
| 284 | } |
| 285 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 286 | template<typename T> |
| 287 | typename std::enable_if<std::is_base_of<LpFace, T>::value, std::ostream&>::type |
| 288 | operator<<(std::ostream& os, const FaceLogHelper<T>& flh) |
| 289 | { |
| 290 | const LpFace& face = flh.obj; |
| 291 | os << "[id=" << face.getId() << ",local=" << face.getLocalUri() << |
| 292 | ",remote=" << face.getRemoteUri() << "] "; |
| 293 | return os; |
| 294 | } |
| 295 | |
| 296 | } // namespace face |
| 297 | |
| 298 | // using face::FaceId; // TODO uncomment in #3172 |
| 299 | using face::LpFace; |
| 300 | |
| 301 | } // namespace nfd |
| 302 | |
| 303 | #endif // NFD_DAEMON_LP_FACE_HPP |