blob: 315f2711f3402d71ebd9f586bec96418ad3219f2 [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
Jeff Thompson990599b2013-08-27 15:14:25 -070011#include "name.hpp"
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070012#include "forwarding-flags.hpp"
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080013#include "encoding/block.hpp"
Jeff Thompson990599b2013-08-27 15:14:25 -070014
15namespace ndn {
16
17/**
18 * An ForwardingEntry holds an action and Name prefix and other fields for an forwarding entry.
19 */
20class ForwardingEntry {
21public:
Alexander Afanasyev816cf942014-01-03 15:33:41 -080022 ForwardingEntry(const std::string& action,
23 const Name& prefix,
24 int faceId,
25 const ForwardingFlags& forwardingFlags,
26 int freshnessPeriod)
27 : action_(action)
28 , prefix_(prefix)
29 , faceId_(faceId)
30 , forwardingFlags_(forwardingFlags)
31 , freshnessPeriod_(freshnessPeriod)
Jeff Thompson990599b2013-08-27 15:14:25 -070032 {
33 }
34
35 ForwardingEntry()
Alexander Afanasyev816cf942014-01-03 15:33:41 -080036 : faceId_(-1)
37 , freshnessPeriod_(-1)
Jeff Thompson990599b2013-08-27 15:14:25 -070038 {
Jeff Thompson990599b2013-08-27 15:14:25 -070039 }
Jeff Thompson0050abe2013-09-17 12:50:25 -070040
Jeff Thompson0050abe2013-09-17 12:50:25 -070041 const std::string&
42 getAction() const { return action_; }
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080043
44 void
45 setAction(const std::string& action) { action_ = action; wire_.reset(); }
46
Jeff Thompson0050abe2013-09-17 12:50:25 -070047 const Name&
48 getPrefix() const { return prefix_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070049
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080050 void
51 setPrefix(const Name &prefix) { prefix_ = prefix; wire_.reset(); }
52
Jeff Thompson0050abe2013-09-17 12:50:25 -070053 int
54 getFaceId() const { return faceId_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070055
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080056 void
57 setFaceId(int faceId) { faceId_ = faceId; wire_.reset(); }
58
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070059 const ForwardingFlags&
Jeff Thompson0050abe2013-09-17 12:50:25 -070060 getForwardingFlags() const { return forwardingFlags_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070061
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080062 void
63 setForwardingFlags(const ForwardingFlags& forwardingFlags) { forwardingFlags_ = forwardingFlags; wire_.reset(); }
64
Jeff Thompson0050abe2013-09-17 12:50:25 -070065 int
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080066 getFreshnessPeriod() const { return freshnessPeriod_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070067
Jeff Thompson0050abe2013-09-17 12:50:25 -070068 void
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -080069 setFreshnessPeriod(int freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); }
Alexander Afanasyev816cf942014-01-03 15:33:41 -080070
71 inline const Block&
72 wireEncode() const;
73
74 inline void
75 wireDecode(const Block &wire);
76
Jeff Thompson0050abe2013-09-17 12:50:25 -070077private:
Jeff Thompson990599b2013-08-27 15:14:25 -070078 std::string action_; /**< empty for none. */
79 Name prefix_;
Jeff Thompson990599b2013-08-27 15:14:25 -070080 int faceId_; /**< -1 for none. */
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070081 ForwardingFlags forwardingFlags_;
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080082 int freshnessPeriod_; /**< -1 for none. */
83
Alexander Afanasyev816cf942014-01-03 15:33:41 -080084 mutable Block wire_;
Jeff Thompson990599b2013-08-27 15:14:25 -070085};
86
Alexander Afanasyev816cf942014-01-03 15:33:41 -080087inline const Block&
88ForwardingEntry::wireEncode() const
89{
90 if (wire_.hasWire())
91 return wire_;
92
93 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
94 // Action?
95 // Name?
96 // FaceID?
97 // ForwardingFlags?
98 // FreshnessPeriod?
99
100 wire_ = Block(Tlv::FaceManagement::ForwardingEntry);
101
102 // Action
103 if (!action_.empty())
104 {
105 wire_.push_back
106 (dataBlock(Tlv::FaceManagement::Action, action_.c_str(), action_.size()));
107 }
108
109 // Name
110 if (!prefix_.empty())
111 {
112 wire_.push_back
113 (prefix_.wireEncode());
114 }
115
116 // FaceID
117 if (faceId_ >= 0)
118 {
119 wire_.push_back
120 (nonNegativeIntegerBlock(Tlv::FaceManagement::FaceID, faceId_));
121 }
122
123 // ForwardingFlags
124 wire_.push_back
125 (forwardingFlags_.wireEncode());
126
127 // FreshnessPeriod
128 if (freshnessPeriod_ >= 0)
129 {
130 wire_.push_back
131 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_));
132 }
133
134 wire_.encode();
135 return wire_;
136}
137
138inline void
139ForwardingEntry::wireDecode(const Block &wire)
140{
141 action_.clear();
142 prefix_.clear();
143 faceId_ = -1;
144 forwardingFlags_ = ForwardingFlags();
145 freshnessPeriod_ = -1;
146
147 wire_ = wire;
148 wire_.parse();
149
150 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
151 // Action?
152 // Name?
153 // FaceID?
154 // ForwardingFlags?
155 // FreshnessPeriod?
156
157 // Action
158 Block::element_iterator val = wire_.find(Tlv::FaceManagement::Action);
159 if (val != wire_.getAll().end())
160 {
161 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
162 }
163
164 // Name
165 val = wire_.find(Tlv::Name);
166 if (val != wire_.getAll().end())
167 {
168 prefix_.wireDecode(*val);
169 }
170
171 // FaceID
172 val = wire_.find(Tlv::FaceManagement::FaceID);
173 if (val != wire_.getAll().end())
174 {
175 faceId_ = readNonNegativeInteger(*val);
176 }
177
178 // ForwardingFlags
179 val = wire_.find(Tlv::FaceManagement::ForwardingFlags);
180 if (val != wire_.getAll().end())
181 {
182 forwardingFlags_.wireDecode(*val);
183 }
184
185 // FreshnessPeriod
186 val = wire_.find(Tlv::FreshnessPeriod);
187 if (val != wire_.getAll().end())
188 {
189 freshnessPeriod_ = readNonNegativeInteger(*val);
190 }
191}
192
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800193inline std::ostream&
194operator << (std::ostream &os, const ForwardingEntry &entry)
195{
196 os << "ForwardingEntry(";
197
198 // Action
199 if (!entry.getAction().empty())
200 {
201 os << "Action:" << entry.getAction() << ", ";
202 }
203
204 // Name
205 if (!entry.getPrefix().empty())
206 {
207 os << "Prefix:" << entry.getPrefix() << ", ";
208 }
209
210 // FaceID
211 if (entry.getFaceId() >= 0)
212 {
213 os << "FaceID:" << entry.getFaceId() << ", ";
214 }
215
216 // ForwardingFlags
217 os << "ForwardingFlags:" << entry.getForwardingFlags() << ", ";
218
219 // FreshnessPeriod
220 if (entry.getFreshnessPeriod() >= 0)
221 {
222 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
223 }
224
225 os << ")";
226 return os;
227}
228
Jeff Thompson990599b2013-08-27 15:14:25 -0700229}
230
231#endif