blob: 156a82dff2e9c60654e80980c7b8d33daa1f3513 [file] [log] [blame]
Junxiao Shi2c29f3a2014-01-24 19:59:00 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi08d07a72014-06-09 23:17:57 -07003 * Copyright (c) 2014, 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"
Junxiao Shi33152f12014-07-16 19:54:32 -070030#include "face-counters.hpp"
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070031
Junxiao Shia1937bf2014-11-06 11:43:40 -070032#include <ndn-cxx/util/face-uri.hpp>
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070033#include <ndn-cxx/management/nfd-face-status.hpp>
Chengyu Fanf9c2bb12014-10-06 11:52:44 -060034#include <ndn-cxx/management/nfd-face-event-notification.hpp>
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070035
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
Junxiao Shia1937bf2014-11-06 11:43:40 -070055using ndn::util::FaceUri;
Junxiao Shi7b984c62014-07-17 22:18:34 -070056
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
Junxiao Shi79494162014-04-02 18:25:11 -070075 Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal = 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
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000086 /// fires when an Interest is sent out
Junxiao Shic099ddb2014-12-25 20:53:20 -070087 signal::Signal<Face, Interest> onSendInterest;
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000088
89 /// fires when a Data is sent out
Junxiao Shic099ddb2014-12-25 20:53:20 -070090 signal::Signal<Face, Data> onSendData;
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000091
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080092 /// fires when face disconnects or fails to perform properly
Junxiao Shic099ddb2014-12-25 20:53:20 -070093 signal::Signal<Face, std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070094
Junxiao Shi32bfeb32014-01-25 18:22:02 -070095 /// send an Interest
96 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080097 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070098
Junxiao Shi32bfeb32014-01-25 18:22:02 -070099 /// send a Data
100 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800101 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800102
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800103 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800104 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800105 * This terminates all communication on the face and cause
106 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800107 */
108 virtual void
109 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700110
Junxiao Shi79494162014-04-02 18:25:11 -0700111public: // attributes
112 FaceId
113 getId() const;
114
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700115 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -0700116 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700117 * This is typically invoked by mgmt on set description command
118 */
119 virtual void
120 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700121
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700122 /// Get the description
123 virtual const std::string&
124 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -0700125
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
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700131 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -0700132 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700133 * In this base class this property is always false.
134 */
135 virtual bool
136 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700137
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100138 /** \brief Get whether underlying communication is up
139 *
140 * In this base class this property is always true.
141 */
142 virtual bool
143 isUp() const;
144
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700145 /** \brief Get whether face is created on demand or explicitly via FaceManagement protocol
146 */
147 bool
148 isOnDemand() const;
149
Junxiao Shi7860d482014-02-21 23:57:20 -0700150 const FaceCounters&
151 getCounters() const;
152
Junxiao Shi79494162014-04-02 18:25:11 -0700153 /** \return a FaceUri that represents the remote endpoint
154 */
155 const FaceUri&
156 getRemoteUri() const;
157
158 /** \return a FaceUri that represents the local endpoint (NFD side)
159 */
160 const FaceUri&
161 getLocalUri() const;
162
Chengyu Fanf9c2bb12014-10-06 11:52:44 -0600163 /** \return FaceTraits data structure filled with the current FaceTraits status
164 */
165 template<typename FaceTraits>
166 void
167 copyStatusTo(FaceTraits& traits) const;
168
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700169 /** \return FaceStatus data structure filled with the current Face status
170 */
171 virtual ndn::nfd::FaceStatus
172 getFaceStatus() const;
173
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800174protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800175 // this is a non-virtual method
176 bool
177 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700178
Junxiao Shi7860d482014-02-21 23:57:20 -0700179 FaceCounters&
180 getMutableCounters();
181
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700182 void
183 setOnDemand(bool isOnDemand);
184
Junxiao Shi08d07a72014-06-09 23:17:57 -0700185 /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing
186 */
187 void
188 fail(const std::string& reason);
189
Junxiao Shic099ddb2014-12-25 20:53:20 -0700190 DECLARE_SIGNAL_EMIT(onReceiveInterest)
191 DECLARE_SIGNAL_EMIT(onReceiveData)
192 DECLARE_SIGNAL_EMIT(onSendInterest)
193 DECLARE_SIGNAL_EMIT(onSendData)
194
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700195private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800196 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700197 setId(FaceId faceId);
198
199private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700200 FaceId m_id;
201 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800202 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700203 FaceCounters m_counters;
Junxiao Shi79494162014-04-02 18:25:11 -0700204 FaceUri m_remoteUri;
205 FaceUri m_localUri;
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700206 bool m_isOnDemand;
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 Pesavento0ff10db2014-02-28 03:12:27 +0100213inline bool
214Face::isLocal() const
215{
216 return m_isLocal;
217}
218
Junxiao Shi7860d482014-02-21 23:57:20 -0700219inline const FaceCounters&
220Face::getCounters() const
221{
222 return m_counters;
223}
224
225inline FaceCounters&
226Face::getMutableCounters()
227{
228 return m_counters;
229}
230
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000231inline const FaceUri&
Junxiao Shi79494162014-04-02 18:25:11 -0700232Face::getRemoteUri() const
233{
234 return m_remoteUri;
235}
236
237inline const FaceUri&
238Face::getLocalUri() const
239{
240 return m_localUri;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000241}
242
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700243inline void
244Face::setOnDemand(bool isOnDemand)
245{
246 m_isOnDemand = isOnDemand;
247}
248
249inline bool
250Face::isOnDemand() const
251{
252 return m_isOnDemand;
253}
254
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800255} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700256
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700257#endif // NFD_DAEMON_FACE_FACE_HPP