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