Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [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_FORWARDING_FLAGS_HPP |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 9 | #define NDN_FORWARDING_FLAGS_HPP |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 10 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 11 | #include "../encoding/block.hpp" |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 12 | #include "../encoding/tlv-ndnd.hpp" |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 13 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 14 | namespace ndn { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 15 | namespace ndnd { |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * A ForwardingFlags object holds the flags which specify how the forwarding daemon should forward an interest for |
| 19 | * a registered prefix. We use a separate ForwardingFlags object to retain future compatibility if the daemon forwarding |
| 20 | * bits are changed, amended or deprecated. |
| 21 | */ |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 22 | class ForwardingFlags { |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 23 | public: |
| 24 | /** |
| 25 | * Create a new ForwardingFlags with "active" and "childInherit" set and all other flags cleared. |
| 26 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 27 | ForwardingFlags() |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 28 | : active_(true) |
| 29 | , childInherit_(true) |
| 30 | , advertise_(false) |
| 31 | , last_(false) |
| 32 | , capture_(false) |
| 33 | , local_(false) |
| 34 | , tap_(false) |
| 35 | , captureOk_(false) |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 40 | * @brief Create from wire encoding |
| 41 | */ |
| 42 | explicit |
| 43 | ForwardingFlags(const Block& wire) |
| 44 | { |
| 45 | wireDecode(wire); |
| 46 | } |
| 47 | |
| 48 | /** |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 49 | * Get the value of the "active" flag. |
| 50 | * @return true if the flag is set, false if it is cleared. |
| 51 | */ |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 52 | bool getActive() const { return active_; } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 53 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Get the value of the "childInherit" 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 getChildInherit() const { return childInherit_; } |
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 "advertise" 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 getAdvertise() const { return advertise_; } |
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 "last" 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 getLast() const { return last_; } |
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 "capture" 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 getCapture() const { return capture_; } |
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 "local" 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 getLocal() const { return local_; } |
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 "tap" 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 getTap() const { return tap_; } |
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 "captureOk" 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 getCaptureOk() const { return captureOk_; } |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Set the value of the "active" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 98 | * @param active true to set the flag, false to clear it. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 99 | */ |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 100 | void setActive(bool active) { this->active_ = active; wire_.reset(); } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 101 | |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Set the value of the "childInherit" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 104 | * @param childInherit 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 setChildInherit(bool childInherit) { this->childInherit_ = childInherit; 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 "advertise" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 110 | * @param advertise 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 setAdvertise(bool advertise) { this->advertise_ = advertise; 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 "last" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 116 | * @param last 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 setLast(bool last) { this->last_ = last; 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 "capture" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 122 | * @param capture 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 setCapture(bool capture) { this->capture_ = capture; 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 "local" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 128 | * @param local 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 setLocal(bool local) { this->local_ = local; 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 "tap" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 134 | * @param tap 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 setTap(bool tap) { this->tap_ = tap; 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 "captureOk" flag |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 140 | * @param captureOk 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 setCaptureOk(bool captureOk) { this->captureOk_ = captureOk; wire_.reset(); } |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 143 | |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 144 | inline const Block& |
| 145 | wireEncode() const; |
| 146 | |
| 147 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 148 | wireDecode(const Block& block); |
| 149 | |
Alexander Afanasyev | 895b2f3 | 2014-01-03 13:24:52 -0800 | [diff] [blame] | 150 | private: |
| 151 | bool active_; |
| 152 | bool childInherit_; |
| 153 | bool advertise_; |
| 154 | bool last_; |
| 155 | bool capture_; |
| 156 | bool local_; |
| 157 | bool tap_; |
| 158 | bool captureOk_; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 159 | |
| 160 | mutable Block wire_; |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 163 | inline const Block& |
| 164 | ForwardingFlags::wireEncode() const |
| 165 | { |
| 166 | if (wire_.hasWire()) |
| 167 | return wire_; |
| 168 | |
| 169 | uint32_t result = 0; |
| 170 | if (active_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 171 | result |= tlv::ndnd::FORW_ACTIVE; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 172 | if (childInherit_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 173 | result |= tlv::ndnd::FORW_CHILD_INHERIT; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 174 | if (advertise_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 175 | result |= tlv::ndnd::FORW_ADVERTISE; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 176 | if (last_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 177 | result |= tlv::ndnd::FORW_LAST; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 178 | if (capture_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 179 | result |= tlv::ndnd::FORW_CAPTURE; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 180 | if (local_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 181 | result |= tlv::ndnd::FORW_LOCAL; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 182 | if (tap_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 183 | result |= tlv::ndnd::FORW_TAP; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 184 | if (captureOk_) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 185 | result |= tlv::ndnd::FORW_CAPTURE_OK; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 186 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 187 | wire_ = nonNegativeIntegerBlock(tlv::ndnd::ForwardingFlags, result); |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 188 | |
| 189 | return wire_; |
| 190 | } |
| 191 | |
| 192 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 193 | ForwardingFlags::wireDecode(const Block& wire) |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 194 | { |
| 195 | wire_ = wire; |
| 196 | |
| 197 | uint32_t flags = readNonNegativeInteger(wire_); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 199 | active_ = (flags & tlv::ndnd::FORW_ACTIVE) ? true : false; |
| 200 | childInherit_ = (flags & tlv::ndnd::FORW_CHILD_INHERIT) ? true : false; |
| 201 | advertise_ = (flags & tlv::ndnd::FORW_ADVERTISE) ? true : false; |
| 202 | last_ = (flags & tlv::ndnd::FORW_LAST) ? true : false; |
| 203 | capture_ = (flags & tlv::ndnd::FORW_CAPTURE) ? true : false; |
| 204 | local_ = (flags & tlv::ndnd::FORW_LOCAL) ? true : false; |
| 205 | tap_ = (flags & tlv::ndnd::FORW_TAP) ? true : false; |
| 206 | captureOk_ = (flags & tlv::ndnd::FORW_CAPTURE_OK) ? true : false; |
Alexander Afanasyev | 10d902e | 2014-01-03 15:27:40 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 209 | inline std::ostream& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 210 | operator << (std::ostream& os, const ForwardingFlags& flags) |
Alexander Afanasyev | 816cf94 | 2014-01-03 15:33:41 -0800 | [diff] [blame] | 211 | { |
| 212 | if (flags.getActive()) |
| 213 | os << "ACTIVE "; |
| 214 | if (flags.getChildInherit()) |
| 215 | os << "CHILE_INHERIT "; |
| 216 | if (flags.getAdvertise()) |
| 217 | os << "ADVERTISE "; |
| 218 | if (flags.getLast()) |
| 219 | os << "LAST "; |
| 220 | if (flags.getCapture()) |
| 221 | os << "CAPTURE "; |
| 222 | if (flags.getLocal()) |
| 223 | os << "LOCAL "; |
| 224 | if (flags.getTap()) |
| 225 | os << "TAP "; |
| 226 | if (flags.getCaptureOk()) |
| 227 | os << "CAPTURE_OK "; |
| 228 | |
| 229 | return os; |
| 230 | } |
| 231 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 232 | } // namespace ndnd |
| 233 | } // namespace ndn |
Jeff Thompson | 1f8a31a | 2013-09-30 16:18:47 -0700 | [diff] [blame] | 234 | |
| 235 | #endif |