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