All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ndnd-forwarding-entry.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #ifndef NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP
8 #define NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP
9 
10 #include "../encoding/tlv-ndnd.hpp"
11 #include "../name.hpp"
12 #include "../encoding/block.hpp"
13 
15 
16 namespace ndn {
17 namespace ndnd {
18 
23 {
24 public:
25  ForwardingEntry(const std::string& action,
26  const Name& prefix,
27  int faceId = -1,
28  const ForwardingFlags& forwardingFlags = ForwardingFlags(),
29  time::milliseconds freshnessPeriod = time::milliseconds::min())
30  : action_(action)
31  , prefix_(prefix)
32  , faceId_(faceId)
33  , forwardingFlags_(forwardingFlags)
34  , freshnessPeriod_(freshnessPeriod)
35  {
36  }
37 
39  : faceId_(-1)
40  , freshnessPeriod_(time::milliseconds::min())
41  {
42  }
43 
47  explicit
48  ForwardingEntry(const Block& wire)
49  {
50  wireDecode(wire);
51  }
52 
53  const std::string&
54  getAction() const { return action_; }
55 
56  void
57  setAction(const std::string& action) { action_ = action; wire_.reset(); }
58 
59  const Name&
60  getPrefix() const { return prefix_; }
61 
62  void
63  setPrefix(const Name& prefix) { prefix_ = prefix; wire_.reset(); }
64 
65  int
66  getFaceId() const { return faceId_; }
67 
68  void
69  setFaceId(int faceId) { faceId_ = faceId; wire_.reset(); }
70 
71  const ForwardingFlags&
72  getForwardingFlags() const { return forwardingFlags_; }
73 
74  void
75  setForwardingFlags(const ForwardingFlags& forwardingFlags) { forwardingFlags_ = forwardingFlags; wire_.reset(); }
76 
77  const time::milliseconds&
78  getFreshnessPeriod() const { return freshnessPeriod_; }
79 
80  void
81  setFreshnessPeriod(const time::milliseconds& freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); }
82 
83  inline const Block&
84  wireEncode() const;
85 
86  inline void
87  wireDecode(const Block& wire);
88 
89 private:
90  std::string action_;
91  Name prefix_;
92  int faceId_;
93  ForwardingFlags forwardingFlags_;
94  time::milliseconds freshnessPeriod_;
96  mutable Block wire_;
97 };
98 
99 inline const Block&
101 {
102  if (wire_.hasWire())
103  return wire_;
104 
105  // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
106  // Action?
107  // Name?
108  // FaceID?
109  // ForwardingFlags?
110  // FreshnessPeriod?
111 
113 
114  // Action
115  if (!action_.empty())
116  {
117  wire_.push_back
118  (dataBlock(tlv::ndnd::Action, action_.c_str(), action_.size()));
119  }
120 
121  // Name
122  wire_.push_back
123  (prefix_.wireEncode());
124 
125  // FaceID
126  if (faceId_ >= 0)
127  {
128  wire_.push_back
130  }
131 
132  // ForwardingFlags
133  wire_.push_back
134  (forwardingFlags_.wireEncode());
135 
136  // FreshnessPeriod
137  if (freshnessPeriod_ >= time::milliseconds::zero())
138  {
139  wire_.push_back
140  (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_.count()));
141  }
142 
143  wire_.encode();
144  return wire_;
145 }
146 
147 inline void
149 {
150  action_.clear();
151  prefix_.clear();
152  faceId_ = -1;
153  forwardingFlags_ = ForwardingFlags();
154  freshnessPeriod_ = time::milliseconds::min();
155 
156  wire_ = wire;
157  wire_.parse();
158 
159  // ForwardingEntry ::= FORWARDING-ENTRY TLV-LENGTH
160  // Action?
161  // Name?
162  // FaceID?
163  // ForwardingFlags?
164  // FreshnessPeriod?
165 
166  // Action
168  if (val != wire_.elements_end())
169  {
170  action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
171  }
172 
173  // Name
174  val = wire_.find(Tlv::Name);
175  if (val != wire_.elements_end())
176  {
177  prefix_.wireDecode(*val);
178  }
179 
180  // FaceID
181  val = wire_.find(tlv::ndnd::FaceID);
182  if (val != wire_.elements_end())
183  {
184  faceId_ = readNonNegativeInteger(*val);
185  }
186 
187  // ForwardingFlags
188  val = wire_.find(tlv::ndnd::ForwardingFlags);
189  if (val != wire_.elements_end())
190  {
191  forwardingFlags_.wireDecode(*val);
192  }
193 
194  // FreshnessPeriod
195  val = wire_.find(Tlv::FreshnessPeriod);
196  if (val != wire_.elements_end())
197  {
198  freshnessPeriod_ = time::milliseconds(readNonNegativeInteger(*val));
199  }
200 }
201 
202 inline std::ostream&
203 operator << (std::ostream& os, const ForwardingEntry& entry)
204 {
205  os << "ForwardingEntry(";
206 
207  // Action
208  if (!entry.getAction().empty())
209  {
210  os << "Action:" << entry.getAction() << ", ";
211  }
212 
213  // Name
214  os << "Prefix:" << entry.getPrefix() << ", ";
215 
216  // FaceID
217  if (entry.getFaceId() >= 0)
218  {
219  os << "FaceID:" << entry.getFaceId() << ", ";
220  }
221 
222  // ForwardingFlags
223  os << "ForwardingFlags:" << entry.getForwardingFlags() << ", ";
224 
225  // FreshnessPeriod
226  if (entry.getFreshnessPeriod() >= time::milliseconds::zero())
227  {
228  os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
229  }
230 
231  os << ")";
232  return os;
233 }
234 
235 } // namespace ndnd
236 } // namespace ndn
237 
238 #endif // NDN_MANAGEMENT_NDND_FORWARDING_ENTRY_HPP
Block nonNegativeIntegerBlock(uint32_t type, uint64_t value)
A ForwardingFlags object holds the flags which specify how the forwarding daemon should forward an in...
const std::string & getAction() const
element_const_iterator find(uint32_t type) const
Definition: block.hpp:343
Class representing wire element of the NDN packet.
Definition: block.hpp:26
void wireDecode(const Block &block)
uint64_t readNonNegativeInteger(const Block &block)
element_const_iterator elements_end() const
Definition: block.hpp:476
void setAction(const std::string &action)
void wireDecode(const Block &wire)
An ForwardingEntry holds an action and Name prefix and other fields for an forwarding entry...
const Block & wireEncode() const
element_container::const_iterator element_const_iterator
Definition: block.hpp:31
void setForwardingFlags(const ForwardingFlags &forwardingFlags)
void reset()
Reset wire buffer of the element.
Definition: block.hpp:300
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
void push_back(const Block &element)
Definition: block.hpp:390
ForwardingEntry(const std::string &action, const Name &prefix, int faceId=-1, const ForwardingFlags &forwardingFlags=ForwardingFlags(), time::milliseconds freshnessPeriod=time::milliseconds::min())
size_t wireEncode(EncodingImpl< T > &block) const
Fast encoding or block size estimation.
Definition: name.hpp:711
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:265
const time::milliseconds & getFreshnessPeriod() const
void encode()
Encode subblocks into wire buffer.
Definition: block.cpp:298
void setPrefix(const Name &prefix)
const ForwardingFlags & getForwardingFlags() const
const Block & wireEncode() const
Block dataBlock(uint32_t type, const char *data, size_t dataSize)
std::ostream & operator<<(std::ostream &os, const FaceInstance &entry)
ForwardingEntry(const Block &wire)
Create from wire encoding.
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.hpp:288
void wireDecode(const Block &wire)
Definition: name.hpp:746
void clear()
Clear all the components.
Definition: name.hpp:211
void setFreshnessPeriod(const time::milliseconds &freshnessPeriod)