blob: e9912b08b5731e17fb54b3bf824276d85c6c6ab5 [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 **/
21
22#include "adjacency.hpp"
23#include "tlv-nlsr.hpp"
24
25#include <ndn-cxx/util/concepts.hpp>
26#include <ndn-cxx/encoding/block-helpers.hpp>
27
28namespace nlsr {
29namespace tlv {
30
31BOOST_CONCEPT_ASSERT((ndn::WireEncodable<Adjacency>));
32BOOST_CONCEPT_ASSERT((ndn::WireDecodable<Adjacency>));
33static_assert(std::is_base_of<ndn::tlv::Error, Adjacency::Error>::value,
34 "Adjacency::Error must inherit from tlv::Error");
35
36Adjacency::Adjacency()
37 : m_cost(0)
38{
39}
40
41Adjacency::Adjacency(const ndn::Block& block)
42{
43 wireDecode(block);
44}
45
46template<bool T>
47size_t
48Adjacency::wireEncode(ndn::EncodingImpl<T>& block) const
49{
50 size_t totalLength = 0;
51
52 totalLength += ndn::prependNonNegativeIntegerBlock(block,
53 ndn::tlv::nlsr::Cost,
54 m_cost);
55
56 totalLength += block.prependByteArrayBlock(
57 ndn::tlv::nlsr::Uri, reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size());
58
59 totalLength += m_name.wireEncode(block);
60
61 totalLength += block.prependVarNumber(totalLength);
62 totalLength += block.prependVarNumber(ndn::tlv::nlsr::Adjacency);
63
64 return totalLength;
65}
66
67template size_t
68Adjacency::wireEncode<true>(ndn::EncodingImpl<true>& block) const;
69
70template size_t
71Adjacency::wireEncode<false>(ndn::EncodingImpl<false>& block) const;
72
73const ndn::Block&
74Adjacency::wireEncode() const
75{
76 if (m_wire.hasWire()) {
77 return m_wire;
78 }
79
80 ndn::EncodingEstimator estimator;
81 size_t estimatedSize = wireEncode(estimator);
82
83 ndn::EncodingBuffer buffer(estimatedSize, 0);
84 wireEncode(buffer);
85
86 m_wire = buffer.block();
87
88 return m_wire;
89}
90
91void
92Adjacency::wireDecode(const ndn::Block& wire)
93{
94 m_name.clear();
95 m_uri = "";
96 m_cost = 0;
97
98 m_wire = wire;
99
100 if (m_wire.type() != ndn::tlv::nlsr::Adjacency) {
101 std::stringstream error;
102 error << "Expected Adjacency Block, but Block is of a different type: #"
103 << m_wire.type();
104 throw Error(error.str());
105 }
106
107 m_wire.parse();
108
109 ndn::Block::element_const_iterator val = m_wire.elements_begin();
110
111 if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) {
112 m_name.wireDecode(*val);
113 ++val;
114 }
115 else {
116 throw Error("Missing required Name field");
117 }
118
119 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Uri) {
120 m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
121 ++val;
122 }
123 else {
124 throw Error("Missing required Uri field");
125 }
126
127 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Cost) {
128 m_cost = ndn::readNonNegativeInteger(*val);
129 ++val;
130 }
131 else {
132 throw Error("Missing required Cost field");
133 }
134}
135
136std::ostream&
137operator<<(std::ostream& os, const Adjacency& adjacency)
138{
139 os << "Adjacency("
140 << "Name: " << adjacency.getName() << ", "
141 << "Uri: " << adjacency.getUri() << ", "
142 << "Cost: " << adjacency.getCost() << ")";
143
144 return os;
145}
146
147} // namespace tlv
148} // namespace nlsr