Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 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 | |
| 10 | #include "../encoding/encoding-buffer.hpp" |
| 11 | #include "../encoding/tlv-nfd.hpp" |
| 12 | |
| 13 | namespace ndn { |
| 14 | namespace nfd { |
| 15 | |
| 16 | enum FaceEventKind |
| 17 | { |
| 18 | FACE_EVENT_CREATED = 1, |
| 19 | FACE_EVENT_DESTROYED = 2 |
| 20 | }; |
| 21 | |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 22 | enum FaceFlags |
| 23 | { |
| 24 | FACE_IS_LOCAL = 1, |
| 25 | FACE_IS_ON_DEMAND = 2 |
| 26 | // FACE_? = 4 |
| 27 | // FACE_? = 8 |
| 28 | }; |
| 29 | |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 30 | class FaceEventNotification |
| 31 | { |
| 32 | public: |
| 33 | class Error : public Tlv::Error |
| 34 | { |
| 35 | public: |
| 36 | Error(const std::string& what) : Tlv::Error(what) { } |
| 37 | }; |
| 38 | |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 39 | FaceEventNotification(FaceEventKind eventKind, |
| 40 | uint64_t faceId, |
| 41 | const std::string& uri, |
| 42 | uint64_t flags); |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 43 | |
| 44 | explicit |
| 45 | FaceEventNotification(const Block& block); |
| 46 | |
| 47 | uint64_t |
| 48 | getFaceId() const |
| 49 | { |
| 50 | return m_faceId; |
| 51 | } |
| 52 | |
| 53 | const std::string& |
| 54 | getUri() const |
| 55 | { |
| 56 | return m_uri; |
| 57 | } |
| 58 | |
| 59 | FaceEventKind |
| 60 | getEventKind() const |
| 61 | { |
| 62 | return m_kind; |
| 63 | } |
| 64 | |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 65 | uint64_t |
| 66 | getFlags() const |
| 67 | { |
| 68 | return m_flags; |
| 69 | } |
| 70 | |
| 71 | bool |
| 72 | isLocal() const |
| 73 | { |
| 74 | return m_flags & FACE_IS_LOCAL; |
| 75 | } |
| 76 | |
| 77 | bool |
| 78 | isOnDemand() const |
| 79 | { |
| 80 | return m_flags & FACE_IS_ON_DEMAND; |
| 81 | } |
| 82 | |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 83 | template<bool T> |
| 84 | size_t |
| 85 | wireEncode(EncodingImpl<T>& buffer) const; |
| 86 | |
| 87 | const Block& |
| 88 | wireEncode() const; |
| 89 | |
| 90 | void |
| 91 | wireDecode(const Block& wire); |
| 92 | |
| 93 | private: |
| 94 | FaceEventKind m_kind; |
| 95 | uint64_t m_faceId; |
| 96 | std::string m_uri; |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 97 | uint64_t m_flags; |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 98 | |
| 99 | mutable Block m_wire; |
| 100 | }; |
| 101 | |
| 102 | inline |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 103 | FaceEventNotification::FaceEventNotification(FaceEventKind eventKind, |
| 104 | uint64_t faceId, |
| 105 | const std::string& uri, |
| 106 | uint64_t flags) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 107 | : m_kind(eventKind) |
| 108 | , m_faceId(faceId) |
| 109 | , m_uri(uri) |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 110 | , m_flags(flags) |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 111 | { |
| 112 | } |
| 113 | |
| 114 | inline |
| 115 | FaceEventNotification::FaceEventNotification(const Block& block) |
| 116 | { |
| 117 | wireDecode(block); |
| 118 | } |
| 119 | |
| 120 | template<bool T> |
| 121 | size_t |
| 122 | FaceEventNotification::wireEncode(EncodingImpl<T>& buffer) const |
| 123 | { |
| 124 | size_t totalLength = 0; |
| 125 | |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 126 | totalLength += prependNonNegativeIntegerBlock(buffer, |
| 127 | tlv::nfd::FaceFlags, |
| 128 | m_flags); |
| 129 | |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 130 | totalLength += prependByteArrayBlock(buffer, |
| 131 | tlv::nfd::Uri, |
| 132 | reinterpret_cast<const uint8_t*>(m_uri.c_str()), |
| 133 | m_uri.size()); |
| 134 | |
| 135 | totalLength += prependNonNegativeIntegerBlock(buffer, |
| 136 | tlv::nfd::FaceId, |
| 137 | m_faceId); |
| 138 | |
| 139 | totalLength += prependNonNegativeIntegerBlock(buffer, |
| 140 | tlv::nfd::FaceEventKind, |
| 141 | static_cast<uint32_t>(m_kind)); |
| 142 | |
| 143 | totalLength += buffer.prependVarNumber(totalLength); |
| 144 | totalLength += buffer.prependVarNumber(tlv::nfd::FaceEventNotification); |
| 145 | |
| 146 | return totalLength; |
| 147 | } |
| 148 | |
Steve DiBenedetto | 5ce55ae | 2014-03-20 12:23:32 -0600 | [diff] [blame] | 149 | inline const Block& |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 150 | FaceEventNotification::wireEncode() const |
| 151 | { |
| 152 | if (m_wire.hasWire ()) |
| 153 | return m_wire; |
| 154 | |
| 155 | EncodingEstimator estimator; |
| 156 | size_t estimatedSize = wireEncode(estimator); |
| 157 | |
| 158 | EncodingBuffer buffer(estimatedSize, 0); |
| 159 | wireEncode(buffer); |
| 160 | |
| 161 | m_wire = buffer.block(); |
| 162 | return m_wire; |
| 163 | } |
| 164 | |
| 165 | inline void |
| 166 | FaceEventNotification::wireDecode (const Block &wire) |
| 167 | { |
| 168 | m_wire = wire; |
| 169 | |
| 170 | if (m_wire.type() != tlv::nfd::FaceEventNotification) |
| 171 | throw Error("Requested decoding of FaceEventNotification, but Block is of different type"); |
| 172 | |
| 173 | m_wire.parse(); |
| 174 | |
| 175 | // FaceKind |
| 176 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 177 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::FaceEventKind) |
| 178 | throw Error("Missing required Uri block"); |
| 179 | m_kind = static_cast<FaceEventKind>(readNonNegativeInteger(*val)); |
| 180 | |
| 181 | // FaceID |
| 182 | ++val; |
| 183 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::FaceId) |
| 184 | throw Error("Missing required FaceId block"); |
| 185 | m_faceId = readNonNegativeInteger(*val); |
| 186 | |
| 187 | // URI |
| 188 | ++val; |
| 189 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::Uri) |
| 190 | throw Error("Missing required Uri block"); |
| 191 | m_uri = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 192 | |
| 193 | // FaceFlags |
| 194 | ++val; |
| 195 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::FaceFlags) |
| 196 | throw Error("Missing required FaceFlags block"); |
| 197 | m_flags = readNonNegativeInteger(*val); |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | inline std::ostream& |
| 201 | operator << (std::ostream& os, const FaceEventNotification& event) |
| 202 | { |
| 203 | os << "FaceEventNotification("; |
| 204 | |
| 205 | os << "Kind: "; |
| 206 | switch (event.getEventKind()) |
| 207 | { |
| 208 | case FACE_EVENT_CREATED: |
| 209 | os << "created"; |
| 210 | break; |
| 211 | case FACE_EVENT_DESTROYED: |
| 212 | os << "destroyed"; |
| 213 | break; |
| 214 | } |
| 215 | os << ", "; |
| 216 | |
| 217 | // FaceID |
| 218 | os << "FaceID: " << event.getFaceId() << ", "; |
| 219 | |
| 220 | // URI |
Alexander Afanasyev | 2c75331 | 2014-03-20 17:31:01 -0700 | [diff] [blame] | 221 | os << "Uri: " << event.getUri() << ", "; |
| 222 | |
| 223 | // Flags |
| 224 | os << "Flags: " << event.getFlags(); |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 225 | |
| 226 | os << ")"; |
| 227 | return os; |
| 228 | } |
| 229 | |
| 230 | } // namespace nfd |
| 231 | } // namespace ndn |
| 232 | |
| 233 | #endif // NDN_MANAGEMENT_NFD_FACE_EVENT_NOTIFICATION_HPP |