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