blob: a0849641b84e64c29da4e548c010e13d237bee65 [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 */
22class ForwardingEntry {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070023public:
Alexander Afanasyev816cf942014-01-03 15:33:41 -080024 ForwardingEntry(const std::string& action,
25 const Name& prefix,
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -080026 int faceId = -1,
27 const ForwardingFlags& forwardingFlags = ForwardingFlags(),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070028 time::milliseconds freshnessPeriod = time::milliseconds::min())
Alexander Afanasyev816cf942014-01-03 15:33:41 -080029 : action_(action)
30 , prefix_(prefix)
31 , faceId_(faceId)
32 , forwardingFlags_(forwardingFlags)
33 , freshnessPeriod_(freshnessPeriod)
Jeff Thompson990599b2013-08-27 15:14:25 -070034 {
35 }
36
37 ForwardingEntry()
Alexander Afanasyev816cf942014-01-03 15:33:41 -080038 : faceId_(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070039 , freshnessPeriod_(time::milliseconds::min())
Jeff Thompson990599b2013-08-27 15:14:25 -070040 {
Jeff Thompson990599b2013-08-27 15:14:25 -070041 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070042
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043 /**
44 * @brief Create from wire encoding
45 */
46 explicit
47 ForwardingEntry(const Block& wire)
48 {
49 wireDecode(wire);
50 }
51
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070052 const std::string&
Jeff Thompson0050abe2013-09-17 12:50:25 -070053 getAction() const { return action_; }
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080054
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070055 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080056 setAction(const std::string& action) { action_ = action; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070057
58 const Name&
Jeff Thompson0050abe2013-09-17 12:50:25 -070059 getPrefix() const { return prefix_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080061 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070062 setPrefix(const Name& prefix) { prefix_ = prefix; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063
64 int
Jeff Thompson0050abe2013-09-17 12:50:25 -070065 getFaceId() const { return faceId_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070066
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080068 setFaceId(int faceId) { faceId_ = faceId; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069
70 const ForwardingFlags&
Jeff Thompson0050abe2013-09-17 12:50:25 -070071 getForwardingFlags() const { return forwardingFlags_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070072
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070073 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080074 setForwardingFlags(const ForwardingFlags& forwardingFlags) { forwardingFlags_ = forwardingFlags; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070075
76 const time::milliseconds&
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080077 getFreshnessPeriod() const { return freshnessPeriod_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070078
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070079 void
80 setFreshnessPeriod(const time::milliseconds& freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); }
Alexander Afanasyev816cf942014-01-03 15:33:41 -080081
82 inline const Block&
83 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084
85 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070086 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070087
Jeff Thompson0050abe2013-09-17 12:50:25 -070088private:
Jeff Thompson990599b2013-08-27 15:14:25 -070089 std::string action_; /**< empty for none. */
90 Name prefix_;
Jeff Thompson990599b2013-08-27 15:14:25 -070091 int faceId_; /**< -1 for none. */
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070092 ForwardingFlags forwardingFlags_;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070093 time::milliseconds freshnessPeriod_; /**< time::milliseconds::min() for none. */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080094
Alexander Afanasyev816cf942014-01-03 15:33:41 -080095 mutable Block wire_;
Jeff Thompson990599b2013-08-27 15:14:25 -070096};
97
Alexander Afanasyev816cf942014-01-03 15:33:41 -080098inline const Block&
99ForwardingEntry::wireEncode() const
100{
101 if (wire_.hasWire())
102 return wire_;
103
104 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
105 // Action?
106 // Name?
107 // FaceID?
108 // ForwardingFlags?
109 // FreshnessPeriod?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700110
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800111 wire_ = Block(tlv::ndnd::ForwardingEntry);
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800112
113 // Action
114 if (!action_.empty())
115 {
116 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800117 (dataBlock(tlv::ndnd::Action, action_.c_str(), action_.size()));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800118 }
119
120 // Name
Alexander Afanasyevd453f072014-01-12 22:29:05 -0800121 wire_.push_back
122 (prefix_.wireEncode());
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800123
124 // FaceID
125 if (faceId_ >= 0)
126 {
127 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800128 (nonNegativeIntegerBlock(tlv::ndnd::FaceID, faceId_));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800129 }
130
131 // ForwardingFlags
132 wire_.push_back
133 (forwardingFlags_.wireEncode());
134
135 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700136 if (freshnessPeriod_ >= time::milliseconds::zero())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800137 {
138 wire_.push_back
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700139 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_.count()));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800140 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700141
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800142 wire_.encode();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700143 return wire_;
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800144}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700145
146inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700147ForwardingEntry::wireDecode(const Block& wire)
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800148{
149 action_.clear();
150 prefix_.clear();
151 faceId_ = -1;
152 forwardingFlags_ = ForwardingFlags();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700153 freshnessPeriod_ = time::milliseconds::min();
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800154
155 wire_ = wire;
156 wire_.parse();
157
158 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
159 // Action?
160 // Name?
161 // FaceID?
162 // ForwardingFlags?
163 // FreshnessPeriod?
164
165 // Action
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800166 Block::element_const_iterator val = wire_.find(tlv::ndnd::Action);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800167 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800168 {
169 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
170 }
171
172 // Name
173 val = wire_.find(Tlv::Name);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800174 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800175 {
176 prefix_.wireDecode(*val);
177 }
178
179 // FaceID
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800180 val = wire_.find(tlv::ndnd::FaceID);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800181 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800182 {
183 faceId_ = readNonNegativeInteger(*val);
184 }
185
186 // ForwardingFlags
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800187 val = wire_.find(tlv::ndnd::ForwardingFlags);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800188 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800189 {
190 forwardingFlags_.wireDecode(*val);
191 }
192
193 // FreshnessPeriod
194 val = wire_.find(Tlv::FreshnessPeriod);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800195 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800196 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700197 freshnessPeriod_ = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800198 }
199}
200
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800201inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700202operator << (std::ostream& os, const ForwardingEntry& entry)
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800203{
204 os << "ForwardingEntry(";
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700205
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800206 // Action
207 if (!entry.getAction().empty())
208 {
209 os << "Action:" << entry.getAction() << ", ";
210 }
211
212 // Name
Alexander Afanasyevd453f072014-01-12 22:29:05 -0800213 os << "Prefix:" << entry.getPrefix() << ", ";
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800214
215 // FaceID
216 if (entry.getFaceId() >= 0)
217 {
218 os << "FaceID:" << entry.getFaceId() << ", ";
219 }
220
221 // ForwardingFlags
222 os << "ForwardingFlags:" << entry.getForwardingFlags() << ", ";
223
224 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700225 if (entry.getFreshnessPeriod() >= time::milliseconds::zero())
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800226 {
227 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
228 }
229
230 os << ")";
231 return os;
232}
233
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800234} // namespace ndnd
235} // namespace ndn
Jeff Thompson990599b2013-08-27 15:14:25 -0700236
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800237#endif // NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP