blob: 3422ee84ad84157062e6de05dd1f1e678e830f11 [file] [log] [blame]
Chengyu Fan36dca992014-09-25 13:42:03 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7357ef22016-09-07 02:39:37 +00003 * Copyright (c) 2013-2016 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/tlv-nfd.hpp"
24#include "encoding/block-helpers.hpp"
25#include "util/concepts.hpp"
Chengyu Fan36dca992014-09-25 13:42:03 -060026
27namespace ndn {
28namespace nfd {
Junxiao Shi65f1a712014-11-20 14:59:36 -070029
30//BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceEventNotification>));
31BOOST_CONCEPT_ASSERT((WireEncodable<FaceEventNotification>));
32BOOST_CONCEPT_ASSERT((WireDecodable<FaceEventNotification>));
33static_assert(std::is_base_of<tlv::Error, FaceEventNotification::Error>::value,
34 "FaceEventNotification::Error must inherit from tlv::Error");
35
Chengyu Fan36dca992014-09-25 13:42:03 -060036FaceEventNotification::FaceEventNotification()
Junxiao Shi65f1a712014-11-20 14:59:36 -070037 : m_kind(static_cast<FaceEventKind>(0))
Chengyu Fan36dca992014-09-25 13:42:03 -060038{
39}
40
41FaceEventNotification::FaceEventNotification(const Block& block)
42{
43 this->wireDecode(block);
44}
45
Alexander Afanasyev74633892015-02-08 18:08:46 -080046template<encoding::Tag TAG>
Chengyu Fan36dca992014-09-25 13:42:03 -060047size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080048FaceEventNotification::wireEncode(EncodingImpl<TAG>& encoder) const
Chengyu Fan36dca992014-09-25 13:42:03 -060049{
50 size_t totalLength = 0;
51
52 totalLength += prependNonNegativeIntegerBlock(encoder,
53 tlv::nfd::LinkType, m_linkType);
54 totalLength += prependNonNegativeIntegerBlock(encoder,
55 tlv::nfd::FacePersistency, m_facePersistency);
56 totalLength += prependNonNegativeIntegerBlock(encoder,
57 tlv::nfd::FaceScope, m_faceScope);
Alexander Afanasyev74633892015-02-08 18:08:46 -080058 totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
Chengyu Fan36dca992014-09-25 13:42:03 -060059 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
Alexander Afanasyev74633892015-02-08 18:08:46 -080060 totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
Chengyu Fan36dca992014-09-25 13:42:03 -060061 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
62 totalLength += prependNonNegativeIntegerBlock(encoder,
63 tlv::nfd::FaceId, m_faceId);
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
72const Block&
73FaceEventNotification::wireEncode() const
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
88void
89FaceEventNotification::wireDecode(const Block& block)
90{
91 if (block.type() != tlv::nfd::FaceEventNotification) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070092 BOOST_THROW_EXCEPTION(Error("expecting FaceEventNotification block"));
Chengyu Fan36dca992014-09-25 13:42:03 -060093 }
94 m_wire = block;
95 m_wire.parse();
96 Block::element_const_iterator val = m_wire.elements_begin();
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 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700103 BOOST_THROW_EXCEPTION(Error("missing required FaceEventKind field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600104 }
105
106 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
107 m_faceId = readNonNegativeInteger(*val);
108 ++val;
109 }
110 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700111 BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600112 }
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 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700119 BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600120 }
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 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700127 BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600128 }
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 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700135 BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600136 }
137
138 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
139 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
140 ++val;
141 }
142 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700143 BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600144 }
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 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700151 BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600152 }
153}
154
155FaceEventNotification&
156FaceEventNotification::setKind(FaceEventKind kind)
157{
158 m_wire.reset();
159 m_kind = kind;
160 return *this;
161}
162
163void
164FaceEventNotification::wireReset() const
165{
166 m_wire.reset();
167}
168
169std::ostream&
170operator<<(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;
179 case FACE_EVENT_DESTROYED:
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