blob: 182dc387f1dd9b6a7926cc1118b0467011a18f89 [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
Alexander Afanasyevd453f072014-01-12 22:29:05 -0800111 wire_.push_back
112 (prefix_.wireEncode());
Alexander Afanasyev816cf942014-01-03 15:33:41 -0800113
114 // FaceID
115 if (faceId_ >= 0)
116 {
117 wire_.push_back
118 (nonNegativeIntegerBlock(Tlv::FaceManagement::FaceID, faceId_));
119 }
120
121 // ForwardingFlags
122 wire_.push_back
123 (forwardingFlags_.wireEncode());
124
125 // FreshnessPeriod
126 if (freshnessPeriod_ >= 0)
127 {
128 wire_.push_back
129 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_));
130 }
131
132 wire_.encode();
133 return wire_;
134}
135
136inline void
137ForwardingEntry::wireDecode(const Block &wire)
138{
139 action_.clear();
140 prefix_.clear();
141 faceId_ = -1;
142 forwardingFlags_ = ForwardingFlags();
143 freshnessPeriod_ = -1;
144
145 wire_ = wire;
146 wire_.parse();
147
148 // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
149 // Action?
150 // Name?
151 // FaceID?
152 // ForwardingFlags?
153 // FreshnessPeriod?
154
155 // Action
156 Block::element_iterator val = wire_.find(Tlv::FaceManagement::Action);
157 if (val != wire_.getAll().end())
158 {
159 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
160 }
161
162 // Name
163 val = wire_.find(Tlv::Name);
164 if (val != wire_.getAll().end())
165 {
166 prefix_.wireDecode(*val);
167 }
168
169 // FaceID
170 val = wire_.find(Tlv::FaceManagement::FaceID);
171 if (val != wire_.getAll().end())
172 {
173 faceId_ = readNonNegativeInteger(*val);
174 }
175
176 // ForwardingFlags
177 val = wire_.find(Tlv::FaceManagement::ForwardingFlags);
178 if (val != wire_.getAll().end())
179 {
180 forwardingFlags_.wireDecode(*val);
181 }
182
183 // FreshnessPeriod
184 val = wire_.find(Tlv::FreshnessPeriod);
185 if (val != wire_.getAll().end())
186 {
187 freshnessPeriod_ = readNonNegativeInteger(*val);
188 }
189}
190
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800191inline std::ostream&
192operator << (std::ostream &os, const ForwardingEntry &entry)
193{
194 os << "ForwardingEntry(";
195
196 // Action
197 if (!entry.getAction().empty())
198 {
199 os << "Action:" << entry.getAction() << ", ";
200 }
201
202 // Name
Alexander Afanasyevd453f072014-01-12 22:29:05 -0800203 os << "Prefix:" << entry.getPrefix() << ", ";
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800204
205 // FaceID
206 if (entry.getFaceId() >= 0)
207 {
208 os << "FaceID:" << entry.getFaceId() << ", ";
209 }
210
211 // ForwardingFlags
212 os << "ForwardingFlags:" << entry.getForwardingFlags() << ", ";
213
214 // FreshnessPeriod
215 if (entry.getFreshnessPeriod() >= 0)
216 {
217 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
218 }
219
220 os << ")";
221 return os;
222}
223
Jeff Thompson990599b2013-08-27 15:14:25 -0700224}
225
226#endif