blob: 67e344c64baf2f59792fa85e6439cb100b45fce0 [file] [log] [blame]
Wentao Shang4d5e1de2014-01-28 21:00:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 *
5 * @author: Wentao Shang <wentao@cs.ucla.edu>
6 *
7 * See COPYING for copyright and distribution information.
8 */
9
Alexander Afanasyeve289b532014-02-09 22:14:44 -080010#ifndef NDN_MANAGEMENT_NFD_FIB_MANAGEMENT_OPTIONS_HPP
11#define NDN_MANAGEMENT_NFD_FIB_MANAGEMENT_OPTIONS_HPP
Wentao Shang4d5e1de2014-01-28 21:00:03 -080012
Alexander Afanasyev274f2b02014-01-30 20:08:45 -080013#include "../encoding/block.hpp"
Wentao Shang77949212014-02-01 23:42:24 -080014#include "../encoding/encoding-buffer.hpp"
Alexander Afanasyev274f2b02014-01-30 20:08:45 -080015#include "../encoding/tlv-nfd-control.hpp"
Steve DiBenedetto73f428d2014-01-31 11:56:38 -070016#include "../name.hpp"
Wentao Shang4d5e1de2014-01-28 21:00:03 -080017
18namespace ndn {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080019namespace nfd {
Wentao Shang4d5e1de2014-01-28 21:00:03 -080020
21class FibManagementOptions {
22public:
Alexander Afanasyeve289b532014-02-09 22:14:44 -080023 struct Error : public Tlv::Error { Error(const std::string &what) : Tlv::Error(what) {} };
24
Wentao Shang4d5e1de2014-01-28 21:00:03 -080025 FibManagementOptions ()
26 : faceId_ (-1)
27 , cost_ (-1)
28 {
29 }
Alexander Afanasyeve289b532014-02-09 22:14:44 -080030
31 FibManagementOptions(const Block& block)
32 {
33 wireDecode(block);
34 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080035
36 const Name&
Alexander Afanasyeve289b532014-02-09 22:14:44 -080037 getName () const
38 {
39 return name_;
40 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080041
Alexander Afanasyeve289b532014-02-09 22:14:44 -080042 FibManagementOptions&
43 setName (const Name &name)
44 {
45 name_ = name;
46 wire_.reset ();
47 return *this;
48 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080049
50 int
Alexander Afanasyeve289b532014-02-09 22:14:44 -080051 getFaceId () const
52 {
53 return faceId_;
54 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080055
Alexander Afanasyeve289b532014-02-09 22:14:44 -080056 FibManagementOptions&
57 setFaceId (int faceId)
58 {
59 faceId_ = faceId;
60 wire_.reset ();
61 return *this;
62 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080063
64 int
Alexander Afanasyeve289b532014-02-09 22:14:44 -080065 getCost () const
66 {
67 return cost_;
68 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080069
Alexander Afanasyeve289b532014-02-09 22:14:44 -080070 FibManagementOptions&
71 setCost (int cost)
72 {
73 cost_ = cost;
74 wire_.reset ();
75 return *this;
76 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080077
Alexander Afanasyeve289b532014-02-09 22:14:44 -080078 template<bool T>
79 size_t
80 wireEncode(EncodingImpl<T> &block) const;
Wentao Shang77949212014-02-01 23:42:24 -080081
Alexander Afanasyeve289b532014-02-09 22:14:44 -080082 const Block&
Wentao Shang4d5e1de2014-01-28 21:00:03 -080083 wireEncode () const;
84
Alexander Afanasyeve289b532014-02-09 22:14:44 -080085 void
Wentao Shang4d5e1de2014-01-28 21:00:03 -080086 wireDecode (const Block &wire);
87
88private:
89 Name name_;
90 int faceId_;
91 int cost_;
92 //Name strategy_;
93 //TODO: implement strategy after its use is properly defined
94
95 mutable Block wire_;
96};
97
Alexander Afanasyeve289b532014-02-09 22:14:44 -080098template<bool T>
Wentao Shang77949212014-02-01 23:42:24 -080099inline size_t
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800100FibManagementOptions::wireEncode(EncodingImpl<T>& blk) const
Wentao Shang77949212014-02-01 23:42:24 -0800101{
102 size_t total_len = 0;
103 if (cost_ != -1)
104 {
105 size_t var_len = blk.prependNonNegativeInteger (cost_);
106 total_len += var_len;
107 total_len += blk.prependVarNumber (var_len);
108 total_len += blk.prependVarNumber (tlv::nfd_control::Cost);
109 }
110
111 if (faceId_ != -1)
112 {
113 size_t var_len = blk.prependNonNegativeInteger (faceId_);
114 total_len += var_len;
115 total_len += blk.prependVarNumber (var_len);
116 total_len += blk.prependVarNumber (tlv::nfd_control::FaceId);
117 }
118
119 total_len += name_.wireEncode (blk);
120
121 total_len += blk.prependVarNumber (total_len);
122 total_len += blk.prependVarNumber (tlv::nfd_control::FibManagementOptions);
123 return total_len;
124}
125
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800126template
127size_t
128FibManagementOptions::wireEncode<true>(EncodingBuffer& block) const;
129
130template
131size_t
132FibManagementOptions::wireEncode<false>(EncodingEstimator& block) const;
133
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800134inline const Block&
135FibManagementOptions::wireEncode () const
136{
137 if (wire_.hasWire ())
138 return wire_;
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800139
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800140 EncodingEstimator estimator;
141 size_t estimatedSize = wireEncode(estimator);
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800142
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800143 EncodingBuffer buffer(estimatedSize, 0);
144 wireEncode(buffer);
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800145
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800146 wire_ = buffer.block();
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800147 return wire_;
148}
149
150inline void
151FibManagementOptions::wireDecode (const Block &wire)
152{
153 name_.clear ();
154 faceId_ = -1;
155 cost_ = -1;
156
157 wire_ = wire;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800158
159 if (wire_.type() != tlv::nfd_control::FibManagementOptions)
160 throw Error("Requested decoding of FibManagementOptions, but Block is of different type");
161
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800162 wire_.parse ();
163
164 // Name
165 Block::element_iterator val = wire_.find(Tlv::Name);
166 if (val != wire_.getAll().end())
167 {
168 name_.wireDecode(*val);
169 }
170
171 // FaceID
172 val = wire_.find(tlv::nfd_control::FaceId);
173 if (val != wire_.getAll().end())
174 {
175 faceId_ = readNonNegativeInteger(*val);
176 }
177
178 // Cost
179 val = wire_.find(tlv::nfd_control::Cost);
180 if (val != wire_.getAll().end())
181 {
182 cost_ = readNonNegativeInteger(*val);
183 }
184
185 //TODO: Strategy
186}
187
188inline std::ostream&
189operator << (std::ostream &os, const FibManagementOptions &option)
190{
191 os << "ForwardingEntry(";
192
193 // Name
194 os << "Prefix: " << option.getName () << ", ";
195
196 // FaceID
197 os << "FaceID: " << option.getFaceId () << ", ";
198
199 // Cost
200 os << "Cost: " << option.getCost ();
201
202 os << ")";
203 return os;
204}
205
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800206} // namespace nfd
207} // namespace ndn
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800208
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800209#endif // NDN_MANAGEMENT_NFD_FIB_MANAGEMENT_OPTIONS_HPP