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