blob: 987853ee3612a82676d1b3ccf39b995eb7206ea9 [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 Afanasyev18bbf812014-01-29 01:40:23 -080034namespace nfd {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070035
36/** \class FaceId
37 * \brief identifies a face
38 */
39typedef int FaceId;
40
Junxiao Shi8c8d2182014-01-30 22:33:00 -070041const FaceId INVALID_FACEID = -1;
42
Davide Pesavento0ff10db2014-02-28 03:12:27 +010043const size_t MAX_NDN_PACKET_SIZE = 8800;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080044
Junxiao Shi16b8bc92014-02-17 22:24:55 -070045
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070046/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070047 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080048class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070049{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070050public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080051 /**
52 * \brief Face-related error
53 */
Junxiao Shi79494162014-04-02 18:25:11 -070054 class Error : public std::runtime_error
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080055 {
Junxiao Shi79494162014-04-02 18:25:11 -070056 public:
57 explicit
58 Error(const std::string& what)
59 : std::runtime_error(what)
60 {
61 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080062 };
63
Junxiao Shi79494162014-04-02 18:25:11 -070064 Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal = false);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070065
Junxiao Shi32bfeb32014-01-25 18:22:02 -070066 virtual
67 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070068
Junxiao Shi32bfeb32014-01-25 18:22:02 -070069 /// fires when an Interest is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080070 EventEmitter<Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070071
Junxiao Shi32bfeb32014-01-25 18:22:02 -070072 /// fires when a Data is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080073 EventEmitter<Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080074
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000075 /// fires when an Interest is sent out
76 EventEmitter<Interest> onSendInterest;
77
78 /// fires when a Data is sent out
79 EventEmitter<Data> onSendData;
80
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080081 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080082 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070083
Junxiao Shi32bfeb32014-01-25 18:22:02 -070084 /// send an Interest
85 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080086 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070087
Junxiao Shi32bfeb32014-01-25 18:22:02 -070088 /// send a Data
89 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080090 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080091
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080092 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080093 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080094 * This terminates all communication on the face and cause
95 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080096 */
97 virtual void
98 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070099
Junxiao Shi79494162014-04-02 18:25:11 -0700100public: // attributes
101 FaceId
102 getId() const;
103
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700104 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -0700105 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700106 * This is typically invoked by mgmt on set description command
107 */
108 virtual void
109 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700110
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700111 /// Get the description
112 virtual const std::string&
113 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -0700114
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100115 /** \brief Get whether face is connected to a local app
116 */
117 bool
118 isLocal() const;
119
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700120 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -0700121 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700122 * In this base class this property is always false.
123 */
124 virtual bool
125 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700126
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100127 /** \brief Get whether underlying communication is up
128 *
129 * In this base class this property is always true.
130 */
131 virtual bool
132 isUp() const;
133
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700134 /** \brief Get whether face is created on demand or explicitly via FaceManagement protocol
135 */
136 bool
137 isOnDemand() const;
138
Junxiao Shi7860d482014-02-21 23:57:20 -0700139 const FaceCounters&
140 getCounters() const;
141
Junxiao Shi79494162014-04-02 18:25:11 -0700142 /** \return a FaceUri that represents the remote endpoint
143 */
144 const FaceUri&
145 getRemoteUri() const;
146
147 /** \return a FaceUri that represents the local endpoint (NFD side)
148 */
149 const FaceUri&
150 getLocalUri() const;
151
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800152protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800153 // this is a non-virtual method
154 bool
155 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700156
Junxiao Shi7860d482014-02-21 23:57:20 -0700157 FaceCounters&
158 getMutableCounters();
159
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700160 void
161 setOnDemand(bool isOnDemand);
162
Junxiao Shi08d07a72014-06-09 23:17:57 -0700163 /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing
164 */
165 void
166 fail(const std::string& reason);
167
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700168private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800169 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700170 setId(FaceId faceId);
171
172private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700173 FaceId m_id;
174 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800175 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700176 FaceCounters m_counters;
Junxiao Shi79494162014-04-02 18:25:11 -0700177 FaceUri m_remoteUri;
178 FaceUri m_localUri;
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700179 bool m_isOnDemand;
Junxiao Shi08d07a72014-06-09 23:17:57 -0700180 bool m_isFailed;
Junxiao Shi7860d482014-02-21 23:57:20 -0700181
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700182 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700183 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700184};
185
Junxiao Shi7860d482014-02-21 23:57:20 -0700186
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100187inline bool
188Face::isLocal() const
189{
190 return m_isLocal;
191}
192
Junxiao Shi7860d482014-02-21 23:57:20 -0700193inline const FaceCounters&
194Face::getCounters() const
195{
196 return m_counters;
197}
198
199inline FaceCounters&
200Face::getMutableCounters()
201{
202 return m_counters;
203}
204
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000205inline const FaceUri&
Junxiao Shi79494162014-04-02 18:25:11 -0700206Face::getRemoteUri() const
207{
208 return m_remoteUri;
209}
210
211inline const FaceUri&
212Face::getLocalUri() const
213{
214 return m_localUri;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000215}
216
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700217inline void
218Face::setOnDemand(bool isOnDemand)
219{
220 m_isOnDemand = isOnDemand;
221}
222
223inline bool
224Face::isOnDemand() const
225{
226 return m_isOnDemand;
227}
228
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800229} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700230
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700231#endif // NFD_DAEMON_FACE_FACE_HPP