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: |
| 47 | typedef boost::chrono::time_point<boost::chrono::system_clock, boost::chrono::seconds> Timestamp; |
| 48 | |
| 49 | int |
| 50 | getNfdVersion() const |
| 51 | { |
| 52 | return m_nfdVersion; |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | setNfdVersion(int nfdVersion) |
| 57 | { |
| 58 | m_nfdVersion = nfdVersion; |
| 59 | } |
| 60 | |
| 61 | Timestamp |
| 62 | getStartTimestamp() const |
| 63 | { |
| 64 | return m_startTimestamp; |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | setStartTimestamp(Timestamp startTimestamp) |
| 69 | { |
| 70 | m_startTimestamp = startTimestamp; |
| 71 | } |
| 72 | |
| 73 | Timestamp |
| 74 | getCurrentTimestamp() const |
| 75 | { |
| 76 | return m_currentTimestamp; |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | setCurrentTimestamp(Timestamp currentTimestamp) |
| 81 | { |
| 82 | m_currentTimestamp = currentTimestamp; |
| 83 | } |
| 84 | |
| 85 | size_t |
| 86 | getNNameTreeEntries() const |
| 87 | { |
| 88 | return m_nNameTreeEntries; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | setNNameTreeEntries(size_t nNameTreeEntries) |
| 93 | { |
| 94 | m_nNameTreeEntries = nNameTreeEntries; |
| 95 | } |
| 96 | |
| 97 | size_t |
| 98 | getNFibEntries() const |
| 99 | { |
| 100 | return m_nFibEntries; |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | setNFibEntries(size_t nFibEntries) |
| 105 | { |
| 106 | m_nFibEntries = nFibEntries; |
| 107 | } |
| 108 | |
| 109 | size_t |
| 110 | getNPitEntries() const |
| 111 | { |
| 112 | return m_nPitEntries; |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | setNPitEntries(size_t nPitEntries) |
| 117 | { |
| 118 | m_nPitEntries = nPitEntries; |
| 119 | } |
| 120 | |
| 121 | size_t |
| 122 | getNMeasurementsEntries() const |
| 123 | { |
| 124 | return m_nMeasurementsEntries; |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | setNMeasurementsEntries(size_t nMeasurementsEntries) |
| 129 | { |
| 130 | m_nMeasurementsEntries = nMeasurementsEntries; |
| 131 | } |
| 132 | |
| 133 | size_t |
| 134 | getNCsEntries() const |
| 135 | { |
| 136 | return m_nCsEntries; |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | setNCsEntries(size_t nCsEntries) |
| 141 | { |
| 142 | m_nCsEntries = nCsEntries; |
| 143 | } |
| 144 | |
| 145 | int |
| 146 | getNInInterests() const |
| 147 | { |
| 148 | return m_nInInterests; |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | setNInInterests(int nInInterests) |
| 153 | { |
| 154 | m_nInInterests = nInInterests; |
| 155 | } |
| 156 | |
| 157 | int |
| 158 | getNOutInterests() const |
| 159 | { |
| 160 | return m_nOutInterests; |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | setNOutInterests(int nOutInterests) |
| 165 | { |
| 166 | m_nOutInterests = nOutInterests; |
| 167 | } |
| 168 | |
| 169 | int |
| 170 | getNInDatas() const |
| 171 | { |
| 172 | return m_nInDatas; |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | setNInDatas(int nInDatas) |
| 177 | { |
| 178 | m_nInDatas = nInDatas; |
| 179 | } |
| 180 | |
| 181 | int |
| 182 | getNOutDatas() const |
| 183 | { |
| 184 | return m_nOutDatas; |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | setNOutDatas(int nOutDatas) |
| 189 | { |
| 190 | m_nOutDatas = nOutDatas; |
| 191 | } |
| 192 | |
| 193 | private: |
| 194 | int m_nfdVersion; |
| 195 | Timestamp m_startTimestamp; |
| 196 | Timestamp m_currentTimestamp; |
| 197 | size_t m_nNameTreeEntries; |
| 198 | size_t m_nFibEntries; |
| 199 | size_t m_nPitEntries; |
| 200 | size_t m_nMeasurementsEntries; |
| 201 | size_t m_nCsEntries; |
| 202 | int m_nInInterests; |
| 203 | int m_nOutInterests; |
| 204 | int m_nInDatas; |
| 205 | int m_nOutDatas; |
| 206 | }; |
| 207 | |
| 208 | BOOST_STATIC_ASSERT((boost::is_same<Status::Timestamp::period, boost::ratio<1> >::value)); |
| 209 | |
| 210 | inline |
| 211 | Status::Status() |
| 212 | { |
| 213 | m_nfdVersion = 0; |
| 214 | m_startTimestamp = Timestamp::min(); |
| 215 | m_currentTimestamp = Timestamp::min(); |
| 216 | m_nNameTreeEntries = 0; |
| 217 | m_nFibEntries = 0; |
| 218 | m_nPitEntries = 0; |
| 219 | m_nMeasurementsEntries = 0; |
| 220 | m_nCsEntries = 0; |
| 221 | m_nInInterests = 0; |
| 222 | m_nOutInterests = 0; |
| 223 | m_nInDatas = 0; |
| 224 | m_nOutDatas = 0; |
| 225 | } |
| 226 | |
| 227 | template<bool T> |
| 228 | inline size_t |
| 229 | Status::wireEncode(EncodingImpl<T>& encoder) const |
| 230 | { |
| 231 | size_t total_len = 0; |
| 232 | |
| 233 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutDatas, |
| 234 | m_nOutDatas); |
| 235 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInDatas, |
| 236 | m_nInDatas); |
| 237 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests, |
| 238 | m_nOutInterests); |
| 239 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests, |
| 240 | m_nInInterests); |
| 241 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NCsEntries, |
| 242 | m_nCsEntries); |
| 243 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMeasurementsEntries, |
| 244 | m_nMeasurementsEntries); |
| 245 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NPitEntries, |
| 246 | m_nPitEntries); |
| 247 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NFibEntries, |
| 248 | m_nFibEntries); |
| 249 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NNameTreeEntries, |
| 250 | m_nNameTreeEntries); |
| 251 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::CurrentTimestamp, |
| 252 | m_currentTimestamp.time_since_epoch().count()); |
| 253 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::StartTimestamp, |
| 254 | m_startTimestamp.time_since_epoch().count()); |
| 255 | total_len += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NfdVersion, |
| 256 | m_nfdVersion); |
| 257 | |
| 258 | return total_len; |
| 259 | } |
| 260 | |
| 261 | inline void |
| 262 | Status::wireDecode(const Block& payload) |
| 263 | { |
| 264 | m_nfdVersion = 0; |
| 265 | m_startTimestamp = Timestamp::min(); |
| 266 | m_currentTimestamp = Timestamp::min(); |
| 267 | m_nNameTreeEntries = 0; |
| 268 | m_nFibEntries = 0; |
| 269 | m_nPitEntries = 0; |
| 270 | m_nMeasurementsEntries = 0; |
| 271 | m_nCsEntries = 0; |
| 272 | m_nInInterests = 0; |
| 273 | m_nOutInterests = 0; |
| 274 | m_nInDatas = 0; |
| 275 | m_nOutDatas = 0; |
| 276 | |
| 277 | if (payload.type() != Tlv::Content) { |
| 278 | throw Error("Requested decoding of Status Payload, but block is not Content"); |
| 279 | } |
| 280 | payload.parse(); |
| 281 | |
| 282 | Block::element_const_iterator val; |
| 283 | |
| 284 | val = payload.find(tlv::nfd::NfdVersion); |
| 285 | if (val != payload.elements_end()) { |
| 286 | m_nfdVersion = static_cast<int>(readNonNegativeInteger(*val)); |
| 287 | } |
| 288 | |
| 289 | val = payload.find(tlv::nfd::StartTimestamp); |
| 290 | if (val != payload.elements_end()) { |
| 291 | m_startTimestamp = Timestamp(boost::chrono::seconds(readNonNegativeInteger(*val))); |
| 292 | } |
| 293 | |
| 294 | val = payload.find(tlv::nfd::CurrentTimestamp); |
| 295 | if (val != payload.elements_end()) { |
| 296 | m_currentTimestamp = Timestamp(boost::chrono::seconds(readNonNegativeInteger(*val))); |
| 297 | } |
| 298 | |
| 299 | val = payload.find(tlv::nfd::NNameTreeEntries); |
| 300 | if (val != payload.elements_end()) { |
| 301 | m_nNameTreeEntries = static_cast<size_t>(readNonNegativeInteger(*val)); |
| 302 | } |
| 303 | |
| 304 | val = payload.find(tlv::nfd::NFibEntries); |
| 305 | if (val != payload.elements_end()) { |
| 306 | m_nFibEntries = static_cast<size_t>(readNonNegativeInteger(*val)); |
| 307 | } |
| 308 | |
| 309 | val = payload.find(tlv::nfd::NPitEntries); |
| 310 | if (val != payload.elements_end()) { |
| 311 | m_nPitEntries = static_cast<size_t>(readNonNegativeInteger(*val)); |
| 312 | } |
| 313 | |
| 314 | val = payload.find(tlv::nfd::NMeasurementsEntries); |
| 315 | if (val != payload.elements_end()) { |
| 316 | m_nMeasurementsEntries = static_cast<size_t>(readNonNegativeInteger(*val)); |
| 317 | } |
| 318 | |
| 319 | val = payload.find(tlv::nfd::NCsEntries); |
| 320 | if (val != payload.elements_end()) { |
| 321 | m_nCsEntries = static_cast<size_t>(readNonNegativeInteger(*val)); |
| 322 | } |
| 323 | |
| 324 | val = payload.find(tlv::nfd::NInInterests); |
| 325 | if (val != payload.elements_end()) { |
| 326 | m_nInInterests = static_cast<int>(readNonNegativeInteger(*val)); |
| 327 | } |
| 328 | |
| 329 | val = payload.find(tlv::nfd::NOutInterests); |
| 330 | if (val != payload.elements_end()) { |
| 331 | m_nOutInterests = static_cast<int>(readNonNegativeInteger(*val)); |
| 332 | } |
| 333 | |
| 334 | val = payload.find(tlv::nfd::NInDatas); |
| 335 | if (val != payload.elements_end()) { |
| 336 | m_nInDatas = static_cast<int>(readNonNegativeInteger(*val)); |
| 337 | } |
| 338 | |
| 339 | val = payload.find(tlv::nfd::NOutDatas); |
| 340 | if (val != payload.elements_end()) { |
| 341 | m_nOutDatas = static_cast<int>(readNonNegativeInteger(*val)); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | } // namespace nfd |
| 346 | } // namespace ndn |
| 347 | |
| 348 | #endif // NDN_MANAGEMENT_NFD_STATUS_HPP |