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