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 | |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 135 | private: |
| 136 | FaceEventKind m_kind; |
| 137 | uint64_t m_faceId; |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 138 | std::string m_remoteUri; |
| 139 | std::string m_localUri; |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 140 | uint64_t m_flags; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 141 | |
| 142 | mutable Block m_wire; |
| 143 | }; |
| 144 | |
| 145 | inline |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 146 | FaceEventNotification::FaceEventNotification() |
| 147 | : m_kind(static_cast<FaceEventKind>(0)) |
| 148 | , m_faceId(0) |
| 149 | , m_flags(0) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 150 | { |
| 151 | } |
| 152 | |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 153 | template<bool T> |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 154 | inline size_t |
| 155 | FaceEventNotification::wireEncode(EncodingImpl<T>& encoder) const |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 156 | { |
| 157 | size_t totalLength = 0; |
| 158 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 159 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 160 | tlv::nfd::FaceFlags, m_flags); |
| 161 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri, |
| 162 | reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size()); |
| 163 | totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri, |
| 164 | reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size()); |
| 165 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 166 | tlv::nfd::FaceId, m_faceId); |
| 167 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 168 | tlv::nfd::FaceEventKind, m_kind); |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 169 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 170 | totalLength += encoder.prependVarNumber(totalLength); |
| 171 | totalLength += encoder.prependVarNumber(tlv::nfd::FaceEventNotification); |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 172 | return totalLength; |
| 173 | } |
| 174 | |
Steve DiBenedetto | 5ce55ae | 2014-03-20 12:23:32 -0600 | [diff] [blame] | 175 | inline const Block& |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 176 | FaceEventNotification::wireEncode() const |
| 177 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 178 | if (m_wire.hasWire()) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 179 | return m_wire; |
| 180 | |
| 181 | EncodingEstimator estimator; |
| 182 | size_t estimatedSize = wireEncode(estimator); |
| 183 | |
| 184 | EncodingBuffer buffer(estimatedSize, 0); |
| 185 | wireEncode(buffer); |
| 186 | |
| 187 | m_wire = buffer.block(); |
| 188 | return m_wire; |
| 189 | } |
| 190 | |
| 191 | inline void |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 192 | FaceEventNotification::wireDecode(const Block& block) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 193 | { |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 194 | if (block.type() != tlv::nfd::FaceEventNotification) { |
| 195 | throw Error("expecting FaceEventNotification block"); |
| 196 | } |
| 197 | m_wire = block; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 198 | m_wire.parse(); |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 199 | Block::element_const_iterator val = m_wire.elements_begin(); |
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 (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceEventKind) { |
| 202 | m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val)); |
| 203 | ++val; |
| 204 | } |
| 205 | else { |
| 206 | throw Error("missing required FaceEventKind field"); |
| 207 | } |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 208 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 209 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) { |
| 210 | m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 211 | ++val; |
| 212 | } |
| 213 | else { |
| 214 | throw Error("missing required FaceId field"); |
| 215 | } |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 216 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 217 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) { |
| 218 | m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 219 | ++val; |
| 220 | } |
| 221 | else { |
| 222 | throw Error("missing required Uri field"); |
| 223 | } |
| 224 | |
| 225 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) { |
| 226 | m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 227 | ++val; |
| 228 | } |
| 229 | else { |
| 230 | throw Error("missing required LocalUri field"); |
| 231 | } |
| 232 | |
| 233 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) { |
| 234 | m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val)); |
| 235 | ++val; |
| 236 | } |
| 237 | else { |
| 238 | throw Error("missing required FaceFlags field"); |
| 239 | } |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | inline std::ostream& |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 243 | operator<<(std::ostream& os, const FaceEventNotification& notification) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 244 | { |
| 245 | os << "FaceEventNotification("; |
| 246 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 247 | switch (notification.getKind()) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 248 | { |
| 249 | case FACE_EVENT_CREATED: |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 250 | os << "Kind: created, "; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 251 | break; |
| 252 | case FACE_EVENT_DESTROYED: |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 253 | os << "Kind: destroyed, "; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 254 | break; |
| 255 | } |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 256 | |
Junxiao Shi | 7b1ba1a | 2014-03-29 01:01:56 -0700 | [diff] [blame] | 257 | os << "FaceID: " << notification.getFaceId() << ", " |
| 258 | << "RemoteUri: " << notification.getRemoteUri() << ", " |
| 259 | << "LocalUri: " << notification.getLocalUri() << ", " |
| 260 | << "Flags: " << notification.getFlags() |
| 261 | << ")"; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 262 | return os; |
| 263 | } |
| 264 | |
| 265 | } // namespace nfd |
| 266 | } // namespace ndn |
| 267 | |
| 268 | #endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP |