blob: 24f90f96045fce309723dd1246165539ff7810f4 [file] [log] [blame]
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -08004 * See COPYING for copyright and distribution information.
5 */
6
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -08007#ifndef NDN_MANAGEMENT_NDND_FACE_INSTANCE_HPP
8#define NDN_MANAGEMENT_NDND_FACE_INSTANCE_HPP
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -08009
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080010#include "../encoding/tlv-ndnd.hpp"
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080011#include "../encoding/block.hpp"
12#include "../name.hpp"
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080013
14namespace ndn {
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080015namespace ndnd {
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080016
17/**
18 * An FaceInstance holds an action and Name prefix and other fields for an forwarding entry.
19 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070020class FaceInstance
21{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070022public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070023 FaceInstance(const std::string& action,
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080024 int64_t faceId,
25 uint32_t ipProto,
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070026 const std::string& host,
27 const std::string& port,
28 const std::string& multicastInterface,
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080029 uint32_t multicastTtl,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070030 const time::milliseconds& freshnessPeriod)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080031 : action_(action)
32 , faceId_(faceId)
33 , ipProto_(ipProto)
34 , host_(host)
35 , port_(port)
36 , multicastInterface_(multicastInterface)
37 , multicastTtl_(multicastTtl)
38 , freshnessPeriod_(freshnessPeriod)
39 {
40 }
41
42 FaceInstance()
43 : faceId_(-1)
44 , ipProto_(-1)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080045 , multicastTtl_(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070046 , freshnessPeriod_(time::milliseconds::min())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080047 {
48 }
49
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070050 /**
51 * @brief Create from wire encoding
52 */
53 explicit
54 FaceInstance(const Block& wire)
55 {
56 wireDecode(wire);
57 }
58
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080059 // Action
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080061 getAction() const { return action_; }
62
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080064 setAction(const std::string& action) { action_ = action; wire_.reset(); }
65
66 // FaceID
67 int64_t
68 getFaceId() const { return faceId_; }
69
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070070 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080071 setFaceId(int64_t faceId) { faceId_ = faceId; wire_.reset(); }
72
73 // IPProto
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 int32_t
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080075 getIpProto() const { return ipProto_; }
76
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070077 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080078 setIpProto(int32_t ipProto) { ipProto_ = ipProto; wire_.reset(); }
79
80 // Host
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070081 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080082 getHost() const { return host_; }
83
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080085 setHost(const std::string& host) { host_ = host; wire_.reset(); }
86
87 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -080088 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080089 getPort() const { return port_; }
90
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070092 setPort(const std::string& port) { port_ = port; wire_.reset(); }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080093
94 // MulticastInterface
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070095 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080096 getMulticastInterface() const { return multicastInterface_; }
97
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070098 void
99 setMulticastInterface(const std::string& multicastInterface)
100 {
101 multicastInterface_ = multicastInterface; wire_.reset();
102 }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800103
104 // MulticastTTL
105 int32_t
106 getMulticastTtl() const { return multicastTtl_; }
107
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700108 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800109 setMulticastTtl(int32_t multicastTtl) { multicastTtl_ = multicastTtl; wire_.reset(); }
110
111 // Freshness
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112 const time::milliseconds&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800113 getFreshnessPeriod() const { return freshnessPeriod_; }
114
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700115 void
116 setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
117 {
118 freshnessPeriod_ = freshnessPeriod; wire_.reset();
119 }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800120
121 // Wire
122 inline const Block&
123 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700124
125 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700126 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700127
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800128private:
129 std::string action_;
130 int64_t faceId_;
131 int32_t ipProto_;
132 std::string host_;
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800133 std::string port_;
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800134 std::string multicastInterface_;
135 int32_t multicastTtl_;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700136 time::milliseconds freshnessPeriod_;
137
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800138 mutable Block wire_;
139};
140
141inline const Block&
142FaceInstance::wireEncode() const
143{
144 if (wire_.hasWire())
145 return wire_;
146
147 // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH
148 // Action?
149 // FaceID?
150 // IPProto?
151 // Host?
152 // Port?
153 // MulticastInterface?
154 // MulticastTTL?
155 // FreshnessPeriod?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700156
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800157 wire_ = Block(tlv::ndnd::FaceInstance);
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800158
159 // Action
160 if (!action_.empty())
161 {
162 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800163 (dataBlock(tlv::ndnd::Action, action_.c_str(), action_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800164 }
165
166 // FaceID
167 if (faceId_ >= 0)
168 {
169 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800170 (nonNegativeIntegerBlock(tlv::ndnd::FaceID, faceId_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800171 }
172
173 // IPProto
174 if (ipProto_ >= 0)
175 {
176 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800177 (nonNegativeIntegerBlock(tlv::ndnd::IPProto, ipProto_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800178 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700179
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800180 // Host
181 if (!host_.empty())
182 {
183 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800184 (dataBlock(tlv::ndnd::Host, host_.c_str(), host_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800185 }
186
187 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800188 if (!port_.empty())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800189 {
190 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800191 (dataBlock(tlv::ndnd::Port, port_.c_str(), port_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800192 }
193
194 // MulticastInterface
195 if (!multicastInterface_.empty())
196 {
197 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800198 (dataBlock(tlv::ndnd::MulticastInterface, multicastInterface_.c_str(), multicastInterface_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800199 }
200
201 // MulticastTTL
202 if (multicastTtl_ >= 0)
203 {
204 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800205 (nonNegativeIntegerBlock(tlv::ndnd::MulticastTTL, multicastTtl_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800206 }
207
208 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700209 if (freshnessPeriod_ >= time::milliseconds::zero())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800210 {
211 wire_.push_back
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700212 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_.count()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800213 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700214
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800215 wire_.encode();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700216 return wire_;
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800217}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700218
219inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700220FaceInstance::wireDecode(const Block& wire)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800221{
222 action_.clear();
223 faceId_ = -1;
224 ipProto_ = -1;
225 host_.clear();
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800226 port_.clear();
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800227 multicastInterface_.clear();
228 multicastTtl_ = -1;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700229 freshnessPeriod_ = time::milliseconds::min();
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800230
231 wire_ = wire;
232 wire_.parse();
233
234 // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH
235 // Action?
236 // FaceID?
237 // IPProto?
238 // Host?
239 // Port?
240 // MulticastInterface?
241 // MulticastTTL?
242 // FreshnessPeriod?
243
244 // Action
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800245 Block::element_const_iterator val = wire_.find(tlv::ndnd::Action);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800246 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800247 {
248 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
249 }
250
251 // FaceID
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800252 val = wire_.find(tlv::ndnd::FaceID);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800253 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800254 {
255 faceId_ = readNonNegativeInteger(*val);
256 }
257
258 // IPProto
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800259 val = wire_.find(tlv::ndnd::IPProto);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800260 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800261 {
262 ipProto_ = readNonNegativeInteger(*val);
263 }
264
265 // Host
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800266 val = wire_.find(tlv::ndnd::Host);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800267 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800268 {
269 host_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
270 }
271
272 // Port
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800273 val = wire_.find(tlv::ndnd::Port);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800274 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800275 {
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800276 port_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800277 }
278
279 // MulticastInterface
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800280 val = wire_.find(tlv::ndnd::MulticastInterface);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800281 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800282 {
283 multicastInterface_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
284 }
285
286 // MulticastTTL
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800287 val = wire_.find(tlv::ndnd::MulticastTTL);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800288 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800289 {
290 multicastTtl_ = readNonNegativeInteger(*val);
291 }
292
293 // FreshnessPeriod
294 val = wire_.find(Tlv::FreshnessPeriod);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800295 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800296 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700297 freshnessPeriod_ = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800298 }
299}
300
301inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700302operator << (std::ostream& os, const FaceInstance& entry)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800303{
304 os << "FaceInstance(";
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700305
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800306 // Action
307 if (!entry.getAction().empty())
308 {
309 os << "Action:" << entry.getAction() << ", ";
310 }
311
312 // FaceID
313 if (entry.getFaceId() >= 0)
314 {
315 os << "FaceID:" << entry.getFaceId() << ", ";
316 }
317
318 // IPProto
319 if (entry.getIpProto() >= 0)
320 {
321 os << "IPProto:" << entry.getIpProto() << ", ";
322 }
323
324 // Host
325 if (!entry.getHost().empty())
326 {
327 os << "Host:" << entry.getHost() << ", ";
328 }
329
330 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800331 if (!entry.getPort().empty())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800332 {
333 os << "Port:" << entry.getPort() << ", ";
334 }
335
336 // MulticastInterface
337 if (!entry.getMulticastInterface().empty())
338 {
339 os << "MulticastInterface:" << entry.getMulticastInterface() << ", ";
340 }
341
342 // MulticastTTL
343 if (entry.getMulticastTtl() >= 0)
344 {
345 os << "MulticastTTL:" << entry.getMulticastTtl() << ", ";
346 }
347
348 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700349 if (entry.getFreshnessPeriod() >= time::milliseconds::zero())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800350 {
351 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
352 }
353
354 os << ")";
355 return os;
356}
357
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800358} // namespace ndnd
359} // namespace ndn
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800360
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800361#endif // NDN_MANAGEMENT_NDND_FACE_INSTANCE_HPP