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 | 274f2b0 | 2014-01-30 20:08:45 -0800 | [diff] [blame] | 15 | #include "../encoding/tlv-nfd-control.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 () |
| 26 | : faceId_ (-1) |
| 27 | , cost_ (-1) |
| 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 | { |
| 39 | return name_; |
| 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 | { |
| 45 | name_ = name; |
| 46 | wire_.reset (); |
| 47 | return *this; |
| 48 | } |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 49 | |
| 50 | int |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 51 | getFaceId () const |
| 52 | { |
| 53 | return faceId_; |
| 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& |
| 57 | setFaceId (int faceId) |
| 58 | { |
| 59 | faceId_ = faceId; |
| 60 | wire_.reset (); |
| 61 | return *this; |
| 62 | } |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 63 | |
| 64 | int |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 65 | getCost () const |
| 66 | { |
| 67 | return cost_; |
| 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& |
| 71 | setCost (int cost) |
| 72 | { |
| 73 | cost_ = cost; |
| 74 | wire_.reset (); |
| 75 | return *this; |
| 76 | } |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 78 | template<bool T> |
| 79 | size_t |
| 80 | wireEncode(EncodingImpl<T> &block) const; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 81 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 82 | const Block& |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 83 | wireEncode () const; |
| 84 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 85 | void |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 86 | wireDecode (const Block &wire); |
| 87 | |
| 88 | private: |
| 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 Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 98 | template<bool T> |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 99 | inline size_t |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 100 | FibManagementOptions::wireEncode(EncodingImpl<T>& blk) const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 101 | { |
| 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 Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 126 | template |
| 127 | size_t |
| 128 | FibManagementOptions::wireEncode<true>(EncodingBuffer& block) const; |
| 129 | |
| 130 | template |
| 131 | size_t |
| 132 | FibManagementOptions::wireEncode<false>(EncodingEstimator& block) const; |
| 133 | |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 134 | inline const Block& |
| 135 | FibManagementOptions::wireEncode () const |
| 136 | { |
| 137 | if (wire_.hasWire ()) |
| 138 | return wire_; |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 140 | EncodingEstimator estimator; |
| 141 | size_t estimatedSize = wireEncode(estimator); |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 143 | EncodingBuffer buffer(estimatedSize, 0); |
| 144 | wireEncode(buffer); |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 146 | wire_ = buffer.block(); |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 147 | return wire_; |
| 148 | } |
| 149 | |
| 150 | inline void |
| 151 | FibManagementOptions::wireDecode (const Block &wire) |
| 152 | { |
| 153 | name_.clear (); |
| 154 | faceId_ = -1; |
| 155 | cost_ = -1; |
| 156 | |
| 157 | wire_ = wire; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 158 | |
| 159 | if (wire_.type() != tlv::nfd_control::FibManagementOptions) |
| 160 | throw Error("Requested decoding of FibManagementOptions, but Block is of different type"); |
| 161 | |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 162 | 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 | |
| 188 | inline std::ostream& |
| 189 | operator << (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 Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 206 | } // namespace nfd |
| 207 | } // namespace ndn |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 208 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 209 | #endif // NDN_MANAGEMENT_NFD_FIB_MANAGEMENT_OPTIONS_HPP |