Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, 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-info.hpp" |
| 23 | #include "tlv-nlsr.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/util/concepts.hpp> |
| 26 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 27 | |
| 28 | namespace nlsr { |
| 29 | namespace tlv { |
| 30 | |
| 31 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<LsaInfo>)); |
| 32 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<LsaInfo>)); |
| 33 | static_assert(std::is_base_of<ndn::tlv::Error, LsaInfo::Error>::value, |
| 34 | "LsaInfo::Error must inherit from tlv::Error"); |
| 35 | |
| 36 | const ndn::time::milliseconds LsaInfo::INFINITE_EXPIRATION_PERIOD(ndn::time::milliseconds::max()); |
| 37 | |
| 38 | LsaInfo::LsaInfo() |
| 39 | : m_sequenceNumber(0) |
| 40 | , m_expirationPeriod(INFINITE_EXPIRATION_PERIOD) |
| 41 | , m_hasInfiniteExpirationPeriod(true) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | LsaInfo::LsaInfo(const ndn::Block& block) |
| 46 | { |
| 47 | wireDecode(block); |
| 48 | } |
| 49 | |
| 50 | template<bool T> |
| 51 | size_t |
| 52 | LsaInfo::wireEncode(ndn::EncodingImpl<T>& block) const |
| 53 | { |
| 54 | size_t totalLength = 0; |
| 55 | |
| 56 | // Absence of an ExpirationPeriod signifies non-expiration |
| 57 | if (!m_hasInfiniteExpirationPeriod) { |
| 58 | totalLength += ndn::prependNonNegativeIntegerBlock(block, |
| 59 | ndn::tlv::nlsr::ExpirationPeriod, |
| 60 | m_expirationPeriod.count()); |
| 61 | } |
| 62 | |
| 63 | totalLength += ndn::prependNonNegativeIntegerBlock(block, |
| 64 | ndn::tlv::nlsr::SequenceNumber, |
| 65 | m_sequenceNumber); |
| 66 | |
| 67 | totalLength += ndn::prependNestedBlock(block, ndn::tlv::nlsr::OriginRouter, m_originRouter); |
| 68 | |
| 69 | totalLength += block.prependVarNumber(totalLength); |
| 70 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::LsaInfo); |
| 71 | |
| 72 | return totalLength; |
| 73 | } |
| 74 | |
| 75 | template size_t |
| 76 | LsaInfo::wireEncode<true>(ndn::EncodingImpl<true>& block) const; |
| 77 | |
| 78 | template size_t |
| 79 | LsaInfo::wireEncode<false>(ndn::EncodingImpl<false>& block) const; |
| 80 | |
| 81 | const ndn::Block& |
| 82 | LsaInfo::wireEncode() const |
| 83 | { |
| 84 | if (m_wire.hasWire()) { |
| 85 | return m_wire; |
| 86 | } |
| 87 | |
| 88 | ndn::EncodingEstimator estimator; |
| 89 | size_t estimatedSize = wireEncode(estimator); |
| 90 | |
| 91 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 92 | wireEncode(buffer); |
| 93 | |
| 94 | m_wire = buffer.block(); |
| 95 | |
| 96 | return m_wire; |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | LsaInfo::wireDecode(const ndn::Block& wire) |
| 101 | { |
| 102 | m_originRouter.clear(); |
| 103 | m_sequenceNumber = 0; |
| 104 | m_expirationPeriod = ndn::time::milliseconds::min(); |
| 105 | |
| 106 | m_wire = wire; |
| 107 | |
| 108 | if (m_wire.type() != ndn::tlv::nlsr::LsaInfo) { |
| 109 | std::stringstream error; |
| 110 | error << "Expected LsaInfo Block, but Block is of a different type: #" |
| 111 | << m_wire.type(); |
| 112 | throw Error(error.str()); |
| 113 | } |
| 114 | |
| 115 | m_wire.parse(); |
| 116 | |
| 117 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 118 | |
| 119 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::OriginRouter) { |
| 120 | val->parse(); |
| 121 | ndn::Block::element_const_iterator it = val->elements_begin(); |
| 122 | |
| 123 | if (it != val->elements_end() && it->type() == ndn::tlv::Name) { |
| 124 | m_originRouter.wireDecode(*it); |
| 125 | } |
| 126 | else { |
| 127 | throw Error("OriginRouter: Missing required Name field"); |
| 128 | } |
| 129 | |
| 130 | ++val; |
| 131 | } |
| 132 | else { |
| 133 | throw Error("Missing required OriginRouter field"); |
| 134 | } |
| 135 | |
| 136 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::SequenceNumber) { |
| 137 | m_sequenceNumber = ndn::readNonNegativeInteger(*val); |
| 138 | ++val; |
| 139 | } |
| 140 | else { |
| 141 | throw Error("Missing required SequenceNumber field"); |
| 142 | } |
| 143 | |
| 144 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::ExpirationPeriod) { |
| 145 | m_expirationPeriod = ndn::time::milliseconds(ndn::readNonNegativeInteger(*val)); |
| 146 | m_hasInfiniteExpirationPeriod = false; |
| 147 | } |
| 148 | else { |
| 149 | m_expirationPeriod = INFINITE_EXPIRATION_PERIOD; |
| 150 | m_hasInfiniteExpirationPeriod = true; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | std::ostream& |
| 155 | operator<<(std::ostream& os, const LsaInfo& lsaInfo) |
| 156 | { |
| 157 | os << "LsaInfo(" |
| 158 | << "OriginRouter: " << lsaInfo.getOriginRouter() << ", " |
| 159 | << "SequenceNumber: " << lsaInfo.getSequenceNumber() << ", "; |
| 160 | |
| 161 | if (!lsaInfo.hasInfiniteExpirationPeriod()) { |
| 162 | os << "ExpirationPeriod: " << lsaInfo.getExpirationPeriod(); |
| 163 | } |
| 164 | else { |
| 165 | os << "ExpirationPeriod: Infinity"; |
| 166 | } |
| 167 | |
| 168 | os << ")"; |
| 169 | |
| 170 | return os; |
| 171 | } |
| 172 | |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame^] | 173 | std::shared_ptr<LsaInfo> |
| 174 | makeLsaInfo(const Lsa& lsa) |
| 175 | { |
| 176 | std::shared_ptr<LsaInfo> lsaInfo = std::make_shared<LsaInfo>(); |
| 177 | |
| 178 | lsaInfo->setOriginRouter(lsa.getOrigRouter()); |
| 179 | lsaInfo->setSequenceNumber(lsa.getLsSeqNo()); |
| 180 | |
| 181 | ndn::time::system_clock::duration duration |
| 182 | = lsa.getExpirationTimePoint() - ndn::time::system_clock::now(); |
| 183 | |
| 184 | lsaInfo->setExpirationPeriod(ndn::time::duration_cast<ndn::time::milliseconds>(duration)); |
| 185 | |
| 186 | return lsaInfo; |
| 187 | } |
| 188 | |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 189 | } // namespace tlv |
| 190 | } // namespace nlsr |