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