Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [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 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 7 | #ifndef NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP |
| 8 | #define NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 9 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 10 | #include "../encoding/block.hpp" |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 11 | #include "../encoding/tlv-ndnd.hpp" |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 12 | |
| 13 | namespace ndn { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 14 | namespace ndnd { |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 15 | |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 16 | class StatusResponse |
| 17 | { |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 18 | public: |
| 19 | StatusResponse() |
| 20 | : code_(0) |
| 21 | { |
| 22 | } |
| 23 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 24 | StatusResponse(uint32_t code, const std::string& info) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 25 | : code_(code) |
| 26 | , info_(info) |
| 27 | { |
| 28 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * @brief Create from wire encoding |
| 32 | */ |
| 33 | explicit |
| 34 | StatusResponse(const Block& wire) |
| 35 | { |
| 36 | wireDecode(wire); |
| 37 | } |
| 38 | |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 39 | inline uint32_t |
| 40 | getCode() const; |
| 41 | |
| 42 | inline void |
| 43 | setCode(uint32_t code); |
| 44 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 45 | inline const std::string& |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 46 | getInfo() const; |
| 47 | |
| 48 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 49 | setInfo(const std::string& info); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 50 | |
| 51 | inline const Block& |
| 52 | wireEncode() const; |
| 53 | |
| 54 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 55 | wireDecode(const Block& block); |
| 56 | |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 57 | private: |
| 58 | uint32_t code_; |
| 59 | std::string info_; |
| 60 | |
| 61 | mutable Block wire_; |
| 62 | }; |
| 63 | |
| 64 | inline uint32_t |
| 65 | StatusResponse::getCode() const |
| 66 | { |
| 67 | return code_; |
| 68 | } |
| 69 | |
| 70 | inline void |
| 71 | StatusResponse::setCode(uint32_t code) |
| 72 | { |
| 73 | code_ = code; |
| 74 | wire_.reset(); |
| 75 | } |
| 76 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 77 | inline const std::string& |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 78 | StatusResponse::getInfo() const |
| 79 | { |
| 80 | return info_; |
| 81 | } |
| 82 | |
| 83 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 84 | StatusResponse::setInfo(const std::string& info) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 85 | { |
| 86 | info_ = info; |
| 87 | wire_.reset(); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | inline const Block& |
| 92 | StatusResponse::wireEncode() const |
| 93 | { |
| 94 | if (wire_.hasWire()) |
| 95 | return wire_; |
| 96 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 97 | wire_ = Block(tlv::ndnd::StatusResponse); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 98 | wire_.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 99 | (nonNegativeIntegerBlock(tlv::ndnd::StatusCode, code_)); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 100 | |
| 101 | if (!info_.empty()) |
| 102 | { |
| 103 | wire_.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 104 | (dataBlock(tlv::ndnd::StatusText, info_.c_str(), info_.size())); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 105 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 106 | |
| 107 | wire_.encode(); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 108 | return wire_; |
| 109 | } |
| 110 | |
| 111 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 112 | StatusResponse::wireDecode(const Block& wire) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 113 | { |
| 114 | wire_ = wire; |
| 115 | wire_.parse(); |
| 116 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 117 | code_ = readNonNegativeInteger(wire_.get(tlv::ndnd::StatusCode)); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 119 | Block::element_const_iterator val = wire_.find(tlv::ndnd::StatusText); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 120 | if (val != wire_.elements_end()) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 121 | { |
| 122 | info_.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | inline std::ostream& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 127 | operator << (std::ostream& os, const StatusResponse& status) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 128 | { |
| 129 | os << status.getCode() << " " << status.getInfo(); |
| 130 | return os; |
| 131 | } |
| 132 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 133 | } // namespace ndnd |
| 134 | } // namespace ndn |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 135 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 136 | #endif // NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP |