blob: d783dd013d1ecbb8504e16947f11e61f3b55a3a2 [file] [log] [blame]
Junxiao Shi2c29f3a2014-01-24 19:59:00 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * 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.
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
69DECL_CLASS_FINAL
70#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*
77 getLinkService();
78
79 Transport*
80 getTransport();
81
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
162 /** \brief request the face to be closed
163 *
164 * This operation is effective only if face is in UP or DOWN state,
165 * otherwise it has no effect.
166 * The face changes state to CLOSING, and performs cleanup procedure.
167 * The state will be changed to CLOSED when cleanup is complete, which may
168 * happen synchronously or asynchronously.
169 *
170 * \warning the face must not be deallocated until its state changes to CLOSED
171 */
172 void
173 close();
174
175 const FaceCounters&
176 getCounters() const;
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700177
178private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700179 FaceId m_id;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700180 unique_ptr<LinkService> m_service;
181 unique_ptr<Transport> m_transport;
182 FaceCounters m_counters;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700183};
184
Junxiao Shicde37ad2015-12-24 01:02:05 -0700185inline LinkService*
186Face::getLinkService()
187{
188 return m_service.get();
189}
190
191inline Transport*
192Face::getTransport()
193{
194 return m_transport.get();
195}
196
197inline void
198Face::sendInterest(const Interest& interest)
199{
200 m_service->sendInterest(interest);
201}
202
203inline void
204Face::sendData(const Data& data)
205{
206 m_service->sendData(data);
207}
208
209inline void
210Face::sendNack(const lp::Nack& nack)
211{
212 m_service->sendNack(nack);
213}
214
Davide Pesavento94279412015-02-27 01:29:32 +0100215inline FaceId
216Face::getId() const
217{
218 return m_id;
219}
220
221inline void
Junxiao Shicde37ad2015-12-24 01:02:05 -0700222Face::setId(FaceId id)
Davide Pesavento94279412015-02-27 01:29:32 +0100223{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700224 m_id = id;
Davide Pesavento94279412015-02-27 01:29:32 +0100225}
226
Junxiao Shicde37ad2015-12-24 01:02:05 -0700227inline FaceUri
228Face::getLocalUri() const
Davide Pesavento94279412015-02-27 01:29:32 +0100229{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700230 return m_transport->getLocalUri();
Davide Pesavento94279412015-02-27 01:29:32 +0100231}
232
Junxiao Shicde37ad2015-12-24 01:02:05 -0700233inline FaceUri
234Face::getRemoteUri() const
Davide Pesavento94279412015-02-27 01:29:32 +0100235{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700236 return m_transport->getRemoteUri();
Davide Pesavento94279412015-02-27 01:29:32 +0100237}
238
Junxiao Shicde37ad2015-12-24 01:02:05 -0700239inline ndn::nfd::FaceScope
240Face::getScope() const
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100241{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700242 return m_transport->getScope();
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100243}
244
Yukai Tu731f0d72015-07-04 11:14:44 +0800245inline ndn::nfd::FacePersistency
246Face::getPersistency() const
Davide Pesavento94279412015-02-27 01:29:32 +0100247{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700248 return m_transport->getPersistency();
Davide Pesavento94279412015-02-27 01:29:32 +0100249}
250
251inline void
Yukai Tu731f0d72015-07-04 11:14:44 +0800252Face::setPersistency(ndn::nfd::FacePersistency persistency)
Davide Pesavento94279412015-02-27 01:29:32 +0100253{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700254 return m_transport->setPersistency(persistency);
Davide Pesavento94279412015-02-27 01:29:32 +0100255}
256
Junxiao Shicde37ad2015-12-24 01:02:05 -0700257inline ndn::nfd::LinkType
258Face::getLinkType() const
Davide Pesavento94279412015-02-27 01:29:32 +0100259{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700260 return m_transport->getLinkType();
Davide Pesavento94279412015-02-27 01:29:32 +0100261}
262
Junxiao Shicde37ad2015-12-24 01:02:05 -0700263inline FaceState
264Face::getState() const
265{
266 return m_transport->getState();
267}
268
269inline void
270Face::close()
271{
272 m_transport->close();
273}
274
275inline const FaceCounters&
Junxiao Shi7860d482014-02-21 23:57:20 -0700276Face::getCounters() const
277{
Junxiao Shi7860d482014-02-21 23:57:20 -0700278 return m_counters;
279}
280
Junxiao Shicde37ad2015-12-24 01:02:05 -0700281std::ostream&
282operator<<(std::ostream& os, const FaceLogHelper<Face>& flh);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000283
Eric Newberrya98bf932015-09-21 00:58:47 -0700284template<typename T>
285typename std::enable_if<std::is_base_of<Face, T>::value, std::ostream&>::type
Junxiao Shicde37ad2015-12-24 01:02:05 -0700286operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
Eric Newberrya98bf932015-09-21 00:58:47 -0700287{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700288 return os << FaceLogHelper<Face>(flh.obj);
Eric Newberrya98bf932015-09-21 00:58:47 -0700289}
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100290
Junxiao Shicde37ad2015-12-24 01:02:05 -0700291} // namespace face
292
293using face::FaceId;
294using face::Face;
295
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800296} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700297
Junxiao Shicde37ad2015-12-24 01:02:05 -0700298#endif // NFD_DAEMON_FACE_HPP