NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
local-face.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
25 #ifndef NFD_DAEMON_FACE_LOCAL_FACE_HPP
26 #define NFD_DAEMON_FACE_LOCAL_FACE_HPP
27 
28 #include "face.hpp"
29 #include <ndn-cxx/management/nfd-control-parameters.hpp>
30 
31 namespace nfd {
32 
33 using ndn::nfd::LocalControlFeature;
34 using ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID;
35 using ndn::nfd::LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID;
36 
39 class LocalFace : public Face
40 {
41 public:
42  LocalFace(const FaceUri& remoteUri, const FaceUri& localUri);
43 
48  bool
50 
56  bool
57  isLocalControlHeaderEnabled(LocalControlFeature feature) const;
58 
64  void
65  setLocalControlHeaderFeature(LocalControlFeature feature, bool enabled = true);
66 
67 public:
68 
69  static const size_t LOCAL_CONTROL_FEATURE_MAX = 3;
70  static const size_t LOCAL_CONTROL_FEATURE_ANY = 0;
71 
72 protected:
73  // statically overridden from Face
74 
80  bool
81  decodeAndDispatchInput(const Block& element);
82 
83  // LocalFace-specific methods
84 
89  bool
90  isEmptyFilteredLocalControlHeader(const ndn::nfd::LocalControlHeader& header) const;
91 
94  template<class Packet>
95  Block
96  filterAndEncodeLocalControlHeader(const Packet& packet);
97 
98 private:
99  std::vector<bool> m_localControlHeaderFeatures;
100 };
101 
102 inline
103 LocalFace::LocalFace(const FaceUri& remoteUri, const FaceUri& localUri)
104  : Face(remoteUri, localUri, true)
105  , m_localControlHeaderFeatures(LocalFace::LOCAL_CONTROL_FEATURE_MAX)
106 {
107 }
108 
109 inline bool
111 {
112  return m_localControlHeaderFeatures[LOCAL_CONTROL_FEATURE_ANY];
113 }
114 
115 inline bool
116 LocalFace::isLocalControlHeaderEnabled(LocalControlFeature feature) const
117 {
118  BOOST_ASSERT(0 < feature &&
119  static_cast<size_t>(feature) < m_localControlHeaderFeatures.size());
120  return m_localControlHeaderFeatures[feature];
121 }
122 
123 inline void
124 LocalFace::setLocalControlHeaderFeature(LocalControlFeature feature, bool enabled/* = true*/)
125 {
126  BOOST_ASSERT(0 < feature &&
127  static_cast<size_t>(feature) < m_localControlHeaderFeatures.size());
128 
129  m_localControlHeaderFeatures[feature] = enabled;
130 
131  m_localControlHeaderFeatures[LOCAL_CONTROL_FEATURE_ANY] =
132  std::find(m_localControlHeaderFeatures.begin() + 1,
133  m_localControlHeaderFeatures.end(), true) <
134  m_localControlHeaderFeatures.end();
135  // 'find(..) < .end()' instead of 'find(..) != .end()' due to LLVM Bug 16816
136 }
137 
138 inline bool
140 {
141  try {
142  const Block& payload = ndn::nfd::LocalControlHeader::getPayload(element);
143 
144  // If received LocalControlHeader, but it is not enabled on the face
145  if ((&payload != &element) && !this->isLocalControlHeaderEnabled())
146  return false;
147 
148  if (payload.type() == tlv::Interest)
149  {
150  shared_ptr<Interest> i = make_shared<Interest>();
151  i->wireDecode(payload);
152  if (&payload != &element)
153  {
154  uint8_t mask = 0;
155  if (this->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID)) {
156  mask |= ndn::nfd::LocalControlHeader::ENCODE_NEXT_HOP;
157  }
158  i->getLocalControlHeader().wireDecode(element, mask);
159  }
160 
161  this->onReceiveInterest(*i);
162  }
163  else if (payload.type() == tlv::Data)
164  {
165  shared_ptr<Data> d = make_shared<Data>();
166  d->wireDecode(payload);
167 
171  // if (&payload != &element)
172  // {
173  //
174  // d->getLocalControlHeader().wireDecode(element,
175  // false,
176  // false);
177  // }
178 
179  this->onReceiveData(*d);
180  }
181  else
182  return false;
183 
184  return true;
185  }
186  catch (tlv::Error&) {
187  return false;
188  }
189 }
190 
191 inline bool
192 LocalFace::isEmptyFilteredLocalControlHeader(const ndn::nfd::LocalControlHeader& header) const
193 {
194  if (!this->isLocalControlHeaderEnabled())
195  return true;
196 
197  uint8_t mask = 0;
198  if (this->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID)) {
199  mask |= ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID;
200  }
201  return header.empty(mask);
202 }
203 
204 template<class Packet>
205 inline Block
207 {
208  uint8_t mask = 0;
209  if (this->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID)) {
210  mask |= ndn::nfd::LocalControlHeader::ENCODE_INCOMING_FACE_ID;
211  }
212  return packet.getLocalControlHeader().wireEncode(packet, mask);
213 }
214 
215 } // namespace nfd
216 
217 #endif // NFD_DAEMON_FACE_LOCAL_FACE_HPP
static const size_t LOCAL_CONTROL_FEATURE_ANY
upper bound of LocalControlFeature enum
Definition: local-face.hpp:70
EventEmitter< Data > onReceiveData
fires when a Data is received
Definition: face.hpp:84
Block filterAndEncodeLocalControlHeader(const Packet &packet)
Create LocalControlHeader, considering enabled features.
Definition: local-face.hpp:206
bool isLocalControlHeaderEnabled() const
get whether any LocalControlHeader feature is enabled
Definition: local-face.hpp:110
represents a face
Definition: face.hpp:59
bool decodeAndDispatchInput(const Block &element)
any feature
Definition: local-face.hpp:139
LocalFace(const FaceUri &remoteUri, const FaceUri &localUri)
Definition: local-face.hpp:103
bool isEmptyFilteredLocalControlHeader(const ndn::nfd::LocalControlHeader &header) const
Check if LocalControlHeader needs to be included, taking into account both set parameters in supplied...
Definition: local-face.hpp:192
represents a face
Definition: local-face.hpp:39
static const size_t LOCAL_CONTROL_FEATURE_MAX
Definition: local-face.hpp:69
EventEmitter< Interest > onReceiveInterest
fires when an Interest is received
Definition: face.hpp:81
void setLocalControlHeaderFeature(LocalControlFeature feature, bool enabled=true)
enable or disable a LocalControlHeader feature
Definition: local-face.hpp:124