blob: d459de401f49e21730ed00cb92b1459e85545e2b [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"
23
24namespace ndn {
25namespace nfd {
26FaceEventNotification::FaceEventNotification()
27 : FaceTraits()
28 , m_kind(static_cast<FaceEventKind>(0))
29{
30}
31
32FaceEventNotification::FaceEventNotification(const Block& block)
33{
34 this->wireDecode(block);
35}
36
37template<bool T>
38size_t
39FaceEventNotification::wireEncode(EncodingImpl<T>& encoder) const
40{
41 size_t totalLength = 0;
42
43 totalLength += prependNonNegativeIntegerBlock(encoder,
44 tlv::nfd::LinkType, m_linkType);
45 totalLength += prependNonNegativeIntegerBlock(encoder,
46 tlv::nfd::FacePersistency, m_facePersistency);
47 totalLength += prependNonNegativeIntegerBlock(encoder,
48 tlv::nfd::FaceScope, m_faceScope);
49 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
50 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
51 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
52 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
53 totalLength += prependNonNegativeIntegerBlock(encoder,
54 tlv::nfd::FaceId, m_faceId);
55 totalLength += prependNonNegativeIntegerBlock(encoder,
56 tlv::nfd::FaceEventKind, m_kind);
57
58 totalLength += encoder.prependVarNumber(totalLength);
59 totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification);
60 return totalLength;
61}
62
63const Block&
64FaceEventNotification::wireEncode() const
65{
66 if (m_wire.hasWire())
67 return m_wire;
68
69 EncodingEstimator estimator;
70 size_t estimatedSize = wireEncode(estimator);
71
72 EncodingBuffer buffer(estimatedSize, 0);
73 wireEncode(buffer);
74
75 m_wire = buffer.block();
76 return m_wire;
77}
78
79void
80FaceEventNotification::wireDecode(const Block& block)
81{
82 if (block.type() != tlv::nfd::FaceEventNotification) {
83 throw Error("expecting FaceEventNotification block");
84 }
85 m_wire = block;
86 m_wire.parse();
87 Block::element_const_iterator val = m_wire.elements_begin();
88
89 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) {
90 m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val));
91 ++val;
92 }
93 else {
94 throw Error("missing required FaceEventKind field");
95 }
96
97 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
98 m_faceId = readNonNegativeInteger(*val);
99 ++val;
100 }
101 else {
102 throw Error("missing required FaceId field");
103 }
104
105 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
106 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
107 ++val;
108 }
109 else {
110 throw Error("missing required Uri field");
111 }
112
113 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
114 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
115 ++val;
116 }
117 else {
118 throw Error("missing required LocalUri field");
119 }
120
121 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
122 m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
123 ++val;
124 }
125 else {
126 throw Error("missing required FaceScope field");
127 }
128
129 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
130 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
131 ++val;
132 }
133 else {
134 throw Error("missing required FacePersistency field");
135 }
136
137 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
138 m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
139 ++val;
140 }
141 else {
142 throw Error("missing required LinkType field");
143 }
144}
145
146FaceEventNotification&
147FaceEventNotification::setKind(FaceEventKind kind)
148{
149 m_wire.reset();
150 m_kind = kind;
151 return *this;
152}
153
154void
155FaceEventNotification::wireReset() const
156{
157 m_wire.reset();
158}
159
160std::ostream&
161operator<<(std::ostream& os, const FaceEventNotification& notification)
162{
163 os << "FaceEventNotification(";
164
165 switch (notification.getKind())
166 {
167 case FACE_EVENT_CREATED:
168 os << "Kind: created, ";
169 break;
170 case FACE_EVENT_DESTROYED:
171 os << "Kind: destroyed, ";
172 break;
173 }
174
175 os << "FaceID: " << notification.getFaceId() << ", "
176 << "RemoteUri: " << notification.getRemoteUri() << ", "
177 << "LocalUri: " << notification.getLocalUri() << ", "
178 << "FaceScope: " << notification.getFaceScope() << ", "
179 << "FacePersistency: " << notification.getFacePersistency() << ", "
180 << "LinkType: " << notification.getLinkType()
181 << ")";
182 return os;
183}
184
185} // namespace nfd
186} // namespace ndn