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 | |
| 7 | #ifndef NDN_STATUS_RESPONSE_HPP |
| 8 | #define NDN_STATUS_RESPONSE_HPP |
| 9 | |
| 10 | #include "encoding/block.hpp" |
| 11 | #include "encoding/tlv-face-management.hpp" |
| 12 | |
| 13 | namespace ndn { |
| 14 | |
| 15 | class StatusResponse { |
| 16 | public: |
| 17 | StatusResponse() |
| 18 | : code_(0) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | StatusResponse(uint32_t code, const std::string &info) |
| 23 | : code_(code) |
| 24 | , info_(info) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | inline uint32_t |
| 29 | getCode() const; |
| 30 | |
| 31 | inline void |
| 32 | setCode(uint32_t code); |
| 33 | |
| 34 | inline const std::string & |
| 35 | getInfo() const; |
| 36 | |
| 37 | inline void |
| 38 | setInfo(const std::string &info); |
| 39 | |
| 40 | inline const Block& |
| 41 | wireEncode() const; |
| 42 | |
| 43 | inline void |
| 44 | wireDecode(const Block &block); |
| 45 | |
| 46 | private: |
| 47 | uint32_t code_; |
| 48 | std::string info_; |
| 49 | |
| 50 | mutable Block wire_; |
| 51 | }; |
| 52 | |
| 53 | inline uint32_t |
| 54 | StatusResponse::getCode() const |
| 55 | { |
| 56 | return code_; |
| 57 | } |
| 58 | |
| 59 | inline void |
| 60 | StatusResponse::setCode(uint32_t code) |
| 61 | { |
| 62 | code_ = code; |
| 63 | wire_.reset(); |
| 64 | } |
| 65 | |
| 66 | inline const std::string & |
| 67 | StatusResponse::getInfo() const |
| 68 | { |
| 69 | return info_; |
| 70 | } |
| 71 | |
| 72 | inline void |
| 73 | StatusResponse::setInfo(const std::string &info) |
| 74 | { |
| 75 | info_ = info; |
| 76 | wire_.reset(); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | inline const Block& |
| 81 | StatusResponse::wireEncode() const |
| 82 | { |
| 83 | if (wire_.hasWire()) |
| 84 | return wire_; |
| 85 | |
| 86 | wire_ = Block(Tlv::FaceManagement::StatusResponse); |
| 87 | wire_.push_back |
| 88 | (nonNegativeIntegerBlock(Tlv::FaceManagement::StatusCode, code_)); |
| 89 | |
| 90 | if (!info_.empty()) |
| 91 | { |
| 92 | wire_.push_back |
| 93 | (dataBlock(Tlv::FaceManagement::StatusText, info_.c_str(), info_.size())); |
| 94 | } |
| 95 | |
| 96 | wire_.encode(); |
| 97 | return wire_; |
| 98 | } |
| 99 | |
| 100 | inline void |
| 101 | StatusResponse::wireDecode(const Block &wire) |
| 102 | { |
| 103 | wire_ = wire; |
| 104 | wire_.parse(); |
| 105 | |
| 106 | code_ = readNonNegativeInteger(wire_.get(Tlv::FaceManagement::StatusCode)); |
| 107 | |
| 108 | Block::element_iterator val = wire_.find(Tlv::FaceManagement::StatusText); |
| 109 | if (val != wire_.getAll().end()) |
| 110 | { |
| 111 | info_.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | inline std::ostream& |
| 116 | operator << (std::ostream &os, const StatusResponse &status) |
| 117 | { |
| 118 | os << status.getCode() << " " << status.getInfo(); |
| 119 | return os; |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | #endif // NDN_STATUS_RESPONSE_HPP |