blob: c27e2fb414c01502bc80ef36346e362d6a5682c9 [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 Afanasyev6d48bc12014-02-18 00:10:51 -080015#include "../encoding/tlv-nfd.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);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800108 total_len += blk.prependVarNumber (tlv::nfd::Cost);
Wentao Shang77949212014-02-01 23:42:24 -0800109 }
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);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800116 total_len += blk.prependVarNumber (tlv::nfd::FaceId);
Wentao Shang77949212014-02-01 23:42:24 -0800117 }
118
119 total_len += name_.wireEncode (blk);
120
121 total_len += blk.prependVarNumber (total_len);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800122 total_len += blk.prependVarNumber (tlv::nfd::FibManagementOptions);
Wentao Shang77949212014-02-01 23:42:24 -0800123 return total_len;
124}
125
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800126inline const Block&
127FibManagementOptions::wireEncode () const
128{
129 if (wire_.hasWire ())
130 return wire_;
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800131
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800132 EncodingEstimator estimator;
133 size_t estimatedSize = wireEncode(estimator);
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800134
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800135 EncodingBuffer buffer(estimatedSize, 0);
136 wireEncode(buffer);
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800137
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800138 wire_ = buffer.block();
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800139 return wire_;
140}
141
142inline void
143FibManagementOptions::wireDecode (const Block &wire)
144{
145 name_.clear ();
146 faceId_ = -1;
147 cost_ = -1;
148
149 wire_ = wire;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800150
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800151 if (wire_.type() != tlv::nfd::FibManagementOptions)
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800152 throw Error("Requested decoding of FibManagementOptions, but Block is of different type");
153
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800154 wire_.parse ();
155
156 // Name
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800157 Block::element_const_iterator val = wire_.find(Tlv::Name);
158 if (val != wire_.elements_end())
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800159 {
160 name_.wireDecode(*val);
161 }
162
163 // FaceID
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800164 val = wire_.find(tlv::nfd::FaceId);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800165 if (val != wire_.elements_end())
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800166 {
167 faceId_ = readNonNegativeInteger(*val);
168 }
169
170 // Cost
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800171 val = wire_.find(tlv::nfd::Cost);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800172 if (val != wire_.elements_end())
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800173 {
174 cost_ = readNonNegativeInteger(*val);
175 }
176
177 //TODO: Strategy
178}
179
180inline std::ostream&
181operator << (std::ostream &os, const FibManagementOptions &option)
182{
183 os << "ForwardingEntry(";
184
185 // Name
186 os << "Prefix: " << option.getName () << ", ";
187
188 // FaceID
189 os << "FaceID: " << option.getFaceId () << ", ";
190
191 // Cost
192 os << "Cost: " << option.getCost ();
193
194 os << ")";
195 return os;
196}
197
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800198} // namespace nfd
199} // namespace ndn
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800200
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800201#endif // NDN_MANAGEMENT_NFD_FIB_MANAGEMENT_OPTIONS_HPP