NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
nfd-face-event-notification.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
23 #include "encoding/tlv-nfd.hpp"
25 #include "util/concepts.hpp"
26 
27 namespace ndn {
28 namespace nfd {
29 
30 //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceEventNotification>));
31 BOOST_CONCEPT_ASSERT((WireEncodable<FaceEventNotification>));
32 BOOST_CONCEPT_ASSERT((WireDecodable<FaceEventNotification>));
33 static_assert(std::is_base_of<tlv::Error, FaceEventNotification::Error>::value,
34  "FaceEventNotification::Error must inherit from tlv::Error");
35 
37  : m_kind(static_cast<FaceEventKind>(0))
38 {
39 }
40 
42 {
43  this->wireDecode(block);
44 }
45 
46 template<encoding::Tag TAG>
47 size_t
48 FaceEventNotification::wireEncode(EncodingImpl<TAG>& encoder) const
49 {
50  size_t totalLength = 0;
51 
52  totalLength += prependNonNegativeIntegerBlock(encoder,
54  totalLength += prependNonNegativeIntegerBlock(encoder,
56  totalLength += prependNonNegativeIntegerBlock(encoder,
58  totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
59  reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
60  totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
61  reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
62  totalLength += prependNonNegativeIntegerBlock(encoder,
64  totalLength += prependNonNegativeIntegerBlock(encoder,
65  tlv::nfd::FaceEventKind, m_kind);
66 
67  totalLength += encoder.prependVarNumber(totalLength);
68  totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
69  return totalLength;
70 }
71 
72 const Block&
74 {
75  if (m_wire.hasWire())
76  return m_wire;
77 
78  EncodingEstimator estimator;
79  size_t estimatedSize = wireEncode(estimator);
80 
81  EncodingBuffer buffer(estimatedSize, 0);
82  wireEncode(buffer);
83 
84  m_wire = buffer.block();
85  return m_wire;
86 }
87 
88 void
90 {
91  if (block.type() != tlv::nfd::FaceEventNotification) {
92  BOOST_THROW_EXCEPTION(Error("expecting FaceEventNotification block"));
93  }
94  m_wire = block;
95  m_wire.parse();
97 
98  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
99  m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
100  ++val;
101  }
102  else {
103  BOOST_THROW_EXCEPTION(Error("missing required FaceEventKind field"));
104  }
105 
106  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
108  ++val;
109  }
110  else {
111  BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
112  }
113 
114  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
115  m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
116  ++val;
117  }
118  else {
119  BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
120  }
121 
122  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
123  m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
124  ++val;
125  }
126  else {
127  BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
128  }
129 
130  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
131  m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
132  ++val;
133  }
134  else {
135  BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
136  }
137 
138  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
140  ++val;
141  }
142  else {
143  BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
144  }
145 
146  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
147  m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
148  ++val;
149  }
150  else {
151  BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
152  }
153 }
154 
157 {
158  m_wire.reset();
159  m_kind = kind;
160  return *this;
161 }
162 
163 void
165 {
166  m_wire.reset();
167 }
168 
169 std::ostream&
170 operator<<(std::ostream& os, const FaceEventNotification& notification)
171 {
172  os << "FaceEventNotification(";
173 
174  switch (notification.getKind())
175  {
176  case FACE_EVENT_CREATED:
177  os << "Kind: created, ";
178  break;
180  os << "Kind: destroyed, ";
181  break;
182  }
183 
184  os << "FaceID: " << notification.getFaceId() << ", "
185  << "RemoteUri: " << notification.getRemoteUri() << ", "
186  << "LocalUri: " << notification.getLocalUri() << ", "
187  << "FaceScope: " << notification.getFaceScope() << ", "
188  << "FacePersistency: " << notification.getFacePersistency() << ", "
189  << "LinkType: " << notification.getLinkType()
190  << ")";
191  return os;
192 }
193 
194 } // namespace nfd
195 } // namespace ndn
LinkType getLinkType() const
Copyright (c) 2011-2015 Regents of the University of California.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Helper to prepend TLV block type type containing non-negative integer value.
EncodingImpl< EstimatorTag > EncodingEstimator
FaceScope getFaceScope() const
represents a Face status change notification
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
FaceEventNotification & setKind(FaceEventKind kind)
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
element_const_iterator elements_end() const
Definition: block.cpp:595
element_const_iterator elements_begin() const
Definition: block.cpp:589
EncodingImpl< EncoderTag > EncodingBuffer
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
uint64_t getFaceId() const
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
const std::string & getLocalUri() const
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
const std::string & getRemoteUri() const
uint32_t type() const
Definition: block.hpp:346
FacePersistency getFacePersistency() const
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
void wireDecode(const Block &wire)
decode FaceEventNotification
const Block & wireEncode() const
encode FaceEventNotification
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70