blob: a5923d2e4d0353b37549eab518ef42902acd9286 [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 Shi2c29f3a2014-01-24 19:59:00 -070032
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070033#include <ndn-cxx/management/nfd-face-status.hpp>
34
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080035namespace nfd {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070036
37/** \class FaceId
38 * \brief identifies a face
39 */
40typedef int FaceId;
41
Junxiao Shi7b984c62014-07-17 22:18:34 -070042/// indicates an invalid FaceId
Junxiao Shi8c8d2182014-01-30 22:33:00 -070043const FaceId INVALID_FACEID = -1;
44
Junxiao Shi7b984c62014-07-17 22:18:34 -070045/// identifies the InternalFace used in management
46const FaceId FACEID_INTERNAL_FACE = 1;
47/// identifies a packet comes from the ContentStore, in LocalControlHeader incomingFaceId
48const FaceId FACEID_CONTENT_STORE = 254;
49/// identifies the NullFace that drops every packet
50const FaceId FACEID_NULL = 255;
51/// upper bound of reserved FaceIds
52const FaceId FACEID_RESERVED_MAX = 255;
53
54
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070055/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070056 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080057class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070058{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070059public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080060 /**
61 * \brief Face-related error
62 */
Junxiao Shi79494162014-04-02 18:25:11 -070063 class Error : public std::runtime_error
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080064 {
Junxiao Shi79494162014-04-02 18:25:11 -070065 public:
66 explicit
67 Error(const std::string& what)
68 : std::runtime_error(what)
69 {
70 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080071 };
72
Davide Pesavento94279412015-02-27 01:29:32 +010073 Face(const FaceUri& remoteUri, const FaceUri& localUri,
74 bool isLocal = false, bool isMultiAccess = false);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070075
Junxiao Shi32bfeb32014-01-25 18:22:02 -070076 virtual
77 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070078
Junxiao Shi32bfeb32014-01-25 18:22:02 -070079 /// fires when an Interest is received
Junxiao Shic099ddb2014-12-25 20:53:20 -070080 signal::Signal<Face, Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070081
Junxiao Shi32bfeb32014-01-25 18:22:02 -070082 /// fires when a Data is received
Junxiao Shic099ddb2014-12-25 20:53:20 -070083 signal::Signal<Face, Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080084
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000085 /// fires when an Interest is sent out
Junxiao Shic099ddb2014-12-25 20:53:20 -070086 signal::Signal<Face, Interest> onSendInterest;
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000087
88 /// fires when a Data is sent out
Junxiao Shic099ddb2014-12-25 20:53:20 -070089 signal::Signal<Face, Data> onSendData;
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000090
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080091 /// fires when face disconnects or fails to perform properly
Junxiao Shic099ddb2014-12-25 20:53:20 -070092 signal::Signal<Face, std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070093
Junxiao Shi32bfeb32014-01-25 18:22:02 -070094 /// send an Interest
95 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080096 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070097
Junxiao Shi32bfeb32014-01-25 18:22:02 -070098 /// send a Data
99 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800100 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800101
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800102 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800103 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800104 * This terminates all communication on the face and cause
105 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800106 */
107 virtual void
108 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700109
Junxiao Shi79494162014-04-02 18:25:11 -0700110public: // attributes
111 FaceId
112 getId() const;
113
Davide Pesavento94279412015-02-27 01:29:32 +0100114 /** \brief Get the description
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700115 */
Davide Pesavento94279412015-02-27 01:29:32 +0100116 const std::string&
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700117 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -0700118
Davide Pesavento94279412015-02-27 01:29:32 +0100119 /** \brief Set the face description
120 *
121 * This is typically invoked by management on set description command
122 */
123 void
124 setDescription(const std::string& description);
125
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100126 /** \brief Get whether face is connected to a local app
127 */
128 bool
129 isLocal() const;
130
Yukai Tu731f0d72015-07-04 11:14:44 +0800131 /** \brief Get the persistency setting
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700132 */
Yukai Tu731f0d72015-07-04 11:14:44 +0800133 ndn::nfd::FacePersistency
134 getPersistency() const;
Davide Pesavento94279412015-02-27 01:29:32 +0100135
136 /** \brief Get whether packets sent by this face may reach multiple peers
137 */
138 bool
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700139 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700140
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100141 /** \brief Get whether underlying communication is up
142 *
143 * In this base class this property is always true.
144 */
145 virtual bool
146 isUp() const;
147
Junxiao Shi7860d482014-02-21 23:57:20 -0700148 const FaceCounters&
149 getCounters() const;
150
Junxiao Shi79494162014-04-02 18:25:11 -0700151 /** \return a FaceUri that represents the remote endpoint
152 */
153 const FaceUri&
154 getRemoteUri() const;
155
156 /** \return a FaceUri that represents the local endpoint (NFD side)
157 */
158 const FaceUri&
159 getLocalUri() const;
160
Chengyu Fanf9c2bb12014-10-06 11:52:44 -0600161 /** \return FaceTraits data structure filled with the current FaceTraits status
162 */
163 template<typename FaceTraits>
164 void
165 copyStatusTo(FaceTraits& traits) const;
166
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700167 /** \return FaceStatus data structure filled with the current Face status
168 */
169 virtual ndn::nfd::FaceStatus
170 getFaceStatus() const;
171
Davide Pesavento94279412015-02-27 01:29:32 +0100172PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700173 void
Yukai Tu731f0d72015-07-04 11:14:44 +0800174 setPersistency(ndn::nfd::FacePersistency persistency);
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700175
Davide Pesavento94279412015-02-27 01:29:32 +0100176protected:
177 bool
178 decodeAndDispatchInput(const Block& element);
179
Junxiao Shi08d07a72014-06-09 23:17:57 -0700180 /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing
181 */
182 void
183 fail(const std::string& reason);
184
Davide Pesavento94279412015-02-27 01:29:32 +0100185 FaceCounters&
186 getMutableCounters();
187
Junxiao Shic099ddb2014-12-25 20:53:20 -0700188 DECLARE_SIGNAL_EMIT(onReceiveInterest)
189 DECLARE_SIGNAL_EMIT(onReceiveData)
190 DECLARE_SIGNAL_EMIT(onSendInterest)
191 DECLARE_SIGNAL_EMIT(onSendData)
192
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700193private:
Davide Pesavento94279412015-02-27 01:29:32 +0100194 // this method should be used only by the FaceTable
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800195 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700196 setId(FaceId faceId);
197
198private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700199 FaceId m_id;
200 std::string m_description;
Junxiao Shi7860d482014-02-21 23:57:20 -0700201 FaceCounters m_counters;
Davide Pesavento94279412015-02-27 01:29:32 +0100202 const FaceUri m_remoteUri;
203 const FaceUri m_localUri;
204 const bool m_isLocal;
Yukai Tu731f0d72015-07-04 11:14:44 +0800205 ndn::nfd::FacePersistency m_persistency;
Davide Pesavento94279412015-02-27 01:29:32 +0100206 const bool m_isMultiAccess;
Junxiao Shi08d07a72014-06-09 23:17:57 -0700207 bool m_isFailed;
Junxiao Shi7860d482014-02-21 23:57:20 -0700208
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700209 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700210 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700211};
212
Davide Pesavento94279412015-02-27 01:29:32 +0100213inline FaceId
214Face::getId() const
215{
216 return m_id;
217}
218
219inline void
220Face::setId(FaceId faceId)
221{
222 m_id = faceId;
223}
224
225inline const std::string&
226Face::getDescription() const
227{
228 return m_description;
229}
230
231inline void
232Face::setDescription(const std::string& description)
233{
234 m_description = description;
235}
236
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100237inline bool
238Face::isLocal() const
239{
240 return m_isLocal;
241}
242
Yukai Tu731f0d72015-07-04 11:14:44 +0800243inline ndn::nfd::FacePersistency
244Face::getPersistency() const
Davide Pesavento94279412015-02-27 01:29:32 +0100245{
Yukai Tu731f0d72015-07-04 11:14:44 +0800246 return m_persistency;
Davide Pesavento94279412015-02-27 01:29:32 +0100247}
248
249inline void
Yukai Tu731f0d72015-07-04 11:14:44 +0800250Face::setPersistency(ndn::nfd::FacePersistency persistency)
Davide Pesavento94279412015-02-27 01:29:32 +0100251{
Yukai Tu731f0d72015-07-04 11:14:44 +0800252 m_persistency = persistency;
Davide Pesavento94279412015-02-27 01:29:32 +0100253}
254
255inline bool
256Face::isMultiAccess() const
257{
258 return m_isMultiAccess;
259}
260
Junxiao Shi7860d482014-02-21 23:57:20 -0700261inline const FaceCounters&
262Face::getCounters() const
263{
264 return m_counters;
265}
266
267inline FaceCounters&
268Face::getMutableCounters()
269{
270 return m_counters;
271}
272
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000273inline const FaceUri&
Junxiao Shi79494162014-04-02 18:25:11 -0700274Face::getRemoteUri() const
275{
276 return m_remoteUri;
277}
278
279inline const FaceUri&
280Face::getLocalUri() const
281{
282 return m_localUri;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000283}
284
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100285
286/** \defgroup FaceLogging Face logging macros
287 *
288 * These macros augment the log message with some face-specific information,
289 * such as the face ID, that are useful to distinguish which face produced the
290 * message. It is strongly recommended to use these macros instead of the
291 * generic ones for all logging inside Face subclasses.
292 * @{
293 */
294
295#define NFD_LOG_FACE(level, msg) \
296 NFD_LOG_##level("[id=" << this->getId() << \
297 ",local=" << this->getLocalUri() << \
298 ",remote=" << this->getRemoteUri() << \
299 "] " << msg)
300
301/** \brief Log a message at TRACE level */
302#define NFD_LOG_FACE_TRACE(msg) NFD_LOG_FACE(TRACE, msg)
303
304/** \brief Log a message at DEBUG level */
305#define NFD_LOG_FACE_DEBUG(msg) NFD_LOG_FACE(DEBUG, msg)
306
307/** \brief Log a message at INFO level */
308#define NFD_LOG_FACE_INFO(msg) NFD_LOG_FACE(INFO, msg)
309
310/** \brief Log a message at WARN level */
311#define NFD_LOG_FACE_WARN(msg) NFD_LOG_FACE(WARN, msg)
312
313/** \brief Log a message at ERROR level */
314#define NFD_LOG_FACE_ERROR(msg) NFD_LOG_FACE(ERROR, msg)
315
316/** @} */
317
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800318} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700319
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700320#endif // NFD_DAEMON_FACE_FACE_HPP