blob: 87f04ed1074ff3e326feccb0bc9a400b6870ff2b [file] [log] [blame]
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080011 */
12
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080013#ifndef NDN_MANAGEMENT_NDND_FACE_INSTANCE_HPP
14#define NDN_MANAGEMENT_NDND_FACE_INSTANCE_HPP
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080015
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080016#include "../encoding/tlv-ndnd.hpp"
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080017#include "../encoding/block.hpp"
18#include "../name.hpp"
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080019
20namespace ndn {
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080021namespace ndnd {
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080022
23/**
24 * An FaceInstance holds an action and Name prefix and other fields for an forwarding entry.
25 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070026class FaceInstance
27{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070028public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070029 FaceInstance(const std::string& action,
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080030 int64_t faceId,
31 uint32_t ipProto,
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070032 const std::string& host,
33 const std::string& port,
34 const std::string& multicastInterface,
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080035 uint32_t multicastTtl,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070036 const time::milliseconds& freshnessPeriod)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080037 : action_(action)
38 , faceId_(faceId)
39 , ipProto_(ipProto)
40 , host_(host)
41 , port_(port)
42 , multicastInterface_(multicastInterface)
43 , multicastTtl_(multicastTtl)
44 , freshnessPeriod_(freshnessPeriod)
45 {
46 }
47
48 FaceInstance()
49 : faceId_(-1)
50 , ipProto_(-1)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080051 , multicastTtl_(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070052 , freshnessPeriod_(time::milliseconds::min())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080053 {
54 }
55
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070056 /**
57 * @brief Create from wire encoding
58 */
59 explicit
60 FaceInstance(const Block& wire)
61 {
62 wireDecode(wire);
63 }
64
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080065 // Action
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080067 getAction() const { return action_; }
68
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080070 setAction(const std::string& action) { action_ = action; wire_.reset(); }
71
72 // FaceID
73 int64_t
74 getFaceId() const { return faceId_; }
75
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080077 setFaceId(int64_t faceId) { faceId_ = faceId; wire_.reset(); }
78
79 // IPProto
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070080 int32_t
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080081 getIpProto() const { return ipProto_; }
82
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070083 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080084 setIpProto(int32_t ipProto) { ipProto_ = ipProto; wire_.reset(); }
85
86 // Host
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070087 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080088 getHost() const { return host_; }
89
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070090 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080091 setHost(const std::string& host) { host_ = host; wire_.reset(); }
92
93 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -080094 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080095 getPort() const { return port_; }
96
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070097 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098 setPort(const std::string& port) { port_ = port; wire_.reset(); }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080099
100 // MulticastInterface
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700101 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800102 getMulticastInterface() const { return multicastInterface_; }
103
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700104 void
105 setMulticastInterface(const std::string& multicastInterface)
106 {
107 multicastInterface_ = multicastInterface; wire_.reset();
108 }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800109
110 // MulticastTTL
111 int32_t
112 getMulticastTtl() const { return multicastTtl_; }
113
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700114 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800115 setMulticastTtl(int32_t multicastTtl) { multicastTtl_ = multicastTtl; wire_.reset(); }
116
117 // Freshness
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700118 const time::milliseconds&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800119 getFreshnessPeriod() const { return freshnessPeriod_; }
120
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700121 void
122 setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
123 {
124 freshnessPeriod_ = freshnessPeriod; wire_.reset();
125 }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800126
127 // Wire
128 inline const Block&
129 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700130
131 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700132 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700133
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800134private:
135 std::string action_;
136 int64_t faceId_;
137 int32_t ipProto_;
138 std::string host_;
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800139 std::string port_;
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800140 std::string multicastInterface_;
141 int32_t multicastTtl_;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700142 time::milliseconds freshnessPeriod_;
143
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800144 mutable Block wire_;
145};
146
147inline const Block&
148FaceInstance::wireEncode() const
149{
150 if (wire_.hasWire())
151 return wire_;
152
153 // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH
154 // Action?
155 // FaceID?
156 // IPProto?
157 // Host?
158 // Port?
159 // MulticastInterface?
160 // MulticastTTL?
161 // FreshnessPeriod?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700162
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800163 wire_ = Block(tlv::ndnd::FaceInstance);
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800164
165 // Action
166 if (!action_.empty())
167 {
168 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800169 (dataBlock(tlv::ndnd::Action, action_.c_str(), action_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800170 }
171
172 // FaceID
173 if (faceId_ >= 0)
174 {
175 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800176 (nonNegativeIntegerBlock(tlv::ndnd::FaceID, faceId_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800177 }
178
179 // IPProto
180 if (ipProto_ >= 0)
181 {
182 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800183 (nonNegativeIntegerBlock(tlv::ndnd::IPProto, ipProto_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800184 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700185
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800186 // Host
187 if (!host_.empty())
188 {
189 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800190 (dataBlock(tlv::ndnd::Host, host_.c_str(), host_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800191 }
192
193 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800194 if (!port_.empty())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800195 {
196 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800197 (dataBlock(tlv::ndnd::Port, port_.c_str(), port_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800198 }
199
200 // MulticastInterface
201 if (!multicastInterface_.empty())
202 {
203 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800204 (dataBlock(tlv::ndnd::MulticastInterface, multicastInterface_.c_str(), multicastInterface_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800205 }
206
207 // MulticastTTL
208 if (multicastTtl_ >= 0)
209 {
210 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800211 (nonNegativeIntegerBlock(tlv::ndnd::MulticastTTL, multicastTtl_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800212 }
213
214 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700215 if (freshnessPeriod_ >= time::milliseconds::zero())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800216 {
217 wire_.push_back
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700218 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_.count()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800219 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700220
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800221 wire_.encode();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700222 return wire_;
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800223}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700224
225inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700226FaceInstance::wireDecode(const Block& wire)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800227{
228 action_.clear();
229 faceId_ = -1;
230 ipProto_ = -1;
231 host_.clear();
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800232 port_.clear();
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800233 multicastInterface_.clear();
234 multicastTtl_ = -1;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700235 freshnessPeriod_ = time::milliseconds::min();
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800236
237 wire_ = wire;
238 wire_.parse();
239
240 // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH
241 // Action?
242 // FaceID?
243 // IPProto?
244 // Host?
245 // Port?
246 // MulticastInterface?
247 // MulticastTTL?
248 // FreshnessPeriod?
249
250 // Action
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800251 Block::element_const_iterator val = wire_.find(tlv::ndnd::Action);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800252 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800253 {
254 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
255 }
256
257 // FaceID
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800258 val = wire_.find(tlv::ndnd::FaceID);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800259 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800260 {
261 faceId_ = readNonNegativeInteger(*val);
262 }
263
264 // IPProto
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800265 val = wire_.find(tlv::ndnd::IPProto);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800266 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800267 {
268 ipProto_ = readNonNegativeInteger(*val);
269 }
270
271 // Host
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800272 val = wire_.find(tlv::ndnd::Host);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800273 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800274 {
275 host_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
276 }
277
278 // Port
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800279 val = wire_.find(tlv::ndnd::Port);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800280 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800281 {
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800282 port_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800283 }
284
285 // MulticastInterface
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800286 val = wire_.find(tlv::ndnd::MulticastInterface);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800287 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800288 {
289 multicastInterface_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
290 }
291
292 // MulticastTTL
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800293 val = wire_.find(tlv::ndnd::MulticastTTL);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800294 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800295 {
296 multicastTtl_ = readNonNegativeInteger(*val);
297 }
298
299 // FreshnessPeriod
300 val = wire_.find(Tlv::FreshnessPeriod);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800301 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800302 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700303 freshnessPeriod_ = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800304 }
305}
306
307inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700308operator << (std::ostream& os, const FaceInstance& entry)
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800309{
310 os << "FaceInstance(";
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700311
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800312 // Action
313 if (!entry.getAction().empty())
314 {
315 os << "Action:" << entry.getAction() << ", ";
316 }
317
318 // FaceID
319 if (entry.getFaceId() >= 0)
320 {
321 os << "FaceID:" << entry.getFaceId() << ", ";
322 }
323
324 // IPProto
325 if (entry.getIpProto() >= 0)
326 {
327 os << "IPProto:" << entry.getIpProto() << ", ";
328 }
329
330 // Host
331 if (!entry.getHost().empty())
332 {
333 os << "Host:" << entry.getHost() << ", ";
334 }
335
336 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800337 if (!entry.getPort().empty())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800338 {
339 os << "Port:" << entry.getPort() << ", ";
340 }
341
342 // MulticastInterface
343 if (!entry.getMulticastInterface().empty())
344 {
345 os << "MulticastInterface:" << entry.getMulticastInterface() << ", ";
346 }
347
348 // MulticastTTL
349 if (entry.getMulticastTtl() >= 0)
350 {
351 os << "MulticastTTL:" << entry.getMulticastTtl() << ", ";
352 }
353
354 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700355 if (entry.getFreshnessPeriod() >= time::milliseconds::zero())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800356 {
357 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
358 }
359
360 os << ")";
361 return os;
362}
363
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800364} // namespace ndnd
365} // namespace ndn
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800366
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800367#endif // NDN_MANAGEMENT_NDND_FACE_INSTANCE_HPP