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, |
| 26 | uint32_t port, |
| 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) |
| 44 | , port_(-1) |
| 45 | , multicastTtl_(-1) |
| 46 | , freshnessPeriod_(-1) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | // Action |
| 51 | const std::string& |
| 52 | getAction() const { return action_; } |
| 53 | |
| 54 | void |
| 55 | setAction(const std::string& action) { action_ = action; wire_.reset(); } |
| 56 | |
| 57 | // FaceID |
| 58 | int64_t |
| 59 | getFaceId() const { return faceId_; } |
| 60 | |
| 61 | void |
| 62 | setFaceId(int64_t faceId) { faceId_ = faceId; wire_.reset(); } |
| 63 | |
| 64 | // IPProto |
| 65 | int32_t |
| 66 | getIpProto() const { return ipProto_; } |
| 67 | |
| 68 | void |
| 69 | setIpProto(int32_t ipProto) { ipProto_ = ipProto; wire_.reset(); } |
| 70 | |
| 71 | // Host |
| 72 | const std::string& |
| 73 | getHost() const { return host_; } |
| 74 | |
| 75 | void |
| 76 | setHost(const std::string& host) { host_ = host; wire_.reset(); } |
| 77 | |
| 78 | // Port |
| 79 | int32_t |
| 80 | getPort() const { return port_; } |
| 81 | |
| 82 | void |
| 83 | setPort(int port) { port_ = port; wire_.reset(); } |
| 84 | |
| 85 | // MulticastInterface |
| 86 | const std::string& |
| 87 | getMulticastInterface() const { return multicastInterface_; } |
| 88 | |
| 89 | void |
| 90 | setMulticastInterface(const std::string& multicastInterface) { multicastInterface_ = multicastInterface; wire_.reset(); } |
| 91 | |
| 92 | // MulticastTTL |
| 93 | int32_t |
| 94 | getMulticastTtl() const { return multicastTtl_; } |
| 95 | |
| 96 | void |
| 97 | setMulticastTtl(int32_t multicastTtl) { multicastTtl_ = multicastTtl; wire_.reset(); } |
| 98 | |
| 99 | // Freshness |
| 100 | int |
| 101 | getFreshnessPeriod() const { return freshnessPeriod_; } |
| 102 | |
| 103 | void |
| 104 | setFreshnessPeriod(int freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); } |
| 105 | |
| 106 | // Wire |
| 107 | inline const Block& |
| 108 | wireEncode() const; |
| 109 | |
| 110 | inline void |
| 111 | wireDecode(const Block &wire); |
| 112 | |
| 113 | private: |
| 114 | std::string action_; |
| 115 | int64_t faceId_; |
| 116 | int32_t ipProto_; |
| 117 | std::string host_; |
| 118 | int32_t port_; |
| 119 | std::string multicastInterface_; |
| 120 | int32_t multicastTtl_; |
| 121 | Milliseconds freshnessPeriod_; |
| 122 | |
| 123 | mutable Block wire_; |
| 124 | }; |
| 125 | |
| 126 | inline const Block& |
| 127 | FaceInstance::wireEncode() const |
| 128 | { |
| 129 | if (wire_.hasWire()) |
| 130 | return wire_; |
| 131 | |
| 132 | // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH |
| 133 | // Action? |
| 134 | // FaceID? |
| 135 | // IPProto? |
| 136 | // Host? |
| 137 | // Port? |
| 138 | // MulticastInterface? |
| 139 | // MulticastTTL? |
| 140 | // FreshnessPeriod? |
| 141 | |
| 142 | wire_ = Block(Tlv::FaceManagement::FaceInstance); |
| 143 | |
| 144 | // Action |
| 145 | if (!action_.empty()) |
| 146 | { |
| 147 | wire_.push_back |
| 148 | (dataBlock(Tlv::FaceManagement::Action, action_.c_str(), action_.size())); |
| 149 | } |
| 150 | |
| 151 | // FaceID |
| 152 | if (faceId_ >= 0) |
| 153 | { |
| 154 | wire_.push_back |
| 155 | (nonNegativeIntegerBlock(Tlv::FaceManagement::FaceID, faceId_)); |
| 156 | } |
| 157 | |
| 158 | // IPProto |
| 159 | if (ipProto_ >= 0) |
| 160 | { |
| 161 | wire_.push_back |
| 162 | (nonNegativeIntegerBlock(Tlv::FaceManagement::IPProto, ipProto_)); |
| 163 | } |
| 164 | |
| 165 | // Host |
| 166 | if (!host_.empty()) |
| 167 | { |
| 168 | wire_.push_back |
| 169 | (dataBlock(Tlv::FaceManagement::Host, host_.c_str(), host_.size())); |
| 170 | } |
| 171 | |
| 172 | // Port |
| 173 | if (port_ >= 0) |
| 174 | { |
| 175 | wire_.push_back |
| 176 | (nonNegativeIntegerBlock(Tlv::FaceManagement::Port, port_)); |
| 177 | } |
| 178 | |
| 179 | // MulticastInterface |
| 180 | if (!multicastInterface_.empty()) |
| 181 | { |
| 182 | wire_.push_back |
| 183 | (dataBlock(Tlv::FaceManagement::MulticastInterface, multicastInterface_.c_str(), multicastInterface_.size())); |
| 184 | } |
| 185 | |
| 186 | // MulticastTTL |
| 187 | if (multicastTtl_ >= 0) |
| 188 | { |
| 189 | wire_.push_back |
| 190 | (nonNegativeIntegerBlock(Tlv::FaceManagement::MulticastTTL, multicastTtl_)); |
| 191 | } |
| 192 | |
| 193 | // FreshnessPeriod |
| 194 | if (freshnessPeriod_ >= 0) |
| 195 | { |
| 196 | wire_.push_back |
| 197 | (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_)); |
| 198 | } |
| 199 | |
| 200 | wire_.encode(); |
| 201 | return wire_; |
| 202 | } |
| 203 | |
| 204 | inline void |
| 205 | FaceInstance::wireDecode(const Block &wire) |
| 206 | { |
| 207 | action_.clear(); |
| 208 | faceId_ = -1; |
| 209 | ipProto_ = -1; |
| 210 | host_.clear(); |
| 211 | port_ = -1; |
| 212 | multicastInterface_.clear(); |
| 213 | multicastTtl_ = -1; |
| 214 | freshnessPeriod_ = -1; |
| 215 | |
| 216 | wire_ = wire; |
| 217 | wire_.parse(); |
| 218 | |
| 219 | // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH |
| 220 | // Action? |
| 221 | // FaceID? |
| 222 | // IPProto? |
| 223 | // Host? |
| 224 | // Port? |
| 225 | // MulticastInterface? |
| 226 | // MulticastTTL? |
| 227 | // FreshnessPeriod? |
| 228 | |
| 229 | // Action |
| 230 | Block::element_iterator val = wire_.find(Tlv::FaceManagement::Action); |
| 231 | if (val != wire_.getAll().end()) |
| 232 | { |
| 233 | action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 234 | } |
| 235 | |
| 236 | // FaceID |
| 237 | val = wire_.find(Tlv::FaceManagement::FaceID); |
| 238 | if (val != wire_.getAll().end()) |
| 239 | { |
| 240 | faceId_ = readNonNegativeInteger(*val); |
| 241 | } |
| 242 | |
| 243 | // IPProto |
| 244 | val = wire_.find(Tlv::FaceManagement::IPProto); |
| 245 | if (val != wire_.getAll().end()) |
| 246 | { |
| 247 | ipProto_ = readNonNegativeInteger(*val); |
| 248 | } |
| 249 | |
| 250 | // Host |
| 251 | val = wire_.find(Tlv::FaceManagement::Host); |
| 252 | if (val != wire_.getAll().end()) |
| 253 | { |
| 254 | host_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 255 | } |
| 256 | |
| 257 | // Port |
| 258 | val = wire_.find(Tlv::FaceManagement::Port); |
| 259 | if (val != wire_.getAll().end()) |
| 260 | { |
| 261 | port_ = readNonNegativeInteger(*val); |
| 262 | } |
| 263 | |
| 264 | // MulticastInterface |
| 265 | val = wire_.find(Tlv::FaceManagement::MulticastInterface); |
| 266 | if (val != wire_.getAll().end()) |
| 267 | { |
| 268 | multicastInterface_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 269 | } |
| 270 | |
| 271 | // MulticastTTL |
| 272 | val = wire_.find(Tlv::FaceManagement::MulticastTTL); |
| 273 | if (val != wire_.getAll().end()) |
| 274 | { |
| 275 | multicastTtl_ = readNonNegativeInteger(*val); |
| 276 | } |
| 277 | |
| 278 | // FreshnessPeriod |
| 279 | val = wire_.find(Tlv::FreshnessPeriod); |
| 280 | if (val != wire_.getAll().end()) |
| 281 | { |
| 282 | freshnessPeriod_ = readNonNegativeInteger(*val); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | inline std::ostream& |
| 287 | operator << (std::ostream &os, const FaceInstance &entry) |
| 288 | { |
| 289 | os << "FaceInstance("; |
| 290 | |
| 291 | // Action |
| 292 | if (!entry.getAction().empty()) |
| 293 | { |
| 294 | os << "Action:" << entry.getAction() << ", "; |
| 295 | } |
| 296 | |
| 297 | // FaceID |
| 298 | if (entry.getFaceId() >= 0) |
| 299 | { |
| 300 | os << "FaceID:" << entry.getFaceId() << ", "; |
| 301 | } |
| 302 | |
| 303 | // IPProto |
| 304 | if (entry.getIpProto() >= 0) |
| 305 | { |
| 306 | os << "IPProto:" << entry.getIpProto() << ", "; |
| 307 | } |
| 308 | |
| 309 | // Host |
| 310 | if (!entry.getHost().empty()) |
| 311 | { |
| 312 | os << "Host:" << entry.getHost() << ", "; |
| 313 | } |
| 314 | |
| 315 | // Port |
| 316 | if (entry.getPort() >= 0) |
| 317 | { |
| 318 | os << "Port:" << entry.getPort() << ", "; |
| 319 | } |
| 320 | |
| 321 | // MulticastInterface |
| 322 | if (!entry.getMulticastInterface().empty()) |
| 323 | { |
| 324 | os << "MulticastInterface:" << entry.getMulticastInterface() << ", "; |
| 325 | } |
| 326 | |
| 327 | // MulticastTTL |
| 328 | if (entry.getMulticastTtl() >= 0) |
| 329 | { |
| 330 | os << "MulticastTTL:" << entry.getMulticastTtl() << ", "; |
| 331 | } |
| 332 | |
| 333 | // FreshnessPeriod |
| 334 | if (entry.getFreshnessPeriod() >= 0) |
| 335 | { |
| 336 | os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", "; |
| 337 | } |
| 338 | |
| 339 | os << ")"; |
| 340 | return os; |
| 341 | } |
| 342 | |
| 343 | } |
| 344 | |
| 345 | #endif // FACE_INSTANCE |