Alexander Afanasyev | 5d7db8e | 2014-01-05 22:43:57 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_FACE_INSTANCE_HPP |
| 9 | #define NDN_FACE_INSTANCE_HPP |
| 10 | |
| 11 | #include "encoding/tlv-face-management.hpp" |
| 12 | #include "name.hpp" |
| 13 | #include "encoding/block.hpp" |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | /** |
| 18 | * An FaceInstance holds an action and Name prefix and other fields for an forwarding entry. |
| 19 | */ |
| 20 | class FaceInstance { |
| 21 | public: |
| 22 | FaceInstance(const std::string &action, |
| 23 | int64_t faceId, |
| 24 | uint32_t ipProto, |
| 25 | const std::string &host, |
Alexander Afanasyev | 0a5eabf | 2014-01-02 09:03:25 -0800 | [diff] [blame^] | 26 | const std::string &port, |
Alexander Afanasyev | 5d7db8e | 2014-01-05 22:43:57 -0800 | [diff] [blame] | 27 | const std::string &multicastInterface, |
| 28 | uint32_t multicastTtl, |
| 29 | Milliseconds freshnessPeriod) |
| 30 | : action_(action) |
| 31 | , faceId_(faceId) |
| 32 | , ipProto_(ipProto) |
| 33 | , host_(host) |
| 34 | , port_(port) |
| 35 | , multicastInterface_(multicastInterface) |
| 36 | , multicastTtl_(multicastTtl) |
| 37 | , freshnessPeriod_(freshnessPeriod) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | FaceInstance() |
| 42 | : faceId_(-1) |
| 43 | , ipProto_(-1) |
Alexander Afanasyev | 5d7db8e | 2014-01-05 22:43:57 -0800 | [diff] [blame] | 44 | , multicastTtl_(-1) |
| 45 | , freshnessPeriod_(-1) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | // Action |
| 50 | const std::string& |
| 51 | getAction() const { return action_; } |
| 52 | |
| 53 | void |
| 54 | setAction(const std::string& action) { action_ = action; wire_.reset(); } |
| 55 | |
| 56 | // FaceID |
| 57 | int64_t |
| 58 | getFaceId() const { return faceId_; } |
| 59 | |
| 60 | void |
| 61 | setFaceId(int64_t faceId) { faceId_ = faceId; wire_.reset(); } |
| 62 | |
| 63 | // IPProto |
| 64 | int32_t |
| 65 | getIpProto() const { return ipProto_; } |
| 66 | |
| 67 | void |
| 68 | setIpProto(int32_t ipProto) { ipProto_ = ipProto; wire_.reset(); } |
| 69 | |
| 70 | // Host |
| 71 | const std::string& |
| 72 | getHost() const { return host_; } |
| 73 | |
| 74 | void |
| 75 | setHost(const std::string& host) { host_ = host; wire_.reset(); } |
| 76 | |
| 77 | // Port |
Alexander Afanasyev | 0a5eabf | 2014-01-02 09:03:25 -0800 | [diff] [blame^] | 78 | const std::string& |
Alexander Afanasyev | 5d7db8e | 2014-01-05 22:43:57 -0800 | [diff] [blame] | 79 | getPort() const { return port_; } |
| 80 | |
| 81 | void |
Alexander Afanasyev | 0a5eabf | 2014-01-02 09:03:25 -0800 | [diff] [blame^] | 82 | setPort(const std::string &port) { port_ = port; wire_.reset(); } |
Alexander Afanasyev | 5d7db8e | 2014-01-05 22:43:57 -0800 | [diff] [blame] | 83 | |
| 84 | // MulticastInterface |
| 85 | const std::string& |
| 86 | getMulticastInterface() const { return multicastInterface_; } |
| 87 | |
| 88 | void |
| 89 | setMulticastInterface(const std::string& multicastInterface) { multicastInterface_ = multicastInterface; wire_.reset(); } |
| 90 | |
| 91 | // MulticastTTL |
| 92 | int32_t |
| 93 | getMulticastTtl() const { return multicastTtl_; } |
| 94 | |
| 95 | void |
| 96 | setMulticastTtl(int32_t multicastTtl) { multicastTtl_ = multicastTtl; wire_.reset(); } |
| 97 | |
| 98 | // Freshness |
| 99 | int |
| 100 | getFreshnessPeriod() const { return freshnessPeriod_; } |
| 101 | |
| 102 | void |
| 103 | setFreshnessPeriod(int freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); } |
| 104 | |
| 105 | // Wire |
| 106 | inline const Block& |
| 107 | wireEncode() const; |
| 108 | |
| 109 | inline void |
| 110 | wireDecode(const Block &wire); |
| 111 | |
| 112 | private: |
| 113 | std::string action_; |
| 114 | int64_t faceId_; |
| 115 | int32_t ipProto_; |
| 116 | std::string host_; |
Alexander Afanasyev | 0a5eabf | 2014-01-02 09:03:25 -0800 | [diff] [blame^] | 117 | std::string port_; |
Alexander Afanasyev | 5d7db8e | 2014-01-05 22:43:57 -0800 | [diff] [blame] | 118 | std::string multicastInterface_; |
| 119 | int32_t multicastTtl_; |
| 120 | Milliseconds freshnessPeriod_; |
| 121 | |
| 122 | mutable Block wire_; |
| 123 | }; |
| 124 | |
| 125 | inline const Block& |
| 126 | FaceInstance::wireEncode() const |
| 127 | { |
| 128 | if (wire_.hasWire()) |
| 129 | return wire_; |
| 130 | |
| 131 | // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH |
| 132 | // Action? |
| 133 | // FaceID? |
| 134 | // IPProto? |
| 135 | // Host? |
| 136 | // Port? |
| 137 | // MulticastInterface? |
| 138 | // MulticastTTL? |
| 139 | // FreshnessPeriod? |
| 140 | |
| 141 | wire_ = Block(Tlv::FaceManagement::FaceInstance); |
| 142 | |
| 143 | // Action |
| 144 | if (!action_.empty()) |
| 145 | { |
| 146 | wire_.push_back |
| 147 | (dataBlock(Tlv::FaceManagement::Action, action_.c_str(), action_.size())); |
| 148 | } |
| 149 | |
| 150 | // FaceID |
| 151 | if (faceId_ >= 0) |
| 152 | { |
| 153 | wire_.push_back |
| 154 | (nonNegativeIntegerBlock(Tlv::FaceManagement::FaceID, faceId_)); |
| 155 | } |
| 156 | |
| 157 | // IPProto |
| 158 | if (ipProto_ >= 0) |
| 159 | { |
| 160 | wire_.push_back |
| 161 | (nonNegativeIntegerBlock(Tlv::FaceManagement::IPProto, ipProto_)); |
| 162 | } |
| 163 | |
| 164 | // Host |
| 165 | if (!host_.empty()) |
| 166 | { |
| 167 | wire_.push_back |
| 168 | (dataBlock(Tlv::FaceManagement::Host, host_.c_str(), host_.size())); |
| 169 | } |
| 170 | |
| 171 | // Port |
Alexander Afanasyev | 0a5eabf | 2014-01-02 09:03:25 -0800 | [diff] [blame^] | 172 | if (!port_.empty()) |
Alexander Afanasyev | 5d7db8e | 2014-01-05 22:43:57 -0800 | [diff] [blame] | 173 | { |
| 174 | wire_.push_back |
Alexander Afanasyev | 0a5eabf | 2014-01-02 09:03:25 -0800 | [diff] [blame^] | 175 | (dataBlock(Tlv::FaceManagement::Port, port_.c_str(), port_.size())); |
Alexander Afanasyev | 5d7db8e | 2014-01-05 22:43:57 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // MulticastInterface |
| 179 | if (!multicastInterface_.empty()) |
| 180 | { |
| 181 | wire_.push_back |
| 182 | (dataBlock(Tlv::FaceManagement::MulticastInterface, multicastInterface_.c_str(), multicastInterface_.size())); |
| 183 | } |
| 184 | |
| 185 | // MulticastTTL |
| 186 | if (multicastTtl_ >= 0) |
| 187 | { |
| 188 | wire_.push_back |
| 189 | (nonNegativeIntegerBlock(Tlv::FaceManagement::MulticastTTL, multicastTtl_)); |
| 190 | } |
| 191 | |
| 192 | // FreshnessPeriod |
| 193 | if (freshnessPeriod_ >= 0) |
| 194 | { |
| 195 | wire_.push_back |
| 196 | (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_)); |
| 197 | } |
| 198 | |
| 199 | wire_.encode(); |
| 200 | return wire_; |
| 201 | } |
| 202 | |
| 203 | inline void |
| 204 | FaceInstance::wireDecode(const Block &wire) |
| 205 | { |
| 206 | action_.clear(); |
| 207 | faceId_ = -1; |
| 208 | ipProto_ = -1; |
| 209 | host_.clear(); |
Alexander Afanasyev | 0a5eabf | 2014-01-02 09:03:25 -0800 | [diff] [blame^] | 210 | port_.clear(); |
Alexander Afanasyev | 5d7db8e | 2014-01-05 22:43:57 -0800 | [diff] [blame] | 211 | multicastInterface_.clear(); |
| 212 | multicastTtl_ = -1; |
| 213 | freshnessPeriod_ = -1; |
| 214 | |
| 215 | wire_ = wire; |
| 216 | wire_.parse(); |
| 217 | |
| 218 | // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH |
| 219 | // Action? |
| 220 | // FaceID? |
| 221 | // IPProto? |
| 222 | // Host? |
| 223 | // Port? |
| 224 | // MulticastInterface? |
| 225 | // MulticastTTL? |
| 226 | // FreshnessPeriod? |
| 227 | |
| 228 | // Action |
| 229 | Block::element_iterator val = wire_.find(Tlv::FaceManagement::Action); |
| 230 | if (val != wire_.getAll().end()) |
| 231 | { |
| 232 | action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 233 | } |
| 234 | |
| 235 | // FaceID |
| 236 | val = wire_.find(Tlv::FaceManagement::FaceID); |
| 237 | if (val != wire_.getAll().end()) |
| 238 | { |
| 239 | faceId_ = readNonNegativeInteger(*val); |
| 240 | } |
| 241 | |
| 242 | // IPProto |
| 243 | val = wire_.find(Tlv::FaceManagement::IPProto); |
| 244 | if (val != wire_.getAll().end()) |
| 245 | { |
| 246 | ipProto_ = readNonNegativeInteger(*val); |
| 247 | } |
| 248 | |
| 249 | // Host |
| 250 | val = wire_.find(Tlv::FaceManagement::Host); |
| 251 | if (val != wire_.getAll().end()) |
| 252 | { |
| 253 | host_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 254 | } |
| 255 | |
| 256 | // Port |
| 257 | val = wire_.find(Tlv::FaceManagement::Port); |
| 258 | if (val != wire_.getAll().end()) |
| 259 | { |
Alexander Afanasyev | 0a5eabf | 2014-01-02 09:03:25 -0800 | [diff] [blame^] | 260 | port_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
Alexander Afanasyev | 5d7db8e | 2014-01-05 22:43:57 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // MulticastInterface |
| 264 | val = wire_.find(Tlv::FaceManagement::MulticastInterface); |
| 265 | if (val != wire_.getAll().end()) |
| 266 | { |
| 267 | multicastInterface_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 268 | } |
| 269 | |
| 270 | // MulticastTTL |
| 271 | val = wire_.find(Tlv::FaceManagement::MulticastTTL); |
| 272 | if (val != wire_.getAll().end()) |
| 273 | { |
| 274 | multicastTtl_ = readNonNegativeInteger(*val); |
| 275 | } |
| 276 | |
| 277 | // FreshnessPeriod |
| 278 | val = wire_.find(Tlv::FreshnessPeriod); |
| 279 | if (val != wire_.getAll().end()) |
| 280 | { |
| 281 | freshnessPeriod_ = readNonNegativeInteger(*val); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | inline std::ostream& |
| 286 | operator << (std::ostream &os, const FaceInstance &entry) |
| 287 | { |
| 288 | os << "FaceInstance("; |
| 289 | |
| 290 | // Action |
| 291 | if (!entry.getAction().empty()) |
| 292 | { |
| 293 | os << "Action:" << entry.getAction() << ", "; |
| 294 | } |
| 295 | |
| 296 | // FaceID |
| 297 | if (entry.getFaceId() >= 0) |
| 298 | { |
| 299 | os << "FaceID:" << entry.getFaceId() << ", "; |
| 300 | } |
| 301 | |
| 302 | // IPProto |
| 303 | if (entry.getIpProto() >= 0) |
| 304 | { |
| 305 | os << "IPProto:" << entry.getIpProto() << ", "; |
| 306 | } |
| 307 | |
| 308 | // Host |
| 309 | if (!entry.getHost().empty()) |
| 310 | { |
| 311 | os << "Host:" << entry.getHost() << ", "; |
| 312 | } |
| 313 | |
| 314 | // Port |
Alexander Afanasyev | 0a5eabf | 2014-01-02 09:03:25 -0800 | [diff] [blame^] | 315 | if (!entry.getPort().empty()) |
Alexander Afanasyev | 5d7db8e | 2014-01-05 22:43:57 -0800 | [diff] [blame] | 316 | { |
| 317 | os << "Port:" << entry.getPort() << ", "; |
| 318 | } |
| 319 | |
| 320 | // MulticastInterface |
| 321 | if (!entry.getMulticastInterface().empty()) |
| 322 | { |
| 323 | os << "MulticastInterface:" << entry.getMulticastInterface() << ", "; |
| 324 | } |
| 325 | |
| 326 | // MulticastTTL |
| 327 | if (entry.getMulticastTtl() >= 0) |
| 328 | { |
| 329 | os << "MulticastTTL:" << entry.getMulticastTtl() << ", "; |
| 330 | } |
| 331 | |
| 332 | // FreshnessPeriod |
| 333 | if (entry.getFreshnessPeriod() >= 0) |
| 334 | { |
| 335 | os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", "; |
| 336 | } |
| 337 | |
| 338 | os << ")"; |
| 339 | return os; |
| 340 | } |
| 341 | |
| 342 | } |
| 343 | |
| 344 | #endif // FACE_INSTANCE |