blob: a2229513802d38676c64539828a1f8df0d400180 [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
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
13namespace ndn {
14
15class StatusResponse {
16public:
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
46private:
47 uint32_t code_;
48 std::string info_;
49
50 mutable Block wire_;
51};
52
53inline uint32_t
54StatusResponse::getCode() const
55{
56 return code_;
57}
58
59inline void
60StatusResponse::setCode(uint32_t code)
61{
62 code_ = code;
63 wire_.reset();
64}
65
66inline const std::string &
67StatusResponse::getInfo() const
68{
69 return info_;
70}
71
72inline void
73StatusResponse::setInfo(const std::string &info)
74{
75 info_ = info;
76 wire_.reset();
77}
78
79
80inline const Block&
81StatusResponse::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
100inline void
101StatusResponse::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
115inline std::ostream&
116operator << (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