blob: 1efcb35f45b9a2a48de83771333e6ad75e07f23f [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
Junxiao Shi7860d482014-02-21 23:57:20 -0700109 const FaceCounters&
110 getCounters() const;
111
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000112 const FaceUri&
113 getUri() const;
114
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800115protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800116 // this is a non-virtual method
117 bool
118 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700119
Junxiao Shi7860d482014-02-21 23:57:20 -0700120 FaceCounters&
121 getMutableCounters();
122
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700123private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800124 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700125 setId(FaceId faceId);
126
127private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700128 FaceId m_id;
129 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800130 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700131 FaceCounters m_counters;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000132 FaceUri m_uri;
Junxiao Shi7860d482014-02-21 23:57:20 -0700133
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700134 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700135 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700136};
137
Junxiao Shi7860d482014-02-21 23:57:20 -0700138
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100139inline bool
140Face::isLocal() const
141{
142 return m_isLocal;
143}
144
Junxiao Shi7860d482014-02-21 23:57:20 -0700145inline const FaceCounters&
146Face::getCounters() const
147{
148 return m_counters;
149}
150
151inline FaceCounters&
152Face::getMutableCounters()
153{
154 return m_counters;
155}
156
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000157inline const FaceUri&
158Face::getUri() const
159{
160 return m_uri;
161}
162
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800163} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700164
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700165#endif // NFD_FACE_FACE_HPP