blob: 505026b709821b16065a32b9377386841351d43f [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
Davide Pesavento88eb7482017-02-18 19:25:58 -050028#include <iomanip>
29
Chengyu Fan36dca992014-09-25 13:42:03 -060030namespace ndn {
31namespace nfd {
Junxiao Shi65f1a712014-11-20 14:59:36 -070032
Davide Pesavento88eb7482017-02-18 19:25:58 -050033BOOST_CONCEPT_ASSERT((NotificationStreamItem<FaceEventNotification>));
Junxiao Shi65f1a712014-11-20 14:59:36 -070034
Chengyu Fan36dca992014-09-25 13:42:03 -060035FaceEventNotification::FaceEventNotification()
Davide Pesavento0a1afdf2017-02-18 16:34:00 -050036 : m_kind(FACE_EVENT_NONE)
Chengyu Fan36dca992014-09-25 13:42:03 -060037{
38}
39
40FaceEventNotification::FaceEventNotification(const Block& block)
41{
42 this->wireDecode(block);
43}
44
Alexander Afanasyev74633892015-02-08 18:08:46 -080045template<encoding::Tag TAG>
Chengyu Fan36dca992014-09-25 13:42:03 -060046size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080047FaceEventNotification::wireEncode(EncodingImpl<TAG>& encoder) const
Chengyu Fan36dca992014-09-25 13:42:03 -060048{
49 size_t totalLength = 0;
50
51 totalLength += prependNonNegativeIntegerBlock(encoder,
Eric Newberry1aa3e1e2016-09-28 20:27:45 -070052 tlv::nfd::Flags, m_flags);
53 totalLength += prependNonNegativeIntegerBlock(encoder,
Chengyu Fan36dca992014-09-25 13:42:03 -060054 tlv::nfd::LinkType, m_linkType);
55 totalLength += prependNonNegativeIntegerBlock(encoder,
56 tlv::nfd::FacePersistency, m_facePersistency);
57 totalLength += prependNonNegativeIntegerBlock(encoder,
58 tlv::nfd::FaceScope, m_faceScope);
Alexander Afanasyev74633892015-02-08 18:08:46 -080059 totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
Davide Pesavento88eb7482017-02-18 19:25:58 -050060 reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size());
Alexander Afanasyev74633892015-02-08 18:08:46 -080061 totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
Davide Pesavento88eb7482017-02-18 19:25:58 -050062 reinterpret_cast<const uint8_t*>(m_remoteUri.data()), m_remoteUri.size());
Chengyu Fan36dca992014-09-25 13:42:03 -060063 totalLength += prependNonNegativeIntegerBlock(encoder,
64 tlv::nfd::FaceId, m_faceId);
65 totalLength += prependNonNegativeIntegerBlock(encoder,
66 tlv::nfd::FaceEventKind, m_kind);
67
68 totalLength += encoder.prependVarNumber(totalLength);
69 totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
70 return totalLength;
71}
72
Davide Pesavento88eb7482017-02-18 19:25:58 -050073template size_t
74FaceEventNotification::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
75
76template size_t
77FaceEventNotification::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
78
Chengyu Fan36dca992014-09-25 13:42:03 -060079const Block&
80FaceEventNotification::wireEncode() const
81{
82 if (m_wire.hasWire())
83 return m_wire;
84
85 EncodingEstimator estimator;
86 size_t estimatedSize = wireEncode(estimator);
87
88 EncodingBuffer buffer(estimatedSize, 0);
89 wireEncode(buffer);
90
91 m_wire = buffer.block();
92 return m_wire;
93}
94
95void
96FaceEventNotification::wireDecode(const Block& block)
97{
98 if (block.type() != tlv::nfd::FaceEventNotification) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070099 BOOST_THROW_EXCEPTION(Error("expecting FaceEventNotification block"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600100 }
101 m_wire = block;
102 m_wire.parse();
103 Block::element_const_iterator val = m_wire.elements_begin();
104
105 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
106 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
107 ++val;
108 }
109 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700110 BOOST_THROW_EXCEPTION(Error("missing required FaceEventKind field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600111 }
112
113 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
114 m_faceId = readNonNegativeInteger(*val);
115 ++val;
116 }
117 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700118 BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600119 }
120
121 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
122 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
123 ++val;
124 }
125 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700126 BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600127 }
128
129 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
130 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
131 ++val;
132 }
133 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700134 BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600135 }
136
137 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
138 m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
139 ++val;
140 }
141 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700142 BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600143 }
144
145 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
146 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
147 ++val;
148 }
149 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700150 BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600151 }
152
153 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
154 m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
155 ++val;
156 }
157 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700158 BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600159 }
Eric Newberry1aa3e1e2016-09-28 20:27:45 -0700160
161 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
162 m_flags = readNonNegativeInteger(*val);
Davide Pesavento88eb7482017-02-18 19:25:58 -0500163 ++val;
Eric Newberry1aa3e1e2016-09-28 20:27:45 -0700164 }
165 else {
166 BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
167 }
Chengyu Fan36dca992014-09-25 13:42:03 -0600168}
169
170FaceEventNotification&
171FaceEventNotification::setKind(FaceEventKind kind)
172{
173 m_wire.reset();
174 m_kind = kind;
175 return *this;
176}
177
Davide Pesavento88eb7482017-02-18 19:25:58 -0500178bool
179operator==(const FaceEventNotification& a, const FaceEventNotification& b)
180{
181 return a.getFaceId() == b.getFaceId() &&
182 a.getRemoteUri() == b.getRemoteUri() &&
183 a.getLocalUri() == b.getLocalUri() &&
184 a.getFaceScope() == b.getFaceScope() &&
185 a.getFacePersistency() == b.getFacePersistency() &&
186 a.getLinkType() == b.getLinkType() &&
187 a.getFlags() == b.getFlags() &&
188 a.getKind() == b.getKind();
189}
190
191std::ostream&
Chengyu Fan36dca992014-09-25 13:42:03 -0600192operator<<(std::ostream& os, const FaceEventNotification& notification)
193{
Davide Pesavento88eb7482017-02-18 19:25:58 -0500194 os << "FaceEvent(Kind: " << notification.getKind() << ",\n"
195 << " FaceId: " << notification.getFaceId() << ",\n"
196 << " RemoteUri: " << notification.getRemoteUri() << ",\n"
197 << " LocalUri: " << notification.getLocalUri() << ",\n"
198 << " FaceScope: " << notification.getFaceScope() << ",\n"
199 << " FacePersistency: " << notification.getFacePersistency() << ",\n"
200 << " LinkType: " << notification.getLinkType() << ",\n";
Eric Newberry1aa3e1e2016-09-28 20:27:45 -0700201
202 auto osFlags = os.flags();
Davide Pesavento88eb7482017-02-18 19:25:58 -0500203 // std::showbase doesn't work with number 0
204 os << " Flags: 0x" << std::noshowbase << std::noshowpos << std::nouppercase
205 << std::hex << notification.getFlags() << "\n";
Eric Newberry1aa3e1e2016-09-28 20:27:45 -0700206 os.flags(osFlags);
207
Davide Pesavento88eb7482017-02-18 19:25:58 -0500208 return os << " )";
Chengyu Fan36dca992014-09-25 13:42:03 -0600209}
210
211} // namespace nfd
212} // namespace ndn