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