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