blob: d4e45787b26947739d8878bf59f98d21a1ee09c5 [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 Shi2c29f3a2014-01-24 19:59:00 -070012
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080013namespace nfd {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070014
15/** \class FaceId
16 * \brief identifies a face
17 */
18typedef int FaceId;
19
Junxiao Shi8c8d2182014-01-30 22:33:00 -070020const FaceId INVALID_FACEID = -1;
21
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080022const std::size_t MAX_NDN_PACKET_SIZE = 8800;
23
Junxiao Shi16b8bc92014-02-17 22:24:55 -070024
25/* \brief indicates a feature in LocalControlHeader
26 */
27enum 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 Shi9b0d3e92014-02-15 12:27:12 -070040/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070041 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080042class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070043{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070044public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080045 /**
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 Shi8c8d2182014-01-30 22:33:00 -070053 Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070054
Junxiao Shi32bfeb32014-01-25 18:22:02 -070055 virtual
56 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070057
Junxiao Shi8c8d2182014-01-30 22:33:00 -070058 FaceId
59 getId() const;
Junxiao Shi32bfeb32014-01-25 18:22:02 -070060
61 /// fires when an Interest is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080062 EventEmitter<Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070063
Junxiao Shi32bfeb32014-01-25 18:22:02 -070064 /// fires when a Data is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080065 EventEmitter<Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080066
67 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080068 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070069
Junxiao Shi32bfeb32014-01-25 18:22:02 -070070 /// send an Interest
71 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080072 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070073
Junxiao Shi32bfeb32014-01-25 18:22:02 -070074 /// send a Data
75 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080076 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080077
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 Shi16b8bc92014-02-17 22:24:55 -070086
Junxiao Shi88884492014-02-15 15:57:43 -070087 /** \brief Get whether underlying communication is up
88 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070089 * In this base class this property is always true.
90 */
91 virtual bool
92 isUp() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070093
Junxiao Shi32bfeb32014-01-25 18:22:02 -070094 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -070095 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070096 * This is typically invoked by mgmt on set description command
97 */
98 virtual void
99 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700100
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700101 /// Get the description
102 virtual const std::string&
103 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -0700104
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 Shi16b8bc92014-02-17 22:24:55 -0700111
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700112 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -0700113 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700114 * In this base class this property is always false.
115 */
116 virtual bool
117 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700118
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 Shi32bfeb32014-01-25 18:22:02 -0700135
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700136private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800137 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700138 setId(FaceId faceId);
139
140private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700141 FaceId m_id;
142 std::string m_description;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700143 std::vector<bool> m_localControlHeaderFeatures;
144
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700145 // allow setting FaceId
146 friend class Forwarder;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700147};
148
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700149inline bool
150Face::isLocalControlHeaderEnabled(LocalControlHeaderFeature feature) const
151{
152 BOOST_ASSERT(feature < m_localControlHeaderFeatures.size());
153 return m_localControlHeaderFeatures[feature];
154}
155
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800156} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700157
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700158#endif // NFD_FACE_FACE_HPP