Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 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/tlv-nfd.hpp" |
| 24 | #include "encoding/block-helpers.hpp" |
| 25 | #include "util/concepts.hpp" |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 29 | |
| 30 | //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceEventNotification>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireEncodable<FaceEventNotification>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireDecodable<FaceEventNotification>)); |
| 33 | static_assert(std::is_base_of<tlv::Error, FaceEventNotification::Error>::value, |
| 34 | "FaceEventNotification::Error must inherit from tlv::Error"); |
| 35 | |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 36 | FaceEventNotification::FaceEventNotification() |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 37 | : m_kind(static_cast<FaceEventKind>(0)) |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
| 41 | FaceEventNotification::FaceEventNotification(const Block& block) |
| 42 | { |
| 43 | this->wireDecode(block); |
| 44 | } |
| 45 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 46 | template<encoding::Tag TAG> |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 47 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 48 | FaceEventNotification::wireEncode(EncodingImpl<TAG>& encoder) const |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 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); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 58 | totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri, |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 59 | reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size()); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 60 | totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri, |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 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 | |
| 72 | const Block& |
| 73 | FaceEventNotification::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 | |
| 88 | void |
| 89 | FaceEventNotification::wireDecode(const Block& block) |
| 90 | { |
| 91 | if (block.type() != tlv::nfd::FaceEventNotification) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 92 | BOOST_THROW_EXCEPTION(Error("expecting FaceEventNotification block")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 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 { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 103 | BOOST_THROW_EXCEPTION(Error("missing required FaceEventKind field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) { |
| 107 | m_faceId = readNonNegativeInteger(*val); |
| 108 | ++val; |
| 109 | } |
| 110 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 111 | BOOST_THROW_EXCEPTION(Error("missing required FaceId field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 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 { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 119 | BOOST_THROW_EXCEPTION(Error("missing required Uri field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 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 { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 127 | BOOST_THROW_EXCEPTION(Error("missing required LocalUri field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 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 { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 135 | BOOST_THROW_EXCEPTION(Error("missing required FaceScope field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 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 { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 143 | BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 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 { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 151 | BOOST_THROW_EXCEPTION(Error("missing required LinkType field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | FaceEventNotification& |
| 156 | FaceEventNotification::setKind(FaceEventKind kind) |
| 157 | { |
| 158 | m_wire.reset(); |
| 159 | m_kind = kind; |
| 160 | return *this; |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | FaceEventNotification::wireReset() const |
| 165 | { |
| 166 | m_wire.reset(); |
| 167 | } |
| 168 | |
| 169 | std::ostream& |
| 170 | operator<<(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 |