blob: a322ae997bbca21425abaf373b91b3f3bb287815 [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,
Eric Newberry1aa3e1e2016-09-28 20:27:45 -070053 tlv::nfd::Flags, m_flags);
54 totalLength += prependNonNegativeIntegerBlock(encoder,
Chengyu Fan36dca992014-09-25 13:42:03 -060055 tlv::nfd::LinkType, m_linkType);
56 totalLength += prependNonNegativeIntegerBlock(encoder,
57 tlv::nfd::FacePersistency, m_facePersistency);
58 totalLength += prependNonNegativeIntegerBlock(encoder,
59 tlv::nfd::FaceScope, m_faceScope);
Alexander Afanasyev74633892015-02-08 18:08:46 -080060 totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
Chengyu Fan36dca992014-09-25 13:42:03 -060061 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
Alexander Afanasyev74633892015-02-08 18:08:46 -080062 totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
Chengyu Fan36dca992014-09-25 13:42:03 -060063 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
64 totalLength += prependNonNegativeIntegerBlock(encoder,
65 tlv::nfd::FaceId, m_faceId);
66 totalLength += prependNonNegativeIntegerBlock(encoder,
67 tlv::nfd::FaceEventKind, m_kind);
68
69 totalLength += encoder.prependVarNumber(totalLength);
70 totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
71 return totalLength;
72}
73
74const Block&
75FaceEventNotification::wireEncode() const
76{
77 if (m_wire.hasWire())
78 return m_wire;
79
80 EncodingEstimator estimator;
81 size_t estimatedSize = wireEncode(estimator);
82
83 EncodingBuffer buffer(estimatedSize, 0);
84 wireEncode(buffer);
85
86 m_wire = buffer.block();
87 return m_wire;
88}
89
90void
91FaceEventNotification::wireDecode(const Block& block)
92{
93 if (block.type() != tlv::nfd::FaceEventNotification) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070094 BOOST_THROW_EXCEPTION(Error("expecting FaceEventNotification block"));
Chengyu Fan36dca992014-09-25 13:42:03 -060095 }
96 m_wire = block;
97 m_wire.parse();
98 Block::element_const_iterator val = m_wire.elements_begin();
99
100 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
101 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
102 ++val;
103 }
104 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700105 BOOST_THROW_EXCEPTION(Error("missing required FaceEventKind field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600106 }
107
108 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
109 m_faceId = readNonNegativeInteger(*val);
110 ++val;
111 }
112 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700113 BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600114 }
115
116 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
117 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
118 ++val;
119 }
120 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700121 BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600122 }
123
124 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
125 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
126 ++val;
127 }
128 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700129 BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600130 }
131
132 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
133 m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
134 ++val;
135 }
136 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700137 BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600138 }
139
140 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
141 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
142 ++val;
143 }
144 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700145 BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600146 }
147
148 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
149 m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
150 ++val;
151 }
152 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700153 BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600154 }
Eric Newberry1aa3e1e2016-09-28 20:27:45 -0700155
156 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
157 m_flags = readNonNegativeInteger(*val);
158 }
159 else {
160 BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
161 }
Chengyu Fan36dca992014-09-25 13:42:03 -0600162}
163
164FaceEventNotification&
165FaceEventNotification::setKind(FaceEventKind kind)
166{
167 m_wire.reset();
168 m_kind = kind;
169 return *this;
170}
171
172void
173FaceEventNotification::wireReset() const
174{
175 m_wire.reset();
176}
177
178std::ostream&
179operator<<(std::ostream& os, const FaceEventNotification& notification)
180{
181 os << "FaceEventNotification(";
182
183 switch (notification.getKind())
184 {
185 case FACE_EVENT_CREATED:
186 os << "Kind: created, ";
187 break;
188 case FACE_EVENT_DESTROYED:
189 os << "Kind: destroyed, ";
190 break;
Eric Newberryf767eba2016-10-07 13:37:02 -0700191 case FACE_EVENT_UP:
192 os << "Kind: up, ";
193 break;
194 case FACE_EVENT_DOWN:
195 os << "Kind: down, ";
196 break;
Chengyu Fan36dca992014-09-25 13:42:03 -0600197 }
198
199 os << "FaceID: " << notification.getFaceId() << ", "
200 << "RemoteUri: " << notification.getRemoteUri() << ", "
201 << "LocalUri: " << notification.getLocalUri() << ", "
202 << "FaceScope: " << notification.getFaceScope() << ", "
203 << "FacePersistency: " << notification.getFacePersistency() << ", "
Eric Newberry1aa3e1e2016-09-28 20:27:45 -0700204 << "LinkType: " << notification.getLinkType() << ", ";
205
206 auto osFlags = os.flags();
207 os << "Flags: " << std::showbase << std::hex << notification.getFlags();
208 os.flags(osFlags);
209
210 os << ")";
Chengyu Fan36dca992014-09-25 13:42:03 -0600211 return os;
212}
213
214} // namespace nfd
215} // namespace ndn