blob: 9c2f2e658edb08cbfebc20ca435c0e409aa47c77 [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
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_FACE_HPP
27#define NFD_DAEMON_FACE_FACE_HPP
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070028
29#include "common.hpp"
Davide Pesaventobe40fb12015-02-23 21:09:34 +010030#include "core/logger.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 Afanasyeve515f0a2014-06-30 15:28:10 -070034#include <ndn-cxx/management/nfd-face-status.hpp>
35
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080036namespace nfd {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070037
38/** \class FaceId
39 * \brief identifies a face
40 */
41typedef int FaceId;
42
Junxiao Shi7b984c62014-07-17 22:18:34 -070043/// indicates an invalid FaceId
Junxiao Shi8c8d2182014-01-30 22:33:00 -070044const FaceId INVALID_FACEID = -1;
45
Junxiao Shi7b984c62014-07-17 22:18:34 -070046/// identifies the InternalFace used in management
47const FaceId FACEID_INTERNAL_FACE = 1;
48/// identifies a packet comes from the ContentStore, in LocalControlHeader incomingFaceId
49const FaceId FACEID_CONTENT_STORE = 254;
50/// identifies the NullFace that drops every packet
51const FaceId FACEID_NULL = 255;
52/// upper bound of reserved FaceIds
53const FaceId FACEID_RESERVED_MAX = 255;
54
55
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070056/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070057 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080058class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070059{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070060public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080061 /**
62 * \brief Face-related error
63 */
Junxiao Shi79494162014-04-02 18:25:11 -070064 class Error : public std::runtime_error
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080065 {
Junxiao Shi79494162014-04-02 18:25:11 -070066 public:
67 explicit
68 Error(const std::string& what)
69 : std::runtime_error(what)
70 {
71 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080072 };
73
Davide Pesavento94279412015-02-27 01:29:32 +010074 Face(const FaceUri& remoteUri, const FaceUri& localUri,
75 bool isLocal = false, bool isMultiAccess = false);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070076
Junxiao Shi32bfeb32014-01-25 18:22:02 -070077 virtual
78 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070079
Junxiao Shi32bfeb32014-01-25 18:22:02 -070080 /// fires when an Interest is received
Junxiao Shic099ddb2014-12-25 20:53:20 -070081 signal::Signal<Face, Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070082
Junxiao Shi32bfeb32014-01-25 18:22:02 -070083 /// fires when a Data is received
Junxiao Shic099ddb2014-12-25 20:53:20 -070084 signal::Signal<Face, Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080085
Eric Newberrya98bf932015-09-21 00:58:47 -070086 /// fires when a Nack is received
87 signal::Signal<Face, lp::Nack> onReceiveNack;
88
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000089 /// fires when an Interest is sent out
Junxiao Shic099ddb2014-12-25 20:53:20 -070090 signal::Signal<Face, Interest> onSendInterest;
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000091
92 /// fires when a Data is sent out
Junxiao Shic099ddb2014-12-25 20:53:20 -070093 signal::Signal<Face, Data> onSendData;
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000094
Eric Newberrya98bf932015-09-21 00:58:47 -070095 /// fires when a Nack is sent out
96 signal::Signal<Face, lp::Nack> onSendNack;
97
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080098 /// fires when face disconnects or fails to perform properly
Junxiao Shic099ddb2014-12-25 20:53:20 -070099 signal::Signal<Face, std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700100
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700101 /// send an Interest
102 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800103 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700104
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700105 /// send a Data
106 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800107 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800108
Eric Newberrya98bf932015-09-21 00:58:47 -0700109 /// send a Nack
110 virtual void
111 sendNack(const ndn::lp::Nack& nack)
112 {
113 }
114
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800115 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800116 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800117 * This terminates all communication on the face and cause
118 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800119 */
120 virtual void
121 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700122
Junxiao Shi79494162014-04-02 18:25:11 -0700123public: // attributes
124 FaceId
125 getId() const;
126
Davide Pesavento94279412015-02-27 01:29:32 +0100127 /** \brief Get the description
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700128 */
Davide Pesavento94279412015-02-27 01:29:32 +0100129 const std::string&
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700130 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -0700131
Davide Pesavento94279412015-02-27 01:29:32 +0100132 /** \brief Set the face description
133 *
134 * This is typically invoked by management on set description command
135 */
136 void
137 setDescription(const std::string& description);
138
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100139 /** \brief Get whether face is connected to a local app
140 */
141 bool
142 isLocal() const;
143
Yukai Tu731f0d72015-07-04 11:14:44 +0800144 /** \brief Get the persistency setting
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700145 */
Yukai Tu731f0d72015-07-04 11:14:44 +0800146 ndn::nfd::FacePersistency
147 getPersistency() const;
Davide Pesavento94279412015-02-27 01:29:32 +0100148
Eric Newberrya98bf932015-09-21 00:58:47 -0700149 // 'virtual' to allow override in LpFaceWrapper
150 virtual void
151 setPersistency(ndn::nfd::FacePersistency persistency);
152
Davide Pesavento94279412015-02-27 01:29:32 +0100153 /** \brief Get whether packets sent by this face may reach multiple peers
154 */
155 bool
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700156 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700157
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100158 /** \brief Get whether underlying communication is up
159 *
160 * In this base class this property is always true.
161 */
162 virtual bool
163 isUp() const;
164
Eric Newberrya98bf932015-09-21 00:58:47 -0700165 // 'virtual' to allow override in LpFaceWrapper
166 virtual const FaceCounters&
Junxiao Shi7860d482014-02-21 23:57:20 -0700167 getCounters() const;
168
Junxiao Shi79494162014-04-02 18:25:11 -0700169 /** \return a FaceUri that represents the remote endpoint
170 */
171 const FaceUri&
172 getRemoteUri() const;
173
174 /** \return a FaceUri that represents the local endpoint (NFD side)
175 */
176 const FaceUri&
177 getLocalUri() const;
178
Chengyu Fanf9c2bb12014-10-06 11:52:44 -0600179 /** \return FaceTraits data structure filled with the current FaceTraits status
180 */
181 template<typename FaceTraits>
182 void
183 copyStatusTo(FaceTraits& traits) const;
184
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700185 /** \return FaceStatus data structure filled with the current Face status
186 */
187 virtual ndn::nfd::FaceStatus
188 getFaceStatus() const;
189
Davide Pesavento94279412015-02-27 01:29:32 +0100190protected:
191 bool
192 decodeAndDispatchInput(const Block& element);
193
Junxiao Shi08d07a72014-06-09 23:17:57 -0700194 /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing
195 */
196 void
197 fail(const std::string& reason);
198
Davide Pesavento94279412015-02-27 01:29:32 +0100199 FaceCounters&
200 getMutableCounters();
201
Junxiao Shic099ddb2014-12-25 20:53:20 -0700202 DECLARE_SIGNAL_EMIT(onReceiveInterest)
203 DECLARE_SIGNAL_EMIT(onReceiveData)
Eric Newberrya98bf932015-09-21 00:58:47 -0700204 DECLARE_SIGNAL_EMIT(onReceiveNack)
Junxiao Shic099ddb2014-12-25 20:53:20 -0700205 DECLARE_SIGNAL_EMIT(onSendInterest)
206 DECLARE_SIGNAL_EMIT(onSendData)
Eric Newberrya98bf932015-09-21 00:58:47 -0700207 DECLARE_SIGNAL_EMIT(onSendNack)
Junxiao Shic099ddb2014-12-25 20:53:20 -0700208
Davide Pesavento94279412015-02-27 01:29:32 +0100209 // this method should be used only by the FaceTable
Eric Newberrya98bf932015-09-21 00:58:47 -0700210 // 'virtual' to allow override in LpFaceWrapper
211 virtual void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700212 setId(FaceId faceId);
213
214private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700215 FaceId m_id;
216 std::string m_description;
Junxiao Shi7860d482014-02-21 23:57:20 -0700217 FaceCounters m_counters;
Davide Pesavento94279412015-02-27 01:29:32 +0100218 const FaceUri m_remoteUri;
219 const FaceUri m_localUri;
220 const bool m_isLocal;
Yukai Tu731f0d72015-07-04 11:14:44 +0800221 ndn::nfd::FacePersistency m_persistency;
Davide Pesavento94279412015-02-27 01:29:32 +0100222 const bool m_isMultiAccess;
Junxiao Shi08d07a72014-06-09 23:17:57 -0700223 bool m_isFailed;
Junxiao Shi7860d482014-02-21 23:57:20 -0700224
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700225 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700226 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700227};
228
Davide Pesavento94279412015-02-27 01:29:32 +0100229inline FaceId
230Face::getId() const
231{
232 return m_id;
233}
234
235inline void
236Face::setId(FaceId faceId)
237{
238 m_id = faceId;
239}
240
241inline const std::string&
242Face::getDescription() const
243{
244 return m_description;
245}
246
247inline void
248Face::setDescription(const std::string& description)
249{
250 m_description = description;
251}
252
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100253inline bool
254Face::isLocal() const
255{
256 return m_isLocal;
257}
258
Yukai Tu731f0d72015-07-04 11:14:44 +0800259inline ndn::nfd::FacePersistency
260Face::getPersistency() const
Davide Pesavento94279412015-02-27 01:29:32 +0100261{
Yukai Tu731f0d72015-07-04 11:14:44 +0800262 return m_persistency;
Davide Pesavento94279412015-02-27 01:29:32 +0100263}
264
265inline void
Yukai Tu731f0d72015-07-04 11:14:44 +0800266Face::setPersistency(ndn::nfd::FacePersistency persistency)
Davide Pesavento94279412015-02-27 01:29:32 +0100267{
Yukai Tu731f0d72015-07-04 11:14:44 +0800268 m_persistency = persistency;
Davide Pesavento94279412015-02-27 01:29:32 +0100269}
270
271inline bool
272Face::isMultiAccess() const
273{
274 return m_isMultiAccess;
275}
276
Junxiao Shi7860d482014-02-21 23:57:20 -0700277inline const FaceCounters&
278Face::getCounters() const
279{
280 return m_counters;
281}
282
283inline FaceCounters&
284Face::getMutableCounters()
285{
286 return m_counters;
287}
288
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000289inline const FaceUri&
Junxiao Shi79494162014-04-02 18:25:11 -0700290Face::getRemoteUri() const
291{
292 return m_remoteUri;
293}
294
295inline const FaceUri&
296Face::getLocalUri() const
297{
298 return m_localUri;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000299}
300
Eric Newberrya98bf932015-09-21 00:58:47 -0700301template<typename T>
302typename std::enable_if<std::is_base_of<Face, T>::value, std::ostream&>::type
303operator<<(std::ostream& os, const face::FaceLogHelper<T>& flh)
304{
305 const Face& face = flh.obj;
306 os << "[id=" << face.getId() << ",local=" << face.getLocalUri() <<
307 ",remote=" << face.getRemoteUri() << "] ";
308 return os;
309}
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100310
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800311} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700312
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700313#endif // NFD_DAEMON_FACE_FACE_HPP