Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP |
| 23 | #define NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP |
| 24 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 25 | // This include must be kept as the first one, to ensure nfd-face-flags.hpp compiles on its own. |
| 26 | #include "nfd-face-flags.hpp" |
| 27 | |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 28 | #include "../encoding/tlv-nfd.hpp" |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 29 | #include "../encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 30 | #include "../encoding/block-helpers.hpp" |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace nfd { |
| 34 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 35 | /** |
| 36 | * \ingroup management |
| 37 | */ |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 38 | enum FaceEventKind { |
| 39 | FACE_EVENT_CREATED = 1, |
| 40 | FACE_EVENT_DESTROYED = 2 |
| 41 | }; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 43 | /** |
| 44 | * \ingroup management |
| 45 | * \brief represents a Face status change notification |
| 46 | * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Status-Change-Notification |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 47 | */ |
| 48 | class FaceEventNotification : public FaceFlagsTraits<FaceEventNotification> |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 49 | { |
| 50 | public: |
| 51 | class Error : public Tlv::Error |
| 52 | { |
| 53 | public: |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 54 | explicit |
| 55 | Error(const std::string& what) |
| 56 | : Tlv::Error(what) |
| 57 | { |
| 58 | } |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 61 | FaceEventNotification(); |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 62 | |
| 63 | explicit |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 64 | FaceEventNotification(const Block& block) |
| 65 | { |
| 66 | this->wireDecode(block); |
| 67 | } |
| 68 | |
| 69 | /** \brief prepend FaceEventNotification to the encoder |
| 70 | */ |
| 71 | template<bool T> |
| 72 | size_t |
| 73 | wireEncode(EncodingImpl<T>& encoder) const; |
| 74 | |
| 75 | /** \brief encode FaceEventNotification |
| 76 | */ |
| 77 | const Block& |
| 78 | wireEncode() const; |
| 79 | |
| 80 | /** \brief decode FaceEventNotification |
| 81 | */ |
| 82 | void |
| 83 | wireDecode(const Block& wire); |
| 84 | |
| 85 | public: // getters & setters |
| 86 | FaceEventKind |
| 87 | getKind() const |
| 88 | { |
| 89 | return m_kind; |
| 90 | } |
| 91 | |
| 92 | FaceEventNotification& |
| 93 | setKind(FaceEventKind kind) |
| 94 | { |
| 95 | m_wire.reset(); |
| 96 | m_kind = kind; |
| 97 | return *this; |
| 98 | } |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 99 | |
| 100 | uint64_t |
| 101 | getFaceId() const |
| 102 | { |
| 103 | return m_faceId; |
| 104 | } |
| 105 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 106 | FaceEventNotification& |
| 107 | setFaceId(uint64_t faceId) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 108 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 109 | m_wire.reset(); |
| 110 | m_faceId = faceId; |
| 111 | return *this; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 114 | const std::string& |
| 115 | getRemoteUri() const |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 116 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 117 | return m_remoteUri; |
| 118 | } |
| 119 | |
| 120 | FaceEventNotification& |
| 121 | setRemoteUri(const std::string& remoteUri) |
| 122 | { |
| 123 | m_wire.reset(); |
| 124 | m_remoteUri = remoteUri; |
| 125 | return *this; |
| 126 | } |
| 127 | |
| 128 | const std::string& |
| 129 | getLocalUri() const |
| 130 | { |
| 131 | return m_localUri; |
| 132 | } |
| 133 | |
| 134 | FaceEventNotification& |
| 135 | setLocalUri(const std::string& localUri) |
| 136 | { |
| 137 | m_wire.reset(); |
| 138 | m_localUri = localUri; |
| 139 | return *this; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 142 | uint64_t |
| 143 | getFlags() const |
| 144 | { |
| 145 | return m_flags; |
| 146 | } |
| 147 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 148 | FaceEventNotification& |
| 149 | setFlags(uint64_t flags) |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 150 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 151 | m_wire.reset(); |
| 152 | m_flags = flags; |
| 153 | return *this; |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 156 | private: |
| 157 | FaceEventKind m_kind; |
| 158 | uint64_t m_faceId; |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 159 | std::string m_remoteUri; |
| 160 | std::string m_localUri; |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 161 | uint64_t m_flags; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 162 | |
| 163 | mutable Block m_wire; |
| 164 | }; |
| 165 | |
| 166 | inline |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 167 | FaceEventNotification::FaceEventNotification() |
| 168 | : m_kind(static_cast<FaceEventKind>(0)) |
| 169 | , m_faceId(0) |
| 170 | , m_flags(0) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 171 | { |
| 172 | } |
| 173 | |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 174 | template<bool T> |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 175 | inline size_t |
| 176 | FaceEventNotification::wireEncode(EncodingImpl<T>& encoder) const |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 177 | { |
| 178 | size_t totalLength = 0; |
| 179 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 180 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 181 | tlv::nfd::FaceFlags, m_flags); |
| 182 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri, |
| 183 | reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size()); |
| 184 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri, |
| 185 | reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size()); |
| 186 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 187 | tlv::nfd::FaceId, m_faceId); |
| 188 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 189 | tlv::nfd::FaceEventKind, m_kind); |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 190 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 191 | totalLength += encoder.prependVarNumber(totalLength); |
| 192 | totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification); |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 193 | return totalLength; |
| 194 | } |
| 195 | |
Steve DiBenedetto | 5ce55ae | 2014-03-20 12:23:32 -0600 | [diff] [blame] | 196 | inline const Block& |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 197 | FaceEventNotification::wireEncode() const |
| 198 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 199 | if (m_wire.hasWire()) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 200 | return m_wire; |
| 201 | |
| 202 | EncodingEstimator estimator; |
| 203 | size_t estimatedSize = wireEncode(estimator); |
| 204 | |
| 205 | EncodingBuffer buffer(estimatedSize, 0); |
| 206 | wireEncode(buffer); |
| 207 | |
| 208 | m_wire = buffer.block(); |
| 209 | return m_wire; |
| 210 | } |
| 211 | |
| 212 | inline void |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 213 | FaceEventNotification::wireDecode(const Block& block) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 214 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 215 | if (block.type() != tlv::nfd::FaceEventNotification) { |
| 216 | throw Error("expecting FaceEventNotification block"); |
| 217 | } |
| 218 | m_wire = block; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 219 | m_wire.parse(); |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 220 | Block::element_const_iterator val = m_wire.elements_begin(); |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 221 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 222 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) { |
| 223 | m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val)); |
| 224 | ++val; |
| 225 | } |
| 226 | else { |
| 227 | throw Error("missing required FaceEventKind field"); |
| 228 | } |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 229 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 230 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) { |
| 231 | m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 232 | ++val; |
| 233 | } |
| 234 | else { |
| 235 | throw Error("missing required FaceId field"); |
| 236 | } |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 237 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 238 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) { |
| 239 | m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 240 | ++val; |
| 241 | } |
| 242 | else { |
| 243 | throw Error("missing required Uri field"); |
| 244 | } |
| 245 | |
| 246 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) { |
| 247 | m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 248 | ++val; |
| 249 | } |
| 250 | else { |
| 251 | throw Error("missing required LocalUri field"); |
| 252 | } |
| 253 | |
| 254 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) { |
| 255 | m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 256 | ++val; |
| 257 | } |
| 258 | else { |
| 259 | throw Error("missing required FaceFlags field"); |
| 260 | } |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | inline std::ostream& |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 264 | operator<<(std::ostream& os, const FaceEventNotification& notification) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 265 | { |
| 266 | os << "FaceEventNotification("; |
| 267 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 268 | switch (notification.getKind()) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 269 | { |
| 270 | case FACE_EVENT_CREATED: |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 271 | os << "Kind: created, "; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 272 | break; |
| 273 | case FACE_EVENT_DESTROYED: |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 274 | os << "Kind: destroyed, "; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 275 | break; |
| 276 | } |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 277 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 278 | os << "FaceID: " << notification.getFaceId() << ", " |
| 279 | << "RemoteUri: " << notification.getRemoteUri() << ", " |
| 280 | << "LocalUri: " << notification.getLocalUri() << ", " |
| 281 | << "Flags: " << notification.getFlags() |
| 282 | << ")"; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 283 | return os; |
| 284 | } |
| 285 | |
| 286 | } // namespace nfd |
| 287 | } // namespace ndn |
| 288 | |
| 289 | #endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP |