blob: 6c48015ddfb3b24dfe975f5666b55a01bd2810f7 [file] [log] [blame]
Junxiao Shi8127d1a2018-08-24 15:29:21 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_CXX_PREFIX_ANNOUNCEMENT_HPP
23#define NDN_CXX_PREFIX_ANNOUNCEMENT_HPP
24
25#include "security/v2/key-chain.hpp"
26
27namespace ndn {
28
29/** \brief A prefix announcement object that represents an application's intent of registering a
30 * prefix toward itself.
31 * \sa https://redmine.named-data.net/projects/nfd/wiki/PrefixAnnouncement
32 */
33class PrefixAnnouncement
34{
35public:
36 class Error : public tlv::Error
37 {
38 public:
39 using tlv::Error::Error;
40 };
41
42 /** \brief Construct an empty prefix announcement.
43 * \post getData() == nullopt
44 * \post getAnnouncedName() == "/"
45 * \post getExpiration() == 0_ms
46 */
47 PrefixAnnouncement();
48
49 /** \brief Decode a prefix announcement from Data.
50 * \throw tlv::Error the Data is not a prefix announcement.
51 * \post getData() == data
52 */
53 explicit
54 PrefixAnnouncement(Data data);
55
56 /** \brief Get the Data representing the prefix announcement, if available.
57 */
58 const optional<Data>&
59 getData() const
60 {
61 return m_data;
62 }
63
64 /** \brief Create a Data packet representing the prefix announcement, if it does not exist.
65 * \param keyChain KeyChain to sign the Data.
66 * \param si signing parameters.
67 * \param version version number in Data name; if nullopt, use current Unix timestamp (in
68 * milliseconds) as the version number.
69 * \post getData() == the returned Data
70 */
71 const Data&
72 toData(KeyChain& keyChain,
73 const ndn::security::SigningInfo& si = KeyChain::getDefaultSigningInfo(),
74 optional<uint64_t> version = nullopt) const;
75
76 /** \brief Return announced name.
77 */
78 const Name&
79 getAnnouncedName() const
80 {
81 return m_announcedName;
82 }
83
84 /** \brief Set announced name.
85 * \post getData() == nullopt
86 */
87 PrefixAnnouncement&
88 setAnnouncedName(Name name);
89
90 /** \brief Return relative expiration period.
91 */
92 time::milliseconds
93 getExpiration() const
94 {
95 return m_expiration;
96 }
97
98 /** \brief Set relative expiration period.
99 * \throw std::invalid_argument expiration period is negative.
100 * \post getData() == nullopt
101 */
102 PrefixAnnouncement&
103 setExpiration(time::milliseconds expiration);
104
105 /** \brief Return absolute validity period.
106 */
107 optional<security::ValidityPeriod>
108 getValidityPeriod() const
109 {
110 return m_validity;
111 }
112
113 /** \brief Set absolute validity period.
114 * \post getData() == nullopt
115 */
116 PrefixAnnouncement&
117 setValidityPeriod(optional<security::ValidityPeriod> validity);
118
119private:
120 mutable optional<Data> m_data;
121 Name m_announcedName;
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000122 time::milliseconds m_expiration = 0_ms;
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600123 optional<security::ValidityPeriod> m_validity;
124};
125
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000126/** \brief Test whether two prefix announcements has the same name, expiration period,
127 * and validity period.
128 */
129bool
130operator==(const PrefixAnnouncement& lhs, const PrefixAnnouncement& rhs);
131
132inline bool
133operator!=(const PrefixAnnouncement& lhs, const PrefixAnnouncement& rhs)
134{
135 return !(lhs == rhs);
136}
137
138/** \brief Print prefix announcement to a stream.
139 *
140 * This string is for debugging purpose. Its syntax is not public API.
141 */
142std::ostream&
143operator<<(std::ostream& os, const PrefixAnnouncement& pa);
144
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600145} // namespace ndn
146
147#endif // NDN_CXX_PREFIX_ANNOUNCEMENT_HPP