blob: 5486a4337446cc51d5ab2ed1a844bb713d0f4fc9 [file] [log] [blame]
Junxiao Shi2c29f3a2014-01-24 19:59:00 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventob84bd3a2016-04-22 02:21:45 +02003 * Copyright (c) 2014-2016, 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
Junxiao Shicde37ad2015-12-24 01:02:05 -0700122 /** \return a FaceUri representing local endpoint
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100123 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700124 FaceUri
125 getLocalUri() const;
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100126
Junxiao Shicde37ad2015-12-24 01:02:05 -0700127 /** \return a FaceUri representing remote endpoint
128 */
129 FaceUri
130 getRemoteUri() const;
131
132 /** \return whether face is local or non-local for scope control purpose
133 */
134 ndn::nfd::FaceScope
135 getScope() const;
136
137 /** \return face persistency setting
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700138 */
Yukai Tu731f0d72015-07-04 11:14:44 +0800139 ndn::nfd::FacePersistency
140 getPersistency() const;
Davide Pesavento94279412015-02-27 01:29:32 +0100141
Junxiao Shicde37ad2015-12-24 01:02:05 -0700142 /** \brief changes face persistency setting
Junxiao Shi08d07a72014-06-09 23:17:57 -0700143 */
144 void
Junxiao Shicde37ad2015-12-24 01:02:05 -0700145 setPersistency(ndn::nfd::FacePersistency persistency);
Junxiao Shi08d07a72014-06-09 23:17:57 -0700146
Junxiao Shicde37ad2015-12-24 01:02:05 -0700147 /** \return whether face is point-to-point or multi-access
148 */
149 ndn::nfd::LinkType
150 getLinkType() const;
Davide Pesavento94279412015-02-27 01:29:32 +0100151
Junxiao Shicde37ad2015-12-24 01:02:05 -0700152public: // dynamic properties
153 /** \return face state
154 */
155 FaceState
156 getState() const;
Junxiao Shic099ddb2014-12-25 20:53:20 -0700157
Junxiao Shicde37ad2015-12-24 01:02:05 -0700158 /** \brief signals after face state changed
159 */
160 signal::Signal<Transport, FaceState/*old*/, FaceState/*new*/>& afterStateChange;
161
Eric Newberryc64d30a2015-12-26 11:07:27 -0700162 /** \return expiration time of the face
163 * \retval time::steady_clock::TimePoint::max() the face has an indefinite lifetime
164 */
165 time::steady_clock::TimePoint
166 getExpirationTime() const;
167
Junxiao Shicde37ad2015-12-24 01:02:05 -0700168 /** \brief request the face to be closed
169 *
170 * This operation is effective only if face is in UP or DOWN state,
171 * otherwise it has no effect.
172 * The face changes state to CLOSING, and performs cleanup procedure.
173 * The state will be changed to CLOSED when cleanup is complete, which may
174 * happen synchronously or asynchronously.
175 *
176 * \warning the face must not be deallocated until its state changes to CLOSED
177 */
178 void
179 close();
180
181 const FaceCounters&
182 getCounters() const;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700183
184private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700185 FaceId m_id;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700186 unique_ptr<LinkService> m_service;
187 unique_ptr<Transport> m_transport;
188 FaceCounters m_counters;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700189};
190
Junxiao Shicde37ad2015-12-24 01:02:05 -0700191inline LinkService*
Eric Newberryc05918c2016-09-29 10:38:13 -0700192Face::getLinkService() const
Junxiao Shicde37ad2015-12-24 01:02:05 -0700193{
194 return m_service.get();
195}
196
197inline Transport*
Eric Newberryc05918c2016-09-29 10:38:13 -0700198Face::getTransport() const
Junxiao Shicde37ad2015-12-24 01:02:05 -0700199{
200 return m_transport.get();
201}
202
203inline void
204Face::sendInterest(const Interest& interest)
205{
206 m_service->sendInterest(interest);
207}
208
209inline void
210Face::sendData(const Data& data)
211{
212 m_service->sendData(data);
213}
214
215inline void
216Face::sendNack(const lp::Nack& nack)
217{
218 m_service->sendNack(nack);
219}
220
Davide Pesavento94279412015-02-27 01:29:32 +0100221inline FaceId
222Face::getId() const
223{
224 return m_id;
225}
226
227inline void
Junxiao Shicde37ad2015-12-24 01:02:05 -0700228Face::setId(FaceId id)
Davide Pesavento94279412015-02-27 01:29:32 +0100229{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700230 m_id = id;
Davide Pesavento94279412015-02-27 01:29:32 +0100231}
232
Junxiao Shicde37ad2015-12-24 01:02:05 -0700233inline FaceUri
234Face::getLocalUri() const
Davide Pesavento94279412015-02-27 01:29:32 +0100235{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700236 return m_transport->getLocalUri();
Davide Pesavento94279412015-02-27 01:29:32 +0100237}
238
Junxiao Shicde37ad2015-12-24 01:02:05 -0700239inline FaceUri
240Face::getRemoteUri() const
Davide Pesavento94279412015-02-27 01:29:32 +0100241{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700242 return m_transport->getRemoteUri();
Davide Pesavento94279412015-02-27 01:29:32 +0100243}
244
Junxiao Shicde37ad2015-12-24 01:02:05 -0700245inline ndn::nfd::FaceScope
246Face::getScope() const
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100247{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700248 return m_transport->getScope();
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100249}
250
Yukai Tu731f0d72015-07-04 11:14:44 +0800251inline ndn::nfd::FacePersistency
252Face::getPersistency() const
Davide Pesavento94279412015-02-27 01:29:32 +0100253{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700254 return m_transport->getPersistency();
Davide Pesavento94279412015-02-27 01:29:32 +0100255}
256
257inline void
Yukai Tu731f0d72015-07-04 11:14:44 +0800258Face::setPersistency(ndn::nfd::FacePersistency persistency)
Davide Pesavento94279412015-02-27 01:29:32 +0100259{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700260 return m_transport->setPersistency(persistency);
Davide Pesavento94279412015-02-27 01:29:32 +0100261}
262
Junxiao Shicde37ad2015-12-24 01:02:05 -0700263inline ndn::nfd::LinkType
264Face::getLinkType() const
Davide Pesavento94279412015-02-27 01:29:32 +0100265{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700266 return m_transport->getLinkType();
Davide Pesavento94279412015-02-27 01:29:32 +0100267}
268
Junxiao Shicde37ad2015-12-24 01:02:05 -0700269inline FaceState
270Face::getState() const
271{
272 return m_transport->getState();
273}
274
Eric Newberryc64d30a2015-12-26 11:07:27 -0700275inline time::steady_clock::TimePoint
276Face::getExpirationTime() const
277{
278 return m_transport->getExpirationTime();
279}
280
Junxiao Shicde37ad2015-12-24 01:02:05 -0700281inline void
282Face::close()
283{
284 m_transport->close();
285}
286
287inline const FaceCounters&
Junxiao Shi7860d482014-02-21 23:57:20 -0700288Face::getCounters() const
289{
Junxiao Shi7860d482014-02-21 23:57:20 -0700290 return m_counters;
291}
292
Junxiao Shicde37ad2015-12-24 01:02:05 -0700293std::ostream&
294operator<<(std::ostream& os, const FaceLogHelper<Face>& flh);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000295
Eric Newberrya98bf932015-09-21 00:58:47 -0700296template<typename T>
297typename std::enable_if<std::is_base_of<Face, T>::value, std::ostream&>::type
Junxiao Shicde37ad2015-12-24 01:02:05 -0700298operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
Eric Newberrya98bf932015-09-21 00:58:47 -0700299{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700300 return os << FaceLogHelper<Face>(flh.obj);
Eric Newberrya98bf932015-09-21 00:58:47 -0700301}
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100302
Junxiao Shicde37ad2015-12-24 01:02:05 -0700303} // namespace face
304
305using face::FaceId;
306using face::Face;
307
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800308} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700309
Junxiao Shicde37ad2015-12-24 01:02:05 -0700310#endif // NFD_DAEMON_FACE_HPP