Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #ifndef NDN_FORWARDING_FLAGS_HPP |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 14 | #define NDN_FORWARDING_FLAGS_HPP |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 15 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 16 | #include "../encoding/block.hpp" |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 17 | #include "../encoding/tlv-ndnd.hpp" |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 18 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 19 | namespace ndn { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 20 | namespace ndnd { |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * A ForwardingFlags object holds the flags which specify how the forwarding daemon should forward an interest for |
| 24 | * a registered prefix. We use a separate ForwardingFlags object to retain future compatibility if the daemon forwarding |
| 25 | * bits are changed, amended or deprecated. |
| 26 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 27 | class ForwardingFlags |
| 28 | { |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 29 | public: |
| 30 | /** |
| 31 | * Create a new ForwardingFlags with "active" and "childInherit" set and all other flags cleared. |
| 32 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 33 | ForwardingFlags() |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 34 | : active_(true) |
| 35 | , childInherit_(true) |
| 36 | , advertise_(false) |
| 37 | , last_(false) |
| 38 | , capture_(false) |
| 39 | , local_(false) |
| 40 | , tap_(false) |
| 41 | , captureOk_(false) |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 46 | * @brief Create from wire encoding |
| 47 | */ |
| 48 | explicit |
| 49 | ForwardingFlags(const Block& wire) |
| 50 | { |
| 51 | wireDecode(wire); |
| 52 | } |
| 53 | |
| 54 | /** |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 55 | * Get the value of the "active" flag. |
| 56 | * @return true if the flag is set, false if it is cleared. |
| 57 | */ |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 58 | bool getActive() const { return active_; } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Get the value of the "childInherit" flag. |
| 62 | * @return true if the flag is set, false if it is cleared. |
| 63 | */ |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 64 | bool getChildInherit() const { return childInherit_; } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 65 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Get the value of the "advertise" flag. |
| 68 | * @return true if the flag is set, false if it is cleared. |
| 69 | */ |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 70 | bool getAdvertise() const { return advertise_; } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 71 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Get the value of the "last" flag. |
| 74 | * @return true if the flag is set, false if it is cleared. |
| 75 | */ |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 76 | bool getLast() const { return last_; } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 77 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Get the value of the "capture" flag. |
| 80 | * @return true if the flag is set, false if it is cleared. |
| 81 | */ |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 82 | bool getCapture() const { return capture_; } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 83 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Get the value of the "local" flag. |
| 86 | * @return true if the flag is set, false if it is cleared. |
| 87 | */ |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 88 | bool getLocal() const { return local_; } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 89 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Get the value of the "tap" flag. |
| 92 | * @return true if the flag is set, false if it is cleared. |
| 93 | */ |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 94 | bool getTap() const { return tap_; } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 95 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Get the value of the "captureOk" flag. |
| 98 | * @return true if the flag is set, false if it is cleared. |
| 99 | */ |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 100 | bool getCaptureOk() const { return captureOk_; } |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Set the value of the "active" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 104 | * @param active true to set the flag, false to clear it. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 105 | */ |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 106 | void setActive(bool active) { this->active_ = active; wire_.reset(); } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 107 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Set the value of the "childInherit" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 110 | * @param childInherit true to set the flag, false to clear it. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 111 | */ |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 112 | void setChildInherit(bool childInherit) { this->childInherit_ = childInherit; wire_.reset(); } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 113 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Set the value of the "advertise" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 116 | * @param advertise true to set the flag, false to clear it. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 117 | */ |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 118 | void setAdvertise(bool advertise) { this->advertise_ = advertise; wire_.reset(); } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 119 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Set the value of the "last" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 122 | * @param last true to set the flag, false to clear it. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 123 | */ |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 124 | void setLast(bool last) { this->last_ = last; wire_.reset(); } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 125 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Set the value of the "capture" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 128 | * @param capture true to set the flag, false to clear it. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 129 | */ |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 130 | void setCapture(bool capture) { this->capture_ = capture; wire_.reset(); } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 131 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Set the value of the "local" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 134 | * @param local true to set the flag, false to clear it. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 135 | */ |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 136 | void setLocal(bool local) { this->local_ = local; wire_.reset(); } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 137 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Set the value of the "tap" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 140 | * @param tap true to set the flag, false to clear it. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 141 | */ |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 142 | void setTap(bool tap) { this->tap_ = tap; wire_.reset(); } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 143 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Set the value of the "captureOk" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 146 | * @param captureOk true to set the flag, false to clear it. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 147 | */ |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 148 | void setCaptureOk(bool captureOk) { this->captureOk_ = captureOk; wire_.reset(); } |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 150 | inline const Block& |
| 151 | wireEncode() const; |
| 152 | |
| 153 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 154 | wireDecode(const Block& block); |
| 155 | |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 156 | private: |
| 157 | bool active_; |
| 158 | bool childInherit_; |
| 159 | bool advertise_; |
| 160 | bool last_; |
| 161 | bool capture_; |
| 162 | bool local_; |
| 163 | bool tap_; |
| 164 | bool captureOk_; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 165 | |
| 166 | mutable Block wire_; |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 169 | inline const Block& |
| 170 | ForwardingFlags::wireEncode() const |
| 171 | { |
| 172 | if (wire_.hasWire()) |
| 173 | return wire_; |
| 174 | |
| 175 | uint32_t result = 0; |
| 176 | if (active_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 177 | result |= tlv::ndnd::FORW_ACTIVE; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 178 | if (childInherit_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 179 | result |= tlv::ndnd::FORW_CHILD_INHERIT; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 180 | if (advertise_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 181 | result |= tlv::ndnd::FORW_ADVERTISE; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 182 | if (last_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 183 | result |= tlv::ndnd::FORW_LAST; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 184 | if (capture_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 185 | result |= tlv::ndnd::FORW_CAPTURE; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 186 | if (local_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 187 | result |= tlv::ndnd::FORW_LOCAL; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 188 | if (tap_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 189 | result |= tlv::ndnd::FORW_TAP; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 190 | if (captureOk_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 191 | result |= tlv::ndnd::FORW_CAPTURE_OK; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 192 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 193 | wire_ = nonNegativeIntegerBlock(tlv::ndnd::ForwardingFlags, result); |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 194 | |
| 195 | return wire_; |
| 196 | } |
| 197 | |
| 198 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 199 | ForwardingFlags::wireDecode(const Block& wire) |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 200 | { |
| 201 | wire_ = wire; |
| 202 | |
| 203 | uint32_t flags = readNonNegativeInteger(wire_); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 204 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 205 | active_ = (flags & tlv::ndnd::FORW_ACTIVE) ? true : false; |
| 206 | childInherit_ = (flags & tlv::ndnd::FORW_CHILD_INHERIT) ? true : false; |
| 207 | advertise_ = (flags & tlv::ndnd::FORW_ADVERTISE) ? true : false; |
| 208 | last_ = (flags & tlv::ndnd::FORW_LAST) ? true : false; |
| 209 | capture_ = (flags & tlv::ndnd::FORW_CAPTURE) ? true : false; |
| 210 | local_ = (flags & tlv::ndnd::FORW_LOCAL) ? true : false; |
| 211 | tap_ = (flags & tlv::ndnd::FORW_TAP) ? true : false; |
| 212 | captureOk_ = (flags & tlv::ndnd::FORW_CAPTURE_OK) ? true : false; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 215 | inline std::ostream& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 216 | operator << (std::ostream& os, const ForwardingFlags& flags) |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 217 | { |
| 218 | if (flags.getActive()) |
| 219 | os << "ACTIVE "; |
| 220 | if (flags.getChildInherit()) |
| 221 | os << "CHILE_INHERIT "; |
| 222 | if (flags.getAdvertise()) |
| 223 | os << "ADVERTISE "; |
| 224 | if (flags.getLast()) |
| 225 | os << "LAST "; |
| 226 | if (flags.getCapture()) |
| 227 | os << "CAPTURE "; |
| 228 | if (flags.getLocal()) |
| 229 | os << "LOCAL "; |
| 230 | if (flags.getTap()) |
| 231 | os << "TAP "; |
| 232 | if (flags.getCaptureOk()) |
| 233 | os << "CAPTURE_OK "; |
| 234 | |
| 235 | return os; |
| 236 | } |
| 237 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 238 | } // namespace ndnd |
| 239 | } // namespace ndn |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 240 | |
| 241 | #endif |