Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 0a1afdf | 2017-02-18 16:34:00 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 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 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 22 | #include "face-event-notification.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 23 | #include "encoding/block-helpers.hpp" |
Davide Pesavento | 0a1afdf | 2017-02-18 16:34:00 -0500 | [diff] [blame] | 24 | #include "encoding/encoding-buffer.hpp" |
| 25 | #include "encoding/tlv-nfd.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 26 | #include "util/concepts.hpp" |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 27 | |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 28 | #include <iomanip> |
| 29 | |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace nfd { |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 33 | BOOST_CONCEPT_ASSERT((NotificationStreamItem<FaceEventNotification>)); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 34 | |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 35 | FaceEventNotification::FaceEventNotification() |
Davide Pesavento | 0a1afdf | 2017-02-18 16:34:00 -0500 | [diff] [blame] | 36 | : m_kind(FACE_EVENT_NONE) |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
| 40 | FaceEventNotification::FaceEventNotification(const Block& block) |
| 41 | { |
| 42 | this->wireDecode(block); |
| 43 | } |
| 44 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 45 | template<encoding::Tag TAG> |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 46 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 47 | FaceEventNotification::wireEncode(EncodingImpl<TAG>& encoder) const |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 48 | { |
| 49 | size_t totalLength = 0; |
| 50 | |
| 51 | totalLength += prependNonNegativeIntegerBlock(encoder, |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame] | 52 | tlv::nfd::Flags, m_flags); |
| 53 | totalLength += prependNonNegativeIntegerBlock(encoder, |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 54 | tlv::nfd::LinkType, m_linkType); |
| 55 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 56 | tlv::nfd::FacePersistency, m_facePersistency); |
| 57 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 58 | tlv::nfd::FaceScope, m_faceScope); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 59 | totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri, |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 60 | reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size()); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 61 | totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri, |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 62 | reinterpret_cast<const uint8_t*>(m_remoteUri.data()), m_remoteUri.size()); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 63 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 64 | tlv::nfd::FaceId, m_faceId); |
| 65 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 66 | tlv::nfd::FaceEventKind, m_kind); |
| 67 | |
| 68 | totalLength += encoder.prependVarNumber(totalLength); |
| 69 | totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification); |
| 70 | return totalLength; |
| 71 | } |
| 72 | |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 73 | template size_t |
| 74 | FaceEventNotification::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const; |
| 75 | |
| 76 | template size_t |
| 77 | FaceEventNotification::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const; |
| 78 | |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 79 | const Block& |
| 80 | FaceEventNotification::wireEncode() const |
| 81 | { |
| 82 | if (m_wire.hasWire()) |
| 83 | return m_wire; |
| 84 | |
| 85 | EncodingEstimator estimator; |
| 86 | size_t estimatedSize = wireEncode(estimator); |
| 87 | |
| 88 | EncodingBuffer buffer(estimatedSize, 0); |
| 89 | wireEncode(buffer); |
| 90 | |
| 91 | m_wire = buffer.block(); |
| 92 | return m_wire; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | FaceEventNotification::wireDecode(const Block& block) |
| 97 | { |
| 98 | if (block.type() != tlv::nfd::FaceEventNotification) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 99 | BOOST_THROW_EXCEPTION(Error("expecting FaceEventNotification block")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 100 | } |
| 101 | m_wire = block; |
| 102 | m_wire.parse(); |
| 103 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 104 | |
| 105 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) { |
| 106 | m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val)); |
| 107 | ++val; |
| 108 | } |
| 109 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 110 | BOOST_THROW_EXCEPTION(Error("missing required FaceEventKind field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) { |
| 114 | m_faceId = readNonNegativeInteger(*val); |
| 115 | ++val; |
| 116 | } |
| 117 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 118 | BOOST_THROW_EXCEPTION(Error("missing required FaceId field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) { |
| 122 | m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 123 | ++val; |
| 124 | } |
| 125 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 126 | BOOST_THROW_EXCEPTION(Error("missing required Uri field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) { |
| 130 | m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 131 | ++val; |
| 132 | } |
| 133 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 134 | BOOST_THROW_EXCEPTION(Error("missing required LocalUri field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) { |
| 138 | m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val)); |
| 139 | ++val; |
| 140 | } |
| 141 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 142 | BOOST_THROW_EXCEPTION(Error("missing required FaceScope field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) { |
| 146 | m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val)); |
| 147 | ++val; |
| 148 | } |
| 149 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 150 | BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) { |
| 154 | m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val)); |
| 155 | ++val; |
| 156 | } |
| 157 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 158 | BOOST_THROW_EXCEPTION(Error("missing required LinkType field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 159 | } |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame] | 160 | |
| 161 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) { |
| 162 | m_flags = readNonNegativeInteger(*val); |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 163 | ++val; |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame] | 164 | } |
| 165 | else { |
| 166 | BOOST_THROW_EXCEPTION(Error("missing required Flags field")); |
| 167 | } |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | FaceEventNotification& |
| 171 | FaceEventNotification::setKind(FaceEventKind kind) |
| 172 | { |
| 173 | m_wire.reset(); |
| 174 | m_kind = kind; |
| 175 | return *this; |
| 176 | } |
| 177 | |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 178 | bool |
| 179 | operator==(const FaceEventNotification& a, const FaceEventNotification& b) |
| 180 | { |
| 181 | return a.getFaceId() == b.getFaceId() && |
| 182 | a.getRemoteUri() == b.getRemoteUri() && |
| 183 | a.getLocalUri() == b.getLocalUri() && |
| 184 | a.getFaceScope() == b.getFaceScope() && |
| 185 | a.getFacePersistency() == b.getFacePersistency() && |
| 186 | a.getLinkType() == b.getLinkType() && |
| 187 | a.getFlags() == b.getFlags() && |
| 188 | a.getKind() == b.getKind(); |
| 189 | } |
| 190 | |
| 191 | std::ostream& |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 192 | operator<<(std::ostream& os, const FaceEventNotification& notification) |
| 193 | { |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 194 | os << "FaceEvent(Kind: " << notification.getKind() << ",\n" |
| 195 | << " FaceId: " << notification.getFaceId() << ",\n" |
| 196 | << " RemoteUri: " << notification.getRemoteUri() << ",\n" |
| 197 | << " LocalUri: " << notification.getLocalUri() << ",\n" |
| 198 | << " FaceScope: " << notification.getFaceScope() << ",\n" |
| 199 | << " FacePersistency: " << notification.getFacePersistency() << ",\n" |
| 200 | << " LinkType: " << notification.getLinkType() << ",\n"; |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame] | 201 | |
| 202 | auto osFlags = os.flags(); |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 203 | // std::showbase doesn't work with number 0 |
| 204 | os << " Flags: 0x" << std::noshowbase << std::noshowpos << std::nouppercase |
| 205 | << std::hex << notification.getFlags() << "\n"; |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame] | 206 | os.flags(osFlags); |
| 207 | |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 208 | return os << " )"; |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | } // namespace nfd |
| 212 | } // namespace ndn |