blob: 60c07dc8dbe2858d14058941d6249eaea26ef2a6 [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 Afanasyev5d7db8e2014-01-05 22:43:57 -080022 FaceInstance(const std::string &action,
23 int64_t faceId,
24 uint32_t ipProto,
25 const std::string &host,
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -080026 const std::string &port,
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080027 const std::string &multicastInterface,
28 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
49 // Action
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070050 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080051 getAction() const { return action_; }
52
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080054 setAction(const std::string& action) { action_ = action; wire_.reset(); }
55
56 // FaceID
57 int64_t
58 getFaceId() const { return faceId_; }
59
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080061 setFaceId(int64_t faceId) { faceId_ = faceId; wire_.reset(); }
62
63 // IPProto
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064 int32_t
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080065 getIpProto() const { return ipProto_; }
66
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080068 setIpProto(int32_t ipProto) { ipProto_ = ipProto; wire_.reset(); }
69
70 // Host
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080072 getHost() const { return host_; }
73
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080075 setHost(const std::string& host) { host_ = host; wire_.reset(); }
76
77 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -080078 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080079 getPort() const { return port_; }
80
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070081 void
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -080082 setPort(const std::string &port) { port_ = port; wire_.reset(); }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080083
84 // MulticastInterface
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070085 const std::string&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080086 getMulticastInterface() const { return multicastInterface_; }
87
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088 void
89 setMulticastInterface(const std::string& multicastInterface)
90 {
91 multicastInterface_ = multicastInterface; wire_.reset();
92 }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080093
94 // MulticastTTL
95 int32_t
96 getMulticastTtl() const { return multicastTtl_; }
97
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070098 void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080099 setMulticastTtl(int32_t multicastTtl) { multicastTtl_ = multicastTtl; wire_.reset(); }
100
101 // Freshness
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700102 const time::milliseconds&
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800103 getFreshnessPeriod() const { return freshnessPeriod_; }
104
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700105 void
106 setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
107 {
108 freshnessPeriod_ = freshnessPeriod; wire_.reset();
109 }
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800110
111 // Wire
112 inline const Block&
113 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700114
115 inline void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800116 wireDecode(const Block &wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700117
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800118private:
119 std::string action_;
120 int64_t faceId_;
121 int32_t ipProto_;
122 std::string host_;
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800123 std::string port_;
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800124 std::string multicastInterface_;
125 int32_t multicastTtl_;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700126 time::milliseconds freshnessPeriod_;
127
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800128 mutable Block wire_;
129};
130
131inline const Block&
132FaceInstance::wireEncode() const
133{
134 if (wire_.hasWire())
135 return wire_;
136
137 // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH
138 // Action?
139 // FaceID?
140 // IPProto?
141 // Host?
142 // Port?
143 // MulticastInterface?
144 // MulticastTTL?
145 // FreshnessPeriod?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700146
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800147 wire_ = Block(tlv::ndnd::FaceInstance);
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800148
149 // Action
150 if (!action_.empty())
151 {
152 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800153 (dataBlock(tlv::ndnd::Action, action_.c_str(), action_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800154 }
155
156 // FaceID
157 if (faceId_ >= 0)
158 {
159 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800160 (nonNegativeIntegerBlock(tlv::ndnd::FaceID, faceId_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800161 }
162
163 // IPProto
164 if (ipProto_ >= 0)
165 {
166 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800167 (nonNegativeIntegerBlock(tlv::ndnd::IPProto, ipProto_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800168 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700169
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800170 // Host
171 if (!host_.empty())
172 {
173 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800174 (dataBlock(tlv::ndnd::Host, host_.c_str(), host_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800175 }
176
177 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800178 if (!port_.empty())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800179 {
180 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800181 (dataBlock(tlv::ndnd::Port, port_.c_str(), port_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800182 }
183
184 // MulticastInterface
185 if (!multicastInterface_.empty())
186 {
187 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800188 (dataBlock(tlv::ndnd::MulticastInterface, multicastInterface_.c_str(), multicastInterface_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800189 }
190
191 // MulticastTTL
192 if (multicastTtl_ >= 0)
193 {
194 wire_.push_back
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800195 (nonNegativeIntegerBlock(tlv::ndnd::MulticastTTL, multicastTtl_));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800196 }
197
198 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700199 if (freshnessPeriod_ >= time::milliseconds::zero())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800200 {
201 wire_.push_back
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700202 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_.count()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800203 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700204
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800205 wire_.encode();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700206 return wire_;
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800207}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700208
209inline void
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800210FaceInstance::wireDecode(const Block &wire)
211{
212 action_.clear();
213 faceId_ = -1;
214 ipProto_ = -1;
215 host_.clear();
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800216 port_.clear();
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800217 multicastInterface_.clear();
218 multicastTtl_ = -1;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700219 freshnessPeriod_ = time::milliseconds::min();
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800220
221 wire_ = wire;
222 wire_.parse();
223
224 // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH
225 // Action?
226 // FaceID?
227 // IPProto?
228 // Host?
229 // Port?
230 // MulticastInterface?
231 // MulticastTTL?
232 // FreshnessPeriod?
233
234 // Action
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800235 Block::element_const_iterator val = wire_.find(tlv::ndnd::Action);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800236 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800237 {
238 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
239 }
240
241 // FaceID
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800242 val = wire_.find(tlv::ndnd::FaceID);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800243 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800244 {
245 faceId_ = readNonNegativeInteger(*val);
246 }
247
248 // IPProto
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800249 val = wire_.find(tlv::ndnd::IPProto);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800250 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800251 {
252 ipProto_ = readNonNegativeInteger(*val);
253 }
254
255 // Host
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800256 val = wire_.find(tlv::ndnd::Host);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800257 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800258 {
259 host_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
260 }
261
262 // Port
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800263 val = wire_.find(tlv::ndnd::Port);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800264 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800265 {
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800266 port_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800267 }
268
269 // MulticastInterface
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800270 val = wire_.find(tlv::ndnd::MulticastInterface);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800271 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800272 {
273 multicastInterface_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
274 }
275
276 // MulticastTTL
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800277 val = wire_.find(tlv::ndnd::MulticastTTL);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800278 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800279 {
280 multicastTtl_ = readNonNegativeInteger(*val);
281 }
282
283 // FreshnessPeriod
284 val = wire_.find(Tlv::FreshnessPeriod);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800285 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800286 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700287 freshnessPeriod_ = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800288 }
289}
290
291inline std::ostream&
292operator << (std::ostream &os, const FaceInstance &entry)
293{
294 os << "FaceInstance(";
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700295
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800296 // Action
297 if (!entry.getAction().empty())
298 {
299 os << "Action:" << entry.getAction() << ", ";
300 }
301
302 // FaceID
303 if (entry.getFaceId() >= 0)
304 {
305 os << "FaceID:" << entry.getFaceId() << ", ";
306 }
307
308 // IPProto
309 if (entry.getIpProto() >= 0)
310 {
311 os << "IPProto:" << entry.getIpProto() << ", ";
312 }
313
314 // Host
315 if (!entry.getHost().empty())
316 {
317 os << "Host:" << entry.getHost() << ", ";
318 }
319
320 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800321 if (!entry.getPort().empty())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800322 {
323 os << "Port:" << entry.getPort() << ", ";
324 }
325
326 // MulticastInterface
327 if (!entry.getMulticastInterface().empty())
328 {
329 os << "MulticastInterface:" << entry.getMulticastInterface() << ", ";
330 }
331
332 // MulticastTTL
333 if (entry.getMulticastTtl() >= 0)
334 {
335 os << "MulticastTTL:" << entry.getMulticastTtl() << ", ";
336 }
337
338 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700339 if (entry.getFreshnessPeriod() >= time::milliseconds::zero())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800340 {
341 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
342 }
343
344 os << ")";
345 return os;
346}
347
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800348} // namespace ndnd
349} // namespace ndn
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800350
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800351#endif // NDN_MANAGEMENT_NDND_FACE_INSTANCE_HPP