Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "nfd-status-dataset.hpp" |
| 23 | #include "../util/concepts.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace nfd { |
| 27 | |
| 28 | StatusDataset::StatusDataset(const PartialName& datasetName) |
| 29 | : m_datasetName(datasetName) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | Name |
| 34 | StatusDataset::getDatasetPrefix(const Name& prefix) const |
| 35 | { |
| 36 | Name name; |
| 37 | name.append(prefix).append(m_datasetName); |
| 38 | this->addParameters(name); |
| 39 | return name; |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | StatusDataset::addParameters(Name& name) const |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * \brief parses elements into a vector of T |
| 49 | * \tparam T element type |
| 50 | * \param payload pointer to a buffer of zero or more blocks of decodable by T |
| 51 | * \return a vector of T |
| 52 | * \throw tlv::Error cannot parse payload |
| 53 | */ |
| 54 | template<typename T> |
| 55 | static std::vector<T> |
| 56 | parseDatasetVector(ConstBufferPtr payload) |
| 57 | { |
| 58 | BOOST_CONCEPT_ASSERT((WireDecodable<T>)); |
| 59 | |
| 60 | std::vector<T> result; |
| 61 | |
| 62 | size_t offset = 0; |
| 63 | while (offset < payload->size()) { |
| 64 | bool isOk = false; |
| 65 | Block block; |
| 66 | std::tie(isOk, block) = Block::fromBuffer(payload, offset); |
| 67 | if (!isOk) { |
| 68 | BOOST_THROW_EXCEPTION(StatusDataset::ParseResultError("cannot decode Block")); |
| 69 | } |
| 70 | |
| 71 | offset += block.size(); |
| 72 | result.emplace_back(block); |
| 73 | } |
| 74 | |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | ForwarderGeneralStatusDataset::ForwarderGeneralStatusDataset() |
| 79 | : StatusDataset("status/general") |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | ForwarderGeneralStatusDataset::ResultType |
| 84 | ForwarderGeneralStatusDataset::parseResult(ConstBufferPtr payload) const |
| 85 | { |
| 86 | return ForwarderStatus(Block(tlv::Content, payload)); |
| 87 | } |
| 88 | |
| 89 | FaceDatasetBase::FaceDatasetBase(const PartialName& datasetName) |
| 90 | : StatusDataset(datasetName) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | FaceDatasetBase::ResultType |
| 95 | FaceDatasetBase::parseResult(ConstBufferPtr payload) const |
| 96 | { |
| 97 | return parseDatasetVector<FaceStatus>(payload); |
| 98 | } |
| 99 | |
| 100 | FaceDataset::FaceDataset() |
| 101 | : FaceDatasetBase("faces/list") |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | FaceQueryDataset::FaceQueryDataset(const FaceQueryFilter& filter) |
| 106 | : FaceDatasetBase("faces/query") |
| 107 | , m_filter(filter) |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | FaceQueryDataset::addParameters(Name& name) const |
| 113 | { |
| 114 | name.append(m_filter.wireEncode()); |
| 115 | } |
| 116 | |
| 117 | FibDataset::FibDataset() |
| 118 | : StatusDataset("fib/list") |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | FibDataset::ResultType |
| 123 | FibDataset::parseResult(ConstBufferPtr payload) const |
| 124 | { |
| 125 | return parseDatasetVector<FibEntry>(payload); |
| 126 | } |
| 127 | |
| 128 | StrategyChoiceDataset::StrategyChoiceDataset() |
| 129 | : StatusDataset("strategy-choice/list") |
| 130 | { |
| 131 | } |
| 132 | |
| 133 | StrategyChoiceDataset::ResultType |
| 134 | StrategyChoiceDataset::parseResult(ConstBufferPtr payload) const |
| 135 | { |
| 136 | return parseDatasetVector<StrategyChoice>(payload); |
| 137 | } |
| 138 | |
| 139 | RibDataset::RibDataset() |
| 140 | : StatusDataset("rib/list") |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | RibDataset::ResultType |
| 145 | RibDataset::parseResult(ConstBufferPtr payload) const |
| 146 | { |
| 147 | return parseDatasetVector<RibEntry>(payload); |
| 148 | } |
| 149 | |
| 150 | } // namespace nfd |
| 151 | } // namespace ndn |