blob: 2409254c0dcb2a50b9984145532ca06f6bfcfc9e [file] [log] [blame]
Junxiao Shi034c1882016-06-24 18:06:51 +00001/* -*- 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
25namespace ndn {
26namespace nfd {
27
28StatusDataset::StatusDataset(const PartialName& datasetName)
29 : m_datasetName(datasetName)
30{
31}
32
33Name
34StatusDataset::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
42void
43StatusDataset::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 */
54template<typename T>
55static std::vector<T>
56parseDatasetVector(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
78ForwarderGeneralStatusDataset::ForwarderGeneralStatusDataset()
79 : StatusDataset("status/general")
80{
81}
82
83ForwarderGeneralStatusDataset::ResultType
84ForwarderGeneralStatusDataset::parseResult(ConstBufferPtr payload) const
85{
86 return ForwarderStatus(Block(tlv::Content, payload));
87}
88
89FaceDatasetBase::FaceDatasetBase(const PartialName& datasetName)
90 : StatusDataset(datasetName)
91{
92}
93
94FaceDatasetBase::ResultType
95FaceDatasetBase::parseResult(ConstBufferPtr payload) const
96{
97 return parseDatasetVector<FaceStatus>(payload);
98}
99
100FaceDataset::FaceDataset()
101 : FaceDatasetBase("faces/list")
102{
103}
104
105FaceQueryDataset::FaceQueryDataset(const FaceQueryFilter& filter)
106 : FaceDatasetBase("faces/query")
107 , m_filter(filter)
108{
109}
110
111void
112FaceQueryDataset::addParameters(Name& name) const
113{
114 name.append(m_filter.wireEncode());
115}
116
117FibDataset::FibDataset()
118 : StatusDataset("fib/list")
119{
120}
121
122FibDataset::ResultType
123FibDataset::parseResult(ConstBufferPtr payload) const
124{
125 return parseDatasetVector<FibEntry>(payload);
126}
127
128StrategyChoiceDataset::StrategyChoiceDataset()
129 : StatusDataset("strategy-choice/list")
130{
131}
132
133StrategyChoiceDataset::ResultType
134StrategyChoiceDataset::parseResult(ConstBufferPtr payload) const
135{
136 return parseDatasetVector<StrategyChoice>(payload);
137}
138
139RibDataset::RibDataset()
140 : StatusDataset("rib/list")
141{
142}
143
144RibDataset::ResultType
145RibDataset::parseResult(ConstBufferPtr payload) const
146{
147 return parseDatasetVector<RibEntry>(payload);
148}
149
150} // namespace nfd
151} // namespace ndn