Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * |
| 5 | * BSD license, See the LICENSE file for more information |
| 6 | * |
| 7 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 8 | */ |
| 9 | |
| 10 | #ifndef NDN_BLOCK_HPP |
| 11 | #define NDN_BLOCK_HPP |
| 12 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 13 | #include "../common.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 14 | |
| 15 | #include <list> |
| 16 | #include <exception> |
| 17 | |
| 18 | #include "buffer.hpp" |
| 19 | #include "tlv.hpp" |
| 20 | |
| 21 | #include <boost/lexical_cast.hpp> |
| 22 | |
| 23 | namespace ndn { |
| 24 | |
| 25 | /** |
| 26 | * @brief Class representing wire element of the NDN packet |
| 27 | */ |
| 28 | class Block |
| 29 | { |
| 30 | public: |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 31 | typedef std::vector<Block> element_container; |
| 32 | typedef element_container::iterator element_iterator; |
| 33 | typedef element_container::const_iterator element_const_iterator; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 34 | |
| 35 | /// @brief Error that can be thrown from the block |
| 36 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 37 | |
| 38 | /** |
| 39 | * @brief Default constructor to create an empty Block |
| 40 | */ |
| 41 | Block(); |
| 42 | |
| 43 | /** |
| 44 | * @brief A helper version of a constructor to create Block from the raw buffer (type and value-length parsing) |
| 45 | */ |
| 46 | Block(const ConstBufferPtr &buffer); |
| 47 | |
| 48 | /** |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 49 | * @brief Another helper to create block from a buffer, directly specifying boundaries |
| 50 | * of the block within the buffer |
| 51 | * |
| 52 | * This version will automatically detect type and position of the value within the block |
| 53 | */ |
| 54 | Block(const ConstBufferPtr &buffer, |
| 55 | const Buffer::const_iterator &begin, const Buffer::const_iterator &end); |
| 56 | |
| 57 | /** |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 58 | * @brief A helper version of a constructor to create Block from the raw buffer (type and value-length parsing) |
| 59 | */ |
| 60 | Block(const uint8_t *buffer, size_t maxlength); |
| 61 | |
| 62 | Block(const void *buffer, size_t maxlength); |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * @brief A helper version of a constructor to create Block from the stream. |
| 66 | */ |
| 67 | Block(std::istream& is); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * @brief Create Block from the wire buffer (no parsing) |
| 71 | * |
| 72 | * This version of the constructor does not do any parsing |
| 73 | */ |
| 74 | Block(const ConstBufferPtr &wire, |
| 75 | uint32_t type, |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 76 | const Buffer::const_iterator &begin, const Buffer::const_iterator &end, |
| 77 | const Buffer::const_iterator &valueBegin, const Buffer::const_iterator &valueEnd); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @brief Create Block of a specific type with empty wire buffer |
| 81 | */ |
Alexander Afanasyev | f42ce13 | 2014-01-07 13:32:30 -0800 | [diff] [blame] | 82 | explicit |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 83 | Block(uint32_t type); |
| 84 | |
| 85 | /** |
| 86 | * @brief Create Block of a specific type with the specified value |
| 87 | * |
| 88 | * The underlying buffer hold only value, additional operations are needed |
| 89 | * to construct wire encoding, one need to prepend the wire buffer with type |
| 90 | * and value-length VAR-NUMBERs |
| 91 | */ |
| 92 | Block(uint32_t type, const ConstBufferPtr &value); |
| 93 | |
| 94 | /** |
| 95 | * @brief Create nested Block of a specific type with the specified value |
| 96 | * |
| 97 | * The underlying buffer hold only value, additional operations are needed |
| 98 | * to construct wire encoding, one need to prepend the wire buffer with type |
| 99 | * and value-length VAR-NUMBERs |
| 100 | */ |
Alexander Afanasyev | f42ce13 | 2014-01-07 13:32:30 -0800 | [diff] [blame] | 101 | explicit |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 102 | Block(uint32_t type, const Block &value); |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * @brief Check if the Block is empty |
| 106 | */ |
| 107 | inline bool |
| 108 | empty() const; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * @brief Check if the Block has fully encoded wire |
| 112 | */ |
| 113 | inline bool |
| 114 | hasWire() const; |
| 115 | |
| 116 | /** |
| 117 | * @brief Check if the Block has value block (no type and length are encoded) |
| 118 | */ |
| 119 | inline bool |
| 120 | hasValue() const; |
| 121 | |
| 122 | /** |
| 123 | * @brief Reset wire buffer of the element |
| 124 | */ |
| 125 | inline void |
| 126 | reset(); |
| 127 | |
| 128 | void |
| 129 | parse(); |
| 130 | |
| 131 | void |
| 132 | encode(); |
| 133 | |
| 134 | inline uint32_t |
| 135 | type() const; |
| 136 | |
| 137 | /** |
| 138 | * @brief Get the first subelement of the requested type |
| 139 | */ |
| 140 | inline const Block & |
| 141 | get(uint32_t type) const; |
| 142 | |
| 143 | inline Block & |
| 144 | get(uint32_t type); |
| 145 | |
| 146 | inline element_iterator |
| 147 | find(uint32_t type); |
| 148 | |
| 149 | inline element_const_iterator |
| 150 | find(uint32_t type) const; |
| 151 | |
| 152 | inline void |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 153 | remove(uint32_t type); |
| 154 | |
| 155 | inline element_iterator |
| 156 | erase(element_iterator position); |
| 157 | |
| 158 | inline element_iterator |
| 159 | erase(element_iterator first, element_iterator last); |
| 160 | |
| 161 | inline void |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 162 | push_back(const Block &element); |
| 163 | |
| 164 | /** |
| 165 | * @brief Get all subelements |
| 166 | */ |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 167 | inline const element_container& |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 168 | getAll () const; |
| 169 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 170 | inline element_container& |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 171 | getAll (); |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 172 | |
| 173 | inline const element_container& |
| 174 | elements () const; |
| 175 | |
| 176 | inline element_container& |
| 177 | elements (); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * @brief Get all elements of the requested type |
| 181 | */ |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 182 | element_container |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 183 | getAll(uint32_t type) const; |
| 184 | |
| 185 | inline Buffer::const_iterator |
| 186 | begin() const; |
| 187 | |
| 188 | inline Buffer::const_iterator |
| 189 | end() const; |
| 190 | |
| 191 | inline size_t |
| 192 | size() const; |
| 193 | |
| 194 | inline Buffer::const_iterator |
| 195 | value_begin() const; |
| 196 | |
| 197 | inline Buffer::const_iterator |
| 198 | value_end() const; |
| 199 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 200 | inline element_iterator |
| 201 | element_begin(); |
| 202 | |
| 203 | inline element_iterator |
| 204 | element_end(); |
| 205 | |
| 206 | inline element_const_iterator |
| 207 | element_begin() const; |
| 208 | |
| 209 | inline element_const_iterator |
| 210 | element_end() const; |
| 211 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 212 | inline const uint8_t* |
| 213 | wire() const; |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 214 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 215 | // inline const uint8_t* |
| 216 | // buf() const; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 217 | |
| 218 | inline const uint8_t* |
| 219 | value() const; |
| 220 | |
| 221 | inline size_t |
| 222 | value_size() const; |
| 223 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 224 | Block |
| 225 | blockFromValue() const; |
| 226 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 227 | protected: |
| 228 | ConstBufferPtr m_buffer; |
| 229 | |
| 230 | uint32_t m_type; |
| 231 | |
| 232 | Buffer::const_iterator m_begin; |
| 233 | Buffer::const_iterator m_end; |
| 234 | uint32_t m_size; |
| 235 | |
| 236 | Buffer::const_iterator m_value_begin; |
| 237 | Buffer::const_iterator m_value_end; |
| 238 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 239 | element_container m_subBlocks; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | //////////////////////////////////////////////////////////////////////////////// |
| 243 | //////////////////////////////////////////////////////////////////////////////// |
| 244 | //////////////////////////////////////////////////////////////////////////////// |
| 245 | |
| 246 | inline bool |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 247 | Block::empty() const |
| 248 | { |
| 249 | return m_type == std::numeric_limits<uint32_t>::max(); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | inline bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 254 | Block::hasWire() const |
| 255 | { |
| 256 | return m_buffer && (m_begin != m_end); |
| 257 | } |
| 258 | |
| 259 | inline bool |
| 260 | Block::hasValue() const |
| 261 | { |
| 262 | return static_cast<bool>(m_buffer); |
| 263 | } |
| 264 | |
| 265 | inline void |
| 266 | Block::reset() |
| 267 | { |
| 268 | m_buffer.reset(); // reset of the shared_ptr |
| 269 | m_subBlocks.clear(); // remove all parsed subelements |
| 270 | |
| 271 | m_type = std::numeric_limits<uint32_t>::max(); |
| 272 | m_begin = m_end = m_value_begin = m_value_end = Buffer::const_iterator(); // not really necessary, but for safety |
| 273 | } |
| 274 | |
| 275 | inline uint32_t |
| 276 | Block::type() const |
| 277 | { |
| 278 | return m_type; |
| 279 | } |
| 280 | |
| 281 | inline const Block & |
| 282 | Block::get(uint32_t type) const |
| 283 | { |
| 284 | for (element_const_iterator i = m_subBlocks.begin (); |
| 285 | i != m_subBlocks.end(); |
| 286 | i++) |
| 287 | { |
| 288 | if (i->type () == type) |
| 289 | { |
| 290 | return *i; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | throw Error("(Block::get) Requested a non-existed type [" + boost::lexical_cast<std::string>(type) + "] from Block"); |
| 295 | } |
| 296 | |
| 297 | inline Block & |
| 298 | Block::get(uint32_t type) |
| 299 | { |
| 300 | for (element_iterator i = m_subBlocks.begin (); |
| 301 | i != m_subBlocks.end(); |
| 302 | i++) |
| 303 | { |
| 304 | if (i->type () == type) |
| 305 | { |
| 306 | return *i; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | throw Error("(Block::get) Requested a non-existed type [" + boost::lexical_cast<std::string>(type) + "] from Block"); |
| 311 | } |
| 312 | |
| 313 | inline Block::element_const_iterator |
| 314 | Block::find(uint32_t type) const |
| 315 | { |
| 316 | for (element_const_iterator i = m_subBlocks.begin (); |
| 317 | i != m_subBlocks.end(); |
| 318 | i++) |
| 319 | { |
| 320 | if (i->type () == type) |
| 321 | { |
| 322 | return i; |
| 323 | } |
| 324 | } |
| 325 | return m_subBlocks.end(); |
| 326 | } |
| 327 | |
| 328 | inline Block::element_iterator |
| 329 | Block::find(uint32_t type) |
| 330 | { |
| 331 | for (element_iterator i = m_subBlocks.begin (); |
| 332 | i != m_subBlocks.end(); |
| 333 | i++) |
| 334 | { |
| 335 | if (i->type () == type) |
| 336 | { |
| 337 | return i; |
| 338 | } |
| 339 | } |
| 340 | return m_subBlocks.end(); |
| 341 | } |
| 342 | |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 343 | inline void |
| 344 | Block::remove(uint32_t type) |
| 345 | { |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 346 | element_container newContainer; |
| 347 | newContainer.reserve(m_subBlocks.size()); |
| 348 | for (element_iterator i = m_subBlocks.begin(); |
| 349 | i != m_subBlocks.end(); |
| 350 | ++i) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 351 | { |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 352 | if (i->type() != type) |
| 353 | newContainer.push_back(*i); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 354 | } |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 355 | m_subBlocks.swap(newContainer); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 356 | } |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 357 | |
| 358 | inline Block::element_iterator |
| 359 | Block::erase(Block::element_iterator position) |
| 360 | { |
| 361 | return m_subBlocks.erase(position); |
| 362 | } |
| 363 | |
| 364 | inline Block::element_iterator |
| 365 | Block::erase(Block::element_iterator first, Block::element_iterator last) |
| 366 | { |
| 367 | return m_subBlocks.erase(first, last); |
| 368 | } |
| 369 | |
| 370 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 371 | inline void |
| 372 | Block::push_back(const Block &element) |
| 373 | { |
| 374 | m_subBlocks.push_back(element); |
| 375 | } |
| 376 | |
| 377 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 378 | inline const Block::element_container& |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 379 | Block::getAll () const |
| 380 | { |
| 381 | return m_subBlocks; |
| 382 | } |
| 383 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 384 | inline Block::element_container& |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 385 | Block::getAll () |
| 386 | { |
| 387 | return m_subBlocks; |
| 388 | } |
| 389 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 390 | inline const Block::element_container& |
| 391 | Block::elements () const |
| 392 | { |
| 393 | return m_subBlocks; |
| 394 | } |
| 395 | |
| 396 | inline Block::element_container& |
| 397 | Block::elements () |
| 398 | { |
| 399 | return m_subBlocks; |
| 400 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 401 | |
| 402 | inline Buffer::const_iterator |
| 403 | Block::begin() const |
| 404 | { |
| 405 | if (!hasWire()) |
| 406 | throw Error("Underlying wire buffer is empty"); |
| 407 | |
| 408 | return m_begin; |
| 409 | } |
| 410 | |
| 411 | inline Buffer::const_iterator |
| 412 | Block::end() const |
| 413 | { |
| 414 | if (!hasWire()) |
| 415 | throw Error("Underlying wire buffer is empty"); |
| 416 | |
| 417 | return m_end; |
| 418 | } |
| 419 | |
| 420 | inline size_t |
| 421 | Block::size() const |
| 422 | { |
| 423 | if (hasWire() || hasValue()) { |
| 424 | return m_size; |
| 425 | } |
| 426 | else |
| 427 | throw Error("Block size cannot be determined (undefined block size)"); |
| 428 | } |
| 429 | |
| 430 | inline Buffer::const_iterator |
| 431 | Block::value_begin() const |
| 432 | { |
| 433 | if (!hasValue()) |
| 434 | throw Error("(Block::value_begin) Underlying value buffer is empty"); |
| 435 | |
| 436 | return m_value_begin; |
| 437 | } |
| 438 | |
| 439 | inline Buffer::const_iterator |
| 440 | Block::value_end() const |
| 441 | { |
| 442 | if (!hasValue()) |
| 443 | throw Error("(Block::value_end) Underlying value buffer is empty"); |
| 444 | |
| 445 | return m_value_end; |
| 446 | } |
| 447 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 448 | inline Block::element_iterator |
| 449 | Block::element_begin() |
| 450 | { |
| 451 | return m_subBlocks.begin(); |
| 452 | } |
| 453 | |
| 454 | inline Block::element_iterator |
| 455 | Block::element_end() |
| 456 | { |
| 457 | return m_subBlocks.end(); |
| 458 | } |
| 459 | |
| 460 | inline Block::element_const_iterator |
| 461 | Block::element_begin() const |
| 462 | { |
| 463 | return m_subBlocks.begin(); |
| 464 | } |
| 465 | |
| 466 | inline Block::element_const_iterator |
| 467 | Block::element_end() const |
| 468 | { |
| 469 | return m_subBlocks.end(); |
| 470 | } |
| 471 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 472 | inline const uint8_t* |
| 473 | Block::wire() const |
| 474 | { |
| 475 | if (!hasWire()) |
| 476 | throw Error("(Block::wire) Underlying wire buffer is empty"); |
| 477 | |
| 478 | return &*m_begin; |
| 479 | } |
| 480 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 481 | // inline const uint8_t* |
| 482 | // Block::buf() const |
| 483 | // { |
| 484 | // if (!hasWire()) |
| 485 | // throw Error("(Block::wire) Underlying wire buffer is empty"); |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 486 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 487 | // return &*m_begin; |
| 488 | // } |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 489 | |
| 490 | inline const uint8_t* |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 491 | Block::value() const |
| 492 | { |
| 493 | if (!hasValue()) |
| 494 | throw Error("(Block::value) Underlying value buffer is empty"); |
| 495 | |
| 496 | return &*m_value_begin; |
| 497 | } |
| 498 | |
| 499 | inline size_t |
| 500 | Block::value_size() const |
| 501 | { |
| 502 | if (!hasValue()) |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 503 | return 0; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 504 | |
| 505 | return m_value_end - m_value_begin; |
| 506 | } |
| 507 | |
| 508 | } // ndn |
| 509 | |
| 510 | #include "block-helpers.hpp" |
| 511 | |
| 512 | #endif // NDN_BLOCK_HPP |