blob: 409627716c4b27c918e88399b34a6a5595e76066 [file] [log] [blame]
Jeff Thompson1f8a31a2013-09-30 16:18:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Thompson1f8a31a2013-09-30 16:18:47 -070011 */
12
13#ifndef NDN_FORWARDING_FLAGS_HPP
Jeff Thompsone589c3f2013-10-12 17:30:50 -070014#define NDN_FORWARDING_FLAGS_HPP
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070015
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080016#include "../encoding/block.hpp"
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080017#include "../encoding/tlv-ndnd.hpp"
Alexander Afanasyev10d902e2014-01-03 15:27:40 -080018
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070019namespace ndn {
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080020namespace ndnd {
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070021
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 Afanasyev2a7f7202014-04-23 14:25:29 -070027class ForwardingFlags
28{
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070029public:
30 /**
31 * Create a new ForwardingFlags with "active" and "childInherit" set and all other flags cleared.
32 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070033 ForwardingFlags()
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080034 : active_(true)
35 , childInherit_(true)
36 , advertise_(false)
37 , last_(false)
38 , capture_(false)
39 , local_(false)
40 , tap_(false)
41 , captureOk_(false)
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070042 {
43 }
44
45 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070046 * @brief Create from wire encoding
47 */
48 explicit
49 ForwardingFlags(const Block& wire)
50 {
51 wireDecode(wire);
52 }
53
54 /**
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070055 * Get the value of the "active" flag.
56 * @return true if the flag is set, false if it is cleared.
57 */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080058 bool getActive() const { return active_; }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070059
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070060 /**
61 * Get the value of the "childInherit" flag.
62 * @return true if the flag is set, false if it is cleared.
63 */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080064 bool getChildInherit() const { return childInherit_; }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070065
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070066 /**
67 * Get the value of the "advertise" flag.
68 * @return true if the flag is set, false if it is cleared.
69 */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080070 bool getAdvertise() const { return advertise_; }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070071
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070072 /**
73 * Get the value of the "last" flag.
74 * @return true if the flag is set, false if it is cleared.
75 */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080076 bool getLast() const { return last_; }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070077
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070078 /**
79 * Get the value of the "capture" flag.
80 * @return true if the flag is set, false if it is cleared.
81 */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080082 bool getCapture() const { return capture_; }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070084 /**
85 * Get the value of the "local" flag.
86 * @return true if the flag is set, false if it is cleared.
87 */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080088 bool getLocal() const { return local_; }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070090 /**
91 * Get the value of the "tap" flag.
92 * @return true if the flag is set, false if it is cleared.
93 */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080094 bool getTap() const { return tap_; }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070096 /**
97 * Get the value of the "captureOk" flag.
98 * @return true if the flag is set, false if it is cleared.
99 */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -0800100 bool getCaptureOk() const { return captureOk_; }
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700101
102 /**
103 * Set the value of the "active" flag
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800104 * @param active true to set the flag, false to clear it.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700105 */
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800106 void setActive(bool active) { this->active_ = active; wire_.reset(); }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700107
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700108 /**
109 * Set the value of the "childInherit" flag
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800110 * @param childInherit true to set the flag, false to clear it.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700111 */
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800112 void setChildInherit(bool childInherit) { this->childInherit_ = childInherit; wire_.reset(); }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700113
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700114 /**
115 * Set the value of the "advertise" flag
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800116 * @param advertise true to set the flag, false to clear it.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700117 */
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800118 void setAdvertise(bool advertise) { this->advertise_ = advertise; wire_.reset(); }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700119
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700120 /**
121 * Set the value of the "last" flag
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800122 * @param last true to set the flag, false to clear it.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123 */
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800124 void setLast(bool last) { this->last_ = last; wire_.reset(); }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700126 /**
127 * Set the value of the "capture" flag
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800128 * @param capture true to set the flag, false to clear it.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700129 */
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800130 void setCapture(bool capture) { this->capture_ = capture; wire_.reset(); }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700131
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700132 /**
133 * Set the value of the "local" flag
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800134 * @param local true to set the flag, false to clear it.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700135 */
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800136 void setLocal(bool local) { this->local_ = local; wire_.reset(); }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700137
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700138 /**
139 * Set the value of the "tap" flag
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800140 * @param tap true to set the flag, false to clear it.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700141 */
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800142 void setTap(bool tap) { this->tap_ = tap; wire_.reset(); }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700143
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700144 /**
145 * Set the value of the "captureOk" flag
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800146 * @param captureOk true to set the flag, false to clear it.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700147 */
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800148 void setCaptureOk(bool captureOk) { this->captureOk_ = captureOk; wire_.reset(); }
Alexander Afanasyev895b2f32014-01-03 13:24:52 -0800149
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800150 inline const Block&
151 wireEncode() const;
152
153 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700154 wireDecode(const Block& block);
155
Alexander Afanasyev895b2f32014-01-03 13:24:52 -0800156private:
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 Afanasyev10d902e2014-01-03 15:27:40 -0800165
166 mutable Block wire_;
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700167};
168
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800169inline const Block&
170ForwardingFlags::wireEncode() const
171{
172 if (wire_.hasWire())
173 return wire_;
174
175 uint32_t result = 0;
176 if (active_)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800177 result |= tlv::ndnd::FORW_ACTIVE;
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800178 if (childInherit_)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800179 result |= tlv::ndnd::FORW_CHILD_INHERIT;
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800180 if (advertise_)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800181 result |= tlv::ndnd::FORW_ADVERTISE;
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800182 if (last_)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800183 result |= tlv::ndnd::FORW_LAST;
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800184 if (capture_)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800185 result |= tlv::ndnd::FORW_CAPTURE;
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800186 if (local_)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800187 result |= tlv::ndnd::FORW_LOCAL;
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800188 if (tap_)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800189 result |= tlv::ndnd::FORW_TAP;
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800190 if (captureOk_)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800191 result |= tlv::ndnd::FORW_CAPTURE_OK;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700192
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800193 wire_ = nonNegativeIntegerBlock(tlv::ndnd::ForwardingFlags, result);
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800194
195 return wire_;
196}
197
198inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700199ForwardingFlags::wireDecode(const Block& wire)
Alexander Afanasyev10d902e2014-01-03 15:27:40 -0800200{
201 wire_ = wire;
202
203 uint32_t flags = readNonNegativeInteger(wire_);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700204
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800205 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 Afanasyev10d902e2014-01-03 15:27:40 -0800213}
214
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800215inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700216operator << (std::ostream& os, const ForwardingFlags& flags)
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800217{
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 Afanasyevc8823bc2014-02-09 19:33:33 -0800238} // namespace ndnd
239} // namespace ndn
Jeff Thompson1f8a31a2013-09-30 16:18:47 -0700240
241#endif