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