Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 | /** | ||||
3 | * Copyright (C) 2014 Named Data Networking Project | ||||
4 | * See COPYING for copyright and distribution information. | ||||
5 | */ | ||||
6 | |||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 7 | #ifndef NFD_FACE_FACE_HPP |
8 | #define NFD_FACE_FACE_HPP | ||||
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 9 | |
10 | #include "common.hpp" | ||||
Junxiao Shi | 7f02c1e | 2014-01-29 22:57:01 -0700 | [diff] [blame] | 11 | #include "core/event-emitter.hpp" |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 12 | #include "core/face-uri.hpp" |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 13 | #include "face-counter.hpp" |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 14 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 15 | namespace nfd { |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 16 | |
17 | /** \class FaceId | ||||
18 | * \brief identifies a face | ||||
19 | */ | ||||
20 | typedef int FaceId; | ||||
21 | |||||
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 22 | const FaceId INVALID_FACEID = -1; |
23 | |||||
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 24 | const size_t MAX_NDN_PACKET_SIZE = 8800; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 25 | |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 26 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 27 | /** \brief represents a face |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 28 | */ |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 29 | class Face : noncopyable, public enable_shared_from_this<Face> |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 30 | { |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 31 | public: |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 32 | /** |
33 | * \brief Face-related error | ||||
34 | */ | ||||
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame^] | 35 | class Error : public std::runtime_error |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 36 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame^] | 37 | public: |
38 | explicit | ||||
39 | Error(const std::string& what) | ||||
40 | : std::runtime_error(what) | ||||
41 | { | ||||
42 | } | ||||
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 43 | }; |
44 | |||||
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame^] | 45 | Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal = false); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 46 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 47 | virtual |
48 | ~Face(); | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 49 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 50 | /// fires when an Interest is received |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 51 | EventEmitter<Interest> onReceiveInterest; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 53 | /// fires when a Data is received |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 54 | EventEmitter<Data> onReceiveData; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 56 | /// fires when an Interest is sent out |
57 | EventEmitter<Interest> onSendInterest; | ||||
58 | |||||
59 | /// fires when a Data is sent out | ||||
60 | EventEmitter<Data> onSendData; | ||||
61 | |||||
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 62 | /// fires when face disconnects or fails to perform properly |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 63 | EventEmitter<std::string/*reason*/> onFail; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 64 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 65 | /// send an Interest |
66 | virtual void | ||||
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 67 | sendInterest(const Interest& interest) = 0; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 68 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 69 | /// send a Data |
70 | virtual void | ||||
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 71 | sendData(const Data& data) = 0; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 73 | /** \brief Close the face |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 74 | * |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 75 | * This terminates all communication on the face and cause |
76 | * onFail() method event to be invoked | ||||
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 77 | */ |
78 | virtual void | ||||
79 | close() = 0; | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 80 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame^] | 81 | public: // attributes |
82 | FaceId | ||||
83 | getId() const; | ||||
84 | |||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 85 | /** \brief Set the description |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 86 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 87 | * This is typically invoked by mgmt on set description command |
88 | */ | ||||
89 | virtual void | ||||
90 | setDescription(const std::string& description); | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 91 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 92 | /// Get the description |
93 | virtual const std::string& | ||||
94 | getDescription() const; | ||||
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 95 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 96 | /** \brief Get whether face is connected to a local app |
97 | */ | ||||
98 | bool | ||||
99 | isLocal() const; | ||||
100 | |||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 101 | /** \brief Get whether packets sent this Face may reach multiple peers |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 102 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 103 | * In this base class this property is always false. |
104 | */ | ||||
105 | virtual bool | ||||
106 | isMultiAccess() const; | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 107 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 108 | /** \brief Get whether underlying communication is up |
109 | * | ||||
110 | * In this base class this property is always true. | ||||
111 | */ | ||||
112 | virtual bool | ||||
113 | isUp() const; | ||||
114 | |||||
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 115 | /** \brief Get whether face is created on demand or explicitly via FaceManagement protocol |
116 | */ | ||||
117 | bool | ||||
118 | isOnDemand() const; | ||||
119 | |||||
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 120 | const FaceCounters& |
121 | getCounters() const; | ||||
122 | |||||
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame^] | 123 | /** \deprecated use getRemoteUri instead |
124 | */ | ||||
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 125 | const FaceUri& |
126 | getUri() const; | ||||
127 | |||||
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame^] | 128 | /** \return a FaceUri that represents the remote endpoint |
129 | */ | ||||
130 | const FaceUri& | ||||
131 | getRemoteUri() const; | ||||
132 | |||||
133 | /** \return a FaceUri that represents the local endpoint (NFD side) | ||||
134 | */ | ||||
135 | const FaceUri& | ||||
136 | getLocalUri() const; | ||||
137 | |||||
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 138 | protected: |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 139 | // this is a non-virtual method |
140 | bool | ||||
141 | decodeAndDispatchInput(const Block& element); | ||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 142 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 143 | FaceCounters& |
144 | getMutableCounters(); | ||||
145 | |||||
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 146 | void |
147 | setOnDemand(bool isOnDemand); | ||||
148 | |||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 149 | private: |
Alexander Afanasyev | 46f6647 | 2014-01-31 16:50:58 -0800 | [diff] [blame] | 150 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 151 | setId(FaceId faceId); |
152 | |||||
153 | private: | ||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 154 | FaceId m_id; |
155 | std::string m_description; | ||||
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 156 | bool m_isLocal; // for scoping purposes |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 157 | FaceCounters m_counters; |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame^] | 158 | FaceUri m_remoteUri; |
159 | FaceUri m_localUri; | ||||
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 160 | bool m_isOnDemand; |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 161 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 162 | // allow setting FaceId |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 163 | friend class FaceTable; |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 164 | }; |
165 | |||||
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 166 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 167 | inline bool |
168 | Face::isLocal() const | ||||
169 | { | ||||
170 | return m_isLocal; | ||||
171 | } | ||||
172 | |||||
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 173 | inline const FaceCounters& |
174 | Face::getCounters() const | ||||
175 | { | ||||
176 | return m_counters; | ||||
177 | } | ||||
178 | |||||
179 | inline FaceCounters& | ||||
180 | Face::getMutableCounters() | ||||
181 | { | ||||
182 | return m_counters; | ||||
183 | } | ||||
184 | |||||
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 185 | inline const FaceUri& |
186 | Face::getUri() const | ||||
187 | { | ||||
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame^] | 188 | return this->getRemoteUri(); |
189 | } | ||||
190 | |||||
191 | inline const FaceUri& | ||||
192 | Face::getRemoteUri() const | ||||
193 | { | ||||
194 | return m_remoteUri; | ||||
195 | } | ||||
196 | |||||
197 | inline const FaceUri& | ||||
198 | Face::getLocalUri() const | ||||
199 | { | ||||
200 | return m_localUri; | ||||
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 201 | } |
202 | |||||
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 203 | inline void |
204 | Face::setOnDemand(bool isOnDemand) | ||||
205 | { | ||||
206 | m_isOnDemand = isOnDemand; | ||||
207 | } | ||||
208 | |||||
209 | inline bool | ||||
210 | Face::isOnDemand() const | ||||
211 | { | ||||
212 | return m_isOnDemand; | ||||
213 | } | ||||
214 | |||||
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 215 | } // namespace nfd |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 216 | |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 217 | #endif // NFD_FACE_FACE_HPP |