blob: aa77a4fa04de7f46337463df5085257bd6559d65 [file] [log] [blame]
Eric Newberry261dbc22015-07-22 23:18:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 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#ifndef NDN_CXX_LP_PACKET_HPP
23#define NDN_CXX_LP_PACKET_HPP
24
25#include "fields.hpp"
26
27namespace ndn {
28namespace lp {
29
30class Packet
31{
32public:
33 class Error : public ndn::tlv::Error
34 {
35 public:
36 explicit
37 Error(const std::string& what)
38 : ndn::tlv::Error(what)
39 {
40 }
41 };
42
43 Packet();
44
45 explicit
46 Packet(const Block& wire);
47
48 /**
49 * \brief append packet to encoder
50 */
51 template<encoding::Tag TAG>
52 size_t
53 wireEncode(EncodingImpl<TAG>& encoder) const;
54
55 /**
56 * \brief encode packet into wire format
57 */
Eric Newberry83872fd2015-08-06 17:01:24 -070058 const Block
Eric Newberry261dbc22015-07-22 23:18:18 -070059 wireEncode() const;
60
61 /**
62 * \brief decode packet from wire format
Eric Newberry43bf6bb2015-10-09 16:12:09 -070063 * \throws Error unknown TLV-TYPE
Eric Newberry261dbc22015-07-22 23:18:18 -070064 */
65 void
66 wireDecode(const Block& wire);
67
68public: // field access
69 /**
70 * \return true if FIELD occurs one or more times
Davide Pesavento18cf81b2015-09-12 23:36:43 +020071 * \details This is equivalent to count() > 0
Eric Newberry261dbc22015-07-22 23:18:18 -070072 */
73 template<typename FIELD>
74 bool
75 has() const
76 {
77 return count<FIELD>() > 0;
78 }
79
80 /**
81 * \return number of occurrences of FIELD
82 */
83 template<typename FIELD>
84 size_t
85 count() const
86 {
87 m_wire.parse();
88
89 return std::count_if(m_wire.elements_begin(), m_wire.elements_end(),
90 [] (const Block& block) {
91 return block.type() == FIELD::TlvType::value; });
92 }
93
94 /**
95 * \return value of index-th occurrence of FIELD
96 * \throw std::out_of_range if index>=count()
97 */
98 template<typename FIELD>
99 typename FIELD::ValueType
100 get(size_t index = 0) const
101 {
102 m_wire.parse();
103
104 size_t count = 0;
105 for (const Block& element : m_wire.elements()) {
106 if (element.type() != FIELD::TlvType::value) {
107 continue;
108 }
109 if (count++ == index) {
110 return FIELD::decode(element);
111 }
112 }
113
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700114 BOOST_THROW_EXCEPTION(std::out_of_range("Index out of range"));
Eric Newberry261dbc22015-07-22 23:18:18 -0700115 }
116
117 /**
118 * \return values of all occurrences of FIELD
119 */
120 template<typename FIELD>
121 std::vector<typename FIELD::ValueType>
122 list() const
123 {
124 std::vector<typename FIELD::ValueType> output;
125
126 m_wire.parse();
127
128 for (const Block& element : m_wire.elements()) {
129 if (element.type() != FIELD::TlvType::value) {
130 continue;
131 }
132 output.push_back(FIELD::decode(element));
133 }
134
135 return output;
136 }
137
138 /**
139 * \brief remove all occurrences of FIELD, and add a FIELD with value
Davide Pesavento18cf81b2015-09-12 23:36:43 +0200140 * \details This equivalent to clear() followed by add(value)
Eric Newberry261dbc22015-07-22 23:18:18 -0700141 */
142 template<typename FIELD>
143 Packet&
144 set(const typename FIELD::ValueType& value)
145 {
146 clear<FIELD>();
147 return add<FIELD>(value);
148 }
149
150 /**
151 * \brief add a FIELD with value
152 * \throw std::length_error if field already exists and is not repeatable
153 */
154 template<typename FIELD>
155 Packet&
156 add(const typename FIELD::ValueType& value)
157 {
158 if (!FIELD::IsRepeatable::value && has<FIELD>()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700159 BOOST_THROW_EXCEPTION(std::length_error("Field cannot be repeated"));
Eric Newberry261dbc22015-07-22 23:18:18 -0700160 }
161
162 EncodingEstimator estimator;
163 size_t estimatedSize = FIELD::encode(estimator, value);
164 EncodingBuffer buffer(estimatedSize, 0);
165 FIELD::encode(buffer, value);
166 Block block = buffer.block();
167
168 Block::element_const_iterator pos = std::lower_bound(m_wire.elements_begin(),
169 m_wire.elements_end(),
170 FIELD::TlvType::value,
171 comparePos);
172 m_wire.insert(pos, block);
173
174 return *this;
175 }
176
177 /**
178 * \brief remove the index-th occurrence of FIELD
179 * \throw std::out_of_range if index>=count()
180 */
181 template<typename FIELD>
182 Packet&
183 remove(size_t index = 0)
184 {
185 m_wire.parse();
186
187 size_t count = 0;
188 for (Block::element_const_iterator it = m_wire.elements_begin(); it != m_wire.elements_end();
189 ++it) {
190 if (it->type() == FIELD::TlvType::value) {
191 if (count == index) {
192 m_wire.erase(it);
193 return *this;
194 }
195 count++;
196 }
197 }
198
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700199 BOOST_THROW_EXCEPTION(std::out_of_range("Index out of range"));
Eric Newberry261dbc22015-07-22 23:18:18 -0700200 }
201
202 /**
203 * \brief remove all occurrences of FIELD
204 */
205 template<typename FIELD>
206 Packet&
207 clear()
208 {
209 m_wire.parse();
210 m_wire.remove(FIELD::TlvType::value);
211 return *this;
212 }
213
214private:
215 static bool
216 comparePos(const Block& first, const uint64_t second);
217
218private:
219 mutable Block m_wire;
220};
221
222} // namespace lp
223} // namespace ndn
224
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700225#endif // NDN_CXX_LP_PACKET_HPP