Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 13 | #ifndef NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP |
| 14 | #define NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 15 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 16 | #include "../encoding/block.hpp" |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 17 | #include "../encoding/tlv-ndnd.hpp" |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 18 | |
| 19 | namespace ndn { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 20 | namespace ndnd { |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 22 | class StatusResponse |
| 23 | { |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 24 | public: |
| 25 | StatusResponse() |
| 26 | : code_(0) |
| 27 | { |
| 28 | } |
| 29 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 30 | StatusResponse(uint32_t code, const std::string& info) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 31 | : code_(code) |
| 32 | , info_(info) |
| 33 | { |
| 34 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * @brief Create from wire encoding |
| 38 | */ |
| 39 | explicit |
| 40 | StatusResponse(const Block& wire) |
| 41 | { |
| 42 | wireDecode(wire); |
| 43 | } |
| 44 | |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 45 | inline uint32_t |
| 46 | getCode() const; |
| 47 | |
| 48 | inline void |
| 49 | setCode(uint32_t code); |
| 50 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 51 | inline const std::string& |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 52 | getInfo() const; |
| 53 | |
| 54 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 55 | setInfo(const std::string& info); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 56 | |
| 57 | inline const Block& |
| 58 | wireEncode() const; |
| 59 | |
| 60 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 61 | wireDecode(const Block& block); |
| 62 | |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 63 | private: |
| 64 | uint32_t code_; |
| 65 | std::string info_; |
| 66 | |
| 67 | mutable Block wire_; |
| 68 | }; |
| 69 | |
| 70 | inline uint32_t |
| 71 | StatusResponse::getCode() const |
| 72 | { |
| 73 | return code_; |
| 74 | } |
| 75 | |
| 76 | inline void |
| 77 | StatusResponse::setCode(uint32_t code) |
| 78 | { |
| 79 | code_ = code; |
| 80 | wire_.reset(); |
| 81 | } |
| 82 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 83 | inline const std::string& |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 84 | StatusResponse::getInfo() const |
| 85 | { |
| 86 | return info_; |
| 87 | } |
| 88 | |
| 89 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 90 | StatusResponse::setInfo(const std::string& info) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 91 | { |
| 92 | info_ = info; |
| 93 | wire_.reset(); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | inline const Block& |
| 98 | StatusResponse::wireEncode() const |
| 99 | { |
| 100 | if (wire_.hasWire()) |
| 101 | return wire_; |
| 102 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 103 | wire_ = Block(tlv::ndnd::StatusResponse); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 104 | wire_.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 105 | (nonNegativeIntegerBlock(tlv::ndnd::StatusCode, code_)); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 106 | |
| 107 | if (!info_.empty()) |
| 108 | { |
| 109 | wire_.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 110 | (dataBlock(tlv::ndnd::StatusText, info_.c_str(), info_.size())); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 111 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 112 | |
| 113 | wire_.encode(); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 114 | return wire_; |
| 115 | } |
| 116 | |
| 117 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 118 | StatusResponse::wireDecode(const Block& wire) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 119 | { |
| 120 | wire_ = wire; |
| 121 | wire_.parse(); |
| 122 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 123 | code_ = readNonNegativeInteger(wire_.get(tlv::ndnd::StatusCode)); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 125 | Block::element_const_iterator val = wire_.find(tlv::ndnd::StatusText); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 126 | if (val != wire_.elements_end()) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 127 | { |
| 128 | info_.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | inline std::ostream& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 133 | operator << (std::ostream& os, const StatusResponse& status) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 134 | { |
| 135 | os << status.getCode() << " " << status.getInfo(); |
| 136 | return os; |
| 137 | } |
| 138 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 139 | } // namespace ndnd |
| 140 | } // namespace ndn |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 141 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 142 | #endif // NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP |