blob: 16d688eed71631733e4df36541bbc015d24be958 [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 ()
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080026 : m_faceId (std::numeric_limits<uint64_t>::max())
27 , m_cost (0)
Wentao Shang4d5e1de2014-01-28 21:00:03 -080028 {
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 {
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080039 return m_name;
Alexander Afanasyeve289b532014-02-09 22:14:44 -080040 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080041
Alexander Afanasyeve289b532014-02-09 22:14:44 -080042 FibManagementOptions&
43 setName (const Name &name)
44 {
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080045 m_name = name;
46 m_wire.reset ();
Alexander Afanasyeve289b532014-02-09 22:14:44 -080047 return *this;
48 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080049
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080050 uint64_t
Alexander Afanasyeve289b532014-02-09 22:14:44 -080051 getFaceId () const
52 {
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080053 return m_faceId;
Alexander Afanasyeve289b532014-02-09 22:14:44 -080054 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080055
Alexander Afanasyeve289b532014-02-09 22:14:44 -080056 FibManagementOptions&
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080057 setFaceId (uint64_t faceId)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080058 {
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080059 m_faceId = faceId;
60 m_wire.reset ();
Alexander Afanasyeve289b532014-02-09 22:14:44 -080061 return *this;
62 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080063
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080064 uint64_t
Alexander Afanasyeve289b532014-02-09 22:14:44 -080065 getCost () const
66 {
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080067 return m_cost;
Alexander Afanasyeve289b532014-02-09 22:14:44 -080068 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080069
Alexander Afanasyeve289b532014-02-09 22:14:44 -080070 FibManagementOptions&
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080071 setCost (uint64_t cost)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080072 {
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080073 m_cost = cost;
74 m_wire.reset ();
Alexander Afanasyeve289b532014-02-09 22:14:44 -080075 return *this;
76 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -080077
Alexander Afanasyev0553cd52014-02-18 02:34:45 -080078 const Name&
79 getStrategy () const
80 {
81 return m_strategy;
82 }
83
84 FibManagementOptions&
85 setStrategy (const Name& strategy)
86 {
87 m_strategy = strategy;
88 m_wire.reset ();
89 return *this;
90 }
91
Alexander Afanasyeve289b532014-02-09 22:14:44 -080092 template<bool T>
93 size_t
94 wireEncode(EncodingImpl<T> &block) const;
Wentao Shang77949212014-02-01 23:42:24 -080095
Alexander Afanasyeve289b532014-02-09 22:14:44 -080096 const Block&
Wentao Shang4d5e1de2014-01-28 21:00:03 -080097 wireEncode () const;
98
Alexander Afanasyeve289b532014-02-09 22:14:44 -080099 void
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800100 wireDecode (const Block &wire);
101
102private:
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800103 Name m_name;
104 uint64_t m_faceId;
105 uint64_t m_cost;
106 Name m_strategy;
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800107
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800108 mutable Block m_wire;
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800109};
110
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800111template<bool T>
Wentao Shang77949212014-02-01 23:42:24 -0800112inline size_t
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800113FibManagementOptions::wireEncode(EncodingImpl<T>& blk) const
Wentao Shang77949212014-02-01 23:42:24 -0800114{
115 size_t total_len = 0;
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800116
117 // if (!m_strategy.empty())
118 // {
119 // total_len += prependNestedBlock(blk, tlv::nfd::Strategy, m_strategy);
120 // }
121
122 if (m_cost != 0)
Wentao Shang77949212014-02-01 23:42:24 -0800123 {
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800124 total_len += prependNonNegativeIntegerBlock(blk, tlv::nfd::Cost, m_cost);
Wentao Shang77949212014-02-01 23:42:24 -0800125 }
126
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800127 if (m_faceId != std::numeric_limits<uint64_t>::max())
Wentao Shang77949212014-02-01 23:42:24 -0800128 {
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800129 total_len += prependNonNegativeIntegerBlock(blk, tlv::nfd::FaceId, m_faceId);
Wentao Shang77949212014-02-01 23:42:24 -0800130 }
131
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800132 total_len += m_name.wireEncode(blk);
Wentao Shang77949212014-02-01 23:42:24 -0800133
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800134 total_len += blk.prependVarNumber(total_len);
135 total_len += blk.prependVarNumber(tlv::nfd::FibManagementOptions);
Wentao Shang77949212014-02-01 23:42:24 -0800136 return total_len;
137}
138
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800139inline const Block&
140FibManagementOptions::wireEncode () const
141{
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800142 if (m_wire.hasWire ())
143 return m_wire;
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800144
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800145 EncodingEstimator estimator;
146 size_t estimatedSize = wireEncode(estimator);
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800147
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800148 EncodingBuffer buffer(estimatedSize, 0);
149 wireEncode(buffer);
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800150
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800151 m_wire = buffer.block();
152 return m_wire;
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800153}
154
155inline void
156FibManagementOptions::wireDecode (const Block &wire)
157{
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800158 m_name.clear();
159 m_faceId = std::numeric_limits<uint64_t>::max();
160 m_cost = 0;
161 m_strategy.clear();
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800162
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800163 m_wire = wire;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800164
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800165 if (m_wire.type() != tlv::nfd::FibManagementOptions)
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800166 throw Error("Requested decoding of FibManagementOptions, but Block is of different type");
167
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800168 m_wire.parse ();
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800169
170 // Name
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800171 Block::element_const_iterator val = m_wire.find(Tlv::Name);
172 if (val != m_wire.elements_end())
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800173 {
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800174 m_name.wireDecode(*val);
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800175 }
176
177 // FaceID
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800178 val = m_wire.find(tlv::nfd::FaceId);
179 if (val != m_wire.elements_end())
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800180 {
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800181 m_faceId = readNonNegativeInteger(*val);
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800182 }
183
184 // Cost
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800185 val = m_wire.find(tlv::nfd::Cost);
186 if (val != m_wire.elements_end())
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800187 {
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800188 m_cost = readNonNegativeInteger(*val);
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800189 }
190
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800191 // Strategy
192 val = m_wire.find(tlv::nfd::Strategy);
193 if (val != m_wire.elements_end())
194 {
195 val->parse();
196 if (!val->elements().empty())
197 m_strategy.wireDecode(*val->elements_begin());
198 }
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800199}
200
201inline std::ostream&
202operator << (std::ostream &os, const FibManagementOptions &option)
203{
204 os << "ForwardingEntry(";
205
206 // Name
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800207 os << "Prefix: " << option.getName() << ", ";
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800208
209 // FaceID
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800210 os << "FaceID: " << option.getFaceId() << ", ";
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800211
212 // Cost
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800213 os << "Cost: " << option.getCost() << ", ";
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800214
Alexander Afanasyev0553cd52014-02-18 02:34:45 -0800215 // Strategy
216 os << "Strategy: " << option.getStrategy() << ", ";
217
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800218 os << ")";
219 return os;
220}
221
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800222} // namespace nfd
223} // namespace ndn
Wentao Shang4d5e1de2014-01-28 21:00:03 -0800224
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800225#endif // NDN_MANAGEMENT_NFD_FIB_MANAGEMENT_OPTIONS_HPP