Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 984ca9d | 2016-12-19 13:09:14 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | */ |
| 43 | const time::seconds DEFAULT_RR_FRESHNESS_PERIOD(3600); |
| 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: |
| 53 | Response(); |
| 54 | |
| 55 | Response(const Name& zone, const name::Component& queryType); |
| 56 | |
| 57 | /** |
| 58 | * @brief fill the attributes from Data packet. |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 59 | * |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 60 | * @param zone NDNS zone name |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame^] | 61 | * @param data Data.getName() must the same zone as its prefix, |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 62 | * otherwise it's undefined behavior |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 63 | * @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] | 64 | * changing any attributes, otherwise return true and fill the attributes |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 65 | */ |
| 66 | bool |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame^] | 67 | fromData(const Name& zone, const Data& data); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 68 | |
| 69 | shared_ptr<Data> |
| 70 | toData(); |
| 71 | |
| 72 | Response& |
| 73 | addRr(const Block& rr); |
| 74 | |
| 75 | /** |
| 76 | * @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] | 77 | * |
| 78 | * @return Response that is service level information, the encoding level abstraction, |
| 79 | * i.e., Block is not very convenient. |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 80 | */ |
| 81 | Response& |
| 82 | addRr(const std::string& rr); |
| 83 | |
| 84 | bool |
| 85 | removeRr(const Block& rr); |
| 86 | |
| 87 | bool |
| 88 | operator==(const Response& other) const; |
| 89 | |
| 90 | bool |
| 91 | operator!=(const Response& other) const |
| 92 | { |
| 93 | return !(*this == other); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @brief encode the app-level data |
| 98 | */ |
| 99 | const Block |
| 100 | wireEncode() const; |
| 101 | |
| 102 | /** |
| 103 | * @brief decode the app-level data |
| 104 | */ |
| 105 | void |
| 106 | wireDecode(const Block& wire); |
| 107 | |
| 108 | /** |
| 109 | * @brief encode app-level data |
| 110 | */ |
| 111 | template<bool T> |
| 112 | size_t |
| 113 | wireEncode(EncodingImpl<T> & block) const; |
| 114 | |
| 115 | public: |
| 116 | /////////////////////////////////////////////// |
| 117 | // getter and setter |
| 118 | |
| 119 | const Name& |
| 120 | getZone() const |
| 121 | { |
| 122 | return m_zone; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | setZone(const Name& zone) |
| 127 | { |
| 128 | m_zone = zone; |
| 129 | } |
| 130 | |
| 131 | const name::Component& |
| 132 | getQueryType() const |
| 133 | { |
| 134 | return m_queryType; |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | setQueryType(const name::Component& queryType) |
| 139 | { |
| 140 | m_queryType = queryType; |
| 141 | } |
| 142 | |
| 143 | const Name& |
| 144 | getRrLabel() const |
| 145 | { |
| 146 | return m_rrLabel; |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | setRrLabel(const Name& rrLabel) |
| 151 | { |
| 152 | m_rrLabel = rrLabel; |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | setRrType(const name::Component& rrType) |
| 157 | { |
| 158 | m_rrType = rrType; |
| 159 | } |
| 160 | |
| 161 | const name::Component& |
| 162 | getRrType() const |
| 163 | { |
| 164 | return m_rrType; |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | setVersion(const name::Component& version) |
| 169 | { |
| 170 | m_version = version; |
| 171 | } |
| 172 | |
| 173 | const name::Component& |
| 174 | getVersion() const |
| 175 | { |
| 176 | return m_version; |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | setNdnsType(NdnsType ndnsType) |
| 181 | { |
| 182 | m_ndnsType = ndnsType; |
| 183 | } |
| 184 | |
Alexander Afanasyev | 984ca9d | 2016-12-19 13:09:14 -0800 | [diff] [blame] | 185 | NdnsType |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 186 | getNdnsType() const |
| 187 | { |
| 188 | return m_ndnsType; |
| 189 | } |
| 190 | |
| 191 | const Block& |
| 192 | getAppContent() const |
| 193 | { |
| 194 | return m_appContent; |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | setAppContent(const Block& block); |
| 199 | |
| 200 | const std::vector<Block>& |
| 201 | getRrs() const |
| 202 | { |
| 203 | return m_rrs; |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | setRrs(const std::vector<Block>& rrs) |
| 208 | { |
| 209 | m_rrs = rrs; |
| 210 | } |
| 211 | |
| 212 | time::seconds |
| 213 | getFreshnessPeriod() const |
| 214 | { |
| 215 | return m_freshnessPeriod; |
| 216 | } |
| 217 | |
| 218 | void |
| 219 | setFreshnessPeriod(time::seconds freshnessPeriod) |
| 220 | { |
| 221 | m_freshnessPeriod = freshnessPeriod; |
| 222 | } |
| 223 | |
| 224 | private: |
| 225 | Name m_zone; |
| 226 | name::Component m_queryType; |
| 227 | Name m_rrLabel; |
| 228 | name::Component m_rrType; |
| 229 | name::Component m_version; |
| 230 | |
| 231 | NdnsType m_ndnsType; |
| 232 | time::seconds m_freshnessPeriod; |
| 233 | |
| 234 | /** |
| 235 | * @brief App content. Be valid only for NDNS-NULL Response |
| 236 | */ |
| 237 | Block m_appContent; |
| 238 | |
| 239 | /** |
| 240 | * @brief Content of Resource Record. Be valid only when this is not a NDNS-NULL Response |
| 241 | */ |
| 242 | std::vector<Block> m_rrs; |
| 243 | }; |
| 244 | |
| 245 | std::ostream& |
| 246 | operator<<(std::ostream& os, const Response& response); |
| 247 | |
| 248 | } // namespace ndns |
| 249 | } // namespace ndn |
| 250 | |
| 251 | #endif // NDNS_CLIENTS_RESPONSE_HPP |