blob: e654ff4363bbf830e2244a2d3b6b35ca83fdad27 [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/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompson990599b2013-08-27 15:14:25 -07004 * See COPYING for copyright and distribution information.
5 */
6
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -08007#ifndef NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP
8#define NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP
Jeff Thompson990599b2013-08-27 15:14:25 -07009
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080010#include "../encoding/tlv-ndnd.hpp"
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080011#include "../name.hpp"
12#include "../encoding/block.hpp"
13
14#include "ndnd-forwarding-flags.hpp"
Jeff Thompson990599b2013-08-27 15:14:25 -070015
16namespace ndn {
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080017namespace ndnd {
Jeff Thompson990599b2013-08-27 15:14:25 -070018
19/**
20 * An ForwardingEntry holds an action and Name prefix and other fields for an forwarding entry.
21 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070022class ForwardingEntry
23{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070024public:
Alexander Afanasyev816cf942014-01-03 15:33:41 -080025 ForwardingEntry(const std::string& action,
26 const Name& prefix,
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -080027 int faceId = -1,
28 const ForwardingFlags& forwardingFlags = ForwardingFlags(),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070029 time::milliseconds freshnessPeriod = time::milliseconds::min())
Alexander Afanasyev816cf942014-01-03 15:33:41 -080030 : action_(action)
31 , prefix_(prefix)
32 , faceId_(faceId)
33 , forwardingFlags_(forwardingFlags)
34 , freshnessPeriod_(freshnessPeriod)
Jeff Thompson990599b2013-08-27 15:14:25 -070035 {
36 }
37
38 ForwardingEntry()
Alexander Afanasyev816cf942014-01-03 15:33:41 -080039 : faceId_(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070040 , freshnessPeriod_(time::milliseconds::min())
Jeff Thompson990599b2013-08-27 15:14:25 -070041 {
Jeff Thompson990599b2013-08-27 15:14:25 -070042 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070043
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044 /**
45 * @brief Create from wire encoding
46 */
47 explicit
48 ForwardingEntry(const Block& wire)
49 {
50 wireDecode(wire);
51 }
52
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053 const std::string&
Jeff Thompson0050abe2013-09-17 12:50:25 -070054 getAction() const { return action_; }
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080055
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070056 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080057 setAction(const std::string& action) { action_ = action; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070058
59 const Name&
Jeff Thompson0050abe2013-09-17 12:50:25 -070060 getPrefix() const { return prefix_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080062 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070063 setPrefix(const Name& prefix) { prefix_ = prefix; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064
65 int
Jeff Thompson0050abe2013-09-17 12:50:25 -070066 getFaceId() const { return faceId_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070067
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070068 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080069 setFaceId(int faceId) { faceId_ = faceId; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070070
71 const ForwardingFlags&
Jeff Thompson0050abe2013-09-17 12:50:25 -070072 getForwardingFlags() const { return forwardingFlags_; }
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 setForwardingFlags(const ForwardingFlags& forwardingFlags) { forwardingFlags_ = forwardingFlags; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076
77 const time::milliseconds&
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080078 getFreshnessPeriod() const { return freshnessPeriod_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070079
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070080 void
81 setFreshnessPeriod(const time::milliseconds& freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); }
Alexander Afanasyev816cf942014-01-03 15:33:41 -080082
83 inline const Block&
84 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070085
86 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088
Jeff Thompson0050abe2013-09-17 12:50:25 -070089private:
Jeff Thompson990599b2013-08-27 15:14:25 -070090 std::string action_; /**< empty for none. */
91 Name prefix_;
Jeff Thompson990599b2013-08-27 15:14:25 -070092 int faceId_; /**< -1 for none. */
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070093 ForwardingFlags forwardingFlags_;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070094 time::milliseconds freshnessPeriod_; /**< time::milliseconds::min() for none. */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080095
Alexander Afanasyev816cf942014-01-03 15:33:41 -080096 mutable Block wire_;
Jeff Thompson990599b2013-08-27 15:14:25 -070097};
98
Alexander Afanasyev816cf942014-01-03 15:33:41 -080099inline const Block&
100ForwardingEntry::wireEncode() const
101{
102 if (wire_.hasWire())
103 return wire_;
104
105 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
106 // Action?
107 // Name?
108 // FaceID?
109 // ForwardingFlags?
110 // FreshnessPeriod?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700111
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800112 wire_ = Block(tlv::ndnd::ForwardingEntry);
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800113
114 // Action
115 if (!action_.empty())
116 {
117 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800118 (dataBlock(tlv::ndnd::Action, action_.c_str(), action_.size()));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800119 }
120
121 // Name
Alexander Afanasyevd453f072014-01-12 22:29:05 -0800122 wire_.push_back
123 (prefix_.wireEncode());
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800124
125 // FaceID
126 if (faceId_ >= 0)
127 {
128 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800129 (nonNegativeIntegerBlock(tlv::ndnd::FaceID, faceId_));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800130 }
131
132 // ForwardingFlags
133 wire_.push_back
134 (forwardingFlags_.wireEncode());
135
136 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700137 if (freshnessPeriod_ >= time::milliseconds::zero())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800138 {
139 wire_.push_back
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700140 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_.count()));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800141 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700142
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800143 wire_.encode();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700144 return wire_;
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800145}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700146
147inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700148ForwardingEntry::wireDecode(const Block& wire)
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800149{
150 action_.clear();
151 prefix_.clear();
152 faceId_ = -1;
153 forwardingFlags_ = ForwardingFlags();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700154 freshnessPeriod_ = time::milliseconds::min();
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800155
156 wire_ = wire;
157 wire_.parse();
158
159 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
160 // Action?
161 // Name?
162 // FaceID?
163 // ForwardingFlags?
164 // FreshnessPeriod?
165
166 // Action
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800167 Block::element_const_iterator val = wire_.find(tlv::ndnd::Action);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800168 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800169 {
170 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
171 }
172
173 // Name
174 val = wire_.find(Tlv::Name);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800175 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800176 {
177 prefix_.wireDecode(*val);
178 }
179
180 // FaceID
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800181 val = wire_.find(tlv::ndnd::FaceID);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800182 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800183 {
184 faceId_ = readNonNegativeInteger(*val);
185 }
186
187 // ForwardingFlags
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800188 val = wire_.find(tlv::ndnd::ForwardingFlags);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800189 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800190 {
191 forwardingFlags_.wireDecode(*val);
192 }
193
194 // FreshnessPeriod
195 val = wire_.find(Tlv::FreshnessPeriod);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800196 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800197 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700198 freshnessPeriod_ = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800199 }
200}
201
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800202inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700203operator << (std::ostream& os, const ForwardingEntry& entry)
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800204{
205 os << "ForwardingEntry(";
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700206
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800207 // Action
208 if (!entry.getAction().empty())
209 {
210 os << "Action:" << entry.getAction() << ", ";
211 }
212
213 // Name
Alexander Afanasyevd453f072014-01-12 22:29:05 -0800214 os << "Prefix:" << entry.getPrefix() << ", ";
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800215
216 // FaceID
217 if (entry.getFaceId() >= 0)
218 {
219 os << "FaceID:" << entry.getFaceId() << ", ";
220 }
221
222 // ForwardingFlags
223 os << "ForwardingFlags:" << entry.getForwardingFlags() << ", ";
224
225 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700226 if (entry.getFreshnessPeriod() >= time::milliseconds::zero())
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800227 {
228 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
229 }
230
231 os << ")";
232 return os;
233}
234
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800235} // namespace ndnd
236} // namespace ndn
Jeff Thompson990599b2013-08-27 15:14:25 -0700237
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800238#endif // NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP