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