blob: 97791a7f385048e6c5b617fd986010dfa27aebec [file] [log] [blame]
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -07001/* -*- 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_RESPONSE_HPP
21#define NDNS_RESPONSE_HPP
22
23#include <boost/asio.hpp>
24#include <boost/bind.hpp>
25#include <boost/date_time/posix_time/posix_time.hpp>
26#include <boost/noncopyable.hpp>
27
28#include <ndn-cxx/face.hpp> // /usr/local/include
29#include <ndn-cxx/name.hpp>
30#include <ndn-cxx/data.hpp>
31
32#include <ndn-cxx/encoding/block-helpers.hpp>
33#include <ndn-cxx/encoding/block.hpp>
34#include <ndn-cxx/encoding/tlv-ndnd.hpp>
35
36#include <vector>
37
38#include "ndns-tlv.hpp"
shockjianga5ae48c2014-07-27 23:21:41 -070039#include "query.hpp"
40#include "rr.hpp"
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070041
42namespace ndn {
43namespace ndns {
44
shockjiang99ad3892014-08-03 14:56:13 -070045class Response
46{
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070047public:
shockjianga5ae48c2014-07-27 23:21:41 -070048 enum ResponseType
shockjiang99ad3892014-08-03 14:56:13 -070049 {
50 NDNS_Resp,
51 NDNS_Nack,
52 NDNS_Auth,
53 UNKNOWN
54 };
shockjianga5ae48c2014-07-27 23:21:41 -070055
shockjiang99ad3892014-08-03 14:56:13 -070056 static std::string toString(ResponseType responseType)
shockjianga5ae48c2014-07-27 23:21:41 -070057 {
58 std::string label;
shockjiang99ad3892014-08-03 14:56:13 -070059 switch (responseType) {
60 case NDNS_Resp:
61 label = "NDNS Resp";
62 break;
63 case NDNS_Nack:
64 label = "NDNS Nack";
65 break;
66 case NDNS_Auth:
67 label = "NDNS Auth";
68 break;
69 default:
70 label = "UNKNOWN";
71 break;
72 }
shockjianga5ae48c2014-07-27 23:21:41 -070073 return label;
74 }
75
shockjiang99ad3892014-08-03 14:56:13 -070076 static ResponseType toResponseType(const std::string& str)
shockjianga5ae48c2014-07-27 23:21:41 -070077 {
shockjiang99ad3892014-08-03 14:56:13 -070078 ResponseType atype;
79 if (str == "NDNS Resp") {
80 atype = NDNS_Resp;
81 } else if (str == "NDNS Nack") {
82 atype = NDNS_Nack;
83 } else if (str == "NDNS Auth") {
84 atype = NDNS_Auth;
85 } else {
86 atype = UNKNOWN;
87 }
88 return atype;
shockjianga5ae48c2014-07-27 23:21:41 -070089 }
90
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070091 Response();
92 virtual ~Response();
93
shockjianga5ae48c2014-07-27 23:21:41 -070094 inline void addRr(const uint32_t rrId, const std::string rrData)
95 {
96 RR rr;
97 rr.setId(rrId);
98 rr.setRrdata(rrData);
99 this->m_rrs.push_back(rr);
100 }
101
102 const Block&
103 wireEncode() const;
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700104
105 void
shockjianga5ae48c2014-07-27 23:21:41 -0700106 wireDecode(const Block& wire);
107
108 template<bool T>
109 size_t
110 wireEncode(EncodingImpl<T> & block) const;
111
shockjianga5ae48c2014-07-27 23:21:41 -0700112 void
113 fromData(const Name& name, const Data& data);
114
115 void
116 fromData(const Data &data);
117
118 Data
119 toData() const;
120
shockjiang99ad3892014-08-03 14:56:13 -0700121 const std::string getStringRRs() const
122 {
shockjianga5ae48c2014-07-27 23:21:41 -0700123 std::stringstream str;
shockjiang99ad3892014-08-03 14:56:13 -0700124 str << "[";
shockjianga5ae48c2014-07-27 23:21:41 -0700125 std::vector<RR>::const_iterator iter = m_rrs.begin();
shockjiang99ad3892014-08-03 14:56:13 -0700126 while (iter != m_rrs.end()) {
127 str << " " << *iter;
128 iter++;
shockjianga5ae48c2014-07-27 23:21:41 -0700129 }
shockjiang99ad3892014-08-03 14:56:13 -0700130 str << "]";
shockjianga5ae48c2014-07-27 23:21:41 -0700131 return str.str();
132 }
133
shockjiang99ad3892014-08-03 14:56:13 -0700134 Query::QueryType getContentType() const
135 {
shockjianga5ae48c2014-07-27 23:21:41 -0700136 return m_contentType;
137 }
138
shockjiang99ad3892014-08-03 14:56:13 -0700139 void setContentType(Query::QueryType contentType)
140 {
shockjianga5ae48c2014-07-27 23:21:41 -0700141 m_contentType = contentType;
142 }
143
shockjiang99ad3892014-08-03 14:56:13 -0700144 time::milliseconds getFreshness() const
145 {
shockjianga5ae48c2014-07-27 23:21:41 -0700146 return m_freshness;
147 }
148
shockjiang99ad3892014-08-03 14:56:13 -0700149 void setFreshness(time::milliseconds freshness)
150 {
shockjianga5ae48c2014-07-27 23:21:41 -0700151 m_freshness = freshness;
152 }
153
shockjiang99ad3892014-08-03 14:56:13 -0700154 const Name& getQueryName() const
155 {
shockjianga5ae48c2014-07-27 23:21:41 -0700156 return m_queryName;
157 }
158
shockjiang99ad3892014-08-03 14:56:13 -0700159 void setQueryName(const Name& queryName)
160 {
shockjianga5ae48c2014-07-27 23:21:41 -0700161 m_queryName = queryName;
162 }
163
shockjiang99ad3892014-08-03 14:56:13 -0700164 ResponseType getResponseType() const
165 {
shockjianga5ae48c2014-07-27 23:21:41 -0700166 return m_responseType;
167 }
168
shockjiang99ad3892014-08-03 14:56:13 -0700169 void setResponseType(ResponseType responseType)
170 {
shockjianga5ae48c2014-07-27 23:21:41 -0700171 m_responseType = responseType;
172 }
173
shockjiang99ad3892014-08-03 14:56:13 -0700174 const std::vector<RR>& getRrs() const
175 {
shockjianga5ae48c2014-07-27 23:21:41 -0700176 return m_rrs;
177 }
178
shockjiang99ad3892014-08-03 14:56:13 -0700179 void setRrs(const std::vector<RR>& rrs)
180 {
shockjianga5ae48c2014-07-27 23:21:41 -0700181 m_rrs = rrs;
182 }
183
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700184private:
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700185 time::milliseconds m_freshness;
shockjianga5ae48c2014-07-27 23:21:41 -0700186 Name m_queryName;
187 //std::string m_serial;
188 Query::QueryType m_contentType;
189 ResponseType m_responseType;
190 //unsigned int m_numberOfRR;
shockjiang99ad3892014-08-03 14:56:13 -0700191 std::vector<RR> m_rrs;
shockjianga5ae48c2014-07-27 23:21:41 -0700192 mutable Block m_wire;
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700193
194};
195
shockjianga5ae48c2014-07-27 23:21:41 -0700196inline std::ostream&
197operator<<(std::ostream& os, const Response& response)
198{
shockjiang99ad3892014-08-03 14:56:13 -0700199 os << "Response: queryName=" << response.getQueryName().toUri()
200 << " responseType=" << Response::toString(response.getResponseType())
201 << " contentType=" << Query::toString(response.getContentType()) << " [";
shockjianga5ae48c2014-07-27 23:21:41 -0700202 std::vector<RR>::const_iterator iter = response.getRrs().begin();
shockjiang99ad3892014-08-03 14:56:13 -0700203 while (iter != response.getRrs().end()) {
204 os << " " << *iter;
205 iter++;
shockjianga5ae48c2014-07-27 23:21:41 -0700206 }
shockjiang99ad3892014-08-03 14:56:13 -0700207 os << "]";
shockjianga5ae48c2014-07-27 23:21:41 -0700208 return os;
209}
210
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700211} // namespace ndns
212} // namespace ndn
213
214#endif // NDNS_RESPONSE_HPP