blob: b9adec9b872e1933dc0b57c19e3b3da4152d1e92 [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev67758b12018-03-06 18:36:44 -05002/*
3 * Copyright (c) 2014-2018, The University of Memphis,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08004 * 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/>.
Alexander Afanasyev67758b12018-03-06 18:36:44 -050020 */
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080021
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 {
Alexander Afanasyev67758b12018-03-06 18:36:44 -050029namespace tlv {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080030
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
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080046template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080047size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080048Adjacency::wireEncode(ndn::EncodingImpl<TAG>& encoder) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080049{
50 size_t totalLength = 0;
51
Vince Lehmanf4772d42015-06-29 14:02:22 -050052 totalLength += prependNonNegativeIntegerBlock(encoder, ndn::tlv::nlsr::Cost, m_cost);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080053
Vince Lehmanf4772d42015-06-29 14:02:22 -050054 totalLength += encoder.prependByteArrayBlock(
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080055 ndn::tlv::nlsr::Uri, reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size());
56
Vince Lehmanf4772d42015-06-29 14:02:22 -050057 totalLength += m_name.wireEncode(encoder);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080058
Vince Lehmanf4772d42015-06-29 14:02:22 -050059 totalLength += encoder.prependVarNumber(totalLength);
60 totalLength += encoder.prependVarNumber(ndn::tlv::nlsr::Adjacency);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080061
62 return totalLength;
63}
64
Alexander Afanasyev67758b12018-03-06 18:36:44 -050065NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Adjacency);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080066
67const ndn::Block&
68Adjacency::wireEncode() const
69{
70 if (m_wire.hasWire()) {
71 return m_wire;
72 }
73
74 ndn::EncodingEstimator estimator;
75 size_t estimatedSize = wireEncode(estimator);
76
77 ndn::EncodingBuffer buffer(estimatedSize, 0);
78 wireEncode(buffer);
79
80 m_wire = buffer.block();
81
82 return m_wire;
83}
84
85void
86Adjacency::wireDecode(const ndn::Block& wire)
87{
88 m_name.clear();
89 m_uri = "";
90 m_cost = 0;
91
92 m_wire = wire;
93
94 if (m_wire.type() != ndn::tlv::nlsr::Adjacency) {
Alexander Afanasyev67758b12018-03-06 18:36:44 -050095 BOOST_THROW_EXCEPTION(Error("Expected Adjacency Block, but Block is of a different type: #" +
96 ndn::to_string(m_wire.type())));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080097 }
98
99 m_wire.parse();
100
101 ndn::Block::element_const_iterator val = m_wire.elements_begin();
102
103 if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) {
104 m_name.wireDecode(*val);
105 ++val;
106 }
107 else {
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500108 BOOST_THROW_EXCEPTION(Error("Missing required Name field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800109 }
110
111 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Uri) {
112 m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
113 ++val;
114 }
115 else {
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500116 BOOST_THROW_EXCEPTION(Error("Missing required Uri field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800117 }
118
119 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Cost) {
120 m_cost = ndn::readNonNegativeInteger(*val);
121 ++val;
122 }
123 else {
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500124 BOOST_THROW_EXCEPTION(Error("Missing required Cost field"));
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800125 }
126}
127
128std::ostream&
129operator<<(std::ostream& os, const Adjacency& adjacency)
130{
131 os << "Adjacency("
132 << "Name: " << adjacency.getName() << ", "
133 << "Uri: " << adjacency.getUri() << ", "
134 << "Cost: " << adjacency.getCost() << ")";
135
136 return os;
137}
138
139} // namespace tlv
140} // namespace nlsr