blob: 1c78cc710fed57bd3f27f937197c5b1397d9ef7c [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
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -080065 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
66 // ContentType?
67 // FreshnessPeriod?
68
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080069 wire_ = Block(Tlv::MetaInfo);
70
71 // ContentType
72 if (type_ != TYPE_DEFAULT) {
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -080073 wire_.push_back
74 (nonNegativeIntegerBlock(Tlv::ContentType, type_));
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080075 }
76
77 // FreshnessPeriod
78 if (freshnessPeriod_ >= 0) {
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -080079 wire_.push_back
80 (nonNegativeIntegerBlock(Tlv::FreshnessPeriod, freshnessPeriod_));
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080081 }
82
83 wire_.encode();
84 return wire_;
85}
86
87inline void
88MetaInfo::wireDecode(const Block &wire)
89{
90 wire_ = wire;
91 wire_.parse();
92
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -080093 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
94 // ContentType?
95 // FreshnessPeriod?
96
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080097 // ContentType
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080098 Block::element_const_iterator val = wire_.find(Tlv::ContentType);
99 if (val != wire_.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800100 {
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -0800101 type_ = readNonNegativeInteger(*val);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800102 }
103
104 // FreshnessPeriod
105 val = wire_.find(Tlv::FreshnessPeriod);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800106 if (val != wire_.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800107 {
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -0800108 freshnessPeriod_ = readNonNegativeInteger(*val);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800109 }
110}
111
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800112inline std::ostream&
113operator << (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 Afanasyev2ba8f662014-01-05 22:53:18 -0800125} // namespace ndn
126
127#endif // NDN_META_INFO_HPP