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 |
Alexander Afanasyev | d453f07 | 2014-01-12 22:29:05 -0800 | [diff] [blame^] | 111 | wire_.push_back |
| 112 | (prefix_.wireEncode()); |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 113 | |
| 114 | // FaceID |
| 115 | if (faceId_ >= 0) |
| 116 | { |
| 117 | wire_.push_back |
| 118 | (nonNegativeIntegerBlock(Tlv::FaceManagement::FaceID, faceId_)); |
| 119 | } |
| 120 | |
| 121 | // ForwardingFlags |
| 122 | wire_.push_back |
| 123 | (forwardingFlags_.wireEncode()); |
| 124 | |
| 125 | // FreshnessPeriod |
| 126 | if (freshnessPeriod_ >= 0) |
| 127 | { |
| 128 | wire_.push_back |
| 129 | (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_)); |
| 130 | } |
| 131 | |
| 132 | wire_.encode(); |
| 133 | return wire_; |
| 134 | } |
| 135 | |
| 136 | inline void |
| 137 | ForwardingEntry::wireDecode(const Block &wire) |
| 138 | { |
| 139 | action_.clear(); |
| 140 | prefix_.clear(); |
| 141 | faceId_ = -1; |
| 142 | forwardingFlags_ = ForwardingFlags(); |
| 143 | freshnessPeriod_ = -1; |
| 144 | |
| 145 | wire_ = wire; |
| 146 | wire_.parse(); |
| 147 | |
| 148 | // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH |
| 149 | // Action? |
| 150 | // Name? |
| 151 | // FaceID? |
| 152 | // ForwardingFlags? |
| 153 | // FreshnessPeriod? |
| 154 | |
| 155 | // Action |
| 156 | Block::element_iterator val = wire_.find(Tlv::FaceManagement::Action); |
| 157 | if (val != wire_.getAll().end()) |
| 158 | { |
| 159 | action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 160 | } |
| 161 | |
| 162 | // Name |
| 163 | val = wire_.find(Tlv::Name); |
| 164 | if (val != wire_.getAll().end()) |
| 165 | { |
| 166 | prefix_.wireDecode(*val); |
| 167 | } |
| 168 | |
| 169 | // FaceID |
| 170 | val = wire_.find(Tlv::FaceManagement::FaceID); |
| 171 | if (val != wire_.getAll().end()) |
| 172 | { |
| 173 | faceId_ = readNonNegativeInteger(*val); |
| 174 | } |
| 175 | |
| 176 | // ForwardingFlags |
| 177 | val = wire_.find(Tlv::FaceManagement::ForwardingFlags); |
| 178 | if (val != wire_.getAll().end()) |
| 179 | { |
| 180 | forwardingFlags_.wireDecode(*val); |
| 181 | } |
| 182 | |
| 183 | // FreshnessPeriod |
| 184 | val = wire_.find(Tlv::FreshnessPeriod); |
| 185 | if (val != wire_.getAll().end()) |
| 186 | { |
| 187 | freshnessPeriod_ = readNonNegativeInteger(*val); |
| 188 | } |
| 189 | } |
| 190 | |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 191 | inline std::ostream& |
| 192 | operator << (std::ostream &os, const ForwardingEntry &entry) |
| 193 | { |
| 194 | os << "ForwardingEntry("; |
| 195 | |
| 196 | // Action |
| 197 | if (!entry.getAction().empty()) |
| 198 | { |
| 199 | os << "Action:" << entry.getAction() << ", "; |
| 200 | } |
| 201 | |
| 202 | // Name |
Alexander Afanasyev | d453f07 | 2014-01-12 22:29:05 -0800 | [diff] [blame^] | 203 | os << "Prefix:" << entry.getPrefix() << ", "; |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 204 | |
| 205 | // FaceID |
| 206 | if (entry.getFaceId() >= 0) |
| 207 | { |
| 208 | os << "FaceID:" << entry.getFaceId() << ", "; |
| 209 | } |
| 210 | |
| 211 | // ForwardingFlags |
| 212 | os << "ForwardingFlags:" << entry.getForwardingFlags() << ", "; |
| 213 | |
| 214 | // FreshnessPeriod |
| 215 | if (entry.getFreshnessPeriod() >= 0) |
| 216 | { |
| 217 | os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", "; |
| 218 | } |
| 219 | |
| 220 | os << ")"; |
| 221 | return os; |
| 222 | } |
| 223 | |
Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | #endif |