blob: 3cb71604f6efde0385be20faba561e3669f6d60f [file] [log] [blame]
Junxiao Shi2c29f3a2014-01-24 19:59:00 -07001/* -*- 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 Shi16b8bc92014-02-17 22:24:55 -07007#ifndef NFD_FACE_FACE_HPP
8#define NFD_FACE_FACE_HPP
Junxiao Shi2c29f3a2014-01-24 19:59:00 -07009
10#include "common.hpp"
Junxiao Shi7f02c1e2014-01-29 22:57:01 -070011#include "core/event-emitter.hpp"
Junxiao Shi61e3cc52014-03-03 20:40:28 -070012#include "core/face-uri.hpp"
Junxiao Shi7860d482014-02-21 23:57:20 -070013#include "face-counter.hpp"
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070014
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080015namespace nfd {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070016
17/** \class FaceId
18 * \brief identifies a face
19 */
20typedef int FaceId;
21
Junxiao Shi8c8d2182014-01-30 22:33:00 -070022const FaceId INVALID_FACEID = -1;
23
Davide Pesavento0ff10db2014-02-28 03:12:27 +010024const size_t MAX_NDN_PACKET_SIZE = 8800;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080025
Junxiao Shi16b8bc92014-02-17 22:24:55 -070026
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070027/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070028 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080029class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070030{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070031public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080032 /**
33 * \brief Face-related error
34 */
Junxiao Shi79494162014-04-02 18:25:11 -070035 class Error : public std::runtime_error
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080036 {
Junxiao Shi79494162014-04-02 18:25:11 -070037 public:
38 explicit
39 Error(const std::string& what)
40 : std::runtime_error(what)
41 {
42 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080043 };
44
Junxiao Shi79494162014-04-02 18:25:11 -070045 Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal = false);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070046
Junxiao Shi32bfeb32014-01-25 18:22:02 -070047 virtual
48 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070049
Junxiao Shi32bfeb32014-01-25 18:22:02 -070050 /// fires when an Interest is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080051 EventEmitter<Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070052
Junxiao Shi32bfeb32014-01-25 18:22:02 -070053 /// fires when a Data is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080054 EventEmitter<Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080055
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000056 /// 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 Afanasyevd32cb962014-01-28 12:43:47 -080062 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080063 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070064
Junxiao Shi32bfeb32014-01-25 18:22:02 -070065 /// send an Interest
66 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080067 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070068
Junxiao Shi32bfeb32014-01-25 18:22:02 -070069 /// send a Data
70 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080071 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080072
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080073 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080074 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080075 * This terminates all communication on the face and cause
76 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080077 */
78 virtual void
79 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070080
Junxiao Shi79494162014-04-02 18:25:11 -070081public: // attributes
82 FaceId
83 getId() const;
84
Junxiao Shi32bfeb32014-01-25 18:22:02 -070085 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -070086 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070087 * This is typically invoked by mgmt on set description command
88 */
89 virtual void
90 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070091
Junxiao Shi32bfeb32014-01-25 18:22:02 -070092 /// Get the description
93 virtual const std::string&
94 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -070095
Davide Pesavento0ff10db2014-02-28 03:12:27 +010096 /** \brief Get whether face is connected to a local app
97 */
98 bool
99 isLocal() const;
100
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700101 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -0700102 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700103 * In this base class this property is always false.
104 */
105 virtual bool
106 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700107
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100108 /** \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 Afanasyev355c0662014-03-20 18:08:17 -0700115 /** \brief Get whether face is created on demand or explicitly via FaceManagement protocol
116 */
117 bool
118 isOnDemand() const;
119
Junxiao Shi7860d482014-02-21 23:57:20 -0700120 const FaceCounters&
121 getCounters() const;
122
Junxiao Shi79494162014-04-02 18:25:11 -0700123 /** \deprecated use getRemoteUri instead
124 */
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000125 const FaceUri&
126 getUri() const;
127
Junxiao Shi79494162014-04-02 18:25:11 -0700128 /** \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 Afanasyevbd220a02014-02-20 00:29:56 -0800138protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800139 // this is a non-virtual method
140 bool
141 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700142
Junxiao Shi7860d482014-02-21 23:57:20 -0700143 FaceCounters&
144 getMutableCounters();
145
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700146 void
147 setOnDemand(bool isOnDemand);
148
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700149private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800150 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700151 setId(FaceId faceId);
152
153private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700154 FaceId m_id;
155 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800156 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700157 FaceCounters m_counters;
Junxiao Shi79494162014-04-02 18:25:11 -0700158 FaceUri m_remoteUri;
159 FaceUri m_localUri;
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700160 bool m_isOnDemand;
Junxiao Shi7860d482014-02-21 23:57:20 -0700161
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700162 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700163 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700164};
165
Junxiao Shi7860d482014-02-21 23:57:20 -0700166
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100167inline bool
168Face::isLocal() const
169{
170 return m_isLocal;
171}
172
Junxiao Shi7860d482014-02-21 23:57:20 -0700173inline const FaceCounters&
174Face::getCounters() const
175{
176 return m_counters;
177}
178
179inline FaceCounters&
180Face::getMutableCounters()
181{
182 return m_counters;
183}
184
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000185inline const FaceUri&
186Face::getUri() const
187{
Junxiao Shi79494162014-04-02 18:25:11 -0700188 return this->getRemoteUri();
189}
190
191inline const FaceUri&
192Face::getRemoteUri() const
193{
194 return m_remoteUri;
195}
196
197inline const FaceUri&
198Face::getLocalUri() const
199{
200 return m_localUri;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000201}
202
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700203inline void
204Face::setOnDemand(bool isOnDemand)
205{
206 m_isOnDemand = isOnDemand;
207}
208
209inline bool
210Face::isOnDemand() const
211{
212 return m_isOnDemand;
213}
214
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800215} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700216
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700217#endif // NFD_FACE_FACE_HPP