blob: 2229a1d6e94fe84114ecb82f2ea41d1fb3020bac [file] [log] [blame]
Alexander Afanasyev18371872014-01-05 23:00:26 -08001/* -*- 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 Afanasyevc8823bc2014-02-09 19:33:33 -08007#ifndef NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP
8#define NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP
Alexander Afanasyev18371872014-01-05 23:00:26 -08009
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080010#include "../encoding/block.hpp"
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080011#include "../encoding/tlv-ndnd.hpp"
Alexander Afanasyev18371872014-01-05 23:00:26 -080012
13namespace ndn {
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080014namespace ndnd {
Alexander Afanasyev18371872014-01-05 23:00:26 -080015
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070016class StatusResponse
17{
Alexander Afanasyev18371872014-01-05 23:00:26 -080018public:
19 StatusResponse()
20 : code_(0)
21 {
22 }
23
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070024 StatusResponse(uint32_t code, const std::string& info)
Alexander Afanasyev18371872014-01-05 23:00:26 -080025 : code_(code)
26 , info_(info)
27 {
28 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070029
30 /**
31 * @brief Create from wire encoding
32 */
33 explicit
34 StatusResponse(const Block& wire)
35 {
36 wireDecode(wire);
37 }
38
Alexander Afanasyev18371872014-01-05 23:00:26 -080039 inline uint32_t
40 getCode() const;
41
42 inline void
43 setCode(uint32_t code);
44
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045 inline const std::string&
Alexander Afanasyev18371872014-01-05 23:00:26 -080046 getInfo() const;
47
48 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049 setInfo(const std::string& info);
Alexander Afanasyev18371872014-01-05 23:00:26 -080050
51 inline const Block&
52 wireEncode() const;
53
54 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070055 wireDecode(const Block& block);
56
Alexander Afanasyev18371872014-01-05 23:00:26 -080057private:
58 uint32_t code_;
59 std::string info_;
60
61 mutable Block wire_;
62};
63
64inline uint32_t
65StatusResponse::getCode() const
66{
67 return code_;
68}
69
70inline void
71StatusResponse::setCode(uint32_t code)
72{
73 code_ = code;
74 wire_.reset();
75}
76
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070077inline const std::string&
Alexander Afanasyev18371872014-01-05 23:00:26 -080078StatusResponse::getInfo() const
79{
80 return info_;
81}
82
83inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070084StatusResponse::setInfo(const std::string& info)
Alexander Afanasyev18371872014-01-05 23:00:26 -080085{
86 info_ = info;
87 wire_.reset();
88}
89
90
91inline const Block&
92StatusResponse::wireEncode() const
93{
94 if (wire_.hasWire())
95 return wire_;
96
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080097 wire_ = Block(tlv::ndnd::StatusResponse);
Alexander Afanasyev18371872014-01-05 23:00:26 -080098 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080099 (nonNegativeIntegerBlock(tlv::ndnd::StatusCode, code_));
Alexander Afanasyev18371872014-01-05 23:00:26 -0800100
101 if (!info_.empty())
102 {
103 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800104 (dataBlock(tlv::ndnd::StatusText, info_.c_str(), info_.size()));
Alexander Afanasyev18371872014-01-05 23:00:26 -0800105 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700106
107 wire_.encode();
Alexander Afanasyev18371872014-01-05 23:00:26 -0800108 return wire_;
109}
110
111inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112StatusResponse::wireDecode(const Block& wire)
Alexander Afanasyev18371872014-01-05 23:00:26 -0800113{
114 wire_ = wire;
115 wire_.parse();
116
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800117 code_ = readNonNegativeInteger(wire_.get(tlv::ndnd::StatusCode));
Alexander Afanasyev18371872014-01-05 23:00:26 -0800118
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800119 Block::element_const_iterator val = wire_.find(tlv::ndnd::StatusText);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800120 if (val != wire_.elements_end())
Alexander Afanasyev18371872014-01-05 23:00:26 -0800121 {
122 info_.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
123 }
124}
125
126inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700127operator << (std::ostream& os, const StatusResponse& status)
Alexander Afanasyev18371872014-01-05 23:00:26 -0800128{
129 os << status.getCode() << " " << status.getInfo();
130 return os;
131}
132
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800133} // namespace ndnd
134} // namespace ndn
Alexander Afanasyev18371872014-01-05 23:00:26 -0800135
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800136#endif // NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP