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