blob: 2660045cf4691f1e051275f9ebd9732fcc2a91dd [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/>.
24 **/
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 Shi7860d482014-02-21 23:57:20 -070032#include "face-counter.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>
35
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 Shi8c8d2182014-01-30 22:33:00 -070043const FaceId INVALID_FACEID = -1;
44
Davide Pesavento0ff10db2014-02-28 03:12:27 +010045const size_t MAX_NDN_PACKET_SIZE = 8800;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080046
Junxiao Shi16b8bc92014-02-17 22:24:55 -070047
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070048/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070049 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080050class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070051{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070052public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080053 /**
54 * \brief Face-related error
55 */
Junxiao Shi79494162014-04-02 18:25:11 -070056 class Error : public std::runtime_error
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080057 {
Junxiao Shi79494162014-04-02 18:25:11 -070058 public:
59 explicit
60 Error(const std::string& what)
61 : std::runtime_error(what)
62 {
63 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080064 };
65
Junxiao Shi79494162014-04-02 18:25:11 -070066 Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal = false);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070067
Junxiao Shi32bfeb32014-01-25 18:22:02 -070068 virtual
69 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070070
Junxiao Shi32bfeb32014-01-25 18:22:02 -070071 /// fires when an Interest is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080072 EventEmitter<Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070073
Junxiao Shi32bfeb32014-01-25 18:22:02 -070074 /// fires when a Data is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080075 EventEmitter<Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080076
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000077 /// fires when an Interest is sent out
78 EventEmitter<Interest> onSendInterest;
79
80 /// fires when a Data is sent out
81 EventEmitter<Data> onSendData;
82
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080083 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080084 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070085
Junxiao Shi32bfeb32014-01-25 18:22:02 -070086 /// send an Interest
87 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080088 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070089
Junxiao Shi32bfeb32014-01-25 18:22:02 -070090 /// send a Data
91 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080092 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080093
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080094 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080095 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080096 * This terminates all communication on the face and cause
97 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080098 */
99 virtual void
100 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700101
Junxiao Shi79494162014-04-02 18:25:11 -0700102public: // attributes
103 FaceId
104 getId() const;
105
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700106 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -0700107 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700108 * This is typically invoked by mgmt on set description command
109 */
110 virtual void
111 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700112
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700113 /// Get the description
114 virtual const std::string&
115 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -0700116
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100117 /** \brief Get whether face is connected to a local app
118 */
119 bool
120 isLocal() const;
121
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700122 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -0700123 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700124 * In this base class this property is always false.
125 */
126 virtual bool
127 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700128
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100129 /** \brief Get whether underlying communication is up
130 *
131 * In this base class this property is always true.
132 */
133 virtual bool
134 isUp() const;
135
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700136 /** \brief Get whether face is created on demand or explicitly via FaceManagement protocol
137 */
138 bool
139 isOnDemand() const;
140
Junxiao Shi7860d482014-02-21 23:57:20 -0700141 const FaceCounters&
142 getCounters() const;
143
Junxiao Shi79494162014-04-02 18:25:11 -0700144 /** \return a FaceUri that represents the remote endpoint
145 */
146 const FaceUri&
147 getRemoteUri() const;
148
149 /** \return a FaceUri that represents the local endpoint (NFD side)
150 */
151 const FaceUri&
152 getLocalUri() const;
153
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700154 /** \return FaceStatus data structure filled with the current Face status
155 */
156 virtual ndn::nfd::FaceStatus
157 getFaceStatus() const;
158
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800159protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800160 // this is a non-virtual method
161 bool
162 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700163
Junxiao Shi7860d482014-02-21 23:57:20 -0700164 FaceCounters&
165 getMutableCounters();
166
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700167 void
168 setOnDemand(bool isOnDemand);
169
Junxiao Shi08d07a72014-06-09 23:17:57 -0700170 /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing
171 */
172 void
173 fail(const std::string& reason);
174
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700175private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800176 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700177 setId(FaceId faceId);
178
179private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700180 FaceId m_id;
181 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800182 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700183 FaceCounters m_counters;
Junxiao Shi79494162014-04-02 18:25:11 -0700184 FaceUri m_remoteUri;
185 FaceUri m_localUri;
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700186 bool m_isOnDemand;
Junxiao Shi08d07a72014-06-09 23:17:57 -0700187 bool m_isFailed;
Junxiao Shi7860d482014-02-21 23:57:20 -0700188
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700189 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700190 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700191};
192
Junxiao Shi7860d482014-02-21 23:57:20 -0700193
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100194inline bool
195Face::isLocal() const
196{
197 return m_isLocal;
198}
199
Junxiao Shi7860d482014-02-21 23:57:20 -0700200inline const FaceCounters&
201Face::getCounters() const
202{
203 return m_counters;
204}
205
206inline FaceCounters&
207Face::getMutableCounters()
208{
209 return m_counters;
210}
211
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000212inline const FaceUri&
Junxiao Shi79494162014-04-02 18:25:11 -0700213Face::getRemoteUri() const
214{
215 return m_remoteUri;
216}
217
218inline const FaceUri&
219Face::getLocalUri() const
220{
221 return m_localUri;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000222}
223
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700224inline void
225Face::setOnDemand(bool isOnDemand)
226{
227 m_isOnDemand = isOnDemand;
228}
229
230inline bool
231Face::isOnDemand() const
232{
233 return m_isOnDemand;
234}
235
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800236} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700237
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700238#endif // NFD_DAEMON_FACE_FACE_HPP