blob: 893a2536b5244651f53b24d6122fe772a3025de1 [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
54 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080055 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070056
Junxiao Shi32bfeb32014-01-25 18:22:02 -070057 /// send an Interest
58 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080059 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070060
Junxiao Shi32bfeb32014-01-25 18:22:02 -070061 /// send a Data
62 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080063 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080064
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080065 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080066 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080067 * This terminates all communication on the face and cause
68 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080069 */
70 virtual void
71 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070072
Junxiao Shi32bfeb32014-01-25 18:22:02 -070073 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -070074 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070075 * This is typically invoked by mgmt on set description command
76 */
77 virtual void
78 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070079
Junxiao Shi32bfeb32014-01-25 18:22:02 -070080 /// Get the description
81 virtual const std::string&
82 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -070083
Davide Pesavento0ff10db2014-02-28 03:12:27 +010084 /** \brief Get whether face is connected to a local app
85 */
86 bool
87 isLocal() const;
88
Junxiao Shi32bfeb32014-01-25 18:22:02 -070089 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -070090 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070091 * In this base class this property is always false.
92 */
93 virtual bool
94 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070095
Davide Pesavento0ff10db2014-02-28 03:12:27 +010096 /** \brief Get whether underlying communication is up
97 *
98 * In this base class this property is always true.
99 */
100 virtual bool
101 isUp() const;
102
Junxiao Shi7860d482014-02-21 23:57:20 -0700103 const FaceCounters&
104 getCounters() const;
105
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000106 const FaceUri&
107 getUri() const;
108
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800109protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800110 // this is a non-virtual method
111 bool
112 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700113
Junxiao Shi7860d482014-02-21 23:57:20 -0700114 FaceCounters&
115 getMutableCounters();
116
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700117private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800118 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700119 setId(FaceId faceId);
120
121private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700122 FaceId m_id;
123 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800124 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700125 FaceCounters m_counters;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000126 FaceUri m_uri;
Junxiao Shi7860d482014-02-21 23:57:20 -0700127
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700128 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700129 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700130};
131
Junxiao Shi7860d482014-02-21 23:57:20 -0700132
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100133inline bool
134Face::isLocal() const
135{
136 return m_isLocal;
137}
138
Junxiao Shi7860d482014-02-21 23:57:20 -0700139inline const FaceCounters&
140Face::getCounters() const
141{
142 return m_counters;
143}
144
145inline FaceCounters&
146Face::getMutableCounters()
147{
148 return m_counters;
149}
150
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000151inline const FaceUri&
152Face::getUri() const
153{
154 return m_uri;
155}
156
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800157} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700158
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700159#endif // NFD_FACE_FACE_HPP