nexthop.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, The University of Memphis,
4  * Regents of the University of California
5  *
6  * This file is part of NLSR (Named-data Link State Routing).
7  * See AUTHORS.md for complete list of NLSR authors and contributors.
8  *
9  * NLSR is free software: you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License as published by the Free Software Foundation,
11  * either version 3 of the License, or (at your option) any later version.
12  *
13  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "nexthop.hpp"
22 #include "tlv-nlsr.hpp"
23 
24 #include <ndn-cxx/encoding/block-helpers.hpp>
25 
26 namespace nlsr {
27 
28 template<ndn::encoding::Tag TAG>
29 size_t
30 NextHop::wireEncode(ndn::EncodingImpl<TAG>& block) const
31 {
32  size_t totalLength = 0;
33 
34  totalLength += ndn::encoding::prependDoubleBlock(block, nlsr::tlv::CostDouble, m_routeCost);
35  totalLength += ndn::encoding::prependStringBlock(block, nlsr::tlv::Uri, m_connectingFaceUri);
36 
37  totalLength += block.prependVarNumber(totalLength);
38  totalLength += block.prependVarNumber(nlsr::tlv::NextHop);
39 
40  return totalLength;
41 }
42 
44 
45 const ndn::Block&
47 {
48  if (m_wire.hasWire()) {
49  return m_wire;
50  }
51 
52  ndn::EncodingEstimator estimator;
53  size_t estimatedSize = wireEncode(estimator);
54 
55  ndn::EncodingBuffer buffer(estimatedSize, 0);
56  wireEncode(buffer);
57 
58  m_wire = buffer.block();
59 
60  return m_wire;
61 }
62 
63 void
64 NextHop::wireDecode(const ndn::Block& wire)
65 {
66  m_connectingFaceUri = "";
67  m_routeCost = 0;
68 
69  m_wire = wire;
70 
71  if (m_wire.type() != nlsr::tlv::NextHop) {
72  NDN_THROW(Error("NextHop", m_wire.type()));
73  }
74 
75  m_wire.parse();
76 
77  auto val = m_wire.elements_begin();
78 
79  if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Uri) {
80  m_connectingFaceUri = ndn::encoding::readString(*val);
81  ++val;
82  }
83  else {
84  NDN_THROW(Error("Missing required Uri field"));
85  }
86 
87  if (val != m_wire.elements_end() && val->type() == nlsr::tlv::CostDouble) {
88  m_routeCost = ndn::encoding::readDouble(*val);
89  ++val;
90  }
91  else {
92  NDN_THROW(Error("Missing required CostDouble field"));
93  }
94 }
95 
96 bool
97 operator==(const NextHop& lhs, const NextHop& rhs)
98 {
101 }
102 
103 std::ostream&
104 operator<<(std::ostream& os, const NextHop& hop)
105 {
106  os << "NextHop(Uri: " << hop.getConnectingFaceUri() << ", Cost: " << hop.getRouteCost() << ")";
107  return os;
108 }
109 
110 } // namespace nlsr
Data abstraction for Nexthop.
Definition: nexthop.hpp:44
ndn::tlv::Error Error
Definition: nexthop.hpp:46
const ndn::Block & wireEncode() const
Definition: nexthop.cpp:46
void wireDecode(const ndn::Block &wire)
Definition: nexthop.cpp:64
const std::string & getConnectingFaceUri() const
Definition: nexthop.hpp:68
double getRouteCost() const
Definition: nexthop.hpp:94
uint64_t getRouteCostAsAdjustedInteger() const
Definition: nexthop.hpp:80
@ CostDouble
Definition: tlv-nlsr.hpp:38
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:176
bool operator==(const NamePrefixTableEntry &lhs, const NamePrefixTableEntry &rhs)
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Adjacent)