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 | |
| 10 | #ifndef NDN_FIB_MANAGEMENT_HPP |
| 11 | #define NDN_FIB_MANAGEMENT_HPP |
| 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 { |
| 19 | |
| 20 | class FibManagementOptions { |
| 21 | public: |
| 22 | FibManagementOptions () |
| 23 | : faceId_ (-1) |
| 24 | , cost_ (-1) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | const Name& |
| 29 | getName () const { return name_; } |
| 30 | |
| 31 | void |
| 32 | setName (const Name &name) { name_ = name; wire_.reset (); } |
| 33 | |
| 34 | int |
| 35 | getFaceId () const { return faceId_; } |
| 36 | |
| 37 | void |
| 38 | setFaceId (int faceId) { faceId_ = faceId; wire_.reset (); } |
| 39 | |
| 40 | int |
| 41 | getCost () const { return cost_; } |
| 42 | |
| 43 | void |
| 44 | setCost (int cost) { cost_ = cost; wire_.reset (); } |
| 45 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame^] | 46 | inline size_t |
| 47 | wireEncode (EncodingBuffer& blk); |
| 48 | |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 49 | inline const Block& |
| 50 | wireEncode () const; |
| 51 | |
| 52 | inline void |
| 53 | wireDecode (const Block &wire); |
| 54 | |
| 55 | private: |
| 56 | Name name_; |
| 57 | int faceId_; |
| 58 | int cost_; |
| 59 | //Name strategy_; |
| 60 | //TODO: implement strategy after its use is properly defined |
| 61 | |
| 62 | mutable Block wire_; |
| 63 | }; |
| 64 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame^] | 65 | inline size_t |
| 66 | FibManagementOptions::wireEncode (EncodingBuffer& blk) |
| 67 | { |
| 68 | size_t total_len = 0; |
| 69 | if (cost_ != -1) |
| 70 | { |
| 71 | size_t var_len = blk.prependNonNegativeInteger (cost_); |
| 72 | total_len += var_len; |
| 73 | total_len += blk.prependVarNumber (var_len); |
| 74 | total_len += blk.prependVarNumber (tlv::nfd_control::Cost); |
| 75 | } |
| 76 | |
| 77 | if (faceId_ != -1) |
| 78 | { |
| 79 | size_t var_len = blk.prependNonNegativeInteger (faceId_); |
| 80 | total_len += var_len; |
| 81 | total_len += blk.prependVarNumber (var_len); |
| 82 | total_len += blk.prependVarNumber (tlv::nfd_control::FaceId); |
| 83 | } |
| 84 | |
| 85 | total_len += name_.wireEncode (blk); |
| 86 | |
| 87 | total_len += blk.prependVarNumber (total_len); |
| 88 | total_len += blk.prependVarNumber (tlv::nfd_control::FibManagementOptions); |
| 89 | return total_len; |
| 90 | } |
| 91 | |
Wentao Shang | 4d5e1de | 2014-01-28 21:00:03 -0800 | [diff] [blame] | 92 | inline const Block& |
| 93 | FibManagementOptions::wireEncode () const |
| 94 | { |
| 95 | if (wire_.hasWire ()) |
| 96 | return wire_; |
| 97 | |
| 98 | wire_ = Block (tlv::nfd_control::FibManagementOptions); |
| 99 | |
| 100 | // Name |
| 101 | wire_.push_back (name_.wireEncode ()); |
| 102 | |
| 103 | // FaceId |
| 104 | if (faceId_ != -1) |
| 105 | wire_.push_back (nonNegativeIntegerBlock (tlv::nfd_control::FaceId, faceId_)); |
| 106 | |
| 107 | // Cost |
| 108 | if (cost_ != -1) |
| 109 | wire_.push_back (nonNegativeIntegerBlock (tlv::nfd_control::Cost, cost_)); |
| 110 | |
| 111 | //TODO: Strategy |
| 112 | |
| 113 | wire_.encode (); |
| 114 | return wire_; |
| 115 | } |
| 116 | |
| 117 | inline void |
| 118 | FibManagementOptions::wireDecode (const Block &wire) |
| 119 | { |
| 120 | name_.clear (); |
| 121 | faceId_ = -1; |
| 122 | cost_ = -1; |
| 123 | |
| 124 | wire_ = wire; |
| 125 | wire_.parse (); |
| 126 | |
| 127 | // Name |
| 128 | Block::element_iterator val = wire_.find(Tlv::Name); |
| 129 | if (val != wire_.getAll().end()) |
| 130 | { |
| 131 | name_.wireDecode(*val); |
| 132 | } |
| 133 | |
| 134 | // FaceID |
| 135 | val = wire_.find(tlv::nfd_control::FaceId); |
| 136 | if (val != wire_.getAll().end()) |
| 137 | { |
| 138 | faceId_ = readNonNegativeInteger(*val); |
| 139 | } |
| 140 | |
| 141 | // Cost |
| 142 | val = wire_.find(tlv::nfd_control::Cost); |
| 143 | if (val != wire_.getAll().end()) |
| 144 | { |
| 145 | cost_ = readNonNegativeInteger(*val); |
| 146 | } |
| 147 | |
| 148 | //TODO: Strategy |
| 149 | } |
| 150 | |
| 151 | inline std::ostream& |
| 152 | operator << (std::ostream &os, const FibManagementOptions &option) |
| 153 | { |
| 154 | os << "ForwardingEntry("; |
| 155 | |
| 156 | // Name |
| 157 | os << "Prefix: " << option.getName () << ", "; |
| 158 | |
| 159 | // FaceID |
| 160 | os << "FaceID: " << option.getFaceId () << ", "; |
| 161 | |
| 162 | // Cost |
| 163 | os << "Cost: " << option.getCost (); |
| 164 | |
| 165 | os << ")"; |
| 166 | return os; |
| 167 | } |
| 168 | |
| 169 | } |
| 170 | |
| 171 | #endif |