Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 7 | #ifndef NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP |
| 8 | #define NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 9 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame^] | 10 | #include "../encoding/tlv-ndnd.hpp" |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 11 | #include "../name.hpp" |
| 12 | #include "../encoding/block.hpp" |
| 13 | |
| 14 | #include "ndnd-forwarding-flags.hpp" |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 15 | |
| 16 | namespace ndn { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 17 | namespace ndnd { |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * An ForwardingEntry holds an action and Name prefix and other fields for an forwarding entry. |
| 21 | */ |
| 22 | class ForwardingEntry { |
| 23 | public: |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 24 | ForwardingEntry(const std::string& action, |
| 25 | const Name& prefix, |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 26 | int faceId = -1, |
| 27 | const ForwardingFlags& forwardingFlags = ForwardingFlags(), |
| 28 | int freshnessPeriod = -1) |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 29 | : action_(action) |
| 30 | , prefix_(prefix) |
| 31 | , faceId_(faceId) |
| 32 | , forwardingFlags_(forwardingFlags) |
| 33 | , freshnessPeriod_(freshnessPeriod) |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
| 37 | ForwardingEntry() |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 38 | : faceId_(-1) |
| 39 | , freshnessPeriod_(-1) |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 40 | { |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 41 | } |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 42 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 43 | const std::string& |
| 44 | getAction() const { return action_; } |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 45 | |
| 46 | void |
| 47 | setAction(const std::string& action) { action_ = action; wire_.reset(); } |
| 48 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 49 | const Name& |
| 50 | getPrefix() const { return prefix_; } |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 52 | void |
| 53 | setPrefix(const Name &prefix) { prefix_ = prefix; wire_.reset(); } |
| 54 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 55 | int |
| 56 | getFaceId() const { return faceId_; } |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 58 | void |
| 59 | setFaceId(int faceId) { faceId_ = faceId; wire_.reset(); } |
| 60 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 61 | const ForwardingFlags& |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 62 | getForwardingFlags() const { return forwardingFlags_; } |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 63 | |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 64 | void |
| 65 | setForwardingFlags(const ForwardingFlags& forwardingFlags) { forwardingFlags_ = forwardingFlags; wire_.reset(); } |
| 66 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 67 | int |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 68 | getFreshnessPeriod() const { return freshnessPeriod_; } |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 69 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 70 | void |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 71 | setFreshnessPeriod(int freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); } |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 72 | |
| 73 | inline const Block& |
| 74 | wireEncode() const; |
| 75 | |
| 76 | inline void |
| 77 | wireDecode(const Block &wire); |
| 78 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 79 | private: |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 80 | std::string action_; /**< empty for none. */ |
| 81 | Name prefix_; |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 82 | int faceId_; /**< -1 for none. */ |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 83 | ForwardingFlags forwardingFlags_; |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 84 | int freshnessPeriod_; /**< -1 for none. */ |
| 85 | |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 86 | mutable Block wire_; |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 89 | inline const Block& |
| 90 | ForwardingEntry::wireEncode() const |
| 91 | { |
| 92 | if (wire_.hasWire()) |
| 93 | return wire_; |
| 94 | |
| 95 | // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH |
| 96 | // Action? |
| 97 | // Name? |
| 98 | // FaceID? |
| 99 | // ForwardingFlags? |
| 100 | // FreshnessPeriod? |
| 101 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame^] | 102 | wire_ = Block(tlv::ndnd::ForwardingEntry); |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 103 | |
| 104 | // Action |
| 105 | if (!action_.empty()) |
| 106 | { |
| 107 | wire_.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame^] | 108 | (dataBlock(tlv::ndnd::Action, action_.c_str(), action_.size())); |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Name |
Alexander Afanasyev | d453f07 | 2014-01-12 22:29:05 -0800 | [diff] [blame] | 112 | wire_.push_back |
| 113 | (prefix_.wireEncode()); |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 114 | |
| 115 | // FaceID |
| 116 | if (faceId_ >= 0) |
| 117 | { |
| 118 | wire_.push_back |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame^] | 119 | (nonNegativeIntegerBlock(tlv::ndnd::FaceID, faceId_)); |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // ForwardingFlags |
| 123 | wire_.push_back |
| 124 | (forwardingFlags_.wireEncode()); |
| 125 | |
| 126 | // FreshnessPeriod |
| 127 | if (freshnessPeriod_ >= 0) |
| 128 | { |
| 129 | wire_.push_back |
| 130 | (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_)); |
| 131 | } |
| 132 | |
| 133 | wire_.encode(); |
| 134 | return wire_; |
| 135 | } |
| 136 | |
| 137 | inline void |
| 138 | ForwardingEntry::wireDecode(const Block &wire) |
| 139 | { |
| 140 | action_.clear(); |
| 141 | prefix_.clear(); |
| 142 | faceId_ = -1; |
| 143 | forwardingFlags_ = ForwardingFlags(); |
| 144 | freshnessPeriod_ = -1; |
| 145 | |
| 146 | wire_ = wire; |
| 147 | wire_.parse(); |
| 148 | |
| 149 | // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH |
| 150 | // Action? |
| 151 | // Name? |
| 152 | // FaceID? |
| 153 | // ForwardingFlags? |
| 154 | // FreshnessPeriod? |
| 155 | |
| 156 | // Action |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame^] | 157 | Block::element_const_iterator val = wire_.find(tlv::ndnd::Action); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 158 | if (val != wire_.elements_end()) |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 159 | { |
| 160 | action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 161 | } |
| 162 | |
| 163 | // Name |
| 164 | val = wire_.find(Tlv::Name); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 165 | if (val != wire_.elements_end()) |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 166 | { |
| 167 | prefix_.wireDecode(*val); |
| 168 | } |
| 169 | |
| 170 | // FaceID |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame^] | 171 | val = wire_.find(tlv::ndnd::FaceID); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 172 | if (val != wire_.elements_end()) |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 173 | { |
| 174 | faceId_ = readNonNegativeInteger(*val); |
| 175 | } |
| 176 | |
| 177 | // ForwardingFlags |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame^] | 178 | val = wire_.find(tlv::ndnd::ForwardingFlags); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 179 | if (val != wire_.elements_end()) |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 180 | { |
| 181 | forwardingFlags_.wireDecode(*val); |
| 182 | } |
| 183 | |
| 184 | // FreshnessPeriod |
| 185 | val = wire_.find(Tlv::FreshnessPeriod); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 186 | if (val != wire_.elements_end()) |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 187 | { |
| 188 | freshnessPeriod_ = readNonNegativeInteger(*val); |
| 189 | } |
| 190 | } |
| 191 | |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 192 | inline std::ostream& |
| 193 | operator << (std::ostream &os, const ForwardingEntry &entry) |
| 194 | { |
| 195 | os << "ForwardingEntry("; |
| 196 | |
| 197 | // Action |
| 198 | if (!entry.getAction().empty()) |
| 199 | { |
| 200 | os << "Action:" << entry.getAction() << ", "; |
| 201 | } |
| 202 | |
| 203 | // Name |
Alexander Afanasyev | d453f07 | 2014-01-12 22:29:05 -0800 | [diff] [blame] | 204 | os << "Prefix:" << entry.getPrefix() << ", "; |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 205 | |
| 206 | // FaceID |
| 207 | if (entry.getFaceId() >= 0) |
| 208 | { |
| 209 | os << "FaceID:" << entry.getFaceId() << ", "; |
| 210 | } |
| 211 | |
| 212 | // ForwardingFlags |
| 213 | os << "ForwardingFlags:" << entry.getForwardingFlags() << ", "; |
| 214 | |
| 215 | // FreshnessPeriod |
| 216 | if (entry.getFreshnessPeriod() >= 0) |
| 217 | { |
| 218 | os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", "; |
| 219 | } |
| 220 | |
| 221 | os << ")"; |
| 222 | return os; |
| 223 | } |
| 224 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 225 | } // namespace ndnd |
| 226 | } // namespace ndn |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 227 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 228 | #endif // NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP |