blob: f3ea6550a7325dee540d8521a342bf8454de60b6 [file] [log] [blame]
Chengyu Fan36dca992014-09-25 13:42:03 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento0a1afdf2017-02-18 16:34:00 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Chengyu Fan36dca992014-09-25 13:42:03 -06004 *
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
Junxiao Shi7357ef22016-09-07 02:39:37 +000022#include "face-event-notification.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070023#include "encoding/block-helpers.hpp"
Davide Pesavento0a1afdf2017-02-18 16:34:00 -050024#include "encoding/encoding-buffer.hpp"
25#include "encoding/tlv-nfd.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070026#include "util/concepts.hpp"
Chengyu Fan36dca992014-09-25 13:42:03 -060027
28namespace ndn {
29namespace nfd {
Junxiao Shi65f1a712014-11-20 14:59:36 -070030
31//BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceEventNotification>));
32BOOST_CONCEPT_ASSERT((WireEncodable<FaceEventNotification>));
33BOOST_CONCEPT_ASSERT((WireDecodable<FaceEventNotification>));
34static_assert(std::is_base_of<tlv::Error, FaceEventNotification::Error>::value,
35 "FaceEventNotification::Error must inherit from tlv::Error");
36
Chengyu Fan36dca992014-09-25 13:42:03 -060037FaceEventNotification::FaceEventNotification()
Davide Pesavento0a1afdf2017-02-18 16:34:00 -050038 : m_kind(FACE_EVENT_NONE)
Chengyu Fan36dca992014-09-25 13:42:03 -060039{
40}
41
42FaceEventNotification::FaceEventNotification(const Block& block)
43{
44 this->wireDecode(block);
45}
46
Alexander Afanasyev74633892015-02-08 18:08:46 -080047template<encoding::Tag TAG>
Chengyu Fan36dca992014-09-25 13:42:03 -060048size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080049FaceEventNotification::wireEncode(EncodingImpl<TAG>& encoder) const
Chengyu Fan36dca992014-09-25 13:42:03 -060050{
51 size_t totalLength = 0;
52
53 totalLength += prependNonNegativeIntegerBlock(encoder,
Eric Newberry1aa3e1e2016-09-28 20:27:45 -070054 tlv::nfd::Flags, m_flags);
55 totalLength += prependNonNegativeIntegerBlock(encoder,
Chengyu Fan36dca992014-09-25 13:42:03 -060056 tlv::nfd::LinkType, m_linkType);
57 totalLength += prependNonNegativeIntegerBlock(encoder,
58 tlv::nfd::FacePersistency, m_facePersistency);
59 totalLength += prependNonNegativeIntegerBlock(encoder,
60 tlv::nfd::FaceScope, m_faceScope);
Alexander Afanasyev74633892015-02-08 18:08:46 -080061 totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
Chengyu Fan36dca992014-09-25 13:42:03 -060062 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
Alexander Afanasyev74633892015-02-08 18:08:46 -080063 totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
Chengyu Fan36dca992014-09-25 13:42:03 -060064 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
65 totalLength += prependNonNegativeIntegerBlock(encoder,
66 tlv::nfd::FaceId, m_faceId);
67 totalLength += prependNonNegativeIntegerBlock(encoder,
68 tlv::nfd::FaceEventKind, m_kind);
69
70 totalLength += encoder.prependVarNumber(totalLength);
71 totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
72 return totalLength;
73}
74
75const Block&
76FaceEventNotification::wireEncode() const
77{
78 if (m_wire.hasWire())
79 return m_wire;
80
81 EncodingEstimator estimator;
82 size_t estimatedSize = wireEncode(estimator);
83
84 EncodingBuffer buffer(estimatedSize, 0);
85 wireEncode(buffer);
86
87 m_wire = buffer.block();
88 return m_wire;
89}
90
91void
92FaceEventNotification::wireDecode(const Block& block)
93{
94 if (block.type() != tlv::nfd::FaceEventNotification) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070095 BOOST_THROW_EXCEPTION(Error("expecting FaceEventNotification block"));
Chengyu Fan36dca992014-09-25 13:42:03 -060096 }
97 m_wire = block;
98 m_wire.parse();
99 Block::element_const_iterator val = m_wire.elements_begin();
100
101 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
102 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
103 ++val;
104 }
105 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700106 BOOST_THROW_EXCEPTION(Error("missing required FaceEventKind field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600107 }
108
109 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
110 m_faceId = readNonNegativeInteger(*val);
111 ++val;
112 }
113 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700114 BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600115 }
116
117 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
118 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
119 ++val;
120 }
121 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700122 BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600123 }
124
125 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
126 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
127 ++val;
128 }
129 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700130 BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600131 }
132
133 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
134 m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
135 ++val;
136 }
137 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700138 BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600139 }
140
141 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
142 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
143 ++val;
144 }
145 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700146 BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600147 }
148
149 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
150 m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
151 ++val;
152 }
153 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700154 BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600155 }
Eric Newberry1aa3e1e2016-09-28 20:27:45 -0700156
157 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
158 m_flags = readNonNegativeInteger(*val);
159 }
160 else {
161 BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
162 }
Chengyu Fan36dca992014-09-25 13:42:03 -0600163}
164
165FaceEventNotification&
166FaceEventNotification::setKind(FaceEventKind kind)
167{
168 m_wire.reset();
169 m_kind = kind;
170 return *this;
171}
172
Chengyu Fan36dca992014-09-25 13:42:03 -0600173std::ostream&
174operator<<(std::ostream& os, const FaceEventNotification& notification)
175{
176 os << "FaceEventNotification(";
177
Davide Pesavento0a1afdf2017-02-18 16:34:00 -0500178 switch (notification.getKind()) {
179 case FACE_EVENT_NONE:
180 os << "Kind: none, ";
181 break;
Chengyu Fan36dca992014-09-25 13:42:03 -0600182 case FACE_EVENT_CREATED:
183 os << "Kind: created, ";
184 break;
185 case FACE_EVENT_DESTROYED:
186 os << "Kind: destroyed, ";
187 break;
Eric Newberryf767eba2016-10-07 13:37:02 -0700188 case FACE_EVENT_UP:
189 os << "Kind: up, ";
190 break;
191 case FACE_EVENT_DOWN:
192 os << "Kind: down, ";
193 break;
Davide Pesavento0a1afdf2017-02-18 16:34:00 -0500194 }
Chengyu Fan36dca992014-09-25 13:42:03 -0600195
196 os << "FaceID: " << notification.getFaceId() << ", "
197 << "RemoteUri: " << notification.getRemoteUri() << ", "
198 << "LocalUri: " << notification.getLocalUri() << ", "
199 << "FaceScope: " << notification.getFaceScope() << ", "
200 << "FacePersistency: " << notification.getFacePersistency() << ", "
Eric Newberry1aa3e1e2016-09-28 20:27:45 -0700201 << "LinkType: " << notification.getLinkType() << ", ";
202
203 auto osFlags = os.flags();
204 os << "Flags: " << std::showbase << std::hex << notification.getFlags();
205 os.flags(osFlags);
206
207 os << ")";
Chengyu Fan36dca992014-09-25 13:42:03 -0600208 return os;
209}
210
211} // namespace nfd
212} // namespace ndn