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