Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -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 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_META_INFO_HPP |
| 9 | #define NDN_META_INFO_HPP |
| 10 | |
| 11 | namespace ndn { |
| 12 | |
| 13 | /** |
| 14 | * An MetaInfo holds the meta info which is signed inside the data packet. |
| 15 | */ |
| 16 | class MetaInfo { |
| 17 | public: |
| 18 | enum { |
| 19 | TYPE_DEFAULT = 0, |
| 20 | TYPE_LINK = 1, |
| 21 | TYPE_KEY = 2 |
| 22 | }; |
| 23 | |
| 24 | MetaInfo() |
| 25 | : type_(TYPE_DEFAULT) |
| 26 | , freshnessPeriod_(-1) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | uint32_t |
| 31 | getType() const |
| 32 | { return type_; } |
| 33 | |
| 34 | void |
| 35 | setType(uint32_t type) |
| 36 | { type_ = type; } |
| 37 | |
| 38 | Milliseconds |
| 39 | getFreshnessPeriod() const |
| 40 | { return freshnessPeriod_; } |
| 41 | |
| 42 | void |
| 43 | setFreshnessPeriod(Milliseconds freshnessPeriod) |
| 44 | { freshnessPeriod_ = freshnessPeriod; } |
| 45 | |
| 46 | inline const Block& |
| 47 | wireEncode() const; |
| 48 | |
| 49 | inline void |
| 50 | wireDecode(const Block &wire); |
| 51 | |
| 52 | private: |
| 53 | uint32_t type_; |
| 54 | Milliseconds freshnessPeriod_; |
| 55 | |
| 56 | mutable Block wire_; |
| 57 | }; |
| 58 | |
| 59 | inline const Block& |
| 60 | MetaInfo::wireEncode() const |
| 61 | { |
| 62 | if (wire_.hasWire()) |
| 63 | return wire_; |
| 64 | |
| 65 | wire_ = Block(Tlv::MetaInfo); |
| 66 | |
| 67 | // ContentType |
| 68 | if (type_ != TYPE_DEFAULT) { |
| 69 | OBufferStream os; |
| 70 | Tlv::writeVarNumber(os, Tlv::ContentType); |
| 71 | Tlv::writeVarNumber(os, Tlv::sizeOfNonNegativeInteger(type_)); |
| 72 | Tlv::writeNonNegativeInteger(os, type_); |
| 73 | |
| 74 | wire_.push_back(Block(os.buf())); |
| 75 | } |
| 76 | |
| 77 | // FreshnessPeriod |
| 78 | if (freshnessPeriod_ >= 0) { |
| 79 | OBufferStream os; |
| 80 | Tlv::writeVarNumber(os, Tlv::FreshnessPeriod); |
| 81 | Tlv::writeVarNumber(os, Tlv::sizeOfNonNegativeInteger(freshnessPeriod_)); |
| 82 | Tlv::writeNonNegativeInteger(os, freshnessPeriod_); |
| 83 | |
| 84 | wire_.push_back(Block(os.buf())); |
| 85 | } |
| 86 | |
| 87 | wire_.encode(); |
| 88 | return wire_; |
| 89 | } |
| 90 | |
| 91 | inline void |
| 92 | MetaInfo::wireDecode(const Block &wire) |
| 93 | { |
| 94 | wire_ = wire; |
| 95 | wire_.parse(); |
| 96 | |
| 97 | // ContentType |
| 98 | Block::element_iterator val = wire_.find(Tlv::ContentType); |
| 99 | if (val != wire_.getAll().end()) |
| 100 | { |
| 101 | Buffer::const_iterator begin = val->value_begin(); |
| 102 | type_ = Tlv::readNonNegativeInteger(val->value_size(), begin, val->value_end()); |
| 103 | } |
| 104 | |
| 105 | // FreshnessPeriod |
| 106 | val = wire_.find(Tlv::FreshnessPeriod); |
| 107 | if (val != wire_.getAll().end()) |
| 108 | { |
| 109 | Buffer::const_iterator begin = val->value_begin(); |
| 110 | freshnessPeriod_ = Tlv::readNonNegativeInteger(val->value_size(), begin, val->value_end()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | } // namespace ndn |
| 115 | |
| 116 | #endif // NDN_META_INFO_HPP |