blob: 79b6e97a38117df702467f923071042c8ba86235 [file] [log] [blame]
laqinfan35731852017-08-08 06:17:39 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2018, 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 "nexthop.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<NextHop>));
32BOOST_CONCEPT_ASSERT((ndn::WireDecodable<NextHop>));
33static_assert(std::is_base_of<ndn::tlv::Error, NextHop::Error>::value,
34 "NextHop::Error must inherit from tlv::Error");
35
36NextHop::NextHop()
37 : m_cost(0)
38{
39}
40
41NextHop::NextHop(const ndn::Block& block)
42{
43 wireDecode(block);
44}
45
46template<ndn::encoding::Tag TAG>
47size_t
48NextHop::wireEncode(ndn::EncodingImpl<TAG>& block) const
49{
50 size_t totalLength = 0;
51
52 const uint8_t* doubleBytes = reinterpret_cast<const uint8_t*>(&m_cost);
53 totalLength += block.prependByteArrayBlock(ndn::tlv::nlsr::Double, doubleBytes, 8);
54
55 totalLength += block.prependByteArrayBlock(
56 ndn::tlv::nlsr::Uri, reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size());
57
58 totalLength += block.prependVarNumber(totalLength);
59 totalLength += block.prependVarNumber(ndn::tlv::nlsr::NextHop);
60
61 return totalLength;
62}
63
64NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(NextHop);
65
66template size_t
67NextHop::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const;
68
69template size_t
70NextHop::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const;
71
72const ndn::Block&
73NextHop::wireEncode() const
74{
75 if (m_wire.hasWire()) {
76 return m_wire;
77 }
78
79 ndn::EncodingEstimator estimator;
80 size_t estimatedSize = wireEncode(estimator);
81
82 ndn::EncodingBuffer buffer(estimatedSize, 0);
83 wireEncode(buffer);
84
85 m_wire = buffer.block();
86
87 return m_wire;
88}
89
90void
91NextHop::wireDecode(const ndn::Block& wire)
92{
93 m_uri = "";
94 m_cost = 0;
95
96 m_wire = wire;
97
98 if (m_wire.type() != ndn::tlv::nlsr::NextHop) {
99 std::stringstream error;
100 error << "Expected NextHop Block, but Block is of a different type: #"
101 << m_wire.type();
102 BOOST_THROW_EXCEPTION(Error(error.str()));
103 }
104
105 m_wire.parse();
106
107 ndn::Block::element_const_iterator val = m_wire.elements_begin();
108
109 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Uri) {
110 m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
111 ++val;
112 }
113 else {
114 BOOST_THROW_EXCEPTION(Error("Missing required Uri field"));
115 }
116
117 if (val != val->elements_end() && val->type() == ndn::tlv::nlsr::Double) {
118 m_cost = *reinterpret_cast<const double*>(val->value());
119 ++val;
120 }
121 else {
122 BOOST_THROW_EXCEPTION(Error("HyperbolicCost: Missing required Double field"));
123 }
124}
125
126std::ostream&
127operator<<(std::ostream& os, const NextHop& nexthop)
128{
129 os << "NextHop("
130 << "Uri: " << nexthop.getUri() << ", "<< "Cost: " << nexthop.getCost() << ")" << std::endl;
131
132 return os;
133}
134
135} // namespace tlv
136} // namespace nlsr