blob: 5cd81abaa91e94547e0a6da06a8da0674e6265c0 [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 Afanasyevc8823bc2014-02-09 19:33:33 -080010#include "../encoding/tlv-face-management.hpp"
11#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 {
23public:
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(),
28 int freshnessPeriod = -1)
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)
39 , freshnessPeriod_(-1)
Jeff Thompson990599b2013-08-27 15:14:25 -070040 {
Jeff Thompson990599b2013-08-27 15:14:25 -070041 }
Jeff Thompson0050abe2013-09-17 12:50:25 -070042
Jeff Thompson0050abe2013-09-17 12:50:25 -070043 const std::string&
44 getAction() const { return action_; }
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080045
46 void
47 setAction(const std::string& action) { action_ = action; wire_.reset(); }
48
Jeff Thompson0050abe2013-09-17 12:50:25 -070049 const Name&
50 getPrefix() const { return prefix_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070051
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080052 void
53 setPrefix(const Name &prefix) { prefix_ = prefix; wire_.reset(); }
54
Jeff Thompson0050abe2013-09-17 12:50:25 -070055 int
56 getFaceId() const { return faceId_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070057
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080058 void
59 setFaceId(int faceId) { faceId_ = faceId; wire_.reset(); }
60
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070061 const ForwardingFlags&
Jeff Thompson0050abe2013-09-17 12:50:25 -070062 getForwardingFlags() const { return forwardingFlags_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070063
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080064 void
65 setForwardingFlags(const ForwardingFlags& forwardingFlags) { forwardingFlags_ = forwardingFlags; wire_.reset(); }
66
Jeff Thompson0050abe2013-09-17 12:50:25 -070067 int
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080068 getFreshnessPeriod() const { return freshnessPeriod_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070069
Jeff Thompson0050abe2013-09-17 12:50:25 -070070 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080071 setFreshnessPeriod(int freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); }
Alexander Afanasyev816cf942014-01-03 15:33:41 -080072
73 inline const Block&
74 wireEncode() const;
75
76 inline void
77 wireDecode(const Block &wire);
78
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 Afanasyev895b2f32014-01-03 13:24:52 -080084 int freshnessPeriod_; /**< -1 for none. */
85
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?
101
102 wire_ = Block(Tlv::FaceManagement::ForwardingEntry);
103
104 // Action
105 if (!action_.empty())
106 {
107 wire_.push_back
108 (dataBlock(Tlv::FaceManagement::Action, action_.c_str(), action_.size()));
109 }
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
119 (nonNegativeIntegerBlock(Tlv::FaceManagement::FaceID, faceId_));
120 }
121
122 // ForwardingFlags
123 wire_.push_back
124 (forwardingFlags_.wireEncode());
125
126 // FreshnessPeriod
127 if (freshnessPeriod_ >= 0)
128 {
129 wire_.push_back
130 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_));
131 }
132
133 wire_.encode();
134 return wire_;
135}
136
137inline void
138ForwardingEntry::wireDecode(const Block &wire)
139{
140 action_.clear();
141 prefix_.clear();
142 faceId_ = -1;
143 forwardingFlags_ = ForwardingFlags();
144 freshnessPeriod_ = -1;
145
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 Afanasyev29e5c3d2014-02-11 00:01:10 -0800157 Block::element_const_iterator val = wire_.find(Tlv::FaceManagement::Action);
158 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
171 val = wire_.find(Tlv::FaceManagement::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
178 val = wire_.find(Tlv::FaceManagement::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 {
188 freshnessPeriod_ = readNonNegativeInteger(*val);
189 }
190}
191
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800192inline std::ostream&
193operator << (std::ostream &os, const ForwardingEntry &entry)
194{
195 os << "ForwardingEntry(";
196
197 // 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
216 if (entry.getFreshnessPeriod() >= 0)
217 {
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