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_FACE_LINK_SERVICE_HPP |
| 27 | #define NFD_DAEMON_FACE_LINK_SERVICE_HPP |
| 28 | |
Junxiao Shi | da93f1f | 2015-11-11 06:13:16 -0700 | [diff] [blame] | 29 | #include "core/counter.hpp" |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 30 | #include "transport.hpp" |
| 31 | #include "face-log.hpp" |
| 32 | |
| 33 | namespace nfd { |
| 34 | namespace face { |
| 35 | |
| 36 | class LpFace; |
| 37 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 38 | /** \brief counters provided by LinkService |
| 39 | * \note The type name 'LinkServiceCounters' is implementation detail. |
| 40 | * Use 'LinkService::Counters' in public API. |
| 41 | */ |
| 42 | class LinkServiceCounters |
| 43 | { |
| 44 | public: |
| 45 | /** \brief count of incoming Interests |
| 46 | */ |
| 47 | PacketCounter nInInterests; |
| 48 | |
| 49 | /** \brief count of outgoing Interests |
| 50 | */ |
| 51 | PacketCounter nOutInterests; |
| 52 | |
| 53 | /** \brief count of incoming Data |
| 54 | */ |
| 55 | PacketCounter nInData; |
| 56 | |
| 57 | /** \brief count of outgoing Data |
| 58 | */ |
| 59 | PacketCounter nOutData; |
| 60 | |
| 61 | /** \brief count of incoming Nacks |
| 62 | */ |
| 63 | PacketCounter nInNacks; |
| 64 | |
| 65 | /** \brief count of outgoing Nacks |
| 66 | */ |
| 67 | PacketCounter nOutNacks; |
| 68 | }; |
| 69 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 70 | /** \brief the upper part of an LpFace |
| 71 | * \sa LpFace |
| 72 | */ |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 73 | class LinkService : protected virtual LinkServiceCounters, noncopyable |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 74 | { |
| 75 | public: |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 76 | /** \brief counters provided by LinkService |
| 77 | */ |
| 78 | typedef LinkServiceCounters Counters; |
| 79 | |
| 80 | public: |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 81 | LinkService(); |
| 82 | |
| 83 | virtual |
| 84 | ~LinkService(); |
| 85 | |
| 86 | /** \brief set Face and Transport for LinkService |
| 87 | * \pre setFaceAndTransport has not been called |
| 88 | */ |
| 89 | void |
| 90 | setFaceAndTransport(LpFace& face, Transport& transport); |
| 91 | |
| 92 | /** \return Face to which this LinkService is attached |
| 93 | */ |
| 94 | const LpFace* |
| 95 | getFace() const; |
| 96 | |
| 97 | /** \return Transport to which this LinkService is attached |
| 98 | */ |
| 99 | const Transport* |
| 100 | getTransport() const; |
| 101 | |
| 102 | /** \return Transport to which this LinkService is attached |
| 103 | */ |
| 104 | Transport* |
| 105 | getTransport(); |
| 106 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 107 | virtual const Counters& |
| 108 | getCounters() const; |
| 109 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 110 | public: // upper interface to be used by forwarding |
| 111 | /** \brief send Interest |
| 112 | * \pre setTransport has been called |
| 113 | */ |
| 114 | void |
| 115 | sendInterest(const Interest& interest); |
| 116 | |
| 117 | /** \brief send Data |
| 118 | * \pre setTransport has been called |
| 119 | */ |
| 120 | void |
| 121 | sendData(const Data& data); |
| 122 | |
| 123 | /** \brief send Nack |
| 124 | * \pre setTransport has been called |
| 125 | */ |
| 126 | void |
| 127 | sendNack(const ndn::lp::Nack& nack); |
| 128 | |
| 129 | /** \brief signals on Interest received |
| 130 | */ |
| 131 | signal::Signal<LinkService, Interest> afterReceiveInterest; |
| 132 | |
| 133 | /** \brief signals on Data received |
| 134 | */ |
| 135 | signal::Signal<LinkService, Data> afterReceiveData; |
| 136 | |
| 137 | /** \brief signals on Nack received |
| 138 | */ |
| 139 | signal::Signal<LinkService, lp::Nack> afterReceiveNack; |
| 140 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 141 | public: // lower interface to be invoked by Transport |
| 142 | /** \brief performs LinkService specific operations to receive a lower-layer packet |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 143 | */ |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 144 | void |
| 145 | receivePacket(Transport::Packet&& packet); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 146 | |
| 147 | protected: // upper interface to be invoked in subclass (receive path termination) |
| 148 | /** \brief delivers received Interest to forwarding |
| 149 | */ |
| 150 | void |
| 151 | receiveInterest(const Interest& interest); |
| 152 | |
| 153 | /** \brief delivers received Data to forwarding |
| 154 | */ |
| 155 | void |
| 156 | receiveData(const Data& data); |
| 157 | |
| 158 | /** \brief delivers received Nack to forwarding |
| 159 | */ |
| 160 | void |
| 161 | receiveNack(const lp::Nack& nack); |
| 162 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 163 | protected: // lower interface to be invoked in subclass (send path termination) |
| 164 | /** \brief sends a lower-layer packet via Transport |
| 165 | */ |
| 166 | void |
| 167 | sendPacket(Transport::Packet&& packet); |
| 168 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 169 | private: // upper interface to be overridden in subclass (send path entrypoint) |
| 170 | /** \brief performs LinkService specific operations to send an Interest |
| 171 | */ |
| 172 | virtual void |
| 173 | doSendInterest(const Interest& interest) = 0; |
| 174 | |
| 175 | /** \brief performs LinkService specific operations to send a Data |
| 176 | */ |
| 177 | virtual void |
| 178 | doSendData(const Data& data) = 0; |
| 179 | |
| 180 | /** \brief performs LinkService specific operations to send a Nack |
| 181 | */ |
| 182 | virtual void |
| 183 | doSendNack(const lp::Nack& nack) = 0; |
| 184 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 185 | private: // lower interface to be overridden in subclass |
| 186 | virtual void |
| 187 | doReceivePacket(Transport::Packet&& packet) = 0; |
| 188 | |
| 189 | private: |
| 190 | LpFace* m_face; |
| 191 | Transport* m_transport; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | inline const LpFace* |
| 195 | LinkService::getFace() const |
| 196 | { |
| 197 | return m_face; |
| 198 | } |
| 199 | |
| 200 | inline const Transport* |
| 201 | LinkService::getTransport() const |
| 202 | { |
| 203 | return m_transport; |
| 204 | } |
| 205 | |
| 206 | inline Transport* |
| 207 | LinkService::getTransport() |
| 208 | { |
| 209 | return m_transport; |
| 210 | } |
| 211 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 212 | inline const LinkService::Counters& |
| 213 | LinkService::getCounters() const |
| 214 | { |
| 215 | return *this; |
| 216 | } |
| 217 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 218 | inline void |
| 219 | LinkService::receivePacket(Transport::Packet&& packet) |
| 220 | { |
| 221 | doReceivePacket(std::move(packet)); |
| 222 | } |
| 223 | |
| 224 | inline void |
| 225 | LinkService::sendPacket(Transport::Packet&& packet) |
| 226 | { |
| 227 | m_transport->send(std::move(packet)); |
| 228 | } |
| 229 | |
| 230 | std::ostream& |
| 231 | operator<<(std::ostream& os, const FaceLogHelper<LinkService>& flh); |
| 232 | |
| 233 | template<typename T> |
| 234 | typename std::enable_if<std::is_base_of<LinkService, T>::value && |
| 235 | !std::is_same<LinkService, T>::value, std::ostream&>::type |
| 236 | operator<<(std::ostream& os, const FaceLogHelper<T>& flh) |
| 237 | { |
| 238 | return os << FaceLogHelper<LinkService>(flh.obj); |
| 239 | } |
| 240 | |
| 241 | } // namespace face |
| 242 | } // namespace nfd |
| 243 | |
| 244 | #endif // NFD_DAEMON_FACE_LINK_SERVICE_HPP |