blob: d964ef15c4c98e41fb2c450daf275773461f9dca [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"
Junxiao Shida93f1f2015-11-11 06:13:16 -070032#include "old-face-counters.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070033#include "face-log.hpp"
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070034
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070035#include <ndn-cxx/management/nfd-face-status.hpp>
36
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080037namespace nfd {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070038
39/** \class FaceId
40 * \brief identifies a face
41 */
42typedef int FaceId;
43
Junxiao Shi7b984c62014-07-17 22:18:34 -070044/// indicates an invalid FaceId
Junxiao Shi8c8d2182014-01-30 22:33:00 -070045const FaceId INVALID_FACEID = -1;
46
Junxiao Shi7b984c62014-07-17 22:18:34 -070047/// identifies the InternalFace used in management
48const FaceId FACEID_INTERNAL_FACE = 1;
Junxiao Shi0de23a22015-12-03 20:07:02 +000049/// identifies a packet comes from the ContentStore
Junxiao Shi7b984c62014-07-17 22:18:34 -070050const FaceId FACEID_CONTENT_STORE = 254;
51/// identifies the NullFace that drops every packet
52const FaceId FACEID_NULL = 255;
53/// upper bound of reserved FaceIds
54const FaceId FACEID_RESERVED_MAX = 255;
55
56
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070057/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070058 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080059class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070060{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070061public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080062 /**
63 * \brief Face-related error
64 */
Junxiao Shi79494162014-04-02 18:25:11 -070065 class Error : public std::runtime_error
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080066 {
Junxiao Shi79494162014-04-02 18:25:11 -070067 public:
68 explicit
69 Error(const std::string& what)
70 : std::runtime_error(what)
71 {
72 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080073 };
74
Davide Pesavento94279412015-02-27 01:29:32 +010075 Face(const FaceUri& remoteUri, const FaceUri& localUri,
76 bool isLocal = false, bool isMultiAccess = false);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070077
Junxiao Shi32bfeb32014-01-25 18:22:02 -070078 virtual
79 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070080
Junxiao Shi32bfeb32014-01-25 18:22:02 -070081 /// fires when an Interest is received
Junxiao Shic099ddb2014-12-25 20:53:20 -070082 signal::Signal<Face, Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070083
Junxiao Shi32bfeb32014-01-25 18:22:02 -070084 /// fires when a Data is received
Junxiao Shic099ddb2014-12-25 20:53:20 -070085 signal::Signal<Face, Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080086
Eric Newberrya98bf932015-09-21 00:58:47 -070087 /// fires when a Nack is received
88 signal::Signal<Face, lp::Nack> onReceiveNack;
89
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000090 /// fires when an Interest is sent out
Junxiao Shic099ddb2014-12-25 20:53:20 -070091 signal::Signal<Face, Interest> onSendInterest;
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000092
93 /// fires when a Data is sent out
Junxiao Shic099ddb2014-12-25 20:53:20 -070094 signal::Signal<Face, Data> onSendData;
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000095
Eric Newberrya98bf932015-09-21 00:58:47 -070096 /// fires when a Nack is sent out
97 signal::Signal<Face, lp::Nack> onSendNack;
98
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080099 /// fires when face disconnects or fails to perform properly
Junxiao Shic099ddb2014-12-25 20:53:20 -0700100 signal::Signal<Face, std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700101
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700102 /// send an Interest
103 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800104 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700105
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700106 /// send a Data
107 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800108 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800109
Eric Newberrya98bf932015-09-21 00:58:47 -0700110 /// send a Nack
111 virtual void
112 sendNack(const ndn::lp::Nack& nack)
113 {
114 }
115
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800116 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800117 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800118 * This terminates all communication on the face and cause
119 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800120 */
121 virtual void
122 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700123
Junxiao Shi79494162014-04-02 18:25:11 -0700124public: // attributes
125 FaceId
126 getId() const;
127
Davide Pesavento94279412015-02-27 01:29:32 +0100128 /** \brief Get the description
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700129 */
Davide Pesavento94279412015-02-27 01:29:32 +0100130 const std::string&
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700131 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -0700132
Davide Pesavento94279412015-02-27 01:29:32 +0100133 /** \brief Set the face description
134 *
135 * This is typically invoked by management on set description command
136 */
137 void
138 setDescription(const std::string& description);
139
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100140 /** \brief Get whether face is connected to a local app
141 */
142 bool
143 isLocal() const;
144
Yukai Tu731f0d72015-07-04 11:14:44 +0800145 /** \brief Get the persistency setting
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700146 */
Yukai Tu731f0d72015-07-04 11:14:44 +0800147 ndn::nfd::FacePersistency
148 getPersistency() const;
Davide Pesavento94279412015-02-27 01:29:32 +0100149
Eric Newberrya98bf932015-09-21 00:58:47 -0700150 // 'virtual' to allow override in LpFaceWrapper
151 virtual void
152 setPersistency(ndn::nfd::FacePersistency persistency);
153
Davide Pesavento94279412015-02-27 01:29:32 +0100154 /** \brief Get whether packets sent by this face may reach multiple peers
155 */
156 bool
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700157 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700158
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100159 /** \brief Get whether underlying communication is up
160 *
161 * In this base class this property is always true.
162 */
163 virtual bool
164 isUp() const;
165
Junxiao Shida93f1f2015-11-11 06:13:16 -0700166 virtual const face::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
Davide Pesavento94279412015-02-27 01:29:32 +0100179protected:
180 bool
181 decodeAndDispatchInput(const Block& element);
182
Junxiao Shi08d07a72014-06-09 23:17:57 -0700183 /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing
184 */
185 void
186 fail(const std::string& reason);
187
Junxiao Shida93f1f2015-11-11 06:13:16 -0700188 OldFaceCounters&
Davide Pesavento94279412015-02-27 01:29:32 +0100189 getMutableCounters();
190
Junxiao Shic099ddb2014-12-25 20:53:20 -0700191 DECLARE_SIGNAL_EMIT(onReceiveInterest)
192 DECLARE_SIGNAL_EMIT(onReceiveData)
Eric Newberrya98bf932015-09-21 00:58:47 -0700193 DECLARE_SIGNAL_EMIT(onReceiveNack)
Junxiao Shic099ddb2014-12-25 20:53:20 -0700194 DECLARE_SIGNAL_EMIT(onSendInterest)
195 DECLARE_SIGNAL_EMIT(onSendData)
Eric Newberrya98bf932015-09-21 00:58:47 -0700196 DECLARE_SIGNAL_EMIT(onSendNack)
Junxiao Shic099ddb2014-12-25 20:53:20 -0700197
Davide Pesavento94279412015-02-27 01:29:32 +0100198 // this method should be used only by the FaceTable
Eric Newberrya98bf932015-09-21 00:58:47 -0700199 // 'virtual' to allow override in LpFaceWrapper
200 virtual void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700201 setId(FaceId faceId);
202
203private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700204 FaceId m_id;
205 std::string m_description;
Junxiao Shida93f1f2015-11-11 06:13:16 -0700206 OldFaceCounters m_counters;
207 face::FaceCounters m_countersWrapper;
Davide Pesavento94279412015-02-27 01:29:32 +0100208 const FaceUri m_remoteUri;
209 const FaceUri m_localUri;
210 const bool m_isLocal;
Yukai Tu731f0d72015-07-04 11:14:44 +0800211 ndn::nfd::FacePersistency m_persistency;
Davide Pesavento94279412015-02-27 01:29:32 +0100212 const bool m_isMultiAccess;
Junxiao Shi08d07a72014-06-09 23:17:57 -0700213 bool m_isFailed;
Junxiao Shi7860d482014-02-21 23:57:20 -0700214
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700215 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700216 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700217};
218
Davide Pesavento94279412015-02-27 01:29:32 +0100219inline FaceId
220Face::getId() const
221{
222 return m_id;
223}
224
225inline void
226Face::setId(FaceId faceId)
227{
228 m_id = faceId;
229}
230
231inline const std::string&
232Face::getDescription() const
233{
234 return m_description;
235}
236
237inline void
238Face::setDescription(const std::string& description)
239{
240 m_description = description;
241}
242
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100243inline bool
244Face::isLocal() const
245{
246 return m_isLocal;
247}
248
Yukai Tu731f0d72015-07-04 11:14:44 +0800249inline ndn::nfd::FacePersistency
250Face::getPersistency() const
Davide Pesavento94279412015-02-27 01:29:32 +0100251{
Yukai Tu731f0d72015-07-04 11:14:44 +0800252 return m_persistency;
Davide Pesavento94279412015-02-27 01:29:32 +0100253}
254
255inline void
Yukai Tu731f0d72015-07-04 11:14:44 +0800256Face::setPersistency(ndn::nfd::FacePersistency persistency)
Davide Pesavento94279412015-02-27 01:29:32 +0100257{
Yukai Tu731f0d72015-07-04 11:14:44 +0800258 m_persistency = persistency;
Davide Pesavento94279412015-02-27 01:29:32 +0100259}
260
261inline bool
262Face::isMultiAccess() const
263{
264 return m_isMultiAccess;
265}
266
Junxiao Shida93f1f2015-11-11 06:13:16 -0700267inline const face::FaceCounters&
Junxiao Shi7860d482014-02-21 23:57:20 -0700268Face::getCounters() const
269{
Junxiao Shida93f1f2015-11-11 06:13:16 -0700270 return m_countersWrapper;
Junxiao Shi7860d482014-02-21 23:57:20 -0700271}
272
Junxiao Shida93f1f2015-11-11 06:13:16 -0700273inline OldFaceCounters&
Junxiao Shi7860d482014-02-21 23:57:20 -0700274Face::getMutableCounters()
275{
276 return m_counters;
277}
278
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000279inline const FaceUri&
Junxiao Shi79494162014-04-02 18:25:11 -0700280Face::getRemoteUri() const
281{
282 return m_remoteUri;
283}
284
285inline const FaceUri&
286Face::getLocalUri() const
287{
288 return m_localUri;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000289}
290
Eric Newberrya98bf932015-09-21 00:58:47 -0700291template<typename T>
292typename std::enable_if<std::is_base_of<Face, T>::value, std::ostream&>::type
293operator<<(std::ostream& os, const face::FaceLogHelper<T>& flh)
294{
295 const Face& face = flh.obj;
296 os << "[id=" << face.getId() << ",local=" << face.getLocalUri() <<
297 ",remote=" << face.getRemoteUri() << "] ";
298 return os;
299}
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100300
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800301} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700302
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700303#endif // NFD_DAEMON_FACE_FACE_HPP