blob: 802cde0434f0e65563134374e9af72f32bbb7bf9 [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"
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080011#include "../encoding/tlv-nfd.hpp"
Yingdi Yud3370492014-02-12 17:17:11 -080012
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:
Yingdi Yud3370492014-02-12 17:17:11 -080069 uint64_t m_faceId;
70 std::string m_uri;
71
72 mutable Block wire_;
73};
74
Yingdi Yud3370492014-02-12 17:17:11 -080075
76template<bool T>
77inline size_t
78FaceManagementOptions::wireEncode(EncodingImpl<T>& blk) const
79{
80 size_t total_len = 0;
81
82 if (!m_uri.empty())
83 {
84 size_t var_len = blk.prependByteArray (reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size());
85 total_len += var_len;
86 total_len += blk.prependVarNumber (var_len);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080087 total_len += blk.prependVarNumber (tlv::nfd::Uri);
Yingdi Yud3370492014-02-12 17:17:11 -080088 }
89
90 if (m_faceId != INVALID_FACE_ID)
91 {
92 size_t var_len = blk.prependNonNegativeInteger (m_faceId);
93 total_len += var_len;
94 total_len += blk.prependVarNumber (var_len);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080095 total_len += blk.prependVarNumber (tlv::nfd::FaceId);
Yingdi Yud3370492014-02-12 17:17:11 -080096 }
97
98 total_len += blk.prependVarNumber (total_len);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080099 total_len += blk.prependVarNumber (tlv::nfd::FaceManagementOptions);
Yingdi Yud3370492014-02-12 17:17:11 -0800100 return total_len;
101}
102
103inline const Block&
104FaceManagementOptions::wireEncode () const
105{
106 if (wire_.hasWire ())
107 return wire_;
108
109 EncodingEstimator estimator;
110 size_t estimatedSize = wireEncode(estimator);
111
112 EncodingBuffer buffer(estimatedSize, 0);
113 wireEncode(buffer);
114
115 wire_ = buffer.block();
116 return wire_;
117}
118
119inline void
120FaceManagementOptions::wireDecode (const Block &wire)
121{
122 m_uri.clear ();
123 m_faceId = INVALID_FACE_ID;
124
125 wire_ = wire;
126
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800127 if (wire_.type() != tlv::nfd::FaceManagementOptions)
Yingdi Yud3370492014-02-12 17:17:11 -0800128 throw Error("Requested decoding of FaceManagementOptions, but Block is of different type");
129
130 wire_.parse ();
131
132 // FaceID
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800133 Block::element_const_iterator val = wire_.find(tlv::nfd::FaceId);
Yingdi Yud3370492014-02-12 17:17:11 -0800134 if (val != wire_.elements_end())
135 {
136 m_faceId = readNonNegativeInteger(*val);
137 }
138
139 // Uri
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800140 val = wire_.find(tlv::nfd::Uri);
Yingdi Yud3370492014-02-12 17:17:11 -0800141 if (val != wire_.elements_end())
142 {
143 m_uri.append(reinterpret_cast<const char*>(val->value()), val->value_size());
144 }
145}
146
147inline std::ostream&
148operator << (std::ostream &os, const FaceManagementOptions &option)
149{
150 os << "ForwardingEntry(";
151
152 // FaceID
153 os << "FaceID: " << option.getFaceId () << ", ";
154
155 // Uri
156 os << "Uri: " << option.getUri ();
157
158 os << ")";
159 return os;
160}
161
162} // namespace nfd
163} // namespace ndn
164
165#endif // NDN_MANAGEMENT_NFD_FACE_MANAGEMENT_OPTIONS_HPP