blob: 0de85fff5440462c98d9a39864521dc8ec511f3b [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 Afanasyevc8823bc2014-02-09 19:33:33 -080010#include "../encoding/tlv-face-management.hpp"
11#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 {
21public:
22 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,
29 Milliseconds freshnessPeriod)
30 : 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)
45 , freshnessPeriod_(-1)
46 {
47 }
48
49 // Action
50 const std::string&
51 getAction() const { return action_; }
52
53 void
54 setAction(const std::string& action) { action_ = action; wire_.reset(); }
55
56 // FaceID
57 int64_t
58 getFaceId() const { return faceId_; }
59
60 void
61 setFaceId(int64_t faceId) { faceId_ = faceId; wire_.reset(); }
62
63 // IPProto
64 int32_t
65 getIpProto() const { return ipProto_; }
66
67 void
68 setIpProto(int32_t ipProto) { ipProto_ = ipProto; wire_.reset(); }
69
70 // Host
71 const std::string&
72 getHost() const { return host_; }
73
74 void
75 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
81 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
85 const std::string&
86 getMulticastInterface() const { return multicastInterface_; }
87
88 void
89 setMulticastInterface(const std::string& multicastInterface) { multicastInterface_ = multicastInterface; wire_.reset(); }
90
91 // MulticastTTL
92 int32_t
93 getMulticastTtl() const { return multicastTtl_; }
94
95 void
96 setMulticastTtl(int32_t multicastTtl) { multicastTtl_ = multicastTtl; wire_.reset(); }
97
98 // Freshness
99 int
100 getFreshnessPeriod() const { return freshnessPeriod_; }
101
102 void
103 setFreshnessPeriod(int freshnessPeriod) { freshnessPeriod_ = freshnessPeriod; wire_.reset(); }
104
105 // Wire
106 inline const Block&
107 wireEncode() const;
108
109 inline void
110 wireDecode(const Block &wire);
111
112private:
113 std::string action_;
114 int64_t faceId_;
115 int32_t ipProto_;
116 std::string host_;
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800117 std::string port_;
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800118 std::string multicastInterface_;
119 int32_t multicastTtl_;
120 Milliseconds freshnessPeriod_;
121
122 mutable Block wire_;
123};
124
125inline const Block&
126FaceInstance::wireEncode() const
127{
128 if (wire_.hasWire())
129 return wire_;
130
131 // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH
132 // Action?
133 // FaceID?
134 // IPProto?
135 // Host?
136 // Port?
137 // MulticastInterface?
138 // MulticastTTL?
139 // FreshnessPeriod?
140
141 wire_ = Block(Tlv::FaceManagement::FaceInstance);
142
143 // Action
144 if (!action_.empty())
145 {
146 wire_.push_back
147 (dataBlock(Tlv::FaceManagement::Action, action_.c_str(), action_.size()));
148 }
149
150 // FaceID
151 if (faceId_ >= 0)
152 {
153 wire_.push_back
154 (nonNegativeIntegerBlock(Tlv::FaceManagement::FaceID, faceId_));
155 }
156
157 // IPProto
158 if (ipProto_ >= 0)
159 {
160 wire_.push_back
161 (nonNegativeIntegerBlock(Tlv::FaceManagement::IPProto, ipProto_));
162 }
163
164 // Host
165 if (!host_.empty())
166 {
167 wire_.push_back
168 (dataBlock(Tlv::FaceManagement::Host, host_.c_str(), host_.size()));
169 }
170
171 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800172 if (!port_.empty())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800173 {
174 wire_.push_back
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800175 (dataBlock(Tlv::FaceManagement::Port, port_.c_str(), port_.size()));
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800176 }
177
178 // MulticastInterface
179 if (!multicastInterface_.empty())
180 {
181 wire_.push_back
182 (dataBlock(Tlv::FaceManagement::MulticastInterface, multicastInterface_.c_str(), multicastInterface_.size()));
183 }
184
185 // MulticastTTL
186 if (multicastTtl_ >= 0)
187 {
188 wire_.push_back
189 (nonNegativeIntegerBlock(Tlv::FaceManagement::MulticastTTL, multicastTtl_));
190 }
191
192 // FreshnessPeriod
193 if (freshnessPeriod_ >= 0)
194 {
195 wire_.push_back
196 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_));
197 }
198
199 wire_.encode();
200 return wire_;
201}
202
203inline void
204FaceInstance::wireDecode(const Block &wire)
205{
206 action_.clear();
207 faceId_ = -1;
208 ipProto_ = -1;
209 host_.clear();
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800210 port_.clear();
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800211 multicastInterface_.clear();
212 multicastTtl_ = -1;
213 freshnessPeriod_ = -1;
214
215 wire_ = wire;
216 wire_.parse();
217
218 // FaceInstance ::= FACE-INSTANCE-TYPE TLV-LENGTH
219 // Action?
220 // FaceID?
221 // IPProto?
222 // Host?
223 // Port?
224 // MulticastInterface?
225 // MulticastTTL?
226 // FreshnessPeriod?
227
228 // Action
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800229 Block::element_const_iterator val = wire_.find(Tlv::FaceManagement::Action);
230 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800231 {
232 action_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
233 }
234
235 // FaceID
236 val = wire_.find(Tlv::FaceManagement::FaceID);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800237 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800238 {
239 faceId_ = readNonNegativeInteger(*val);
240 }
241
242 // IPProto
243 val = wire_.find(Tlv::FaceManagement::IPProto);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800244 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800245 {
246 ipProto_ = readNonNegativeInteger(*val);
247 }
248
249 // Host
250 val = wire_.find(Tlv::FaceManagement::Host);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800251 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800252 {
253 host_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
254 }
255
256 // Port
257 val = wire_.find(Tlv::FaceManagement::Port);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800258 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800259 {
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800260 port_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800261 }
262
263 // MulticastInterface
264 val = wire_.find(Tlv::FaceManagement::MulticastInterface);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800265 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800266 {
267 multicastInterface_ = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
268 }
269
270 // MulticastTTL
271 val = wire_.find(Tlv::FaceManagement::MulticastTTL);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800272 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800273 {
274 multicastTtl_ = readNonNegativeInteger(*val);
275 }
276
277 // FreshnessPeriod
278 val = wire_.find(Tlv::FreshnessPeriod);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800279 if (val != wire_.elements_end())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800280 {
281 freshnessPeriod_ = readNonNegativeInteger(*val);
282 }
283}
284
285inline std::ostream&
286operator << (std::ostream &os, const FaceInstance &entry)
287{
288 os << "FaceInstance(";
289
290 // Action
291 if (!entry.getAction().empty())
292 {
293 os << "Action:" << entry.getAction() << ", ";
294 }
295
296 // FaceID
297 if (entry.getFaceId() >= 0)
298 {
299 os << "FaceID:" << entry.getFaceId() << ", ";
300 }
301
302 // IPProto
303 if (entry.getIpProto() >= 0)
304 {
305 os << "IPProto:" << entry.getIpProto() << ", ";
306 }
307
308 // Host
309 if (!entry.getHost().empty())
310 {
311 os << "Host:" << entry.getHost() << ", ";
312 }
313
314 // Port
Alexander Afanasyev0a5eabf2014-01-02 09:03:25 -0800315 if (!entry.getPort().empty())
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800316 {
317 os << "Port:" << entry.getPort() << ", ";
318 }
319
320 // MulticastInterface
321 if (!entry.getMulticastInterface().empty())
322 {
323 os << "MulticastInterface:" << entry.getMulticastInterface() << ", ";
324 }
325
326 // MulticastTTL
327 if (entry.getMulticastTtl() >= 0)
328 {
329 os << "MulticastTTL:" << entry.getMulticastTtl() << ", ";
330 }
331
332 // FreshnessPeriod
333 if (entry.getFreshnessPeriod() >= 0)
334 {
335 os << "FreshnessPeriod:" << entry.getFreshnessPeriod() << ", ";
336 }
337
338 os << ")";
339 return os;
340}
341
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800342} // namespace ndnd
343} // namespace ndn
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -0800344
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800345#endif // NDN_MANAGEMENT_NDND_FACE_INSTANCE_HPP
346