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