blob: 79a94f2c010ce8780ce2b57850e5a0f8fae4a590 [file] [log] [blame]
Chengyu Fan36dca992014-09-25 13:42:03 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5d75fd92017-08-08 18:09:20 +00002/*
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"
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050027#include "util/string-helper.hpp"
Davide Pesavento88eb7482017-02-18 19:25:58 -050028
Chengyu Fan36dca992014-09-25 13:42:03 -060029namespace ndn {
30namespace nfd {
Junxiao Shi65f1a712014-11-20 14:59:36 -070031
Davide Pesavento88eb7482017-02-18 19:25:58 -050032BOOST_CONCEPT_ASSERT((NotificationStreamItem<FaceEventNotification>));
Junxiao Shi65f1a712014-11-20 14:59:36 -070033
Chengyu Fan36dca992014-09-25 13:42:03 -060034FaceEventNotification::FaceEventNotification()
Davide Pesavento0a1afdf2017-02-18 16:34:00 -050035 : m_kind(FACE_EVENT_NONE)
Chengyu Fan36dca992014-09-25 13:42:03 -060036{
37}
38
39FaceEventNotification::FaceEventNotification(const Block& block)
40{
41 this->wireDecode(block);
42}
43
Alexander Afanasyev74633892015-02-08 18:08:46 -080044template<encoding::Tag TAG>
Chengyu Fan36dca992014-09-25 13:42:03 -060045size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080046FaceEventNotification::wireEncode(EncodingImpl<TAG>& encoder) const
Chengyu Fan36dca992014-09-25 13:42:03 -060047{
48 size_t totalLength = 0;
49
Junxiao Shi5d75fd92017-08-08 18:09:20 +000050 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
51 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::LinkType, m_linkType);
52 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FacePersistency, m_facePersistency);
53 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceScope, m_faceScope);
54 totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
55 totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
56 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
57 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceEventKind, m_kind);
Chengyu Fan36dca992014-09-25 13:42:03 -060058
59 totalLength += encoder.prependVarNumber(totalLength);
60 totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
61 return totalLength;
62}
63
Davide Pesavento88eb7482017-02-18 19:25:58 -050064template size_t
65FaceEventNotification::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
66
67template size_t
68FaceEventNotification::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
69
Chengyu Fan36dca992014-09-25 13:42:03 -060070const Block&
71FaceEventNotification::wireEncode() const
72{
73 if (m_wire.hasWire())
74 return m_wire;
75
76 EncodingEstimator estimator;
77 size_t estimatedSize = wireEncode(estimator);
78
79 EncodingBuffer buffer(estimatedSize, 0);
80 wireEncode(buffer);
81
82 m_wire = buffer.block();
83 return m_wire;
84}
85
86void
87FaceEventNotification::wireDecode(const Block& block)
88{
89 if (block.type() != tlv::nfd::FaceEventNotification) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070090 BOOST_THROW_EXCEPTION(Error("expecting FaceEventNotification block"));
Chengyu Fan36dca992014-09-25 13:42:03 -060091 }
92 m_wire = block;
93 m_wire.parse();
94 Block::element_const_iterator val = m_wire.elements_begin();
95
96 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +000097 m_kind = readNonNegativeIntegerAs<FaceEventKind>(*val);
Chengyu Fan36dca992014-09-25 13:42:03 -060098 ++val;
99 }
100 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700101 BOOST_THROW_EXCEPTION(Error("missing required FaceEventKind field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600102 }
103
104 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
105 m_faceId = readNonNegativeInteger(*val);
106 ++val;
107 }
108 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700109 BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600110 }
111
112 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000113 m_remoteUri = readString(*val);
Chengyu Fan36dca992014-09-25 13:42:03 -0600114 ++val;
115 }
116 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700117 BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600118 }
119
120 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000121 m_localUri = readString(*val);
Chengyu Fan36dca992014-09-25 13:42:03 -0600122 ++val;
123 }
124 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700125 BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600126 }
127
128 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000129 m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
Chengyu Fan36dca992014-09-25 13:42:03 -0600130 ++val;
131 }
132 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700133 BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600134 }
135
136 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000137 m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
Chengyu Fan36dca992014-09-25 13:42:03 -0600138 ++val;
139 }
140 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700141 BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600142 }
143
144 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000145 m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
Chengyu Fan36dca992014-09-25 13:42:03 -0600146 ++val;
147 }
148 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700149 BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600150 }
Eric Newberry1aa3e1e2016-09-28 20:27:45 -0700151
152 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
153 m_flags = readNonNegativeInteger(*val);
Davide Pesavento88eb7482017-02-18 19:25:58 -0500154 ++val;
Eric Newberry1aa3e1e2016-09-28 20:27:45 -0700155 }
156 else {
157 BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
158 }
Chengyu Fan36dca992014-09-25 13:42:03 -0600159}
160
161FaceEventNotification&
162FaceEventNotification::setKind(FaceEventKind kind)
163{
164 m_wire.reset();
165 m_kind = kind;
166 return *this;
167}
168
Davide Pesavento88eb7482017-02-18 19:25:58 -0500169bool
170operator==(const FaceEventNotification& a, const FaceEventNotification& b)
171{
172 return a.getFaceId() == b.getFaceId() &&
173 a.getRemoteUri() == b.getRemoteUri() &&
174 a.getLocalUri() == b.getLocalUri() &&
175 a.getFaceScope() == b.getFaceScope() &&
176 a.getFacePersistency() == b.getFacePersistency() &&
177 a.getLinkType() == b.getLinkType() &&
178 a.getFlags() == b.getFlags() &&
179 a.getKind() == b.getKind();
180}
181
182std::ostream&
Chengyu Fan36dca992014-09-25 13:42:03 -0600183operator<<(std::ostream& os, const FaceEventNotification& notification)
184{
Davide Pesavento88eb7482017-02-18 19:25:58 -0500185 os << "FaceEvent(Kind: " << notification.getKind() << ",\n"
186 << " FaceId: " << notification.getFaceId() << ",\n"
187 << " RemoteUri: " << notification.getRemoteUri() << ",\n"
188 << " LocalUri: " << notification.getLocalUri() << ",\n"
189 << " FaceScope: " << notification.getFaceScope() << ",\n"
190 << " FacePersistency: " << notification.getFacePersistency() << ",\n"
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500191 << " LinkType: " << notification.getLinkType() << ",\n"
192 << " Flags: " << AsHex{notification.getFlags()} << "\n";
Eric Newberry1aa3e1e2016-09-28 20:27:45 -0700193
Davide Pesavento88eb7482017-02-18 19:25:58 -0500194 return os << " )";
Chengyu Fan36dca992014-09-25 13:42:03 -0600195}
196
197} // namespace nfd
198} // namespace ndn