blob: 20640dfebe4e64d98070c245fea08fcad75a179b [file] [log] [blame]
Yingdi Yud3370492014-02-12 17:17:11 -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_MANAGEMENT_NFD_FACE_MANAGEMENT_OPTIONS_HPP
8#define NDN_MANAGEMENT_NFD_FACE_MANAGEMENT_OPTIONS_HPP
9
10#include "../encoding/encoding-buffer.hpp"
11#include "../encoding/tlv-nfd-control.hpp"
12
13namespace ndn {
14namespace nfd {
15
16class FaceManagementOptions {
17public:
18 struct Error : public Tlv::Error { Error(const std::string &what) : Tlv::Error(what) {} };
19
20 FaceManagementOptions ()
21 : m_faceId (INVALID_FACE_ID)
22 {
23 }
24
25 FaceManagementOptions(const Block& block)
26 {
27 wireDecode(block);
28 }
29
30 uint64_t
31 getFaceId () const
32 {
33 return m_faceId;
34 }
35
36 FaceManagementOptions&
37 setFaceId (uint64_t faceId)
38 {
39 m_faceId = faceId;
40 wire_.reset ();
41 return *this;
42 }
43
44 const std::string&
45 getUri () const
46 {
47 return m_uri;
48 }
49
50 FaceManagementOptions&
51 setUri (const std::string& uri)
52 {
53 m_uri = uri;
54 wire_.reset ();
55 return *this;
56 }
57
58 template<bool T>
59 size_t
60 wireEncode(EncodingImpl<T> &block) const;
61
62 const Block&
63 wireEncode () const;
64
65 void
66 wireDecode (const Block &wire);
67
68private:
69 static const uint64_t INVALID_FACE_ID;
70
71 uint64_t m_faceId;
72 std::string m_uri;
73
74 mutable Block wire_;
75};
76
77const uint64_t FaceManagementOptions::INVALID_FACE_ID = std::numeric_limits<uint64_t>::max();
78
79template<bool T>
80inline size_t
81FaceManagementOptions::wireEncode(EncodingImpl<T>& blk) const
82{
83 size_t total_len = 0;
84
85 if (!m_uri.empty())
86 {
87 size_t var_len = blk.prependByteArray (reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size());
88 total_len += var_len;
89 total_len += blk.prependVarNumber (var_len);
90 total_len += blk.prependVarNumber (tlv::nfd_control::Uri);
91 }
92
93 if (m_faceId != INVALID_FACE_ID)
94 {
95 size_t var_len = blk.prependNonNegativeInteger (m_faceId);
96 total_len += var_len;
97 total_len += blk.prependVarNumber (var_len);
98 total_len += blk.prependVarNumber (tlv::nfd_control::FaceId);
99 }
100
101 total_len += blk.prependVarNumber (total_len);
102 total_len += blk.prependVarNumber (tlv::nfd_control::FaceManagementOptions);
103 return total_len;
104}
105
106inline const Block&
107FaceManagementOptions::wireEncode () const
108{
109 if (wire_.hasWire ())
110 return wire_;
111
112 EncodingEstimator estimator;
113 size_t estimatedSize = wireEncode(estimator);
114
115 EncodingBuffer buffer(estimatedSize, 0);
116 wireEncode(buffer);
117
118 wire_ = buffer.block();
119 return wire_;
120}
121
122inline void
123FaceManagementOptions::wireDecode (const Block &wire)
124{
125 m_uri.clear ();
126 m_faceId = INVALID_FACE_ID;
127
128 wire_ = wire;
129
130 if (wire_.type() != tlv::nfd_control::FaceManagementOptions)
131 throw Error("Requested decoding of FaceManagementOptions, but Block is of different type");
132
133 wire_.parse ();
134
135 // FaceID
136 Block::element_const_iterator val = wire_.find(tlv::nfd_control::FaceId);
137 if (val != wire_.elements_end())
138 {
139 m_faceId = readNonNegativeInteger(*val);
140 }
141
142 // Uri
143 val = wire_.find(tlv::nfd_control::Uri);
144 if (val != wire_.elements_end())
145 {
146 m_uri.append(reinterpret_cast<const char*>(val->value()), val->value_size());
147 }
148}
149
150inline std::ostream&
151operator << (std::ostream &os, const FaceManagementOptions &option)
152{
153 os << "ForwardingEntry(";
154
155 // FaceID
156 os << "FaceID: " << option.getFaceId () << ", ";
157
158 // Uri
159 os << "Uri: " << option.getUri ();
160
161 os << ")";
162 return os;
163}
164
165} // namespace nfd
166} // namespace ndn
167
168#endif // NDN_MANAGEMENT_NFD_FACE_MANAGEMENT_OPTIONS_HPP