blob: 530f10d703a28ec1f02f033dc2da98c2ea723bd1 [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 */
20class FaceInstance {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070021public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070022 FaceInstance(const std::string& action,
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080023 int64_t faceId,
24 uint32_t ipProto,
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070025 const std::string& host,
26 const std::string& port,
27 const std::string& multicastInterface,
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080028 uint32_t multicastTtl,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070029 const time::milliseconds& freshnessPeriod)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080030 : action_(action)
31 , faceId_(faceId)
32 , ipProto_(ipProto)
33 , host_(host)
34 , port_(port)
35 , multicastInterface_(multicastInterface)
36 , multicastTtl_(multicastTtl)
37 , freshnessPeriod_(freshnessPeriod)
38 {
39 }
40
41 FaceInstance()
42 : faceId_(-1)
43 , ipProto_(-1)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080044 , multicastTtl_(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070045 , freshnessPeriod_(time::milliseconds::min())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080046 {
47 }
48
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049 /**
50 * @brief Create from wire encoding
51 */
52 explicit
53 FaceInstance(const Block& wire)
54 {
55 wireDecode(wire);
56 }
57
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080058 // Action
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070059 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080060 getAction() const { return action_; }
61
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070062 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080063 setAction(const std::string& action) { action_ = action; wire_.reset(); }
64
65 // FaceID
66 int64_t
67 getFaceId() const { return faceId_; }
68
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080070 setFaceId(int64_t faceId) { faceId_ = faceId; wire_.reset(); }
71
72 // IPProto
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070073 int32_t
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080074 getIpProto() const { return ipProto_; }
75
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080077 setIpProto(int32_t ipProto) { ipProto_ = ipProto; wire_.reset(); }
78
79 // Host
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070080 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080081 getHost() const { return host_; }
82
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070083 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080084 setHost(const std::string& host) { host_ = host; wire_.reset(); }
85
86 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -080087 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080088 getPort() const { return port_; }
89
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070090 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091 setPort(const std::string& port) { port_ = port; wire_.reset(); }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080092
93 // MulticastInterface
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070094 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080095 getMulticastInterface() const { return multicastInterface_; }
96
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070097 void
98 setMulticastInterface(const std::string& multicastInterface)
99 {
100 multicastInterface_ = multicastInterface; wire_.reset();
101 }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800102
103 // MulticastTTL
104 int32_t
105 getMulticastTtl() const { return multicastTtl_; }
106
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700107 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800108 setMulticastTtl(int32_t multicastTtl) { multicastTtl_ = multicastTtl; wire_.reset(); }
109
110 // Freshness
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700111 const time::milliseconds&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800112 getFreshnessPeriod() const { return freshnessPeriod_; }
113
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700114 void
115 setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
116 {
117 freshnessPeriod_ = freshnessPeriod; wire_.reset();
118 }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800119
120 // Wire
121 inline const Block&
122 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700123
124 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700126
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800127private:
128 std::string action_;
129 int64_t faceId_;
130 int32_t ipProto_;
131 std::string host_;
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800132 std::string port_;
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800133 std::string multicastInterface_;
134 int32_t multicastTtl_;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700135 time::milliseconds freshnessPeriod_;
136
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800137 mutable Block wire_;
138};
139
140inline const Block&
141FaceInstance::wireEncode() const
142{
143 if (wire_.hasWire())
144 return wire_;
145
146 // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH
147 // Action?
148 // FaceID?
149 // IPProto?
150 // Host?
151 // Port?
152 // MulticastInterface?
153 // MulticastTTL?
154 // FreshnessPeriod?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700155
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800156 wire_ = Block(tlv::ndnd::FaceInstance);
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800157
158 // Action
159 if (!action_.empty())
160 {
161 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800162 (dataBlock(tlv::ndnd::Action, action_.c_str(), action_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800163 }
164
165 // FaceID
166 if (faceId_ >= 0)
167 {
168 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800169 (nonNegativeIntegerBlock(tlv::ndnd::FaceID, faceId_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800170 }
171
172 // IPProto
173 if (ipProto_ >= 0)
174 {
175 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800176 (nonNegativeIntegerBlock(tlv::ndnd::IPProto, ipProto_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800177 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700178
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800179 // Host
180 if (!host_.empty())
181 {
182 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800183 (dataBlock(tlv::ndnd::Host, host_.c_str(), host_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800184 }
185
186 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800187 if (!port_.empty())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800188 {
189 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800190 (dataBlock(tlv::ndnd::Port, port_.c_str(), port_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800191 }
192
193 // MulticastInterface
194 if (!multicastInterface_.empty())
195 {
196 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800197 (dataBlock(tlv::ndnd::MulticastInterface, multicastInterface_.c_str(), multicastInterface_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800198 }
199
200 // MulticastTTL
201 if (multicastTtl_ >= 0)
202 {
203 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800204 (nonNegativeIntegerBlock(tlv::ndnd::MulticastTTL, multicastTtl_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800205 }
206
207 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700208 if (freshnessPeriod_ >= time::milliseconds::zero())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800209 {
210 wire_.push_back
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700211 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_.count()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800212 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700213
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800214 wire_.encode();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700215 return wire_;
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800216}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700217
218inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700219FaceInstance::wireDecode(const Block& wire)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800220{
221 action_.clear();
222 faceId_ = -1;
223 ipProto_ = -1;
224 host_.clear();
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800225 port_.clear();
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800226 multicastInterface_.clear();
227 multicastTtl_ = -1;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700228 freshnessPeriod_ = time::milliseconds::min();
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800229
230 wire_ = wire;
231 wire_.parse();
232
233 // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH
234 // Action?
235 // FaceID?
236 // IPProto?
237 // Host?
238 // Port?
239 // MulticastInterface?
240 // MulticastTTL?
241 // FreshnessPeriod?
242
243 // Action
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800244 Block::element_const_iterator val = wire_.find(tlv::ndnd::Action);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800245 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800246 {
247 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
248 }
249
250 // FaceID
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800251 val = wire_.find(tlv::ndnd::FaceID);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800252 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800253 {
254 faceId_ = readNonNegativeInteger(*val);
255 }
256
257 // IPProto
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800258 val = wire_.find(tlv::ndnd::IPProto);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800259 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800260 {
261 ipProto_ = readNonNegativeInteger(*val);
262 }
263
264 // Host
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800265 val = wire_.find(tlv::ndnd::Host);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800266 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800267 {
268 host_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
269 }
270
271 // Port
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800272 val = wire_.find(tlv::ndnd::Port);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800273 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800274 {
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800275 port_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800276 }
277
278 // MulticastInterface
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800279 val = wire_.find(tlv::ndnd::MulticastInterface);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800280 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800281 {
282 multicastInterface_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
283 }
284
285 // MulticastTTL
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800286 val = wire_.find(tlv::ndnd::MulticastTTL);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800287 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800288 {
289 multicastTtl_ = readNonNegativeInteger(*val);
290 }
291
292 // FreshnessPeriod
293 val = wire_.find(Tlv::FreshnessPeriod);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800294 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800295 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700296 freshnessPeriod_ = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800297 }
298}
299
300inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700301operator << (std::ostream& os, const FaceInstance& entry)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800302{
303 os << "FaceInstance(";
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700304
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800305 // Action
306 if (!entry.getAction().empty())
307 {
308 os << "Action:" << entry.getAction() << ", ";
309 }
310
311 // FaceID
312 if (entry.getFaceId() >= 0)
313 {
314 os << "FaceID:" << entry.getFaceId() << ", ";
315 }
316
317 // IPProto
318 if (entry.getIpProto() >= 0)
319 {
320 os << "IPProto:" << entry.getIpProto() << ", ";
321 }
322
323 // Host
324 if (!entry.getHost().empty())
325 {
326 os << "Host:" << entry.getHost() << ", ";
327 }
328
329 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800330 if (!entry.getPort().empty())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800331 {
332 os << "Port:" << entry.getPort() << ", ";
333 }
334
335 // MulticastInterface
336 if (!entry.getMulticastInterface().empty())
337 {
338 os << "MulticastInterface:" << entry.getMulticastInterface() << ", ";
339 }
340
341 // MulticastTTL
342 if (entry.getMulticastTtl() >= 0)
343 {
344 os << "MulticastTTL:" << entry.getMulticastTtl() << ", ";
345 }
346
347 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700348 if (entry.getFreshnessPeriod() >= time::milliseconds::zero())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800349 {
350 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
351 }
352
353 os << ")";
354 return os;
355}
356
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800357} // namespace ndnd
358} // namespace ndn
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800359
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800360#endif // NDN_MANAGEMENT_NDND_FACE_INSTANCE_HPP