blob: a918be1c6fbaee6390653e001bd069ee011ae6c9 [file] [log] [blame]
Alexander Afanasyev18371872014-01-05 23:00:26 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Afanasyev18371872014-01-05 23:00:26 -080011 */
12
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080013#ifndef NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP
14#define NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP
Alexander Afanasyev18371872014-01-05 23:00:26 -080015
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080016#include "../encoding/block.hpp"
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080017#include "../encoding/tlv-ndnd.hpp"
Alexander Afanasyev18371872014-01-05 23:00:26 -080018
19namespace ndn {
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080020namespace ndnd {
Alexander Afanasyev18371872014-01-05 23:00:26 -080021
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070022class StatusResponse
23{
Alexander Afanasyev18371872014-01-05 23:00:26 -080024public:
25 StatusResponse()
26 : code_(0)
27 {
28 }
29
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070030 StatusResponse(uint32_t code, const std::string& info)
Alexander Afanasyev18371872014-01-05 23:00:26 -080031 : code_(code)
32 , info_(info)
33 {
34 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070035
36 /**
37 * @brief Create from wire encoding
38 */
39 explicit
40 StatusResponse(const Block& wire)
41 {
42 wireDecode(wire);
43 }
44
Alexander Afanasyev18371872014-01-05 23:00:26 -080045 inline uint32_t
46 getCode() const;
47
48 inline void
49 setCode(uint32_t code);
50
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070051 inline const std::string&
Alexander Afanasyev18371872014-01-05 23:00:26 -080052 getInfo() const;
53
54 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070055 setInfo(const std::string& info);
Alexander Afanasyev18371872014-01-05 23:00:26 -080056
57 inline const Block&
58 wireEncode() const;
59
60 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061 wireDecode(const Block& block);
62
Alexander Afanasyev18371872014-01-05 23:00:26 -080063private:
64 uint32_t code_;
65 std::string info_;
66
67 mutable Block wire_;
68};
69
70inline uint32_t
71StatusResponse::getCode() const
72{
73 return code_;
74}
75
76inline void
77StatusResponse::setCode(uint32_t code)
78{
79 code_ = code;
80 wire_.reset();
81}
82
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083inline const std::string&
Alexander Afanasyev18371872014-01-05 23:00:26 -080084StatusResponse::getInfo() const
85{
86 return info_;
87}
88
89inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070090StatusResponse::setInfo(const std::string& info)
Alexander Afanasyev18371872014-01-05 23:00:26 -080091{
92 info_ = info;
93 wire_.reset();
94}
95
96
97inline const Block&
98StatusResponse::wireEncode() const
99{
100 if (wire_.hasWire())
101 return wire_;
102
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800103 wire_ = Block(tlv::ndnd::StatusResponse);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800104 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800105 (nonNegativeIntegerBlock(tlv::ndnd::StatusCode, code_));
Alexander Afanasyev18371872014-01-05 23:00:26 -0800106
107 if (!info_.empty())
108 {
109 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800110 (dataBlock(tlv::ndnd::StatusText, info_.c_str(), info_.size()));
Alexander Afanasyev18371872014-01-05 23:00:26 -0800111 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112
113 wire_.encode();
Alexander Afanasyev18371872014-01-05 23:00:26 -0800114 return wire_;
115}
116
117inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700118StatusResponse::wireDecode(const Block& wire)
Alexander Afanasyev18371872014-01-05 23:00:26 -0800119{
120 wire_ = wire;
121 wire_.parse();
122
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800123 code_ = readNonNegativeInteger(wire_.get(tlv::ndnd::StatusCode));
Alexander Afanasyev18371872014-01-05 23:00:26 -0800124
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800125 Block::element_const_iterator val = wire_.find(tlv::ndnd::StatusText);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800126 if (val != wire_.elements_end())
Alexander Afanasyev18371872014-01-05 23:00:26 -0800127 {
128 info_.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
129 }
130}
131
132inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700133operator << (std::ostream& os, const StatusResponse& status)
Alexander Afanasyev18371872014-01-05 23:00:26 -0800134{
135 os << status.getCode() << " " << status.getInfo();
136 return os;
137}
138
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800139} // namespace ndnd
140} // namespace ndn
Alexander Afanasyev18371872014-01-05 23:00:26 -0800141
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800142#endif // NDN_MANAGEMENT_NDND_STATUS_RESPONSE_HPP