blob: e1ef2f81283885ecae9b63ae07cdd414113f4a41 [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 */
35 struct Error : public std::runtime_error
36 {
37 Error(const std::string& what) : std::runtime_error(what) {}
38 };
39
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +000040 Face(const FaceUri& uri, bool isLocal = false);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070041
Junxiao Shi32bfeb32014-01-25 18:22:02 -070042 virtual
43 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070044
Junxiao Shi8c8d2182014-01-30 22:33:00 -070045 FaceId
46 getId() const;
Junxiao Shi32bfeb32014-01-25 18:22:02 -070047
48 /// fires when an Interest is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080049 EventEmitter<Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070050
Junxiao Shi32bfeb32014-01-25 18:22:02 -070051 /// fires when a Data is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080052 EventEmitter<Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080053
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000054 /// 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 Afanasyevd32cb962014-01-28 12:43:47 -080060 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080061 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070062
Junxiao Shi32bfeb32014-01-25 18:22:02 -070063 /// send an Interest
64 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080065 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070066
Junxiao Shi32bfeb32014-01-25 18:22:02 -070067 /// send a Data
68 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080069 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080070
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080071 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080072 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080073 * This terminates all communication on the face and cause
74 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080075 */
76 virtual void
77 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070078
Junxiao Shi32bfeb32014-01-25 18:22:02 -070079 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -070080 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070081 * This is typically invoked by mgmt on set description command
82 */
83 virtual void
84 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070085
Junxiao Shi32bfeb32014-01-25 18:22:02 -070086 /// Get the description
87 virtual const std::string&
88 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -070089
Davide Pesavento0ff10db2014-02-28 03:12:27 +010090 /** \brief Get whether face is connected to a local app
91 */
92 bool
93 isLocal() const;
94
Junxiao Shi32bfeb32014-01-25 18:22:02 -070095 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -070096 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070097 * In this base class this property is always false.
98 */
99 virtual bool
100 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700101
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100102 /** \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 Afanasyev355c0662014-03-20 18:08:17 -0700109 /** \brief Get whether face is created on demand or explicitly via FaceManagement protocol
110 */
111 bool
112 isOnDemand() const;
113
Junxiao Shi7860d482014-02-21 23:57:20 -0700114 const FaceCounters&
115 getCounters() const;
116
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000117 const FaceUri&
118 getUri() const;
119
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800120protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800121 // this is a non-virtual method
122 bool
123 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700124
Junxiao Shi7860d482014-02-21 23:57:20 -0700125 FaceCounters&
126 getMutableCounters();
127
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700128 void
129 setOnDemand(bool isOnDemand);
130
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700131private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800132 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700133 setId(FaceId faceId);
134
135private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700136 FaceId m_id;
137 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800138 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700139 FaceCounters m_counters;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000140 FaceUri m_uri;
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700141 bool m_isOnDemand;
Junxiao Shi7860d482014-02-21 23:57:20 -0700142
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700143 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700144 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700145};
146
Junxiao Shi7860d482014-02-21 23:57:20 -0700147
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100148inline bool
149Face::isLocal() const
150{
151 return m_isLocal;
152}
153
Junxiao Shi7860d482014-02-21 23:57:20 -0700154inline const FaceCounters&
155Face::getCounters() const
156{
157 return m_counters;
158}
159
160inline FaceCounters&
161Face::getMutableCounters()
162{
163 return m_counters;
164}
165
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000166inline const FaceUri&
167Face::getUri() const
168{
169 return m_uri;
170}
171
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700172inline void
173Face::setOnDemand(bool isOnDemand)
174{
175 m_isOnDemand = isOnDemand;
176}
177
178inline bool
179Face::isOnDemand() const
180{
181 return m_isOnDemand;
182}
183
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800184} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700185
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700186#endif // NFD_FACE_FACE_HPP