| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /* |
| * Copyright (c) 2014-2018, The University of Memphis, |
| * Regents of the University of California, |
| * Arizona Board of Regents. |
| * |
| * This file is part of NLSR (Named-data Link State Routing). |
| * See AUTHORS.md for complete list of NLSR authors and contributors. |
| * |
| * NLSR is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, |
| * either version 3 of the License, or (at your option) any later version. |
| * |
| * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| * PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License along with |
| * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| |
| #include "lsa-info.hpp" |
| #include "tlv-nlsr.hpp" |
| |
| #include <ndn-cxx/util/concepts.hpp> |
| #include <ndn-cxx/encoding/block-helpers.hpp> |
| |
| namespace nlsr { |
| namespace tlv { |
| |
| BOOST_CONCEPT_ASSERT((ndn::WireEncodable<LsaInfo>)); |
| BOOST_CONCEPT_ASSERT((ndn::WireDecodable<LsaInfo>)); |
| static_assert(std::is_base_of<ndn::tlv::Error, LsaInfo::Error>::value, |
| "LsaInfo::Error must inherit from tlv::Error"); |
| |
| const ndn::time::milliseconds LsaInfo::INFINITE_EXPIRATION_PERIOD(ndn::time::milliseconds::max()); |
| |
| LsaInfo::LsaInfo() |
| : m_sequenceNumber(0) |
| , m_expirationPeriod(INFINITE_EXPIRATION_PERIOD) |
| , m_hasInfiniteExpirationPeriod(true) |
| { |
| } |
| |
| LsaInfo::LsaInfo(const ndn::Block& block) |
| { |
| wireDecode(block); |
| } |
| |
| template<ndn::encoding::Tag TAG> |
| size_t |
| LsaInfo::wireEncode(ndn::EncodingImpl<TAG>& encoder) const |
| { |
| size_t totalLength = 0; |
| |
| // Absence of an ExpirationPeriod signifies non-expiration |
| if (!m_hasInfiniteExpirationPeriod) { |
| totalLength += prependNonNegativeIntegerBlock(encoder, |
| ndn::tlv::nlsr::ExpirationPeriod, |
| m_expirationPeriod.count()); |
| } |
| |
| totalLength += prependNonNegativeIntegerBlock(encoder, |
| ndn::tlv::nlsr::SequenceNumber, |
| m_sequenceNumber); |
| |
| totalLength += prependNestedBlock(encoder, ndn::tlv::nlsr::OriginRouter, m_originRouter); |
| |
| totalLength += encoder.prependVarNumber(totalLength); |
| totalLength += encoder.prependVarNumber(ndn::tlv::nlsr::LsaInfo); |
| |
| return totalLength; |
| } |
| |
| NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(LsaInfo); |
| |
| const ndn::Block& |
| LsaInfo::wireEncode() const |
| { |
| if (m_wire.hasWire()) { |
| return m_wire; |
| } |
| |
| ndn::EncodingEstimator estimator; |
| size_t estimatedSize = wireEncode(estimator); |
| |
| ndn::EncodingBuffer buffer(estimatedSize, 0); |
| wireEncode(buffer); |
| |
| m_wire = buffer.block(); |
| |
| return m_wire; |
| } |
| |
| void |
| LsaInfo::wireDecode(const ndn::Block& wire) |
| { |
| m_originRouter.clear(); |
| m_sequenceNumber = 0; |
| m_expirationPeriod = ndn::time::milliseconds::min(); |
| |
| m_wire = wire; |
| |
| if (m_wire.type() != ndn::tlv::nlsr::LsaInfo) { |
| std::stringstream error; |
| error << "Expected LsaInfo Block, but Block is of a different type: #" |
| << m_wire.type(); |
| BOOST_THROW_EXCEPTION(Error(error.str())); |
| } |
| |
| m_wire.parse(); |
| |
| ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| |
| if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::OriginRouter) { |
| val->parse(); |
| ndn::Block::element_const_iterator it = val->elements_begin(); |
| |
| if (it != val->elements_end() && it->type() == ndn::tlv::Name) { |
| m_originRouter.wireDecode(*it); |
| } |
| else { |
| BOOST_THROW_EXCEPTION(Error("OriginRouter: Missing required Name field")); |
| } |
| |
| ++val; |
| } |
| else { |
| BOOST_THROW_EXCEPTION(Error("Missing required OriginRouter field")); |
| } |
| |
| if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::SequenceNumber) { |
| m_sequenceNumber = ndn::readNonNegativeInteger(*val); |
| ++val; |
| } |
| else { |
| BOOST_THROW_EXCEPTION(Error("Missing required SequenceNumber field")); |
| } |
| |
| if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::ExpirationPeriod) { |
| m_expirationPeriod = ndn::time::milliseconds(ndn::readNonNegativeInteger(*val)); |
| m_hasInfiniteExpirationPeriod = false; |
| } |
| else { |
| m_expirationPeriod = INFINITE_EXPIRATION_PERIOD; |
| m_hasInfiniteExpirationPeriod = true; |
| } |
| } |
| |
| std::ostream& |
| operator<<(std::ostream& os, const LsaInfo& lsaInfo) |
| { |
| os << "LsaInfo(" |
| << "OriginRouter: " << lsaInfo.getOriginRouter() << ", " |
| << "SequenceNumber: " << lsaInfo.getSequenceNumber() << ", "; |
| |
| if (!lsaInfo.hasInfiniteExpirationPeriod()) { |
| os << "ExpirationPeriod: " << lsaInfo.getExpirationPeriod(); |
| } |
| else { |
| os << "ExpirationPeriod: Infinity"; |
| } |
| |
| os << ")"; |
| |
| return os; |
| } |
| |
| std::shared_ptr<LsaInfo> |
| makeLsaInfo(const Lsa& lsa) |
| { |
| std::shared_ptr<LsaInfo> lsaInfo = std::make_shared<LsaInfo>(); |
| |
| lsaInfo->setOriginRouter(lsa.getOrigRouter()); |
| lsaInfo->setSequenceNumber(lsa.getLsSeqNo()); |
| |
| ndn::time::system_clock::duration duration |
| = lsa.getExpirationTimePoint() - ndn::time::system_clock::now(); |
| |
| lsaInfo->setExpirationPeriod(ndn::time::duration_cast<ndn::time::milliseconds>(duration)); |
| |
| return lsaInfo; |
| } |
| |
| } // namespace tlv |
| } // namespace nlsr |