blob: 3a33dc4ad23c3947aa62e5bfcfbedba40b2b7715 [file] [log] [blame]
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08001/* -*- 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
11namespace ndn {
12
13/**
14 * An MetaInfo holds the meta info which is signed inside the data packet.
15 */
16class MetaInfo {
17public:
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
52private:
53 uint32_t type_;
54 Milliseconds freshnessPeriod_;
55
56 mutable Block wire_;
57};
58
59inline const Block&
60MetaInfo::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
91inline void
92MetaInfo::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
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800114inline std::ostream&
115operator << (std::ostream &os, const MetaInfo &info)
116{
117 // ContentType
118 os << "ContentType: " << info.getType();
119
120 // FreshnessPeriod
121 if (info.getFreshnessPeriod() >= 0) {
122 os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
123 }
124 return os;
125}
126
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800127} // namespace ndn
128
129#endif // NDN_META_INFO_HPP