blob: 6aeaf7703377e3dd9446af4e68f39bae7d7a026e [file] [log] [blame]
Junxiao Shi2c29f3a2014-01-24 19:59:00 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070024
Junxiao Shi16b8bc92014-02-17 22:24:55 -070025#ifndef NFD_FACE_FACE_HPP
26#define NFD_FACE_FACE_HPP
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070027
28#include "common.hpp"
Junxiao Shi7f02c1e2014-01-29 22:57:01 -070029#include "core/event-emitter.hpp"
Junxiao Shi61e3cc52014-03-03 20:40:28 -070030#include "core/face-uri.hpp"
Junxiao Shi7860d482014-02-21 23:57:20 -070031#include "face-counter.hpp"
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070034
35/** \class FaceId
36 * \brief identifies a face
37 */
38typedef int FaceId;
39
Junxiao Shi8c8d2182014-01-30 22:33:00 -070040const FaceId INVALID_FACEID = -1;
41
Davide Pesavento0ff10db2014-02-28 03:12:27 +010042const size_t MAX_NDN_PACKET_SIZE = 8800;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080043
Junxiao Shi16b8bc92014-02-17 22:24:55 -070044
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070045/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070046 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080047class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070048{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070049public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080050 /**
51 * \brief Face-related error
52 */
Junxiao Shi79494162014-04-02 18:25:11 -070053 class Error : public std::runtime_error
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080054 {
Junxiao Shi79494162014-04-02 18:25:11 -070055 public:
56 explicit
57 Error(const std::string& what)
58 : std::runtime_error(what)
59 {
60 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080061 };
62
Junxiao Shi79494162014-04-02 18:25:11 -070063 Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal = false);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070064
Junxiao Shi32bfeb32014-01-25 18:22:02 -070065 virtual
66 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070067
Junxiao Shi32bfeb32014-01-25 18:22:02 -070068 /// fires when an Interest is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080069 EventEmitter<Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070070
Junxiao Shi32bfeb32014-01-25 18:22:02 -070071 /// fires when a Data is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080072 EventEmitter<Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080073
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000074 /// fires when an Interest is sent out
75 EventEmitter<Interest> onSendInterest;
76
77 /// fires when a Data is sent out
78 EventEmitter<Data> onSendData;
79
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080080 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080081 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070082
Junxiao Shi32bfeb32014-01-25 18:22:02 -070083 /// send an Interest
84 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080085 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070086
Junxiao Shi32bfeb32014-01-25 18:22:02 -070087 /// send a Data
88 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080089 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080090
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080091 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080092 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080093 * This terminates all communication on the face and cause
94 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080095 */
96 virtual void
97 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070098
Junxiao Shi79494162014-04-02 18:25:11 -070099public: // attributes
100 FaceId
101 getId() const;
102
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700103 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -0700104 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700105 * This is typically invoked by mgmt on set description command
106 */
107 virtual void
108 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700109
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700110 /// Get the description
111 virtual const std::string&
112 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -0700113
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100114 /** \brief Get whether face is connected to a local app
115 */
116 bool
117 isLocal() const;
118
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700119 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -0700120 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700121 * In this base class this property is always false.
122 */
123 virtual bool
124 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700125
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100126 /** \brief Get whether underlying communication is up
127 *
128 * In this base class this property is always true.
129 */
130 virtual bool
131 isUp() const;
132
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700133 /** \brief Get whether face is created on demand or explicitly via FaceManagement protocol
134 */
135 bool
136 isOnDemand() const;
137
Junxiao Shi7860d482014-02-21 23:57:20 -0700138 const FaceCounters&
139 getCounters() const;
140
Junxiao Shi79494162014-04-02 18:25:11 -0700141 /** \return a FaceUri that represents the remote endpoint
142 */
143 const FaceUri&
144 getRemoteUri() const;
145
146 /** \return a FaceUri that represents the local endpoint (NFD side)
147 */
148 const FaceUri&
149 getLocalUri() const;
150
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800151protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800152 // this is a non-virtual method
153 bool
154 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700155
Junxiao Shi7860d482014-02-21 23:57:20 -0700156 FaceCounters&
157 getMutableCounters();
158
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700159 void
160 setOnDemand(bool isOnDemand);
161
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700162private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800163 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700164 setId(FaceId faceId);
165
166private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700167 FaceId m_id;
168 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800169 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700170 FaceCounters m_counters;
Junxiao Shi79494162014-04-02 18:25:11 -0700171 FaceUri m_remoteUri;
172 FaceUri m_localUri;
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700173 bool m_isOnDemand;
Junxiao Shi7860d482014-02-21 23:57:20 -0700174
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700175 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700176 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700177};
178
Junxiao Shi7860d482014-02-21 23:57:20 -0700179
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100180inline bool
181Face::isLocal() const
182{
183 return m_isLocal;
184}
185
Junxiao Shi7860d482014-02-21 23:57:20 -0700186inline const FaceCounters&
187Face::getCounters() const
188{
189 return m_counters;
190}
191
192inline FaceCounters&
193Face::getMutableCounters()
194{
195 return m_counters;
196}
197
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000198inline const FaceUri&
Junxiao Shi79494162014-04-02 18:25:11 -0700199Face::getRemoteUri() const
200{
201 return m_remoteUri;
202}
203
204inline const FaceUri&
205Face::getLocalUri() const
206{
207 return m_localUri;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000208}
209
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700210inline void
211Face::setOnDemand(bool isOnDemand)
212{
213 m_isOnDemand = isOnDemand;
214}
215
216inline bool
217Face::isOnDemand() const
218{
219 return m_isOnDemand;
220}
221
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800222} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700223
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700224#endif // NFD_FACE_FACE_HPP