blob: a27c88acd45f9b8bf5bea697a091342eb0521c3b [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson990599b2013-08-27 15:14:25 -07002/**
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 Thompson990599b2013-08-27 15:14:25 -070011 */
12
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080013#ifndef NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP
14#define NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP
Jeff Thompson990599b2013-08-27 15:14:25 -070015
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080016#include "../encoding/tlv-ndnd.hpp"
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080017#include "../name.hpp"
18#include "../encoding/block.hpp"
19
20#include "ndnd-forwarding-flags.hpp"
Jeff Thompson990599b2013-08-27 15:14:25 -070021
22namespace ndn {
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080023namespace ndnd {
Jeff Thompson990599b2013-08-27 15:14:25 -070024
25/**
26 * An ForwardingEntry holds an action and Name prefix and other fields for an forwarding entry.
27 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070028class ForwardingEntry
29{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070030public:
Alexander Afanasyev816cf942014-01-03 15:33:41 -080031 ForwardingEntry(const std::string& action,
32 const Name& prefix,
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -080033 int faceId = -1,
34 const ForwardingFlags& forwardingFlags = ForwardingFlags(),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070035 time::milliseconds freshnessPeriod = time::milliseconds::min())
Alexander Afanasyev816cf942014-01-03 15:33:41 -080036 : action_(action)
37 , prefix_(prefix)
38 , faceId_(faceId)
39 , forwardingFlags_(forwardingFlags)
40 , freshnessPeriod_(freshnessPeriod)
Jeff Thompson990599b2013-08-27 15:14:25 -070041 {
42 }
43
44 ForwardingEntry()
Alexander Afanasyev816cf942014-01-03 15:33:41 -080045 : faceId_(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070046 , freshnessPeriod_(time::milliseconds::min())
Jeff Thompson990599b2013-08-27 15:14:25 -070047 {
Jeff Thompson990599b2013-08-27 15:14:25 -070048 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070049
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070050 /**
51 * @brief Create from wire encoding
52 */
53 explicit
54 ForwardingEntry(const Block& wire)
55 {
56 wireDecode(wire);
57 }
58
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070059 const std::string&
Jeff Thompson0050abe2013-09-17 12:50:25 -070060 getAction() const { return action_; }
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080061
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070062 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080063 setAction(const std::string& action) { action_ = action; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064
65 const Name&
Jeff Thompson0050abe2013-09-17 12:50:25 -070066 getPrefix() const { return prefix_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080068 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 setPrefix(const Name& prefix) { prefix_ = prefix; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070070
71 int
Jeff Thompson0050abe2013-09-17 12:50:25 -070072 getFaceId() const { return faceId_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070073
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080075 setFaceId(int faceId) { faceId_ = faceId; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076
77 const ForwardingFlags&
Jeff Thompson0050abe2013-09-17 12:50:25 -070078 getForwardingFlags() const { return forwardingFlags_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070079
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070080 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080081 setForwardingFlags(const ForwardingFlags& forwardingFlags) { forwardingFlags_ = forwardingFlags; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070082
83 const time::milliseconds&
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080084 getFreshnessPeriod() const { return freshnessPeriod_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070085
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070086 void
87 setFreshnessPeriod(const time::milliseconds& freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); }
Alexander Afanasyev816cf942014-01-03 15:33:41 -080088
89 inline const Block&
90 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091
92 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070093 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070094
Jeff Thompson0050abe2013-09-17 12:50:25 -070095private:
Jeff Thompson990599b2013-08-27 15:14:25 -070096 std::string action_; /**< empty for none. */
97 Name prefix_;
Jeff Thompson990599b2013-08-27 15:14:25 -070098 int faceId_; /**< -1 for none. */
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070099 ForwardingFlags forwardingFlags_;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700100 time::milliseconds freshnessPeriod_; /**< time::milliseconds::min() for none. */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -0800101
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800102 mutable Block wire_;
Jeff Thompson990599b2013-08-27 15:14:25 -0700103};
104
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800105inline const Block&
106ForwardingEntry::wireEncode() const
107{
108 if (wire_.hasWire())
109 return wire_;
110
111 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
112 // Action?
113 // Name?
114 // FaceID?
115 // ForwardingFlags?
116 // FreshnessPeriod?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700117
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800118 wire_ = Block(tlv::ndnd::ForwardingEntry);
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800119
120 // Action
121 if (!action_.empty())
122 {
123 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800124 (dataBlock(tlv::ndnd::Action, action_.c_str(), action_.size()));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800125 }
126
127 // Name
Alexander Afanasyevd453f072014-01-12 22:29:05 -0800128 wire_.push_back
129 (prefix_.wireEncode());
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800130
131 // FaceID
132 if (faceId_ >= 0)
133 {
134 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800135 (nonNegativeIntegerBlock(tlv::ndnd::FaceID, faceId_));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800136 }
137
138 // ForwardingFlags
139 wire_.push_back
140 (forwardingFlags_.wireEncode());
141
142 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700143 if (freshnessPeriod_ >= time::milliseconds::zero())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800144 {
145 wire_.push_back
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700146 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_.count()));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800147 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700148
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800149 wire_.encode();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700150 return wire_;
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800151}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700152
153inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700154ForwardingEntry::wireDecode(const Block& wire)
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800155{
156 action_.clear();
157 prefix_.clear();
158 faceId_ = -1;
159 forwardingFlags_ = ForwardingFlags();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700160 freshnessPeriod_ = time::milliseconds::min();
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800161
162 wire_ = wire;
163 wire_.parse();
164
165 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
166 // Action?
167 // Name?
168 // FaceID?
169 // ForwardingFlags?
170 // FreshnessPeriod?
171
172 // Action
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800173 Block::element_const_iterator val = wire_.find(tlv::ndnd::Action);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800174 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800175 {
176 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
177 }
178
179 // Name
180 val = wire_.find(Tlv::Name);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800181 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800182 {
183 prefix_.wireDecode(*val);
184 }
185
186 // FaceID
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800187 val = wire_.find(tlv::ndnd::FaceID);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800188 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800189 {
190 faceId_ = readNonNegativeInteger(*val);
191 }
192
193 // ForwardingFlags
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800194 val = wire_.find(tlv::ndnd::ForwardingFlags);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800195 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800196 {
197 forwardingFlags_.wireDecode(*val);
198 }
199
200 // FreshnessPeriod
201 val = wire_.find(Tlv::FreshnessPeriod);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800202 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800203 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700204 freshnessPeriod_ = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800205 }
206}
207
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800208inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700209operator << (std::ostream& os, const ForwardingEntry& entry)
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800210{
211 os << "ForwardingEntry(";
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700212
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800213 // Action
214 if (!entry.getAction().empty())
215 {
216 os << "Action:" << entry.getAction() << ", ";
217 }
218
219 // Name
Alexander Afanasyevd453f072014-01-12 22:29:05 -0800220 os << "Prefix:" << entry.getPrefix() << ", ";
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800221
222 // FaceID
223 if (entry.getFaceId() >= 0)
224 {
225 os << "FaceID:" << entry.getFaceId() << ", ";
226 }
227
228 // ForwardingFlags
229 os << "ForwardingFlags:" << entry.getForwardingFlags() << ", ";
230
231 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700232 if (entry.getFreshnessPeriod() >= time::milliseconds::zero())
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800233 {
234 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
235 }
236
237 os << ")";
238 return os;
239}
240
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800241} // namespace ndnd
242} // namespace ndn
Jeff Thompson990599b2013-08-27 15:14:25 -0700243
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800244#endif // NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP