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 | |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 65 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 66 | // ContentType? |
| 67 | // FreshnessPeriod? |
| 68 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 69 | wire_ = Block(Tlv::MetaInfo); |
| 70 | |
| 71 | // ContentType |
| 72 | if (type_ != TYPE_DEFAULT) { |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 73 | wire_.push_back |
| 74 | (nonNegativeIntegerBlock(Tlv::ContentType, type_)); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // FreshnessPeriod |
| 78 | if (freshnessPeriod_ >= 0) { |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 79 | wire_.push_back |
| 80 | (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_)); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | wire_.encode(); |
| 84 | return wire_; |
| 85 | } |
| 86 | |
| 87 | inline void |
| 88 | MetaInfo::wireDecode(const Block &wire) |
| 89 | { |
| 90 | wire_ = wire; |
| 91 | wire_.parse(); |
| 92 | |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 93 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 94 | // ContentType? |
| 95 | // FreshnessPeriod? |
| 96 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 97 | // ContentType |
| 98 | Block::element_iterator val = wire_.find(Tlv::ContentType); |
| 99 | if (val != wire_.getAll().end()) |
| 100 | { |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 101 | type_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // FreshnessPeriod |
| 105 | val = wire_.find(Tlv::FreshnessPeriod); |
| 106 | if (val != wire_.getAll().end()) |
| 107 | { |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 108 | freshnessPeriod_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 112 | inline std::ostream& |
| 113 | operator << (std::ostream &os, const MetaInfo &info) |
| 114 | { |
| 115 | // ContentType |
| 116 | os << "ContentType: " << info.getType(); |
| 117 | |
| 118 | // FreshnessPeriod |
| 119 | if (info.getFreshnessPeriod() >= 0) { |
| 120 | os << ", FreshnessPeriod: " << info.getFreshnessPeriod(); |
| 121 | } |
| 122 | return os; |
| 123 | } |
| 124 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 125 | } // namespace ndn |
| 126 | |
| 127 | #endif // NDN_META_INFO_HPP |