blob: e9a3f47633fd5a4f8908748b01420fcb636bcd34 [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.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson990599b2013-08-27 15:14:25 -07005 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_FORWARDING_ENTRY_HPP
Jeff Thompsone589c3f2013-10-12 17:30:50 -07009#define NDN_FORWARDING_ENTRY_HPP
Jeff Thompson990599b2013-08-27 15:14:25 -070010
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080011#include "encoding/tlv-face-management.hpp"
Jeff Thompson990599b2013-08-27 15:14:25 -070012#include "name.hpp"
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070013#include "forwarding-flags.hpp"
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080014#include "encoding/block.hpp"
Jeff Thompson990599b2013-08-27 15:14:25 -070015
16namespace ndn {
17
18/**
19 * An ForwardingEntry holds an action and Name prefix and other fields for an forwarding entry.
20 */
21class ForwardingEntry {
22public:
Alexander Afanasyev816cf942014-01-03 15:33:41 -080023 ForwardingEntry(const std::string& action,
24 const Name& prefix,
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -080025 int faceId = -1,
26 const ForwardingFlags& forwardingFlags = ForwardingFlags(),
27 int freshnessPeriod = -1)
Alexander Afanasyev816cf942014-01-03 15:33:41 -080028 : action_(action)
29 , prefix_(prefix)
30 , faceId_(faceId)
31 , forwardingFlags_(forwardingFlags)
32 , freshnessPeriod_(freshnessPeriod)
Jeff Thompson990599b2013-08-27 15:14:25 -070033 {
34 }
35
36 ForwardingEntry()
Alexander Afanasyev816cf942014-01-03 15:33:41 -080037 : faceId_(-1)
38 , freshnessPeriod_(-1)
Jeff Thompson990599b2013-08-27 15:14:25 -070039 {
Jeff Thompson990599b2013-08-27 15:14:25 -070040 }
Jeff Thompson0050abe2013-09-17 12:50:25 -070041
Jeff Thompson0050abe2013-09-17 12:50:25 -070042 const std::string&
43 getAction() const { return action_; }
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080044
45 void
46 setAction(const std::string& action) { action_ = action; wire_.reset(); }
47
Jeff Thompson0050abe2013-09-17 12:50:25 -070048 const Name&
49 getPrefix() const { return prefix_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070050
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080051 void
52 setPrefix(const Name &prefix) { prefix_ = prefix; wire_.reset(); }
53
Jeff Thompson0050abe2013-09-17 12:50:25 -070054 int
55 getFaceId() const { return faceId_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070056
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080057 void
58 setFaceId(int faceId) { faceId_ = faceId; wire_.reset(); }
59
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070060 const ForwardingFlags&
Jeff Thompson0050abe2013-09-17 12:50:25 -070061 getForwardingFlags() const { return forwardingFlags_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070062
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080063 void
64 setForwardingFlags(const ForwardingFlags& forwardingFlags) { forwardingFlags_ = forwardingFlags; wire_.reset(); }
65
Jeff Thompson0050abe2013-09-17 12:50:25 -070066 int
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080067 getFreshnessPeriod() const { return freshnessPeriod_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070068
Jeff Thompson0050abe2013-09-17 12:50:25 -070069 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080070 setFreshnessPeriod(int freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); }
Alexander Afanasyev816cf942014-01-03 15:33:41 -080071
72 inline const Block&
73 wireEncode() const;
74
75 inline void
76 wireDecode(const Block &wire);
77
Jeff Thompson0050abe2013-09-17 12:50:25 -070078private:
Jeff Thompson990599b2013-08-27 15:14:25 -070079 std::string action_; /**< empty for none. */
80 Name prefix_;
Jeff Thompson990599b2013-08-27 15:14:25 -070081 int faceId_; /**< -1 for none. */
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070082 ForwardingFlags forwardingFlags_;
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080083 int freshnessPeriod_; /**< -1 for none. */
84
Alexander Afanasyev816cf942014-01-03 15:33:41 -080085 mutable Block wire_;
Jeff Thompson990599b2013-08-27 15:14:25 -070086};
87
Alexander Afanasyev816cf942014-01-03 15:33:41 -080088inline const Block&
89ForwardingEntry::wireEncode() const
90{
91 if (wire_.hasWire())
92 return wire_;
93
94 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
95 // Action?
96 // Name?
97 // FaceID?
98 // ForwardingFlags?
99 // FreshnessPeriod?
100
101 wire_ = Block(Tlv::FaceManagement::ForwardingEntry);
102
103 // Action
104 if (!action_.empty())
105 {
106 wire_.push_back
107 (dataBlock(Tlv::FaceManagement::Action, action_.c_str(), action_.size()));
108 }
109
110 // Name
111 if (!prefix_.empty())
112 {
113 wire_.push_back
114 (prefix_.wireEncode());
115 }
116
117 // FaceID
118 if (faceId_ >= 0)
119 {
120 wire_.push_back
121 (nonNegativeIntegerBlock(Tlv::FaceManagement::FaceID, faceId_));
122 }
123
124 // ForwardingFlags
125 wire_.push_back
126 (forwardingFlags_.wireEncode());
127
128 // FreshnessPeriod
129 if (freshnessPeriod_ >= 0)
130 {
131 wire_.push_back
132 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_));
133 }
134
135 wire_.encode();
136 return wire_;
137}
138
139inline void
140ForwardingEntry::wireDecode(const Block &wire)
141{
142 action_.clear();
143 prefix_.clear();
144 faceId_ = -1;
145 forwardingFlags_ = ForwardingFlags();
146 freshnessPeriod_ = -1;
147
148 wire_ = wire;
149 wire_.parse();
150
151 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
152 // Action?
153 // Name?
154 // FaceID?
155 // ForwardingFlags?
156 // FreshnessPeriod?
157
158 // Action
159 Block::element_iterator val = wire_.find(Tlv::FaceManagement::Action);
160 if (val != wire_.getAll().end())
161 {
162 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
163 }
164
165 // Name
166 val = wire_.find(Tlv::Name);
167 if (val != wire_.getAll().end())
168 {
169 prefix_.wireDecode(*val);
170 }
171
172 // FaceID
173 val = wire_.find(Tlv::FaceManagement::FaceID);
174 if (val != wire_.getAll().end())
175 {
176 faceId_ = readNonNegativeInteger(*val);
177 }
178
179 // ForwardingFlags
180 val = wire_.find(Tlv::FaceManagement::ForwardingFlags);
181 if (val != wire_.getAll().end())
182 {
183 forwardingFlags_.wireDecode(*val);
184 }
185
186 // FreshnessPeriod
187 val = wire_.find(Tlv::FreshnessPeriod);
188 if (val != wire_.getAll().end())
189 {
190 freshnessPeriod_ = readNonNegativeInteger(*val);
191 }
192}
193
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800194inline std::ostream&
195operator << (std::ostream &os, const ForwardingEntry &entry)
196{
197 os << "ForwardingEntry(";
198
199 // Action
200 if (!entry.getAction().empty())
201 {
202 os << "Action:" << entry.getAction() << ", ";
203 }
204
205 // Name
206 if (!entry.getPrefix().empty())
207 {
208 os << "Prefix:" << entry.getPrefix() << ", ";
209 }
210
211 // FaceID
212 if (entry.getFaceId() >= 0)
213 {
214 os << "FaceID:" << entry.getFaceId() << ", ";
215 }
216
217 // ForwardingFlags
218 os << "ForwardingFlags:" << entry.getForwardingFlags() << ", ";
219
220 // FreshnessPeriod
221 if (entry.getFreshnessPeriod() >= 0)
222 {
223 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
224 }
225
226 os << ")";
227 return os;
228}
229
Jeff Thompson990599b2013-08-27 15:14:25 -0700230}
231
232#endif