blob: 8234102d13d368182856b9bda8fd70702a00505f [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 Shi7f02c1e2014-01-29 22:57:01 -070030#include "core/event-emitter.hpp"
Junxiao Shi61e3cc52014-03-03 20:40:28 -070031#include "core/face-uri.hpp"
Junxiao Shi33152f12014-07-16 19:54:32 -070032#include "face-counters.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>
Chengyu Fanf9c2bb12014-10-06 11:52:44 -060035#include <ndn-cxx/management/nfd-face-event-notification.hpp>
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070036
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;
49/// identifies a packet comes from the ContentStore, in LocalControlHeader incomingFaceId
50const 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
57/// pratical limit of packet size in octets
Davide Pesavento0ff10db2014-02-28 03:12:27 +010058const size_t MAX_NDN_PACKET_SIZE = 8800;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080059
Junxiao Shi16b8bc92014-02-17 22:24:55 -070060
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070061/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070062 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080063class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070064{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070065public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080066 /**
67 * \brief Face-related error
68 */
Junxiao Shi79494162014-04-02 18:25:11 -070069 class Error : public std::runtime_error
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080070 {
Junxiao Shi79494162014-04-02 18:25:11 -070071 public:
72 explicit
73 Error(const std::string& what)
74 : std::runtime_error(what)
75 {
76 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080077 };
78
Junxiao Shi79494162014-04-02 18:25:11 -070079 Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal = false);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070080
Junxiao Shi32bfeb32014-01-25 18:22:02 -070081 virtual
82 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070083
Junxiao Shi32bfeb32014-01-25 18:22:02 -070084 /// fires when an Interest is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080085 EventEmitter<Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070086
Junxiao Shi32bfeb32014-01-25 18:22:02 -070087 /// fires when a Data is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080088 EventEmitter<Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080089
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000090 /// fires when an Interest is sent out
91 EventEmitter<Interest> onSendInterest;
92
93 /// fires when a Data is sent out
94 EventEmitter<Data> onSendData;
95
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080096 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080097 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070098
Junxiao Shi32bfeb32014-01-25 18:22:02 -070099 /// send an Interest
100 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800101 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700102
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700103 /// send a Data
104 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800105 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800106
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800107 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800108 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800109 * This terminates all communication on the face and cause
110 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800111 */
112 virtual void
113 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700114
Junxiao Shi79494162014-04-02 18:25:11 -0700115public: // attributes
116 FaceId
117 getId() const;
118
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700119 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -0700120 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700121 * This is typically invoked by mgmt on set description command
122 */
123 virtual void
124 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700125
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700126 /// Get the description
127 virtual const std::string&
128 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -0700129
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100130 /** \brief Get whether face is connected to a local app
131 */
132 bool
133 isLocal() const;
134
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700135 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -0700136 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700137 * In this base class this property is always false.
138 */
139 virtual bool
140 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700141
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100142 /** \brief Get whether underlying communication is up
143 *
144 * In this base class this property is always true.
145 */
146 virtual bool
147 isUp() const;
148
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700149 /** \brief Get whether face is created on demand or explicitly via FaceManagement protocol
150 */
151 bool
152 isOnDemand() 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
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800178protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800179 // this is a non-virtual method
180 bool
181 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700182
Junxiao Shi7860d482014-02-21 23:57:20 -0700183 FaceCounters&
184 getMutableCounters();
185
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700186 void
187 setOnDemand(bool isOnDemand);
188
Junxiao Shi08d07a72014-06-09 23:17:57 -0700189 /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing
190 */
191 void
192 fail(const std::string& reason);
193
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700194private:
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;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800201 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700202 FaceCounters m_counters;
Junxiao Shi79494162014-04-02 18:25:11 -0700203 FaceUri m_remoteUri;
204 FaceUri m_localUri;
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700205 bool m_isOnDemand;
Junxiao Shi08d07a72014-06-09 23:17:57 -0700206 bool m_isFailed;
Junxiao Shi7860d482014-02-21 23:57:20 -0700207
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700208 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700209 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700210};
211
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100212inline bool
213Face::isLocal() const
214{
215 return m_isLocal;
216}
217
Junxiao Shi7860d482014-02-21 23:57:20 -0700218inline const FaceCounters&
219Face::getCounters() const
220{
221 return m_counters;
222}
223
224inline FaceCounters&
225Face::getMutableCounters()
226{
227 return m_counters;
228}
229
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000230inline const FaceUri&
Junxiao Shi79494162014-04-02 18:25:11 -0700231Face::getRemoteUri() const
232{
233 return m_remoteUri;
234}
235
236inline const FaceUri&
237Face::getLocalUri() const
238{
239 return m_localUri;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000240}
241
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700242inline void
243Face::setOnDemand(bool isOnDemand)
244{
245 m_isOnDemand = isOnDemand;
246}
247
248inline bool
249Face::isOnDemand() const
250{
251 return m_isOnDemand;
252}
253
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800254} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700255
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700256#endif // NFD_DAEMON_FACE_FACE_HPP