NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
face-event-notification.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
25 #include "encoding/tlv-nfd.hpp"
26 #include "util/concepts.hpp"
27 #include "util/string-helper.hpp"
28 
29 namespace ndn {
30 namespace nfd {
31 
33 
35  : m_kind(FACE_EVENT_NONE)
36 {
37 }
38 
40 {
41  this->wireDecode(block);
42 }
43 
44 template<encoding::Tag TAG>
45 size_t
47 {
48  size_t totalLength = 0;
49 
50  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
54  totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
55  totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
57  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceEventKind, m_kind);
58 
59  totalLength += encoder.prependVarNumber(totalLength);
60  totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
61  return totalLength;
62 }
63 
65 
66 const Block&
68 {
69  if (m_wire.hasWire())
70  return m_wire;
71 
72  EncodingEstimator estimator;
73  size_t estimatedSize = wireEncode(estimator);
74 
75  EncodingBuffer buffer(estimatedSize, 0);
76  wireEncode(buffer);
77 
78  m_wire = buffer.block();
79  return m_wire;
80 }
81 
82 void
84 {
85  if (block.type() != tlv::nfd::FaceEventNotification) {
86  BOOST_THROW_EXCEPTION(Error("expecting FaceEventNotification block"));
87  }
88  m_wire = block;
89  m_wire.parse();
91 
92  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
93  m_kind = readNonNegativeIntegerAs<FaceEventKind>(*val);
94  ++val;
95  }
96  else {
97  BOOST_THROW_EXCEPTION(Error("missing required FaceEventKind field"));
98  }
99 
100  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
102  ++val;
103  }
104  else {
105  BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
106  }
107 
108  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
109  m_remoteUri = readString(*val);
110  ++val;
111  }
112  else {
113  BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
114  }
115 
116  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
117  m_localUri = readString(*val);
118  ++val;
119  }
120  else {
121  BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
122  }
123 
124  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
125  m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
126  ++val;
127  }
128  else {
129  BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
130  }
131 
132  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
133  m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
134  ++val;
135  }
136  else {
137  BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
138  }
139 
140  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
141  m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
142  ++val;
143  }
144  else {
145  BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
146  }
147 
148  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
150  ++val;
151  }
152  else {
153  BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
154  }
155 }
156 
159 {
160  m_wire.reset();
161  m_kind = kind;
162  return *this;
163 }
164 
165 bool
167 {
168  return a.getFaceId() == b.getFaceId() &&
169  a.getRemoteUri() == b.getRemoteUri() &&
170  a.getLocalUri() == b.getLocalUri() &&
171  a.getFaceScope() == b.getFaceScope() &&
173  a.getLinkType() == b.getLinkType() &&
174  a.getFlags() == b.getFlags() &&
175  a.getKind() == b.getKind();
176 }
177 
178 std::ostream&
179 operator<<(std::ostream& os, const FaceEventNotification& notification)
180 {
181  os << "FaceEvent(Kind: " << notification.getKind() << ",\n"
182  << " FaceId: " << notification.getFaceId() << ",\n"
183  << " RemoteUri: " << notification.getRemoteUri() << ",\n"
184  << " LocalUri: " << notification.getLocalUri() << ",\n"
185  << " FaceScope: " << notification.getFaceScope() << ",\n"
186  << " FacePersistency: " << notification.getFacePersistency() << ",\n"
187  << " LinkType: " << notification.getLinkType() << ",\n"
188  << " Flags: " << AsHex{notification.getFlags()} << "\n";
189 
190  return os << " )";
191 }
192 
193 } // namespace nfd
194 } // namespace ndn
const std::string & getLocalUri() const
Definition: face-traits.hpp:79
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:251
Copyright (c) 2011-2015 Regents of the University of California.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
FacePersistency getFacePersistency() const
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
Helper class to convert a number to hexadecimal format, for use with stream insertion operators...
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:335
represents a Face status change notification
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ChannelStatus)
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
FaceEventNotification & setKind(FaceEventKind kind)
FaceScope getFaceScope() const
Definition: face-traits.hpp:93
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:355
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
const std::string & getRemoteUri() const
Definition: face-traits.hpp:65
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
concept check for an item in a Notification Stream
Definition: concepts.hpp:123
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:257
uint64_t getFlags() const
const Block & wireEncode() const
encode FaceEventNotification
void wireDecode(const Block &wire)
decode FaceEventNotification
uint64_t getFaceId() const
Definition: face-traits.hpp:51
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
LinkType getLinkType() const
EncodingImpl< EstimatorTag > EncodingEstimator