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