Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 10 | #ifndef NDN_MANAGEMENT_NFD_FIB_MANAGEMENT_OPTIONS_HPP |
| 11 | #define NDN_MANAGEMENT_NFD_FIB_MANAGEMENT_OPTIONS_HPP |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 12 | |
Alexander Afanasyev | 274f2b0 | 2014-01-30 20:08:45 -0800 | [diff] [blame] | 13 | #include "../encoding/block.hpp" |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 14 | #include "../encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 15 | #include "../encoding/tlv-nfd.hpp" |
Steve DiBenedetto | 73f428d | 2014-01-31 11:56:38 -0700 | [diff] [blame] | 16 | #include "../name.hpp" |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 17 | |
| 18 | namespace ndn { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 19 | namespace nfd { |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 20 | |
| 21 | class FibManagementOptions { |
| 22 | public: |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 23 | struct Error : public Tlv::Error { Error(const std::string &what) : Tlv::Error(what) {} }; |
| 24 | |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 25 | FibManagementOptions () |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 26 | : m_faceId (std::numeric_limits<uint64_t>::max()) |
| 27 | , m_cost (0) |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 28 | { |
| 29 | } |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 30 | |
| 31 | FibManagementOptions(const Block& block) |
| 32 | { |
| 33 | wireDecode(block); |
| 34 | } |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 35 | |
| 36 | const Name& |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 37 | getName () const |
| 38 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 39 | return m_name; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 40 | } |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 42 | FibManagementOptions& |
| 43 | setName (const Name &name) |
| 44 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 45 | m_name = name; |
| 46 | m_wire.reset (); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 47 | return *this; |
| 48 | } |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 50 | uint64_t |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 51 | getFaceId () const |
| 52 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 53 | return m_faceId; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 54 | } |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 56 | FibManagementOptions& |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 57 | setFaceId (uint64_t faceId) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 58 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 59 | m_faceId = faceId; |
| 60 | m_wire.reset (); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 61 | return *this; |
| 62 | } |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 64 | uint64_t |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 65 | getCost () const |
| 66 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 67 | return m_cost; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 68 | } |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 69 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 70 | FibManagementOptions& |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 71 | setCost (uint64_t cost) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 72 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 73 | m_cost = cost; |
| 74 | m_wire.reset (); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 75 | return *this; |
| 76 | } |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 78 | 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 Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 92 | template<bool T> |
| 93 | size_t |
| 94 | wireEncode(EncodingImpl<T> &block) const; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 96 | const Block& |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 97 | wireEncode () const; |
| 98 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 99 | void |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 100 | wireDecode (const Block &wire); |
| 101 | |
| 102 | private: |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 103 | Name m_name; |
| 104 | uint64_t m_faceId; |
| 105 | uint64_t m_cost; |
| 106 | Name m_strategy; |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 108 | mutable Block m_wire; |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 111 | template<bool T> |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 112 | inline size_t |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 113 | FibManagementOptions::wireEncode(EncodingImpl<T>& blk) const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 114 | { |
| 115 | size_t total_len = 0; |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 116 | |
| 117 | // if (!m_strategy.empty()) |
| 118 | // { |
| 119 | // total_len += prependNestedBlock(blk, tlv::nfd::Strategy, m_strategy); |
| 120 | // } |
| 121 | |
| 122 | if (m_cost != 0) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 123 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 124 | total_len += prependNonNegativeIntegerBlock(blk, tlv::nfd::Cost, m_cost); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 127 | if (m_faceId != std::numeric_limits<uint64_t>::max()) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 128 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 129 | total_len += prependNonNegativeIntegerBlock(blk, tlv::nfd::FaceId, m_faceId); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 132 | total_len += m_name.wireEncode(blk); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 133 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 134 | total_len += blk.prependVarNumber(total_len); |
| 135 | total_len += blk.prependVarNumber(tlv::nfd::FibManagementOptions); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 136 | return total_len; |
| 137 | } |
| 138 | |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 139 | inline const Block& |
| 140 | FibManagementOptions::wireEncode () const |
| 141 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 142 | if (m_wire.hasWire ()) |
| 143 | return m_wire; |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 144 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 145 | EncodingEstimator estimator; |
| 146 | size_t estimatedSize = wireEncode(estimator); |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 147 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 148 | EncodingBuffer buffer(estimatedSize, 0); |
| 149 | wireEncode(buffer); |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 150 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 151 | m_wire = buffer.block(); |
| 152 | return m_wire; |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | inline void |
| 156 | FibManagementOptions::wireDecode (const Block &wire) |
| 157 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 158 | m_name.clear(); |
| 159 | m_faceId = std::numeric_limits<uint64_t>::max(); |
| 160 | m_cost = 0; |
| 161 | m_strategy.clear(); |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 162 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 163 | m_wire = wire; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 165 | if (m_wire.type() != tlv::nfd::FibManagementOptions) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 166 | throw Error("Requested decoding of FibManagementOptions, but Block is of different type"); |
| 167 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 168 | m_wire.parse (); |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 169 | |
| 170 | // Name |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 171 | Block::element_const_iterator val = m_wire.find(Tlv::Name); |
| 172 | if (val != m_wire.elements_end()) |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 173 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 174 | m_name.wireDecode(*val); |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // FaceID |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 178 | val = m_wire.find(tlv::nfd::FaceId); |
| 179 | if (val != m_wire.elements_end()) |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 180 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 181 | m_faceId = readNonNegativeInteger(*val); |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Cost |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 185 | val = m_wire.find(tlv::nfd::Cost); |
| 186 | if (val != m_wire.elements_end()) |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 187 | { |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 188 | m_cost = readNonNegativeInteger(*val); |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 191 | // 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 Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | inline std::ostream& |
| 202 | operator << (std::ostream &os, const FibManagementOptions &option) |
| 203 | { |
| 204 | os << "ForwardingEntry("; |
| 205 | |
| 206 | // Name |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 207 | os << "Prefix: " << option.getName() << ", "; |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 208 | |
| 209 | // FaceID |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 210 | os << "FaceID: " << option.getFaceId() << ", "; |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 211 | |
| 212 | // Cost |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 213 | os << "Cost: " << option.getCost() << ", "; |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 214 | |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 215 | // Strategy |
| 216 | os << "Strategy: " << option.getStrategy() << ", "; |
| 217 | |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 218 | os << ")"; |
| 219 | return os; |
| 220 | } |
| 221 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 222 | } // namespace nfd |
| 223 | } // namespace ndn |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 224 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 225 | #endif // NDN_MANAGEMENT_NFD_FIB_MANAGEMENT_OPTIONS_HPP |