blob: 1de79cee99904ef04f0df4e3b54d04d3b42f6ff3 [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_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070043
Jeff Thompson0050abe2013-09-17 12:50:25 -070044 Name&
45 getPrefix() { return prefix_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070046
Jeff Thompson0050abe2013-09-17 12:50:25 -070047 const Name&
48 getPrefix() const { return prefix_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070049
Jeff Thompson0050abe2013-09-17 12:50:25 -070050 int
51 getFaceId() const { return faceId_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070052
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070053 const ForwardingFlags&
Jeff Thompson0050abe2013-09-17 12:50:25 -070054 getForwardingFlags() const { return forwardingFlags_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070055
Jeff Thompson0050abe2013-09-17 12:50:25 -070056 int
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080057 getFreshnessPeriod() const { return freshnessPeriod_; }
Jeff Thompson990599b2013-08-27 15:14:25 -070058
Jeff Thompson0050abe2013-09-17 12:50:25 -070059 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -080060 setAction(const std::string& action) { action_ = action; }
Jeff Thompson990599b2013-08-27 15:14:25 -070061
Jeff Thompson0050abe2013-09-17 12:50:25 -070062 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -080063 setFaceId(int faceId) { faceId_ = faceId; }
Jeff Thompson990599b2013-08-27 15:14:25 -070064
Jeff Thompson0050abe2013-09-17 12:50:25 -070065 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -080066 setForwardingFlags(const ForwardingFlags& forwardingFlags) { forwardingFlags_ = forwardingFlags; }
Jeff Thompson990599b2013-08-27 15:14:25 -070067
Jeff Thompson0050abe2013-09-17 12:50:25 -070068 void
Alexander Afanasyev895b2f32014-01-03 13:24:52 -080069 setFreshnessPeriod(int freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; }
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
Jeff Thompson990599b2013-08-27 15:14:25 -0700193}
194
195#endif