blob: 58048b527214ab6de9285b2e80bbe2589b9c3a42 [file] [log] [blame]
Wentao Shang4d5e1de2014-01-28 21:00:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 *
5 * @author: Wentao Shang <wentao@cs.ucla.edu>
6 *
7 * See COPYING for copyright and distribution information.
8 */
9
10#ifndef NDN_FIB_MANAGEMENT_HPP
11#define NDN_FIB_MANAGEMENT_HPP
12
Alexander Afanasyev274f2b02014-01-30 20:08:45 -080013#include "../encoding/block.hpp"
Wentao Shang77949212014-02-01 23:42:24 -080014#include "../encoding/encoding-buffer.hpp"
Alexander Afanasyev274f2b02014-01-30 20:08:45 -080015#include "../encoding/tlv-nfd-control.hpp"
Steve DiBenedetto73f428d2014-01-31 11:56:38 -070016#include "../name.hpp"
Wentao Shang4d5e1de2014-01-28 21:00:03 -080017
18namespace ndn {
19
20class FibManagementOptions {
21public:
22 FibManagementOptions ()
23 : faceId_ (-1)
24 , cost_ (-1)
25 {
26 }
27
28 const Name&
29 getName () const { return name_; }
30
31 void
32 setName (const Name &name) { name_ = name; wire_.reset (); }
33
34 int
35 getFaceId () const { return faceId_; }
36
37 void
38 setFaceId (int faceId) { faceId_ = faceId; wire_.reset (); }
39
40 int
41 getCost () const { return cost_; }
42
43 void
44 setCost (int cost) { cost_ = cost; wire_.reset (); }
45
Wentao Shang77949212014-02-01 23:42:24 -080046 inline size_t
47 wireEncode (EncodingBuffer& blk);
48
Wentao Shang4d5e1de2014-01-28 21:00:03 -080049 inline const Block&
50 wireEncode () const;
51
52 inline void
53 wireDecode (const Block &wire);
54
55private:
56 Name name_;
57 int faceId_;
58 int cost_;
59 //Name strategy_;
60 //TODO: implement strategy after its use is properly defined
61
62 mutable Block wire_;
63};
64
Wentao Shang77949212014-02-01 23:42:24 -080065inline size_t
66FibManagementOptions::wireEncode (EncodingBuffer& blk)
67{
68 size_t total_len = 0;
69 if (cost_ != -1)
70 {
71 size_t var_len = blk.prependNonNegativeInteger (cost_);
72 total_len += var_len;
73 total_len += blk.prependVarNumber (var_len);
74 total_len += blk.prependVarNumber (tlv::nfd_control::Cost);
75 }
76
77 if (faceId_ != -1)
78 {
79 size_t var_len = blk.prependNonNegativeInteger (faceId_);
80 total_len += var_len;
81 total_len += blk.prependVarNumber (var_len);
82 total_len += blk.prependVarNumber (tlv::nfd_control::FaceId);
83 }
84
85 total_len += name_.wireEncode (blk);
86
87 total_len += blk.prependVarNumber (total_len);
88 total_len += blk.prependVarNumber (tlv::nfd_control::FibManagementOptions);
89 return total_len;
90}
91
Wentao Shang4d5e1de2014-01-28 21:00:03 -080092inline const Block&
93FibManagementOptions::wireEncode () const
94{
95 if (wire_.hasWire ())
96 return wire_;
97
98 wire_ = Block (tlv::nfd_control::FibManagementOptions);
99
100 // Name
101 wire_.push_back (name_.wireEncode ());
102
103 // FaceId
104 if (faceId_ != -1)
105 wire_.push_back (nonNegativeIntegerBlock (tlv::nfd_control::FaceId, faceId_));
106
107 // Cost
108 if (cost_ != -1)
109 wire_.push_back (nonNegativeIntegerBlock (tlv::nfd_control::Cost, cost_));
110
111 //TODO: Strategy
112
113 wire_.encode ();
114 return wire_;
115}
116
117inline void
118FibManagementOptions::wireDecode (const Block &wire)
119{
120 name_.clear ();
121 faceId_ = -1;
122 cost_ = -1;
123
124 wire_ = wire;
125 wire_.parse ();
126
127 // Name
128 Block::element_iterator val = wire_.find(Tlv::Name);
129 if (val != wire_.getAll().end())
130 {
131 name_.wireDecode(*val);
132 }
133
134 // FaceID
135 val = wire_.find(tlv::nfd_control::FaceId);
136 if (val != wire_.getAll().end())
137 {
138 faceId_ = readNonNegativeInteger(*val);
139 }
140
141 // Cost
142 val = wire_.find(tlv::nfd_control::Cost);
143 if (val != wire_.getAll().end())
144 {
145 cost_ = readNonNegativeInteger(*val);
146 }
147
148 //TODO: Strategy
149}
150
151inline std::ostream&
152operator << (std::ostream &os, const FibManagementOptions &option)
153{
154 os << "ForwardingEntry(";
155
156 // Name
157 os << "Prefix: " << option.getName () << ", ";
158
159 // FaceID
160 os << "FaceID: " << option.getFaceId () << ", ";
161
162 // Cost
163 os << "Cost: " << option.getCost ();
164
165 os << ")";
166 return os;
167}
168
169}
170
171#endif