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