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 | |
| 29 | #include "transport.hpp" |
| 30 | #include "face-log.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | namespace face { |
| 34 | |
| 35 | class LpFace; |
| 36 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame^] | 37 | /** \brief counters provided by LinkService |
| 38 | * \note The type name 'LinkServiceCounters' is implementation detail. |
| 39 | * Use 'LinkService::Counters' in public API. |
| 40 | */ |
| 41 | class LinkServiceCounters |
| 42 | { |
| 43 | public: |
| 44 | /** \brief count of incoming Interests |
| 45 | */ |
| 46 | PacketCounter nInInterests; |
| 47 | |
| 48 | /** \brief count of outgoing Interests |
| 49 | */ |
| 50 | PacketCounter nOutInterests; |
| 51 | |
| 52 | /** \brief count of incoming Data |
| 53 | */ |
| 54 | PacketCounter nInData; |
| 55 | |
| 56 | /** \brief count of outgoing Data |
| 57 | */ |
| 58 | PacketCounter nOutData; |
| 59 | |
| 60 | /** \brief count of incoming Nacks |
| 61 | */ |
| 62 | PacketCounter nInNacks; |
| 63 | |
| 64 | /** \brief count of outgoing Nacks |
| 65 | */ |
| 66 | PacketCounter nOutNacks; |
| 67 | }; |
| 68 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 69 | /** \brief the upper part of an LpFace |
| 70 | * \sa LpFace |
| 71 | */ |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame^] | 72 | class LinkService : protected virtual LinkServiceCounters, noncopyable |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 73 | { |
| 74 | public: |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame^] | 75 | /** \brief counters provided by LinkService |
| 76 | */ |
| 77 | typedef LinkServiceCounters Counters; |
| 78 | |
| 79 | public: |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 80 | LinkService(); |
| 81 | |
| 82 | virtual |
| 83 | ~LinkService(); |
| 84 | |
| 85 | /** \brief set Face and Transport for LinkService |
| 86 | * \pre setFaceAndTransport has not been called |
| 87 | */ |
| 88 | void |
| 89 | setFaceAndTransport(LpFace& face, Transport& transport); |
| 90 | |
| 91 | /** \return Face to which this LinkService is attached |
| 92 | */ |
| 93 | const LpFace* |
| 94 | getFace() const; |
| 95 | |
| 96 | /** \return Transport to which this LinkService is attached |
| 97 | */ |
| 98 | const Transport* |
| 99 | getTransport() const; |
| 100 | |
| 101 | /** \return Transport to which this LinkService is attached |
| 102 | */ |
| 103 | Transport* |
| 104 | getTransport(); |
| 105 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame^] | 106 | virtual const Counters& |
| 107 | getCounters() const; |
| 108 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 109 | public: // upper interface to be used by forwarding |
| 110 | /** \brief send Interest |
| 111 | * \pre setTransport has been called |
| 112 | */ |
| 113 | void |
| 114 | sendInterest(const Interest& interest); |
| 115 | |
| 116 | /** \brief send Data |
| 117 | * \pre setTransport has been called |
| 118 | */ |
| 119 | void |
| 120 | sendData(const Data& data); |
| 121 | |
| 122 | /** \brief send Nack |
| 123 | * \pre setTransport has been called |
| 124 | */ |
| 125 | void |
| 126 | sendNack(const ndn::lp::Nack& nack); |
| 127 | |
| 128 | /** \brief signals on Interest received |
| 129 | */ |
| 130 | signal::Signal<LinkService, Interest> afterReceiveInterest; |
| 131 | |
| 132 | /** \brief signals on Data received |
| 133 | */ |
| 134 | signal::Signal<LinkService, Data> afterReceiveData; |
| 135 | |
| 136 | /** \brief signals on Nack received |
| 137 | */ |
| 138 | signal::Signal<LinkService, lp::Nack> afterReceiveNack; |
| 139 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame^] | 140 | public: // lower interface to be invoked by Transport |
| 141 | /** \brief performs LinkService specific operations to receive a lower-layer packet |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 142 | */ |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame^] | 143 | void |
| 144 | receivePacket(Transport::Packet&& packet); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 145 | |
| 146 | protected: // upper interface to be invoked in subclass (receive path termination) |
| 147 | /** \brief delivers received Interest to forwarding |
| 148 | */ |
| 149 | void |
| 150 | receiveInterest(const Interest& interest); |
| 151 | |
| 152 | /** \brief delivers received Data to forwarding |
| 153 | */ |
| 154 | void |
| 155 | receiveData(const Data& data); |
| 156 | |
| 157 | /** \brief delivers received Nack to forwarding |
| 158 | */ |
| 159 | void |
| 160 | receiveNack(const lp::Nack& nack); |
| 161 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 162 | protected: // lower interface to be invoked in subclass (send path termination) |
| 163 | /** \brief sends a lower-layer packet via Transport |
| 164 | */ |
| 165 | void |
| 166 | sendPacket(Transport::Packet&& packet); |
| 167 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame^] | 168 | private: // upper interface to be overridden in subclass (send path entrypoint) |
| 169 | /** \brief performs LinkService specific operations to send an Interest |
| 170 | */ |
| 171 | virtual void |
| 172 | doSendInterest(const Interest& interest) = 0; |
| 173 | |
| 174 | /** \brief performs LinkService specific operations to send a Data |
| 175 | */ |
| 176 | virtual void |
| 177 | doSendData(const Data& data) = 0; |
| 178 | |
| 179 | /** \brief performs LinkService specific operations to send a Nack |
| 180 | */ |
| 181 | virtual void |
| 182 | doSendNack(const lp::Nack& nack) = 0; |
| 183 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 184 | private: // lower interface to be overridden in subclass |
| 185 | virtual void |
| 186 | doReceivePacket(Transport::Packet&& packet) = 0; |
| 187 | |
| 188 | private: |
| 189 | LpFace* m_face; |
| 190 | Transport* m_transport; |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame^] | 191 | NetworkLayerCounters* m_oldCounters; // old counters from LpFaceWrapper |
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 |