Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_MANAGEMENT_NFD_STATUS_HPP |
| 8 | #define NDN_MANAGEMENT_NFD_STATUS_HPP |
| 9 | |
| 10 | #include "../encoding/tlv-nfd.hpp" |
| 11 | #include "../encoding/tlv.hpp" |
| 12 | #include "../encoding/encoding-buffer.hpp" |
| 13 | #include <boost/chrono/include.hpp> |
| 14 | |
| 15 | namespace ndn { |
| 16 | namespace nfd { |
| 17 | |
| 18 | class Status { |
| 19 | public: |
| 20 | struct Error : public Tlv::Error |
| 21 | { |
| 22 | Error(const std::string& what) |
| 23 | : Tlv::Error(what) |
| 24 | { |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | Status(); |
| 29 | |
| 30 | explicit |
| 31 | Status(const Block& payload) |
| 32 | { |
| 33 | this->wireDecode(payload); |
| 34 | } |
| 35 | |
| 36 | // Status is encoded as a series of blocks within Data Payload, not a single block. |
| 37 | // Thus there is no "const Block& wireEncode() const" method. |
| 38 | |
| 39 | template<bool T> |
| 40 | size_t |
| 41 | wireEncode(EncodingImpl<T>& encoder) const; |
| 42 | |
| 43 | void |
| 44 | wireDecode(const Block& payload); |
| 45 | |
| 46 | public: |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 47 | int |
| 48 | getNfdVersion() const |
| 49 | { |
| 50 | return m_nfdVersion; |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | setNfdVersion(int nfdVersion) |
| 55 | { |
| 56 | m_nfdVersion = nfdVersion; |
| 57 | } |
| 58 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 59 | const time::system_clock::TimePoint& |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 60 | getStartTimestamp() const |
| 61 | { |
| 62 | return m_startTimestamp; |
| 63 | } |
| 64 | |
| 65 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 66 | setStartTimestamp(const time::system_clock::TimePoint& startTimestamp) |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 67 | { |
| 68 | m_startTimestamp = startTimestamp; |
| 69 | } |
| 70 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 71 | const time::system_clock::TimePoint& |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 72 | getCurrentTimestamp() const |
| 73 | { |
| 74 | return m_currentTimestamp; |
| 75 | } |
| 76 | |
| 77 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 78 | setCurrentTimestamp(const time::system_clock::TimePoint& currentTimestamp) |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 79 | { |
| 80 | m_currentTimestamp = currentTimestamp; |
| 81 | } |
| 82 | |
| 83 | size_t |
| 84 | getNNameTreeEntries() const |
| 85 | { |
| 86 | return m_nNameTreeEntries; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | setNNameTreeEntries(size_t nNameTreeEntries) |
| 91 | { |
| 92 | m_nNameTreeEntries = nNameTreeEntries; |
| 93 | } |
| 94 | |
| 95 | size_t |
| 96 | getNFibEntries() const |
| 97 | { |
| 98 | return m_nFibEntries; |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | setNFibEntries(size_t nFibEntries) |
| 103 | { |
| 104 | m_nFibEntries = nFibEntries; |
| 105 | } |
| 106 | |
| 107 | size_t |
| 108 | getNPitEntries() const |
| 109 | { |
| 110 | return m_nPitEntries; |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | setNPitEntries(size_t nPitEntries) |
| 115 | { |
| 116 | m_nPitEntries = nPitEntries; |
| 117 | } |
| 118 | |
| 119 | size_t |
| 120 | getNMeasurementsEntries() const |
| 121 | { |
| 122 | return m_nMeasurementsEntries; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | setNMeasurementsEntries(size_t nMeasurementsEntries) |
| 127 | { |
| 128 | m_nMeasurementsEntries = nMeasurementsEntries; |
| 129 | } |
| 130 | |
| 131 | size_t |
| 132 | getNCsEntries() const |
| 133 | { |
| 134 | return m_nCsEntries; |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | setNCsEntries(size_t nCsEntries) |
| 139 | { |
| 140 | m_nCsEntries = nCsEntries; |
| 141 | } |
| 142 | |
| 143 | int |
| 144 | getNInInterests() const |
| 145 | { |
| 146 | return m_nInInterests; |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | setNInInterests(int nInInterests) |
| 151 | { |
| 152 | m_nInInterests = nInInterests; |
| 153 | } |
| 154 | |
| 155 | int |
| 156 | getNOutInterests() const |
| 157 | { |
| 158 | return m_nOutInterests; |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | setNOutInterests(int nOutInterests) |
| 163 | { |
| 164 | m_nOutInterests = nOutInterests; |
| 165 | } |
| 166 | |
| 167 | int |
| 168 | getNInDatas() const |
| 169 | { |
| 170 | return m_nInDatas; |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | setNInDatas(int nInDatas) |
| 175 | { |
| 176 | m_nInDatas = nInDatas; |
| 177 | } |
| 178 | |
| 179 | int |
| 180 | getNOutDatas() const |
| 181 | { |
| 182 | return m_nOutDatas; |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | setNOutDatas(int nOutDatas) |
| 187 | { |
| 188 | m_nOutDatas = nOutDatas; |
| 189 | } |
| 190 | |
| 191 | private: |
| 192 | int m_nfdVersion; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 193 | time::system_clock::TimePoint m_startTimestamp; |
| 194 | time::system_clock::TimePoint m_currentTimestamp; |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 195 | size_t m_nNameTreeEntries; |
| 196 | size_t m_nFibEntries; |
| 197 | size_t m_nPitEntries; |
| 198 | size_t m_nMeasurementsEntries; |
| 199 | size_t m_nCsEntries; |
| 200 | int m_nInInterests; |
| 201 | int m_nOutInterests; |
| 202 | int m_nInDatas; |
| 203 | int m_nOutDatas; |
| 204 | }; |
| 205 | |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 206 | inline |
| 207 | Status::Status() |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 208 | : m_nfdVersion(0) |
| 209 | , m_startTimestamp(time::system_clock::TimePoint::min()) |
| 210 | , m_currentTimestamp(time::system_clock::TimePoint::min()) |
| 211 | , m_nNameTreeEntries(0) |
| 212 | , m_nFibEntries(0) |
| 213 | , m_nPitEntries(0) |
| 214 | , m_nMeasurementsEntries(0) |
| 215 | , m_nCsEntries(0) |
| 216 | , m_nInInterests(0) |
| 217 | , m_nOutInterests(0) |
| 218 | , m_nInDatas(0) |
| 219 | , m_nOutDatas(0) |
| 220 | { |
| 221 | } |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 222 | |
| 223 | template<bool T> |
| 224 | inline size_t |
| 225 | Status::wireEncode(EncodingImpl<T>& encoder) const |
| 226 | { |
| 227 | size_t total_len = 0; |
| 228 | |
| 229 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutDatas, |
| 230 | m_nOutDatas); |
| 231 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInDatas, |
| 232 | m_nInDatas); |
| 233 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests, |
| 234 | m_nOutInterests); |
| 235 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests, |
| 236 | m_nInInterests); |
| 237 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NCsEntries, |
| 238 | m_nCsEntries); |
| 239 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMeasurementsEntries, |
| 240 | m_nMeasurementsEntries); |
| 241 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NPitEntries, |
| 242 | m_nPitEntries); |
| 243 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NFibEntries, |
| 244 | m_nFibEntries); |
| 245 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NNameTreeEntries, |
| 246 | m_nNameTreeEntries); |
| 247 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::CurrentTimestamp, |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 248 | time::toUnixTimestamp(m_currentTimestamp).count()); |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 249 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::StartTimestamp, |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 250 | time::toUnixTimestamp(m_startTimestamp).count()); |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 251 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NfdVersion, |
| 252 | m_nfdVersion); |
| 253 | |
| 254 | return total_len; |
| 255 | } |
| 256 | |
| 257 | inline void |
| 258 | Status::wireDecode(const Block& payload) |
| 259 | { |
| 260 | m_nfdVersion = 0; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 261 | m_startTimestamp = time::system_clock::TimePoint::min(); |
| 262 | m_currentTimestamp = time::system_clock::TimePoint::min(); |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 263 | m_nNameTreeEntries = 0; |
| 264 | m_nFibEntries = 0; |
| 265 | m_nPitEntries = 0; |
| 266 | m_nMeasurementsEntries = 0; |
| 267 | m_nCsEntries = 0; |
| 268 | m_nInInterests = 0; |
| 269 | m_nOutInterests = 0; |
| 270 | m_nInDatas = 0; |
| 271 | m_nOutDatas = 0; |
| 272 | |
| 273 | if (payload.type() != Tlv::Content) { |
| 274 | throw Error("Requested decoding of Status Payload, but block is not Content"); |
| 275 | } |
| 276 | payload.parse(); |
| 277 | |
| 278 | Block::element_const_iterator val; |
| 279 | |
| 280 | val = payload.find(tlv::nfd::NfdVersion); |
| 281 | if (val != payload.elements_end()) { |
| 282 | m_nfdVersion = static_cast<int>(readNonNegativeInteger(*val)); |
| 283 | } |
| 284 | |
| 285 | val = payload.find(tlv::nfd::StartTimestamp); |
| 286 | if (val != payload.elements_end()) { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 287 | m_startTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val))); |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | val = payload.find(tlv::nfd::CurrentTimestamp); |
| 291 | if (val != payload.elements_end()) { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 292 | m_currentTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val))); |
Junxiao Shi | 28908b7 | 2014-03-15 23:25:45 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | val = payload.find(tlv::nfd::NNameTreeEntries); |
| 296 | if (val != payload.elements_end()) { |
| 297 | m_nNameTreeEntries = static_cast<size_t>(readNonNegativeInteger(*val)); |
| 298 | } |
| 299 | |
| 300 | val = payload.find(tlv::nfd::NFibEntries); |
| 301 | if (val != payload.elements_end()) { |
| 302 | m_nFibEntries = static_cast<size_t>(readNonNegativeInteger(*val)); |
| 303 | } |
| 304 | |
| 305 | val = payload.find(tlv::nfd::NPitEntries); |
| 306 | if (val != payload.elements_end()) { |
| 307 | m_nPitEntries = static_cast<size_t>(readNonNegativeInteger(*val)); |
| 308 | } |
| 309 | |
| 310 | val = payload.find(tlv::nfd::NMeasurementsEntries); |
| 311 | if (val != payload.elements_end()) { |
| 312 | m_nMeasurementsEntries = static_cast<size_t>(readNonNegativeInteger(*val)); |
| 313 | } |
| 314 | |
| 315 | val = payload.find(tlv::nfd::NCsEntries); |
| 316 | if (val != payload.elements_end()) { |
| 317 | m_nCsEntries = static_cast<size_t>(readNonNegativeInteger(*val)); |
| 318 | } |
| 319 | |
| 320 | val = payload.find(tlv::nfd::NInInterests); |
| 321 | if (val != payload.elements_end()) { |
| 322 | m_nInInterests = static_cast<int>(readNonNegativeInteger(*val)); |
| 323 | } |
| 324 | |
| 325 | val = payload.find(tlv::nfd::NOutInterests); |
| 326 | if (val != payload.elements_end()) { |
| 327 | m_nOutInterests = static_cast<int>(readNonNegativeInteger(*val)); |
| 328 | } |
| 329 | |
| 330 | val = payload.find(tlv::nfd::NInDatas); |
| 331 | if (val != payload.elements_end()) { |
| 332 | m_nInDatas = static_cast<int>(readNonNegativeInteger(*val)); |
| 333 | } |
| 334 | |
| 335 | val = payload.find(tlv::nfd::NOutDatas); |
| 336 | if (val != payload.elements_end()) { |
| 337 | m_nOutDatas = static_cast<int>(readNonNegativeInteger(*val)); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | } // namespace nfd |
| 342 | } // namespace ndn |
| 343 | |
| 344 | #endif // NDN_MANAGEMENT_NFD_STATUS_HPP |