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 | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 12 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 13 | namespace nfd { |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 14 | |
| 15 | /** \class FaceId |
| 16 | * \brief identifies a face |
| 17 | */ |
| 18 | typedef int FaceId; |
| 19 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 20 | const FaceId INVALID_FACEID = -1; |
| 21 | |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 22 | const std::size_t MAX_NDN_PACKET_SIZE = 8800; |
| 23 | |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 24 | |
| 25 | /* \brief indicates a feature in LocalControlHeader |
| 26 | */ |
| 27 | enum LocalControlHeaderFeature |
| 28 | { |
| 29 | /// any feature |
| 30 | LOCAL_CONTROL_HEADER_FEATURE_ANY, |
| 31 | /// in-faceid |
| 32 | LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID, |
| 33 | /// out-faceid |
| 34 | LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID, |
| 35 | /// upper bound of enum |
| 36 | LOCAL_CONTROL_HEADER_FEATURE_MAX |
| 37 | }; |
| 38 | |
| 39 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 40 | /** \brief represents a face |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 41 | */ |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 42 | class Face : noncopyable, public enable_shared_from_this<Face> |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 43 | { |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 44 | public: |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 45 | /** |
| 46 | * \brief Face-related error |
| 47 | */ |
| 48 | struct Error : public std::runtime_error |
| 49 | { |
| 50 | Error(const std::string& what) : std::runtime_error(what) {} |
| 51 | }; |
| 52 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 53 | Face(); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 54 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 55 | virtual |
| 56 | ~Face(); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 57 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 58 | FaceId |
| 59 | getId() const; |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 60 | |
| 61 | /// fires when an Interest is received |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 62 | EventEmitter<Interest> onReceiveInterest; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 63 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 64 | /// fires when a Data is received |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 65 | EventEmitter<Data> onReceiveData; |
Alexander Afanasyev | d32cb96 | 2014-01-28 12:43:47 -0800 | [diff] [blame] | 66 | |
| 67 | /// fires when face disconnects or fails to perform properly |
Alexander Afanasyev | d6cf455 | 2014-02-11 17:15:22 -0800 | [diff] [blame] | 68 | EventEmitter<std::string/*reason*/> onFail; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 69 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 70 | /// send an Interest |
| 71 | virtual void |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 72 | sendInterest(const Interest& interest) = 0; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 73 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 74 | /// send a Data |
| 75 | virtual void |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 76 | sendData(const Data& data) = 0; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * \brief Close the face |
| 80 | * |
| 81 | * This terminates all communication on the face and cause |
| 82 | * onFail() method event to be invoked |
| 83 | */ |
| 84 | virtual void |
| 85 | close() = 0; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 86 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 87 | /** \brief Get whether underlying communication is up |
| 88 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 89 | * In this base class this property is always true. |
| 90 | */ |
| 91 | virtual bool |
| 92 | isUp() const; |
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 | /** \brief Set the description |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 95 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 96 | * This is typically invoked by mgmt on set description command |
| 97 | */ |
| 98 | virtual void |
| 99 | setDescription(const std::string& description); |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 100 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 101 | /// Get the description |
| 102 | virtual const std::string& |
| 103 | getDescription() const; |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 104 | |
| 105 | /** \brief Get whether face is connected to a local app |
| 106 | * |
| 107 | * In this base class this property is always false. |
| 108 | */ |
| 109 | virtual bool |
| 110 | isLocal() const; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 112 | /** \brief Get whether packets sent this Face may reach multiple peers |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 113 | * |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 114 | * In this base class this property is always false. |
| 115 | */ |
| 116 | virtual bool |
| 117 | isMultiAccess() const; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 118 | |
| 119 | /** \brief get whether a LocalControlHeader feature is enabled |
| 120 | * |
| 121 | * \param feature The feature. Cannot be LOCAL_CONTROL_HEADER_FEATURE_MAX |
| 122 | * LOCAL_CONTROL_HEADER_FEATURE_ANY returns true if any feature is enabled. |
| 123 | */ |
| 124 | bool |
| 125 | isLocalControlHeaderEnabled(LocalControlHeaderFeature feature = |
| 126 | LOCAL_CONTROL_HEADER_FEATURE_ANY) const; |
| 127 | |
| 128 | /** \brief enable or disable a LocalControlHeader feature |
| 129 | * |
| 130 | * \param feature The feature. Cannot be LOCAL_CONTROL_HEADER_FEATURE_ANY |
| 131 | * or LOCAL_CONTROL_HEADER_FEATURE_MAX |
| 132 | */ |
| 133 | void |
| 134 | setLocalControlHeaderFeature(LocalControlHeaderFeature feature, bool enabled); |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 135 | |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 136 | private: |
Alexander Afanasyev | 46f6647 | 2014-01-31 16:50:58 -0800 | [diff] [blame] | 137 | void |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 138 | setId(FaceId faceId); |
| 139 | |
| 140 | private: |
Junxiao Shi | 32bfeb3 | 2014-01-25 18:22:02 -0700 | [diff] [blame] | 141 | FaceId m_id; |
| 142 | std::string m_description; |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 143 | std::vector<bool> m_localControlHeaderFeatures; |
| 144 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 145 | // allow setting FaceId |
| 146 | friend class Forwarder; |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 149 | inline bool |
| 150 | Face::isLocalControlHeaderEnabled(LocalControlHeaderFeature feature) const |
| 151 | { |
| 152 | BOOST_ASSERT(feature < m_localControlHeaderFeatures.size()); |
| 153 | return m_localControlHeaderFeatures[feature]; |
| 154 | } |
| 155 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 156 | } // namespace nfd |
Junxiao Shi | 2c29f3a | 2014-01-24 19:59:00 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | 16b8bc9 | 2014-02-17 22:24:55 -0700 | [diff] [blame] | 158 | #endif // NFD_FACE_FACE_HPP |