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 | */ | ||||
35 | struct Error : public std::runtime_error | ||||
36 | { | ||||
37 | Error(const std::string& what) : std::runtime_error(what) {} | ||||
38 | }; | ||||
39 | |||||
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 40 | Face(const FaceUri& uri, bool isLocal = false); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 41 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 42 | virtual |
43 | ~Face(); | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 44 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 45 | FaceId |
46 | getId() const; | ||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 47 | |
48 | /// fires when an Interest is received | ||||
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 49 | EventEmitter<Interest> onReceiveInterest; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 50 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 51 | /// fires when a Data is received |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 52 | EventEmitter<Data> onReceiveData; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 54 | /// fires when an Interest is sent out |
55 | EventEmitter<Interest> onSendInterest; | ||||
56 | |||||
57 | /// fires when a Data is sent out | ||||
58 | EventEmitter<Data> onSendData; | ||||
59 | |||||
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 60 | /// fires when face disconnects or fails to perform properly |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 61 | EventEmitter<std::string/*reason*/> onFail; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 62 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 63 | /// send an Interest |
64 | virtual void | ||||
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 65 | sendInterest(const Interest& interest) = 0; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 66 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 67 | /// send a Data |
68 | virtual void | ||||
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 69 | sendData(const Data& data) = 0; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 71 | /** \brief Close the face |
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 | * This terminates all communication on the face and cause |
74 | * onFail() method event to be invoked | ||||
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 75 | */ |
76 | virtual void | ||||
77 | close() = 0; | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 79 | /** \brief Set the description |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 80 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 81 | * This is typically invoked by mgmt on set description command |
82 | */ | ||||
83 | virtual void | ||||
84 | setDescription(const std::string& description); | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 85 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 86 | /// Get the description |
87 | virtual const std::string& | ||||
88 | getDescription() const; | ||||
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 89 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 90 | /** \brief Get whether face is connected to a local app |
91 | */ | ||||
92 | bool | ||||
93 | isLocal() const; | ||||
94 | |||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 95 | /** \brief Get whether packets sent this Face may reach multiple peers |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 96 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 97 | * In this base class this property is always false. |
98 | */ | ||||
99 | virtual bool | ||||
100 | isMultiAccess() const; | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 101 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 102 | /** \brief Get whether underlying communication is up |
103 | * | ||||
104 | * In this base class this property is always true. | ||||
105 | */ | ||||
106 | virtual bool | ||||
107 | isUp() const; | ||||
108 | |||||
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 109 | /** \brief Get whether face is created on demand or explicitly via FaceManagement protocol |
110 | */ | ||||
111 | bool | ||||
112 | isOnDemand() const; | ||||
113 | |||||
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 114 | const FaceCounters& |
115 | getCounters() const; | ||||
116 | |||||
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 117 | const FaceUri& |
118 | getUri() const; | ||||
119 | |||||
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 120 | protected: |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 121 | // this is a non-virtual method |
122 | bool | ||||
123 | decodeAndDispatchInput(const Block& element); | ||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 124 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 125 | FaceCounters& |
126 | getMutableCounters(); | ||||
127 | |||||
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 128 | void |
129 | setOnDemand(bool isOnDemand); | ||||
130 | |||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 131 | private: |
Alexander Afanasyev | 46f6647 | 2014-01-31 16:50:58 -0800 | [diff] [blame] | 132 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 133 | setId(FaceId faceId); |
134 | |||||
135 | private: | ||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 136 | FaceId m_id; |
137 | std::string m_description; | ||||
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 138 | bool m_isLocal; // for scoping purposes |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 139 | FaceCounters m_counters; |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 140 | FaceUri m_uri; |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 141 | bool m_isOnDemand; |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 142 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 143 | // allow setting FaceId |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 144 | friend class FaceTable; |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 145 | }; |
146 | |||||
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 147 | |
Davide Pesavento | 0ff10db | 2014-02-28 03:12:27 +0100 | [diff] [blame] | 148 | inline bool |
149 | Face::isLocal() const | ||||
150 | { | ||||
151 | return m_isLocal; | ||||
152 | } | ||||
153 | |||||
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 154 | inline const FaceCounters& |
155 | Face::getCounters() const | ||||
156 | { | ||||
157 | return m_counters; | ||||
158 | } | ||||
159 | |||||
160 | inline FaceCounters& | ||||
161 | Face::getMutableCounters() | ||||
162 | { | ||||
163 | return m_counters; | ||||
164 | } | ||||
165 | |||||
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 166 | inline const FaceUri& |
167 | Face::getUri() const | ||||
168 | { | ||||
169 | return m_uri; | ||||
170 | } | ||||
171 | |||||
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 172 | inline void |
173 | Face::setOnDemand(bool isOnDemand) | ||||
174 | { | ||||
175 | m_isOnDemand = isOnDemand; | ||||
176 | } | ||||
177 | |||||
178 | inline bool | ||||
179 | Face::isOnDemand() const | ||||
180 | { | ||||
181 | return m_isOnDemand; | ||||
182 | } | ||||
183 | |||||
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 184 | } // namespace nfd |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 185 | |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 186 | #endif // NFD_FACE_FACE_HPP |