Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 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 | |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 50 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags); |
| 51 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::LinkType, m_linkType); |
| 52 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FacePersistency, m_facePersistency); |
| 53 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceScope, m_faceScope); |
| 54 | totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri); |
| 55 | totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri); |
| 56 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId); |
| 57 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceEventKind, m_kind); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 58 | |
| 59 | totalLength += encoder.prependVarNumber(totalLength); |
| 60 | totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification); |
| 61 | return totalLength; |
| 62 | } |
| 63 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame^] | 64 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(FaceEventNotification); |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 65 | |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 66 | const Block& |
| 67 | FaceEventNotification::wireEncode() const |
| 68 | { |
| 69 | if (m_wire.hasWire()) |
| 70 | return m_wire; |
| 71 | |
| 72 | EncodingEstimator estimator; |
| 73 | size_t estimatedSize = wireEncode(estimator); |
| 74 | |
| 75 | EncodingBuffer buffer(estimatedSize, 0); |
| 76 | wireEncode(buffer); |
| 77 | |
| 78 | m_wire = buffer.block(); |
| 79 | return m_wire; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | FaceEventNotification::wireDecode(const Block& block) |
| 84 | { |
| 85 | if (block.type() != tlv::nfd::FaceEventNotification) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 86 | BOOST_THROW_EXCEPTION(Error("expecting FaceEventNotification block")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 87 | } |
| 88 | m_wire = block; |
| 89 | m_wire.parse(); |
| 90 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 91 | |
| 92 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 93 | m_kind = readNonNegativeIntegerAs<FaceEventKind>(*val); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 94 | ++val; |
| 95 | } |
| 96 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 97 | BOOST_THROW_EXCEPTION(Error("missing required FaceEventKind field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) { |
| 101 | m_faceId = 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 FaceId 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::Uri) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 109 | m_remoteUri = readString(*val); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 110 | ++val; |
| 111 | } |
| 112 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 113 | BOOST_THROW_EXCEPTION(Error("missing required Uri 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::LocalUri) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 117 | m_localUri = readString(*val); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 118 | ++val; |
| 119 | } |
| 120 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 121 | BOOST_THROW_EXCEPTION(Error("missing required LocalUri 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::FaceScope) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 125 | m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 126 | ++val; |
| 127 | } |
| 128 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 129 | BOOST_THROW_EXCEPTION(Error("missing required FaceScope 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::FacePersistency) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 133 | m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 134 | ++val; |
| 135 | } |
| 136 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 137 | BOOST_THROW_EXCEPTION(Error("missing required FacePersistency 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::LinkType) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 141 | m_linkType = readNonNegativeIntegerAs<LinkType>(*val); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 142 | ++val; |
| 143 | } |
| 144 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 145 | BOOST_THROW_EXCEPTION(Error("missing required LinkType field")); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 146 | } |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame] | 147 | |
| 148 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) { |
| 149 | m_flags = readNonNegativeInteger(*val); |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 150 | ++val; |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame] | 151 | } |
| 152 | else { |
| 153 | BOOST_THROW_EXCEPTION(Error("missing required Flags field")); |
| 154 | } |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | FaceEventNotification& |
| 158 | FaceEventNotification::setKind(FaceEventKind kind) |
| 159 | { |
| 160 | m_wire.reset(); |
| 161 | m_kind = kind; |
| 162 | return *this; |
| 163 | } |
| 164 | |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 165 | bool |
| 166 | operator==(const FaceEventNotification& a, const FaceEventNotification& b) |
| 167 | { |
| 168 | return a.getFaceId() == b.getFaceId() && |
| 169 | a.getRemoteUri() == b.getRemoteUri() && |
| 170 | a.getLocalUri() == b.getLocalUri() && |
| 171 | a.getFaceScope() == b.getFaceScope() && |
| 172 | a.getFacePersistency() == b.getFacePersistency() && |
| 173 | a.getLinkType() == b.getLinkType() && |
| 174 | a.getFlags() == b.getFlags() && |
| 175 | a.getKind() == b.getKind(); |
| 176 | } |
| 177 | |
| 178 | std::ostream& |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 179 | operator<<(std::ostream& os, const FaceEventNotification& notification) |
| 180 | { |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 181 | os << "FaceEvent(Kind: " << notification.getKind() << ",\n" |
| 182 | << " FaceId: " << notification.getFaceId() << ",\n" |
| 183 | << " RemoteUri: " << notification.getRemoteUri() << ",\n" |
| 184 | << " LocalUri: " << notification.getLocalUri() << ",\n" |
| 185 | << " FaceScope: " << notification.getFaceScope() << ",\n" |
| 186 | << " FacePersistency: " << notification.getFacePersistency() << ",\n" |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 187 | << " LinkType: " << notification.getLinkType() << ",\n" |
| 188 | << " Flags: " << AsHex{notification.getFlags()} << "\n"; |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame] | 189 | |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 190 | return os << " )"; |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | } // namespace nfd |
| 194 | } // namespace ndn |