Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 1 | /* -*- 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 Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 7 | #ifndef NFD_FACE_FACE_HPP |
8 | #define NFD_FACE_FACE_HPP | ||||
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 9 | |
10 | #include "common.hpp" | ||||
Junxiao Shi | 7f02c1e | 2014-01-29 22:57:01 -0700 | [diff] [blame] | 11 | #include "core/event-emitter.hpp" |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 12 | #include "face-counter.hpp" |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 13 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 14 | namespace nfd { |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 15 | |
16 | /** \class FaceId | ||||
17 | * \brief identifies a face | ||||
18 | */ | ||||
19 | typedef int FaceId; | ||||
20 | |||||
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 21 | const FaceId INVALID_FACEID = -1; |
22 | |||||
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 23 | const std::size_t MAX_NDN_PACKET_SIZE = 8800; |
24 | |||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 25 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 26 | /** \brief represents a face |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 27 | */ |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 28 | class Face : noncopyable, public enable_shared_from_this<Face> |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 29 | { |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 30 | public: |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 31 | /** |
32 | * \brief Face-related error | ||||
33 | */ | ||||
34 | struct Error : public std::runtime_error | ||||
35 | { | ||||
36 | Error(const std::string& what) : std::runtime_error(what) {} | ||||
37 | }; | ||||
38 | |||||
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 39 | Face(); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 40 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 41 | virtual |
42 | ~Face(); | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 43 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 44 | FaceId |
45 | getId() const; | ||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 46 | |
47 | /// fires when an Interest is received | ||||
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 48 | EventEmitter<Interest> onReceiveInterest; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 49 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 50 | /// fires when a Data is received |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 51 | EventEmitter<Data> onReceiveData; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 52 | |
53 | /// fires when face disconnects or fails to perform properly | ||||
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 54 | EventEmitter<std::string/*reason*/> onFail; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 56 | /// send an Interest |
57 | virtual void | ||||
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 58 | sendInterest(const Interest& interest) = 0; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 59 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 60 | /// send a Data |
61 | virtual void | ||||
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 62 | sendData(const Data& data) = 0; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 64 | /** \brief Close the face |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 65 | * |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 66 | * This terminates all communication on the face and cause |
67 | * onFail() method event to be invoked | ||||
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 68 | */ |
69 | virtual void | ||||
70 | close() = 0; | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 71 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 72 | /** \brief Get whether underlying communication is up |
73 | * | ||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 74 | * In this base class this property is always true. |
75 | */ | ||||
76 | virtual bool | ||||
77 | isUp() const; | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 79 | /** \brief Get whether face is connected to a local app |
80 | * | ||||
81 | * False by default and can become true if a derived class, implementing | ||||
82 | * one of the local face types, explicitly calls Face::setLocal(true) | ||||
83 | */ | ||||
84 | bool | ||||
85 | isLocal() const; | ||||
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 86 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 87 | /** \brief Set the description |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 88 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 89 | * This is typically invoked by mgmt on set description command |
90 | */ | ||||
91 | virtual void | ||||
92 | setDescription(const std::string& description); | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 94 | /// Get the description |
95 | virtual const std::string& | ||||
96 | getDescription() const; | ||||
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 97 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 98 | /** \brief Get whether packets sent this Face may reach multiple peers |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 99 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 100 | * In this base class this property is always false. |
101 | */ | ||||
102 | virtual bool | ||||
103 | isMultiAccess() const; | ||||
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 104 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 105 | const FaceCounters& |
106 | getCounters() const; | ||||
107 | |||||
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 108 | protected: |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 109 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 110 | setLocal(bool isLocal); |
111 | |||||
112 | // this is a non-virtual method | ||||
113 | bool | ||||
114 | decodeAndDispatchInput(const Block& element); | ||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 115 | |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 116 | FaceCounters& |
117 | getMutableCounters(); | ||||
118 | |||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 119 | private: |
Alexander Afanasyev | 46f6647 | 2014-01-31 16:50:58 -0800 | [diff] [blame] | 120 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 121 | setId(FaceId faceId); |
122 | |||||
123 | private: | ||||
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 124 | FaceId m_id; |
125 | std::string m_description; | ||||
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 126 | bool m_isLocal; // for scoping purposes |
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 127 | FaceCounters m_counters; |
128 | |||||
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 129 | // allow setting FaceId |
130 | friend class Forwarder; | ||||
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 131 | }; |
132 | |||||
Junxiao Shi | 7860d48 | 2014-02-21 23:57:20 -0700 | [diff] [blame] | 133 | |
134 | inline const FaceCounters& | ||||
135 | Face::getCounters() const | ||||
136 | { | ||||
137 | return m_counters; | ||||
138 | } | ||||
139 | |||||
140 | inline FaceCounters& | ||||
141 | Face::getMutableCounters() | ||||
142 | { | ||||
143 | return m_counters; | ||||
144 | } | ||||
145 | |||||
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 146 | } // namespace nfd |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 147 | |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 148 | #endif // NFD_FACE_FACE_HPP |