blob: 415c1858daf14b1f397735b455b8e417a05a2dd6 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry41aba102017-11-01 16:42:13 -07002/*
Davide Pesavento2cae8ca2019-04-18 20:48:05 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -07004 * 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
Davide Pesaventocb425e82019-07-14 21:48:22 -040029#include "face-common.hpp"
Eric Newberry41aba102017-11-01 16:42:13 -070030#include "transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040031#include "common/counter.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070032
33namespace nfd {
34namespace face {
35
Junxiao Shi57df2882015-11-11 06:12:35 -070036/** \brief counters provided by LinkService
37 * \note The type name 'LinkServiceCounters' is implementation detail.
38 * Use 'LinkService::Counters' in public API.
39 */
40class LinkServiceCounters
41{
42public:
43 /** \brief count of incoming Interests
44 */
45 PacketCounter nInInterests;
46
47 /** \brief count of outgoing Interests
48 */
49 PacketCounter nOutInterests;
50
Eric Newberry41aba102017-11-01 16:42:13 -070051 /** \brief count of Interests dropped by reliability system for exceeding allowed number of retx
52 */
53 PacketCounter nDroppedInterests;
54
55 /** \brief count of incoming Data packets
Junxiao Shi57df2882015-11-11 06:12:35 -070056 */
57 PacketCounter nInData;
58
Eric Newberry41aba102017-11-01 16:42:13 -070059 /** \brief count of outgoing Data packets
Junxiao Shi57df2882015-11-11 06:12:35 -070060 */
61 PacketCounter nOutData;
62
63 /** \brief count of incoming Nacks
64 */
65 PacketCounter nInNacks;
66
67 /** \brief count of outgoing Nacks
68 */
69 PacketCounter nOutNacks;
70};
71
Junxiao Shicde37ad2015-12-24 01:02:05 -070072/** \brief the upper part of a Face
73 * \sa Face
Eric Newberrya98bf932015-09-21 00:58:47 -070074 */
Junxiao Shi57df2882015-11-11 06:12:35 -070075class LinkService : protected virtual LinkServiceCounters, noncopyable
Eric Newberrya98bf932015-09-21 00:58:47 -070076{
77public:
Junxiao Shi57df2882015-11-11 06:12:35 -070078 /** \brief counters provided by LinkService
79 */
80 typedef LinkServiceCounters Counters;
81
82public:
Eric Newberrya98bf932015-09-21 00:58:47 -070083 LinkService();
84
85 virtual
86 ~LinkService();
87
88 /** \brief set Face and Transport for LinkService
89 * \pre setFaceAndTransport has not been called
90 */
91 void
Junxiao Shicde37ad2015-12-24 01:02:05 -070092 setFaceAndTransport(Face& face, Transport& transport);
Eric Newberrya98bf932015-09-21 00:58:47 -070093
94 /** \return Face to which this LinkService is attached
95 */
Junxiao Shicde37ad2015-12-24 01:02:05 -070096 const Face*
Eric Newberrya98bf932015-09-21 00:58:47 -070097 getFace() const;
98
99 /** \return Transport to which this LinkService is attached
100 */
101 const Transport*
102 getTransport() const;
103
104 /** \return Transport to which this LinkService is attached
105 */
106 Transport*
107 getTransport();
108
Junxiao Shi57df2882015-11-11 06:12:35 -0700109 virtual const Counters&
110 getCounters() const;
111
Eric Newberrya98bf932015-09-21 00:58:47 -0700112public: // upper interface to be used by forwarding
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400113 /** \brief Send Interest to \p endpoint
Eric Newberrya98bf932015-09-21 00:58:47 -0700114 * \pre setTransport has been called
115 */
116 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400117 sendInterest(const Interest& interest, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700118
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400119 /** \brief Send Data to \p endpoint
Eric Newberrya98bf932015-09-21 00:58:47 -0700120 * \pre setTransport has been called
121 */
122 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400123 sendData(const Data& data, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700124
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400125 /** \brief Send Nack to \p endpoint
Eric Newberrya98bf932015-09-21 00:58:47 -0700126 * \pre setTransport has been called
127 */
128 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400129 sendNack(const ndn::lp::Nack& nack, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700130
131 /** \brief signals on Interest received
132 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000133 signal::Signal<LinkService, Interest, EndpointId> afterReceiveInterest;
Eric Newberrya98bf932015-09-21 00:58:47 -0700134
135 /** \brief signals on Data received
136 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000137 signal::Signal<LinkService, Data, EndpointId> afterReceiveData;
Eric Newberrya98bf932015-09-21 00:58:47 -0700138
139 /** \brief signals on Nack received
140 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000141 signal::Signal<LinkService, lp::Nack, EndpointId> afterReceiveNack;
Eric Newberrya98bf932015-09-21 00:58:47 -0700142
Eric Newberry41aba102017-11-01 16:42:13 -0700143 /** \brief signals on Interest dropped by reliability system for exceeding allowed number of retx
144 */
145 signal::Signal<LinkService, Interest> onDroppedInterest;
146
Junxiao Shi57df2882015-11-11 06:12:35 -0700147public: // lower interface to be invoked by Transport
148 /** \brief performs LinkService specific operations to receive a lower-layer packet
Eric Newberrya98bf932015-09-21 00:58:47 -0700149 */
Junxiao Shi57df2882015-11-11 06:12:35 -0700150 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400151 receivePacket(const Block& packet, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700152
153protected: // upper interface to be invoked in subclass (receive path termination)
154 /** \brief delivers received Interest to forwarding
155 */
156 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400157 receiveInterest(const Interest& interest, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700158
159 /** \brief delivers received Data to forwarding
160 */
161 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400162 receiveData(const Data& data, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700163
164 /** \brief delivers received Nack to forwarding
165 */
166 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400167 receiveNack(const lp::Nack& nack, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700168
Eric Newberrya98bf932015-09-21 00:58:47 -0700169protected: // lower interface to be invoked in subclass (send path termination)
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400170 /** \brief send a lower-layer packet via Transport to \p endpoint
Eric Newberrya98bf932015-09-21 00:58:47 -0700171 */
172 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400173 sendPacket(const Block& packet, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700174
Eric Newberry41aba102017-11-01 16:42:13 -0700175protected:
176 void
177 notifyDroppedInterest(const Interest& packet);
178
Junxiao Shi57df2882015-11-11 06:12:35 -0700179private: // upper interface to be overridden in subclass (send path entrypoint)
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400180 /** \brief performs LinkService specific operations to send an Interest to \p endpoint
Junxiao Shi57df2882015-11-11 06:12:35 -0700181 */
182 virtual void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400183 doSendInterest(const Interest& interest, const EndpointId& endpoint) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700184
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400185 /** \brief performs LinkService specific operations to send a Data to \p endpoint
Junxiao Shi57df2882015-11-11 06:12:35 -0700186 */
187 virtual void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400188 doSendData(const Data& data, const EndpointId& endpoint) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700189
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400190 /** \brief performs LinkService specific operations to send a Nack to \p endpoint
Junxiao Shi57df2882015-11-11 06:12:35 -0700191 */
192 virtual void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400193 doSendNack(const lp::Nack& nack, const EndpointId& endpoint) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700194
Eric Newberrya98bf932015-09-21 00:58:47 -0700195private: // lower interface to be overridden in subclass
196 virtual void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400197 doReceivePacket(const Block& packet, const EndpointId& endpoint) = 0;
Eric Newberrya98bf932015-09-21 00:58:47 -0700198
199private:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700200 Face* m_face;
Eric Newberrya98bf932015-09-21 00:58:47 -0700201 Transport* m_transport;
Eric Newberrya98bf932015-09-21 00:58:47 -0700202};
203
Junxiao Shicde37ad2015-12-24 01:02:05 -0700204inline const Face*
Eric Newberrya98bf932015-09-21 00:58:47 -0700205LinkService::getFace() const
206{
207 return m_face;
208}
209
210inline const Transport*
211LinkService::getTransport() const
212{
213 return m_transport;
214}
215
216inline Transport*
217LinkService::getTransport()
218{
219 return m_transport;
220}
221
Junxiao Shi57df2882015-11-11 06:12:35 -0700222inline const LinkService::Counters&
223LinkService::getCounters() const
224{
225 return *this;
226}
227
Eric Newberrya98bf932015-09-21 00:58:47 -0700228inline void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400229LinkService::receivePacket(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -0700230{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400231 doReceivePacket(packet, endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700232}
233
234inline void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400235LinkService::sendPacket(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -0700236{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400237 m_transport->send(packet, endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700238}
239
240std::ostream&
241operator<<(std::ostream& os, const FaceLogHelper<LinkService>& flh);
242
243template<typename T>
244typename std::enable_if<std::is_base_of<LinkService, T>::value &&
245 !std::is_same<LinkService, T>::value, std::ostream&>::type
246operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
247{
248 return os << FaceLogHelper<LinkService>(flh.obj);
249}
250
251} // namespace face
252} // namespace nfd
253
254#endif // NFD_DAEMON_FACE_LINK_SERVICE_HPP