lsa.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2021, 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 "lsa.hpp"
23 #include "nlsr.hpp"
24 #include "name-prefix-list.hpp"
25 #include "adjacent.hpp"
26 #include "tlv-nlsr.hpp"
27 
28 namespace nlsr {
29 
30 Lsa::Lsa(const ndn::Name& originRouter, uint64_t seqNo,
31  ndn::time::system_clock::TimePoint expirationTimePoint)
32  : m_originRouter(originRouter)
33  , m_seqNo(seqNo)
34  , m_expirationTimePoint(expirationTimePoint)
35 {
36 }
37 
38 template<ndn::encoding::Tag TAG>
39 size_t
40 Lsa::wireEncode(ndn::EncodingImpl<TAG>& encoder) const
41 {
42  size_t totalLength = 0;
43 
44  totalLength += prependStringBlock(encoder,
46  ndn::time::toString(m_expirationTimePoint));
47 
48  totalLength += prependNonNegativeIntegerBlock(encoder, ndn::tlv::nlsr::SequenceNumber,
49  m_seqNo);
50 
51  totalLength += m_originRouter.wireEncode(encoder);
52 
53  totalLength += encoder.prependVarNumber(totalLength);
54  totalLength += encoder.prependVarNumber(ndn::tlv::nlsr::Lsa);
55 
56  return totalLength;
57 }
58 
60 
61 void
62 Lsa::wireDecode(const ndn::Block& wire)
63 {
64  m_originRouter.clear();
65  m_seqNo = 0;
66 
67  ndn::Block baseWire = wire;
68  baseWire.parse();
69 
70  auto val = baseWire.elements_begin();
71 
72  if (val != baseWire.elements_end() && val->type() == ndn::tlv::Name) {
73  m_originRouter.wireDecode(*val);
74  }
75  else {
76  NDN_THROW(Error("OriginRouter: Missing required Name field"));
77  }
78 
79  ++val;
80 
81  if (val != baseWire.elements_end() && val->type() == ndn::tlv::nlsr::SequenceNumber) {
82  m_seqNo = ndn::readNonNegativeInteger(*val);
83  ++val;
84  }
85  else {
86  NDN_THROW(Error("Missing required SequenceNumber field"));
87  }
88 
89  if (val != baseWire.elements_end() && val->type() == ndn::tlv::nlsr::ExpirationTime) {
90  m_expirationTimePoint = ndn::time::fromString(readString(*val));
91  }
92  else {
93  NDN_THROW(Error("Missing required ExpirationTime field"));
94  }
95 }
96 
97 std::ostream&
98 operator<<(std::ostream& os, const Lsa::Type& type)
99 {
100  switch (type) {
102  os << "ADJACENCY";
103  break;
104 
106  os << "COORDINATE";
107  break;
108 
110  os << "NAME";
111  break;
112 
113  default:
114  os << "BASE";
115  break;
116  }
117  return os;
118 }
119 
120 std::istream&
121 operator>>(std::istream& is, Lsa::Type& type)
122 {
123  std::string typeString;
124  is >> typeString;
125  if (typeString == "ADJACENCY") {
126  type = Lsa::Type::ADJACENCY;
127  }
128  else if (typeString == "COORDINATE") {
129  type = Lsa::Type::COORDINATE;
130  }
131  else if (typeString == "NAME") {
132  type = Lsa::Type::NAME;
133  }
134  else {
135  type = Lsa::Type::BASE;
136  }
137  return is;
138 }
139 
140 std::string
142 {
143  std::ostringstream os;
144  auto duration = getExpirationTimePoint() - ndn::time::system_clock::now();
145  os << " " << getType() << " LSA:\n"
146  << " Origin Router : " << getOriginRouter() << "\n"
147  << " Sequence Number : " << getSeqNo() << "\n"
148  << " Expires in : " << ndn::time::duration_cast<ndn::time::milliseconds>(duration)
149  << "\n";
150  return os.str();
151 }
152 
153 } // namespace nlsr
Data abstraction for Lsa Lsa := LSA-TYPE TLV-LENGTH Name SequenceNumber ExpirationTimePoint.
Definition: lsa.hpp:42
virtual const ndn::Block & wireEncode() const =0
const ndn::Name & getOriginRouter() const
Definition: lsa.hpp:84
virtual Type getType() const =0
Lsa()=default
virtual std::string toString() const
Definition: lsa.cpp:141
uint64_t getSeqNo() const
Definition: lsa.hpp:78
ndn::time::system_clock::TimePoint m_expirationTimePoint
Definition: lsa.hpp:139
uint64_t m_seqNo
Definition: lsa.hpp:138
void wireDecode(const ndn::Block &wire)
Definition: lsa.cpp:62
ndn::Name m_originRouter
Definition: lsa.hpp:137
const ndn::time::system_clock::TimePoint & getExpirationTimePoint() const
Definition: lsa.hpp:96
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California,...
std::istream & operator>>(std::istream &is, Lsa::Type &type)
Definition: lsa.cpp:121
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:179
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Adjacent)