blob: c61401161de3821c208bde6c73a411ddbcd0b748 [file] [log] [blame]
Junxiao Shi2c29f3a2014-01-24 19:59:00 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento77911cc2017-04-08 22:12:30 -04003 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shi33152f12014-07-16 19:54:32 -070024 */
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070025
Junxiao Shicde37ad2015-12-24 01:02:05 -070026#ifndef NFD_DAEMON_FACE_HPP
27#define NFD_DAEMON_FACE_HPP
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070028
Junxiao Shicde37ad2015-12-24 01:02:05 -070029#include "transport.hpp"
30#include "link-service.hpp"
Junxiao Shi33152f12014-07-16 19:54:32 -070031#include "face-counters.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070032#include "face-log.hpp"
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070033
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080034namespace nfd {
Junxiao Shicde37ad2015-12-24 01:02:05 -070035namespace face {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070036
Junxiao Shicde37ad2015-12-24 01:02:05 -070037/** \brief identifies a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070038 */
Junxiao Shicde37ad2015-12-24 01:02:05 -070039typedef uint64_t FaceId;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070040
Junxiao Shi7b984c62014-07-17 22:18:34 -070041/// indicates an invalid FaceId
Junxiao Shicde37ad2015-12-24 01:02:05 -070042const FaceId INVALID_FACEID = 0;
Junxiao Shi7b984c62014-07-17 22:18:34 -070043/// identifies the InternalFace used in management
44const FaceId FACEID_INTERNAL_FACE = 1;
Junxiao Shi0de23a22015-12-03 20:07:02 +000045/// identifies a packet comes from the ContentStore
Junxiao Shi7b984c62014-07-17 22:18:34 -070046const FaceId FACEID_CONTENT_STORE = 254;
47/// identifies the NullFace that drops every packet
48const FaceId FACEID_NULL = 255;
49/// upper bound of reserved FaceIds
50const FaceId FACEID_RESERVED_MAX = 255;
51
Junxiao Shicde37ad2015-12-24 01:02:05 -070052/** \brief indicates the state of a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070053 */
Junxiao Shicde37ad2015-12-24 01:02:05 -070054typedef TransportState FaceState;
55
56/** \brief generalization of a network interface
57 *
58 * A face generalizes a network interface.
59 * It provides best-effort network-layer packet delivery services
60 * on a physical interface, an overlay tunnel, or a link to a local application.
61 *
62 * A face combines two parts: LinkService and Transport.
63 * Transport is the lower part, which provides best-effort TLV block deliveries.
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 */
67class Face
68#ifndef WITH_TESTS
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020069final
Junxiao Shicde37ad2015-12-24 01:02:05 -070070#endif
71 : public enable_shared_from_this<Face>, noncopyable
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070072{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070073public:
Junxiao Shicde37ad2015-12-24 01:02:05 -070074 Face(unique_ptr<LinkService> service, unique_ptr<Transport> transport);
75
76 LinkService*
Eric Newberryc05918c2016-09-29 10:38:13 -070077 getLinkService() const;
Junxiao Shicde37ad2015-12-24 01:02:05 -070078
79 Transport*
Eric Newberryc05918c2016-09-29 10:38:13 -070080 getTransport() const;
Junxiao Shicde37ad2015-12-24 01:02:05 -070081
82public: // upper interface connected to forwarding
83 /** \brief sends Interest on Face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080084 */
Junxiao Shicde37ad2015-12-24 01:02:05 -070085 void
86 sendInterest(const Interest& interest);
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080087
Junxiao Shicde37ad2015-12-24 01:02:05 -070088 /** \brief sends Data on Face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080089 */
Junxiao Shicde37ad2015-12-24 01:02:05 -070090 void
91 sendData(const Data& data);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070092
Junxiao Shicde37ad2015-12-24 01:02:05 -070093 /** \brief sends Nack on Face
94 */
95 void
96 sendNack(const lp::Nack& nack);
97
98 /** \brief signals on Interest received
99 */
100 signal::Signal<LinkService, Interest>& afterReceiveInterest;
101
102 /** \brief signals on Data received
103 */
104 signal::Signal<LinkService, Data>& afterReceiveData;
105
106 /** \brief signals on Nack received
107 */
108 signal::Signal<LinkService, lp::Nack>& afterReceiveNack;
109
110public: // static properties
111 /** \return face ID
112 */
Junxiao Shi79494162014-04-02 18:25:11 -0700113 FaceId
114 getId() const;
115
Junxiao Shicde37ad2015-12-24 01:02:05 -0700116 /** \brief sets face ID
117 * \note Normally, this should only be invoked by FaceTable.
Davide Pesavento94279412015-02-27 01:29:32 +0100118 */
119 void
Junxiao Shicde37ad2015-12-24 01:02:05 -0700120 setId(FaceId id);
Davide Pesavento94279412015-02-27 01:29:32 +0100121
Alexander Afanasyev8f2e4832014-12-31 00:11:43 -0800122 void
123 setMetric(uint64_t metric);
124
125 uint64_t
126 getMetric() const;
127
Junxiao Shicde37ad2015-12-24 01:02:05 -0700128 /** \return a FaceUri representing local endpoint
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100129 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700130 FaceUri
131 getLocalUri() const;
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100132
Junxiao Shicde37ad2015-12-24 01:02:05 -0700133 /** \return a FaceUri representing remote endpoint
134 */
135 FaceUri
136 getRemoteUri() const;
137
138 /** \return whether face is local or non-local for scope control purpose
139 */
140 ndn::nfd::FaceScope
141 getScope() const;
142
143 /** \return face persistency setting
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700144 */
Yukai Tu731f0d72015-07-04 11:14:44 +0800145 ndn::nfd::FacePersistency
146 getPersistency() const;
Davide Pesavento94279412015-02-27 01:29:32 +0100147
Junxiao Shicde37ad2015-12-24 01:02:05 -0700148 /** \brief changes face persistency setting
Junxiao Shi08d07a72014-06-09 23:17:57 -0700149 */
150 void
Junxiao Shicde37ad2015-12-24 01:02:05 -0700151 setPersistency(ndn::nfd::FacePersistency persistency);
Junxiao Shi08d07a72014-06-09 23:17:57 -0700152
Junxiao Shicde37ad2015-12-24 01:02:05 -0700153 /** \return whether face is point-to-point or multi-access
154 */
155 ndn::nfd::LinkType
156 getLinkType() const;
Davide Pesavento94279412015-02-27 01:29:32 +0100157
Junxiao Shicde37ad2015-12-24 01:02:05 -0700158public: // dynamic properties
159 /** \return face state
160 */
161 FaceState
162 getState() const;
Junxiao Shic099ddb2014-12-25 20:53:20 -0700163
Junxiao Shicde37ad2015-12-24 01:02:05 -0700164 /** \brief signals after face state changed
165 */
166 signal::Signal<Transport, FaceState/*old*/, FaceState/*new*/>& afterStateChange;
167
Eric Newberryc64d30a2015-12-26 11:07:27 -0700168 /** \return expiration time of the face
169 * \retval time::steady_clock::TimePoint::max() the face has an indefinite lifetime
170 */
171 time::steady_clock::TimePoint
172 getExpirationTime() const;
173
Junxiao Shicde37ad2015-12-24 01:02:05 -0700174 /** \brief request the face to be closed
175 *
176 * This operation is effective only if face is in UP or DOWN state,
177 * otherwise it has no effect.
178 * The face changes state to CLOSING, and performs cleanup procedure.
179 * The state will be changed to CLOSED when cleanup is complete, which may
180 * happen synchronously or asynchronously.
181 *
182 * \warning the face must not be deallocated until its state changes to CLOSED
183 */
184 void
185 close();
186
187 const FaceCounters&
188 getCounters() const;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700189
190private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700191 FaceId m_id;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700192 unique_ptr<LinkService> m_service;
193 unique_ptr<Transport> m_transport;
194 FaceCounters m_counters;
Alexander Afanasyev8f2e4832014-12-31 00:11:43 -0800195 uint64_t m_metric;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700196};
197
Junxiao Shicde37ad2015-12-24 01:02:05 -0700198inline LinkService*
Eric Newberryc05918c2016-09-29 10:38:13 -0700199Face::getLinkService() const
Junxiao Shicde37ad2015-12-24 01:02:05 -0700200{
201 return m_service.get();
202}
203
204inline Transport*
Eric Newberryc05918c2016-09-29 10:38:13 -0700205Face::getTransport() const
Junxiao Shicde37ad2015-12-24 01:02:05 -0700206{
207 return m_transport.get();
208}
209
210inline void
211Face::sendInterest(const Interest& interest)
212{
213 m_service->sendInterest(interest);
214}
215
216inline void
217Face::sendData(const Data& data)
218{
219 m_service->sendData(data);
220}
221
222inline void
223Face::sendNack(const lp::Nack& nack)
224{
225 m_service->sendNack(nack);
226}
227
Davide Pesavento94279412015-02-27 01:29:32 +0100228inline FaceId
229Face::getId() const
230{
231 return m_id;
232}
233
234inline void
Junxiao Shicde37ad2015-12-24 01:02:05 -0700235Face::setId(FaceId id)
Davide Pesavento94279412015-02-27 01:29:32 +0100236{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700237 m_id = id;
Davide Pesavento94279412015-02-27 01:29:32 +0100238}
239
Alexander Afanasyev8f2e4832014-12-31 00:11:43 -0800240inline void
241Face::setMetric(uint64_t metric)
242{
243 m_metric = metric;
244}
245
246inline uint64_t
247Face::getMetric() const
248{
249 return m_metric;
250}
251
Junxiao Shicde37ad2015-12-24 01:02:05 -0700252inline FaceUri
253Face::getLocalUri() const
Davide Pesavento94279412015-02-27 01:29:32 +0100254{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700255 return m_transport->getLocalUri();
Davide Pesavento94279412015-02-27 01:29:32 +0100256}
257
Junxiao Shicde37ad2015-12-24 01:02:05 -0700258inline FaceUri
259Face::getRemoteUri() const
Davide Pesavento94279412015-02-27 01:29:32 +0100260{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700261 return m_transport->getRemoteUri();
Davide Pesavento94279412015-02-27 01:29:32 +0100262}
263
Junxiao Shicde37ad2015-12-24 01:02:05 -0700264inline ndn::nfd::FaceScope
265Face::getScope() const
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100266{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700267 return m_transport->getScope();
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100268}
269
Yukai Tu731f0d72015-07-04 11:14:44 +0800270inline ndn::nfd::FacePersistency
271Face::getPersistency() const
Davide Pesavento94279412015-02-27 01:29:32 +0100272{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700273 return m_transport->getPersistency();
Davide Pesavento94279412015-02-27 01:29:32 +0100274}
275
276inline void
Yukai Tu731f0d72015-07-04 11:14:44 +0800277Face::setPersistency(ndn::nfd::FacePersistency persistency)
Davide Pesavento94279412015-02-27 01:29:32 +0100278{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700279 return m_transport->setPersistency(persistency);
Davide Pesavento94279412015-02-27 01:29:32 +0100280}
281
Junxiao Shicde37ad2015-12-24 01:02:05 -0700282inline ndn::nfd::LinkType
283Face::getLinkType() const
Davide Pesavento94279412015-02-27 01:29:32 +0100284{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700285 return m_transport->getLinkType();
Davide Pesavento94279412015-02-27 01:29:32 +0100286}
287
Junxiao Shicde37ad2015-12-24 01:02:05 -0700288inline FaceState
289Face::getState() const
290{
291 return m_transport->getState();
292}
293
Eric Newberryc64d30a2015-12-26 11:07:27 -0700294inline time::steady_clock::TimePoint
295Face::getExpirationTime() const
296{
297 return m_transport->getExpirationTime();
298}
299
Junxiao Shicde37ad2015-12-24 01:02:05 -0700300inline void
301Face::close()
302{
303 m_transport->close();
304}
305
306inline const FaceCounters&
Junxiao Shi7860d482014-02-21 23:57:20 -0700307Face::getCounters() const
308{
Junxiao Shi7860d482014-02-21 23:57:20 -0700309 return m_counters;
310}
311
Junxiao Shicde37ad2015-12-24 01:02:05 -0700312std::ostream&
313operator<<(std::ostream& os, const FaceLogHelper<Face>& flh);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000314
Junxiao Shicde37ad2015-12-24 01:02:05 -0700315} // namespace face
316
317using face::FaceId;
318using face::Face;
319
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800320} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700321
Junxiao Shicde37ad2015-12-24 01:02:05 -0700322#endif // NFD_DAEMON_FACE_HPP