blob: 23597b9e1caa5dcb138f3ae738fffb86a3bfa6c1 [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
16class StatusResponse {
17public:
18 StatusResponse()
19 : code_(0)
20 {
21 }
22
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070023 StatusResponse(uint32_t code, const std::string& info)
Alexander Afanasyev18371872014-01-05 23:00:26 -080024 : code_(code)
25 , info_(info)
26 {
27 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070028
29 /**
30 * @brief Create from wire encoding
31 */
32 explicit
33 StatusResponse(const Block& wire)
34 {
35 wireDecode(wire);
36 }
37
Alexander Afanasyev18371872014-01-05 23:00:26 -080038 inline uint32_t
39 getCode() const;
40
41 inline void
42 setCode(uint32_t code);
43
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044 inline const std::string&
Alexander Afanasyev18371872014-01-05 23:00:26 -080045 getInfo() const;
46
47 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070048 setInfo(const std::string& info);
Alexander Afanasyev18371872014-01-05 23:00:26 -080049
50 inline const Block&
51 wireEncode() const;
52
53 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054 wireDecode(const Block& block);
55
Alexander Afanasyev18371872014-01-05 23:00:26 -080056private:
57 uint32_t code_;
58 std::string info_;
59
60 mutable Block wire_;
61};
62
63inline uint32_t
64StatusResponse::getCode() const
65{
66 return code_;
67}
68
69inline void
70StatusResponse::setCode(uint32_t code)
71{
72 code_ = code;
73 wire_.reset();
74}
75
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076inline const std::string&
Alexander Afanasyev18371872014-01-05 23:00:26 -080077StatusResponse::getInfo() const
78{
79 return info_;
80}
81
82inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083StatusResponse::setInfo(const std::string& info)
Alexander Afanasyev18371872014-01-05 23:00:26 -080084{
85 info_ = info;
86 wire_.reset();
87}
88
89
90inline const Block&
91StatusResponse::wireEncode() const
92{
93 if (wire_.hasWire())
94 return wire_;
95
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080096 wire_ = Block(tlv::ndnd::StatusResponse);
Alexander Afanasyev18371872014-01-05 23:00:26 -080097 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080098 (nonNegativeIntegerBlock(tlv::ndnd::StatusCode, code_));
Alexander Afanasyev18371872014-01-05 23:00:26 -080099
100 if (!info_.empty())
101 {
102 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800103 (dataBlock(tlv::ndnd::StatusText, info_.c_str(), info_.size()));
Alexander Afanasyev18371872014-01-05 23:00:26 -0800104 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700105
106 wire_.encode();
Alexander Afanasyev18371872014-01-05 23:00:26 -0800107 return wire_;
108}
109
110inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700111StatusResponse::wireDecode(const Block& wire)
Alexander Afanasyev18371872014-01-05 23:00:26 -0800112{
113 wire_ = wire;
114 wire_.parse();
115
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800116 code_ = readNonNegativeInteger(wire_.get(tlv::ndnd::StatusCode));
Alexander Afanasyev18371872014-01-05 23:00:26 -0800117
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800118 Block::element_const_iterator val = wire_.find(tlv::ndnd::StatusText);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800119 if (val != wire_.elements_end())
Alexander Afanasyev18371872014-01-05 23:00:26 -0800120 {
121 info_.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
122 }
123}
124
125inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700126operator << (std::ostream& os, const StatusResponse& status)
Alexander Afanasyev18371872014-01-05 23:00:26 -0800127{
128 os << status.getCode() << " " << status.getInfo();
129 return os;
130}
131
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800132} // namespace ndnd
133} // namespace ndn
Alexander Afanasyev18371872014-01-05 23:00:26 -0800134
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800135#endif // NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP