blob: 91f46097478730b9bdc45fb5d9a1533ef59b2c2b [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>
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070034
35#include <vector>
36
37#include "ndns-tlv.hpp"
shockjianga5ae48c2014-07-27 23:21:41 -070038#include "query.hpp"
39#include "rr.hpp"
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070040
41namespace ndn {
42namespace ndns {
43
shockjiang99ad3892014-08-03 14:56:13 -070044class Response
45{
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070046public:
shockjianga5ae48c2014-07-27 23:21:41 -070047 enum ResponseType
shockjiang99ad3892014-08-03 14:56:13 -070048 {
49 NDNS_Resp,
50 NDNS_Nack,
51 NDNS_Auth,
52 UNKNOWN
53 };
shockjianga5ae48c2014-07-27 23:21:41 -070054
shockjiang99ad3892014-08-03 14:56:13 -070055 static std::string toString(ResponseType responseType)
shockjianga5ae48c2014-07-27 23:21:41 -070056 {
57 std::string label;
shockjiang99ad3892014-08-03 14:56:13 -070058 switch (responseType) {
59 case NDNS_Resp:
60 label = "NDNS Resp";
61 break;
62 case NDNS_Nack:
63 label = "NDNS Nack";
64 break;
65 case NDNS_Auth:
66 label = "NDNS Auth";
67 break;
68 default:
69 label = "UNKNOWN";
70 break;
71 }
shockjianga5ae48c2014-07-27 23:21:41 -070072 return label;
73 }
74
shockjiang99ad3892014-08-03 14:56:13 -070075 static ResponseType toResponseType(const std::string& str)
shockjianga5ae48c2014-07-27 23:21:41 -070076 {
shockjiang99ad3892014-08-03 14:56:13 -070077 ResponseType atype;
78 if (str == "NDNS Resp") {
79 atype = NDNS_Resp;
80 } else if (str == "NDNS Nack") {
81 atype = NDNS_Nack;
82 } else if (str == "NDNS Auth") {
83 atype = NDNS_Auth;
84 } else {
85 atype = UNKNOWN;
86 }
87 return atype;
shockjianga5ae48c2014-07-27 23:21:41 -070088 }
89
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070090 Response();
91 virtual ~Response();
92
shockjianga5ae48c2014-07-27 23:21:41 -070093 inline void addRr(const uint32_t rrId, const std::string rrData)
94 {
95 RR rr;
96 rr.setId(rrId);
97 rr.setRrdata(rrData);
98 this->m_rrs.push_back(rr);
99 }
100
101 const Block&
102 wireEncode() const;
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700103
104 void
shockjianga5ae48c2014-07-27 23:21:41 -0700105 wireDecode(const Block& wire);
106
107 template<bool T>
108 size_t
109 wireEncode(EncodingImpl<T> & block) const;
110
shockjianga5ae48c2014-07-27 23:21:41 -0700111 void
112 fromData(const Name& name, const Data& data);
113
114 void
115 fromData(const Data &data);
116
117 Data
118 toData() const;
119
shockjiang99ad3892014-08-03 14:56:13 -0700120 const std::string getStringRRs() const
121 {
shockjianga5ae48c2014-07-27 23:21:41 -0700122 std::stringstream str;
shockjiang99ad3892014-08-03 14:56:13 -0700123 str << "[";
shockjianga5ae48c2014-07-27 23:21:41 -0700124 std::vector<RR>::const_iterator iter = m_rrs.begin();
shockjiang99ad3892014-08-03 14:56:13 -0700125 while (iter != m_rrs.end()) {
126 str << " " << *iter;
127 iter++;
shockjianga5ae48c2014-07-27 23:21:41 -0700128 }
shockjiang99ad3892014-08-03 14:56:13 -0700129 str << "]";
shockjianga5ae48c2014-07-27 23:21:41 -0700130 return str.str();
131 }
132
shockjiang99ad3892014-08-03 14:56:13 -0700133 Query::QueryType getContentType() const
134 {
shockjianga5ae48c2014-07-27 23:21:41 -0700135 return m_contentType;
136 }
137
shockjiang99ad3892014-08-03 14:56:13 -0700138 void setContentType(Query::QueryType contentType)
139 {
shockjianga5ae48c2014-07-27 23:21:41 -0700140 m_contentType = contentType;
141 }
142
shockjiang99ad3892014-08-03 14:56:13 -0700143 time::milliseconds getFreshness() const
144 {
shockjianga5ae48c2014-07-27 23:21:41 -0700145 return m_freshness;
146 }
147
shockjiang99ad3892014-08-03 14:56:13 -0700148 void setFreshness(time::milliseconds freshness)
149 {
shockjianga5ae48c2014-07-27 23:21:41 -0700150 m_freshness = freshness;
151 }
152
shockjiang99ad3892014-08-03 14:56:13 -0700153 const Name& getQueryName() const
154 {
shockjianga5ae48c2014-07-27 23:21:41 -0700155 return m_queryName;
156 }
157
shockjiang99ad3892014-08-03 14:56:13 -0700158 void setQueryName(const Name& queryName)
159 {
shockjianga5ae48c2014-07-27 23:21:41 -0700160 m_queryName = queryName;
161 }
162
shockjiang99ad3892014-08-03 14:56:13 -0700163 ResponseType getResponseType() const
164 {
shockjianga5ae48c2014-07-27 23:21:41 -0700165 return m_responseType;
166 }
167
shockjiang99ad3892014-08-03 14:56:13 -0700168 void setResponseType(ResponseType responseType)
169 {
shockjianga5ae48c2014-07-27 23:21:41 -0700170 m_responseType = responseType;
171 }
172
shockjiang99ad3892014-08-03 14:56:13 -0700173 const std::vector<RR>& getRrs() const
174 {
shockjianga5ae48c2014-07-27 23:21:41 -0700175 return m_rrs;
176 }
177
shockjiang99ad3892014-08-03 14:56:13 -0700178 void setRrs(const std::vector<RR>& rrs)
179 {
shockjianga5ae48c2014-07-27 23:21:41 -0700180 m_rrs = rrs;
181 }
182
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700183private:
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700184 time::milliseconds m_freshness;
shockjianga5ae48c2014-07-27 23:21:41 -0700185 Name m_queryName;
186 //std::string m_serial;
187 Query::QueryType m_contentType;
188 ResponseType m_responseType;
189 //unsigned int m_numberOfRR;
shockjiang99ad3892014-08-03 14:56:13 -0700190 std::vector<RR> m_rrs;
shockjianga5ae48c2014-07-27 23:21:41 -0700191 mutable Block m_wire;
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700192
193};
194
shockjianga5ae48c2014-07-27 23:21:41 -0700195inline std::ostream&
196operator<<(std::ostream& os, const Response& response)
197{
shockjiang99ad3892014-08-03 14:56:13 -0700198 os << "Response: queryName=" << response.getQueryName().toUri()
199 << " responseType=" << Response::toString(response.getResponseType())
200 << " contentType=" << Query::toString(response.getContentType()) << " [";
shockjianga5ae48c2014-07-27 23:21:41 -0700201 std::vector<RR>::const_iterator iter = response.getRrs().begin();
shockjiang99ad3892014-08-03 14:56:13 -0700202 while (iter != response.getRrs().end()) {
203 os << " " << *iter;
204 iter++;
shockjianga5ae48c2014-07-27 23:21:41 -0700205 }
shockjiang99ad3892014-08-03 14:56:13 -0700206 os << "]";
shockjianga5ae48c2014-07-27 23:21:41 -0700207 return os;
208}
209
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700210} // namespace ndns
211} // namespace ndn
212
213#endif // NDNS_RESPONSE_HPP