blob: 2f555a603f86737812cc9b88fe5947374d3456a1 [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 /** \return a FaceUri that represents the remote endpoint
124 */
125 const FaceUri&
126 getRemoteUri() const;
127
128 /** \return a FaceUri that represents the local endpoint (NFD side)
129 */
130 const FaceUri&
131 getLocalUri() const;
132
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800133protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800134 // this is a non-virtual method
135 bool
136 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700137
Junxiao Shi7860d482014-02-21 23:57:20 -0700138 FaceCounters&
139 getMutableCounters();
140
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700141 void
142 setOnDemand(bool isOnDemand);
143
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700144private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800145 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700146 setId(FaceId faceId);
147
148private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700149 FaceId m_id;
150 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800151 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700152 FaceCounters m_counters;
Junxiao Shi79494162014-04-02 18:25:11 -0700153 FaceUri m_remoteUri;
154 FaceUri m_localUri;
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700155 bool m_isOnDemand;
Junxiao Shi7860d482014-02-21 23:57:20 -0700156
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700157 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700158 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700159};
160
Junxiao Shi7860d482014-02-21 23:57:20 -0700161
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100162inline bool
163Face::isLocal() const
164{
165 return m_isLocal;
166}
167
Junxiao Shi7860d482014-02-21 23:57:20 -0700168inline const FaceCounters&
169Face::getCounters() const
170{
171 return m_counters;
172}
173
174inline FaceCounters&
175Face::getMutableCounters()
176{
177 return m_counters;
178}
179
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000180inline const FaceUri&
Junxiao Shi79494162014-04-02 18:25:11 -0700181Face::getRemoteUri() const
182{
183 return m_remoteUri;
184}
185
186inline const FaceUri&
187Face::getLocalUri() const
188{
189 return m_localUri;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000190}
191
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700192inline void
193Face::setOnDemand(bool isOnDemand)
194{
195 m_isOnDemand = isOnDemand;
196}
197
198inline bool
199Face::isOnDemand() const
200{
201 return m_isOnDemand;
202}
203
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800204} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700205
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700206#endif // NFD_FACE_FACE_HPP