Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifndef NDNS_CLIENTS_RESPONSE_HPP |
| 21 | #define NDNS_CLIENTS_RESPONSE_HPP |
| 22 | |
| 23 | #include "ndns-tlv.hpp" |
| 24 | #include "ndns-enum.hpp" |
| 25 | #include "ndns-label.hpp" |
| 26 | |
| 27 | |
| 28 | #include <ndn-cxx/face.hpp> |
| 29 | #include <ndn-cxx/name.hpp> |
| 30 | #include <ndn-cxx/data.hpp> |
| 31 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 32 | #include <ndn-cxx/encoding/block.hpp> |
| 33 | #include <ndn-cxx/meta-info.hpp> |
| 34 | |
| 35 | #include <vector> |
| 36 | |
| 37 | namespace ndn { |
| 38 | namespace ndns { |
| 39 | |
| 40 | /** |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 41 | * @brief Default life time of resource record |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 42 | */ |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame^] | 43 | const time::seconds DEFAULT_RR_FRESHNESS_PERIOD = 3600_s; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 44 | |
| 45 | |
| 46 | /** |
| 47 | * @brief NDNS Response abstraction. Response is used on client side, |
| 48 | * while Rrset is used on server side, and Data packet is used during transmission in network. |
| 49 | */ |
| 50 | class Response |
| 51 | { |
| 52 | public: |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame^] | 53 | class Error : public ndn::tlv::Error |
| 54 | { |
| 55 | public: |
| 56 | using ndn::tlv::Error::Error; |
| 57 | }; |
| 58 | |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 59 | Response(); |
| 60 | |
| 61 | Response(const Name& zone, const name::Component& queryType); |
| 62 | |
| 63 | /** |
| 64 | * @brief fill the attributes from Data packet. |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 65 | * |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 66 | * @param zone NDNS zone name |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 67 | * @param data Data.getName() must the same zone as its prefix, |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 68 | * otherwise it's undefined behavior |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 69 | * @return false if Data.getName() does not follow the structure of NDNS Response without |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 70 | * changing any attributes, otherwise return true and fill the attributes |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 71 | */ |
| 72 | bool |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 73 | fromData(const Name& zone, const Data& data); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 74 | |
| 75 | shared_ptr<Data> |
| 76 | toData(); |
| 77 | |
| 78 | Response& |
| 79 | addRr(const Block& rr); |
| 80 | |
| 81 | /** |
| 82 | * @brief add Block which contains string information and its tlv type is ndns::tlv::RrData |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 83 | * |
| 84 | * @return Response that is service level information, the encoding level abstraction, |
| 85 | * i.e., Block is not very convenient. |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 86 | */ |
| 87 | Response& |
| 88 | addRr(const std::string& rr); |
| 89 | |
| 90 | bool |
| 91 | removeRr(const Block& rr); |
| 92 | |
| 93 | bool |
| 94 | operator==(const Response& other) const; |
| 95 | |
| 96 | bool |
| 97 | operator!=(const Response& other) const |
| 98 | { |
| 99 | return !(*this == other); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @brief encode the app-level data |
| 104 | */ |
| 105 | const Block |
| 106 | wireEncode() const; |
| 107 | |
| 108 | /** |
| 109 | * @brief decode the app-level data |
| 110 | */ |
| 111 | void |
| 112 | wireDecode(const Block& wire); |
| 113 | |
| 114 | /** |
| 115 | * @brief encode app-level data |
| 116 | */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 117 | template<encoding::Tag T> |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 118 | size_t |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 119 | wireEncode(EncodingImpl<T>& block) const; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 120 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame^] | 121 | static std::pair<Name, Name> |
| 122 | wireDecodeDoe(const Block& wire); |
| 123 | |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 124 | public: |
| 125 | /////////////////////////////////////////////// |
| 126 | // getter and setter |
| 127 | |
| 128 | const Name& |
| 129 | getZone() const |
| 130 | { |
| 131 | return m_zone; |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | setZone(const Name& zone) |
| 136 | { |
| 137 | m_zone = zone; |
| 138 | } |
| 139 | |
| 140 | const name::Component& |
| 141 | getQueryType() const |
| 142 | { |
| 143 | return m_queryType; |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | setQueryType(const name::Component& queryType) |
| 148 | { |
| 149 | m_queryType = queryType; |
| 150 | } |
| 151 | |
| 152 | const Name& |
| 153 | getRrLabel() const |
| 154 | { |
| 155 | return m_rrLabel; |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | setRrLabel(const Name& rrLabel) |
| 160 | { |
| 161 | m_rrLabel = rrLabel; |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | setRrType(const name::Component& rrType) |
| 166 | { |
| 167 | m_rrType = rrType; |
| 168 | } |
| 169 | |
| 170 | const name::Component& |
| 171 | getRrType() const |
| 172 | { |
| 173 | return m_rrType; |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | setVersion(const name::Component& version) |
| 178 | { |
| 179 | m_version = version; |
| 180 | } |
| 181 | |
| 182 | const name::Component& |
| 183 | getVersion() const |
| 184 | { |
| 185 | return m_version; |
| 186 | } |
| 187 | |
| 188 | void |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 189 | setContentType(NdnsContentType contentType) |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 190 | { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 191 | m_contentType = contentType; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 194 | NdnsContentType |
| 195 | getContentType() const |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 196 | { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 197 | return m_contentType; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | const Block& |
| 201 | getAppContent() const |
| 202 | { |
| 203 | return m_appContent; |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | setAppContent(const Block& block); |
| 208 | |
| 209 | const std::vector<Block>& |
| 210 | getRrs() const |
| 211 | { |
| 212 | return m_rrs; |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | setRrs(const std::vector<Block>& rrs) |
| 217 | { |
| 218 | m_rrs = rrs; |
| 219 | } |
| 220 | |
| 221 | time::seconds |
| 222 | getFreshnessPeriod() const |
| 223 | { |
| 224 | return m_freshnessPeriod; |
| 225 | } |
| 226 | |
| 227 | void |
| 228 | setFreshnessPeriod(time::seconds freshnessPeriod) |
| 229 | { |
| 230 | m_freshnessPeriod = freshnessPeriod; |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | Name m_zone; |
| 235 | name::Component m_queryType; |
| 236 | Name m_rrLabel; |
| 237 | name::Component m_rrType; |
| 238 | name::Component m_version; |
| 239 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 240 | NdnsContentType m_contentType; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 241 | time::seconds m_freshnessPeriod; |
| 242 | |
| 243 | /** |
| 244 | * @brief App content. Be valid only for NDNS-NULL Response |
| 245 | */ |
| 246 | Block m_appContent; |
| 247 | |
| 248 | /** |
| 249 | * @brief Content of Resource Record. Be valid only when this is not a NDNS-NULL Response |
| 250 | */ |
| 251 | std::vector<Block> m_rrs; |
| 252 | }; |
| 253 | |
| 254 | std::ostream& |
| 255 | operator<<(std::ostream& os, const Response& response); |
| 256 | |
| 257 | } // namespace ndns |
| 258 | } // namespace ndn |
| 259 | |
| 260 | #endif // NDNS_CLIENTS_RESPONSE_HPP |