blob: cc07b8c53d30388ab018d09ae243461d187e722b [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
Alexander Afanasyeved6cfec2014-12-31 00:11:43 -0800126 void
127 setMetric(uint64_t metric);
128
129 uint64_t
130 getMetric() const;
131
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100132 /** \brief Get whether face is connected to a local app
133 */
134 bool
135 isLocal() const;
136
Yukai Tu731f0d72015-07-04 11:14:44 +0800137 /** \brief Get the 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
142 /** \brief Get whether packets sent by this face may reach multiple peers
143 */
144 bool
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700145 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700146
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100147 /** \brief Get whether underlying communication is up
148 *
149 * In this base class this property is always true.
150 */
151 virtual bool
152 isUp() const;
153
Junxiao Shi7860d482014-02-21 23:57:20 -0700154 const FaceCounters&
155 getCounters() const;
156
Junxiao Shi79494162014-04-02 18:25:11 -0700157 /** \return a FaceUri that represents the remote endpoint
158 */
159 const FaceUri&
160 getRemoteUri() const;
161
162 /** \return a FaceUri that represents the local endpoint (NFD side)
163 */
164 const FaceUri&
165 getLocalUri() const;
166
Chengyu Fanf9c2bb12014-10-06 11:52:44 -0600167 /** \return FaceTraits data structure filled with the current FaceTraits status
168 */
169 template<typename FaceTraits>
170 void
171 copyStatusTo(FaceTraits& traits) const;
172
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700173 /** \return FaceStatus data structure filled with the current Face status
174 */
175 virtual ndn::nfd::FaceStatus
176 getFaceStatus() const;
177
Davide Pesavento94279412015-02-27 01:29:32 +0100178PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700179 void
Yukai Tu731f0d72015-07-04 11:14:44 +0800180 setPersistency(ndn::nfd::FacePersistency persistency);
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700181
Davide Pesavento94279412015-02-27 01:29:32 +0100182protected:
183 bool
184 decodeAndDispatchInput(const Block& element);
185
Junxiao Shi08d07a72014-06-09 23:17:57 -0700186 /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing
187 */
188 void
189 fail(const std::string& reason);
190
Davide Pesavento94279412015-02-27 01:29:32 +0100191 FaceCounters&
192 getMutableCounters();
193
Junxiao Shic099ddb2014-12-25 20:53:20 -0700194 DECLARE_SIGNAL_EMIT(onReceiveInterest)
195 DECLARE_SIGNAL_EMIT(onReceiveData)
196 DECLARE_SIGNAL_EMIT(onSendInterest)
197 DECLARE_SIGNAL_EMIT(onSendData)
198
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700199private:
Davide Pesavento94279412015-02-27 01:29:32 +0100200 // this method should be used only by the FaceTable
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800201 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700202 setId(FaceId faceId);
203
204private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700205 FaceId m_id;
206 std::string m_description;
Junxiao Shi7860d482014-02-21 23:57:20 -0700207 FaceCounters m_counters;
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;
Alexander Afanasyeved6cfec2014-12-31 00:11:43 -0800214 uint64_t m_metric;
Junxiao Shi7860d482014-02-21 23:57:20 -0700215
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700216 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700217 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700218};
219
Davide Pesavento94279412015-02-27 01:29:32 +0100220inline FaceId
221Face::getId() const
222{
223 return m_id;
224}
225
226inline void
227Face::setId(FaceId faceId)
228{
229 m_id = faceId;
230}
231
232inline const std::string&
233Face::getDescription() const
234{
235 return m_description;
236}
237
238inline void
239Face::setDescription(const std::string& description)
240{
241 m_description = description;
242}
243
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100244inline bool
245Face::isLocal() const
246{
247 return m_isLocal;
248}
249
Yukai Tu731f0d72015-07-04 11:14:44 +0800250inline ndn::nfd::FacePersistency
251Face::getPersistency() const
Davide Pesavento94279412015-02-27 01:29:32 +0100252{
Yukai Tu731f0d72015-07-04 11:14:44 +0800253 return m_persistency;
Davide Pesavento94279412015-02-27 01:29:32 +0100254}
255
256inline void
Yukai Tu731f0d72015-07-04 11:14:44 +0800257Face::setPersistency(ndn::nfd::FacePersistency persistency)
Davide Pesavento94279412015-02-27 01:29:32 +0100258{
Yukai Tu731f0d72015-07-04 11:14:44 +0800259 m_persistency = persistency;
Davide Pesavento94279412015-02-27 01:29:32 +0100260}
261
262inline bool
263Face::isMultiAccess() const
264{
265 return m_isMultiAccess;
266}
267
Junxiao Shi7860d482014-02-21 23:57:20 -0700268inline const FaceCounters&
269Face::getCounters() const
270{
271 return m_counters;
272}
273
274inline FaceCounters&
275Face::getMutableCounters()
276{
277 return m_counters;
278}
279
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000280inline const FaceUri&
Junxiao Shi79494162014-04-02 18:25:11 -0700281Face::getRemoteUri() const
282{
283 return m_remoteUri;
284}
285
286inline const FaceUri&
287Face::getLocalUri() const
288{
289 return m_localUri;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000290}
291
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100292
293/** \defgroup FaceLogging Face logging macros
294 *
295 * These macros augment the log message with some face-specific information,
296 * such as the face ID, that are useful to distinguish which face produced the
297 * message. It is strongly recommended to use these macros instead of the
298 * generic ones for all logging inside Face subclasses.
299 * @{
300 */
301
Alexander Afanasyeved6cfec2014-12-31 00:11:43 -0800302inline void
303Face::setMetric(uint64_t metric)
304{
305 m_metric = metric;
306}
307
308inline uint64_t
309Face::getMetric() const
310{
311 return m_metric;
312}
313
Davide Pesaventobe40fb12015-02-23 21:09:34 +0100314#define NFD_LOG_FACE(level, msg) \
315 NFD_LOG_##level("[id=" << this->getId() << \
316 ",local=" << this->getLocalUri() << \
317 ",remote=" << this->getRemoteUri() << \
318 "] " << msg)
319
320/** \brief Log a message at TRACE level */
321#define NFD_LOG_FACE_TRACE(msg) NFD_LOG_FACE(TRACE, msg)
322
323/** \brief Log a message at DEBUG level */
324#define NFD_LOG_FACE_DEBUG(msg) NFD_LOG_FACE(DEBUG, msg)
325
326/** \brief Log a message at INFO level */
327#define NFD_LOG_FACE_INFO(msg) NFD_LOG_FACE(INFO, msg)
328
329/** \brief Log a message at WARN level */
330#define NFD_LOG_FACE_WARN(msg) NFD_LOG_FACE(WARN, msg)
331
332/** \brief Log a message at ERROR level */
333#define NFD_LOG_FACE_ERROR(msg) NFD_LOG_FACE(ERROR, msg)
334
335/** @} */
336
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800337} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700338
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700339#endif // NFD_DAEMON_FACE_FACE_HPP