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