blob: 03272b1a0ee3fb1c5ab45408eaa833f076691861 [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
43 const std::string&
Jeff Thompson0050abe2013-09-17 12:50:25 -070044 getAction() const { return action_; }
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080045
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070046 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080047 setAction(const std::string& action) { action_ = action; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070048
49 const Name&
Jeff Thompson0050abe2013-09-17 12:50:25 -070050 getPrefix() const { return prefix_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070051
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080052 void
53 setPrefix(const Name &prefix) { prefix_ = prefix; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070054
55 int
Jeff Thompson0050abe2013-09-17 12:50:25 -070056 getFaceId() const { return faceId_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070057
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070058 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080059 setFaceId(int faceId) { faceId_ = faceId; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060
61 const ForwardingFlags&
Jeff Thompson0050abe2013-09-17 12:50:25 -070062 getForwardingFlags() const { return forwardingFlags_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070063
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080065 setForwardingFlags(const ForwardingFlags& forwardingFlags) { forwardingFlags_ = forwardingFlags; wire_.reset(); }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066
67 const time::milliseconds&
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080068 getFreshnessPeriod() const { return freshnessPeriod_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070069
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070070 void
71 setFreshnessPeriod(const time::milliseconds& freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); }
Alexander Afanasyev816cf942014-01-03 15:33:41 -080072
73 inline const Block&
74 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070075
76 inline void
Alexander Afanasyev816cf942014-01-03 15:33:41 -080077 wireDecode(const Block &wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070078
Jeff Thompson0050abe2013-09-17 12:50:25 -070079private:
Jeff Thompson990599b2013-08-27 15:14:25 -070080 std::string action_; /**< empty for none. */
81 Name prefix_;
Jeff Thompson990599b2013-08-27 15:14:25 -070082 int faceId_; /**< -1 for none. */
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070083 ForwardingFlags forwardingFlags_;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084 time::milliseconds freshnessPeriod_; /**< time::milliseconds::min() for none. */
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080085
Alexander Afanasyev816cf942014-01-03 15:33:41 -080086 mutable Block wire_;
Jeff Thompson990599b2013-08-27 15:14:25 -070087};
88
Alexander Afanasyev816cf942014-01-03 15:33:41 -080089inline const Block&
90ForwardingEntry::wireEncode() const
91{
92 if (wire_.hasWire())
93 return wire_;
94
95 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
96 // Action?
97 // Name?
98 // FaceID?
99 // ForwardingFlags?
100 // FreshnessPeriod?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700101
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800102 wire_ = Block(tlv::ndnd::ForwardingEntry);
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800103
104 // Action
105 if (!action_.empty())
106 {
107 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800108 (dataBlock(tlv::ndnd::Action, action_.c_str(), action_.size()));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800109 }
110
111 // Name
Alexander Afanasyevd453f072014-01-12 22:29:05 -0800112 wire_.push_back
113 (prefix_.wireEncode());
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800114
115 // FaceID
116 if (faceId_ >= 0)
117 {
118 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800119 (nonNegativeIntegerBlock(tlv::ndnd::FaceID, faceId_));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800120 }
121
122 // ForwardingFlags
123 wire_.push_back
124 (forwardingFlags_.wireEncode());
125
126 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700127 if (freshnessPeriod_ >= time::milliseconds::zero())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800128 {
129 wire_.push_back
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700130 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_.count()));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800131 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700132
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800133 wire_.encode();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700134 return wire_;
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800135}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700136
137inline void
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800138ForwardingEntry::wireDecode(const Block &wire)
139{
140 action_.clear();
141 prefix_.clear();
142 faceId_ = -1;
143 forwardingFlags_ = ForwardingFlags();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700144 freshnessPeriod_ = time::milliseconds::min();
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800145
146 wire_ = wire;
147 wire_.parse();
148
149 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
150 // Action?
151 // Name?
152 // FaceID?
153 // ForwardingFlags?
154 // FreshnessPeriod?
155
156 // Action
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800157 Block::element_const_iterator val = wire_.find(tlv::ndnd::Action);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800158 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800159 {
160 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
161 }
162
163 // Name
164 val = wire_.find(Tlv::Name);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800165 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800166 {
167 prefix_.wireDecode(*val);
168 }
169
170 // FaceID
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800171 val = wire_.find(tlv::ndnd::FaceID);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800172 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800173 {
174 faceId_ = readNonNegativeInteger(*val);
175 }
176
177 // ForwardingFlags
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800178 val = wire_.find(tlv::ndnd::ForwardingFlags);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800179 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800180 {
181 forwardingFlags_.wireDecode(*val);
182 }
183
184 // FreshnessPeriod
185 val = wire_.find(Tlv::FreshnessPeriod);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800186 if (val != wire_.elements_end())
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800187 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700188 freshnessPeriod_ = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800189 }
190}
191
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800192inline std::ostream&
193operator << (std::ostream &os, const ForwardingEntry &entry)
194{
195 os << "ForwardingEntry(";
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700196
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800197 // Action
198 if (!entry.getAction().empty())
199 {
200 os << "Action:" << entry.getAction() << ", ";
201 }
202
203 // Name
Alexander Afanasyevd453f072014-01-12 22:29:05 -0800204 os << "Prefix:" << entry.getPrefix() << ", ";
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800205
206 // FaceID
207 if (entry.getFaceId() >= 0)
208 {
209 os << "FaceID:" << entry.getFaceId() << ", ";
210 }
211
212 // ForwardingFlags
213 os << "ForwardingFlags:" << entry.getForwardingFlags() << ", ";
214
215 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700216 if (entry.getFreshnessPeriod() >= time::milliseconds::zero())
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800217 {
218 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
219 }
220
221 os << ")";
222 return os;
223}
224
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800225} // namespace ndnd
226} // namespace ndn
Jeff Thompson990599b2013-08-27 15:14:25 -0700227
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800228#endif // NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP