blob: 18b12d19047a1e6b43447afd72fc6054daa868e4 [file] [log] [blame]
Chengyu Fan36dca992014-09-25 13:42:03 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
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
22#include "nfd-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
46template<bool T>
47size_t
48FaceEventNotification::wireEncode(EncodingImpl<T>& encoder) const
49{
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);
58 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
59 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
60 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
61 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) {
92 throw Error("expecting FaceEventNotification block");
93 }
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 {
103 throw Error("missing required FaceEventKind field");
104 }
105
106 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
107 m_faceId = readNonNegativeInteger(*val);
108 ++val;
109 }
110 else {
111 throw Error("missing required FaceId field");
112 }
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 {
119 throw Error("missing required Uri field");
120 }
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 {
127 throw Error("missing required LocalUri field");
128 }
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 {
135 throw Error("missing required FaceScope field");
136 }
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 {
143 throw Error("missing required FacePersistency field");
144 }
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 {
151 throw Error("missing required LinkType field");
152 }
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