blob: e2266ac1502c25cb3222f49d6761dd7879d4ccce [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
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070045
46class Response {
47public:
shockjianga5ae48c2014-07-27 23:21:41 -070048 enum ResponseType
49 {
50 NDNS_Resp,
51 NDNS_Nack,
52 NDNS_Auth,
53 UNKNOWN
54 };
55
56
57 static std::string
58 toString(ResponseType responseType)
59 {
60 std::string label;
61 switch (responseType)
62 {
63 case NDNS_Resp:
64 label = "NDNS Resp";
65 break;
66 case NDNS_Nack:
67 label = "NDNS Nack";
68 break;
69 case NDNS_Auth:
70 label = "NDNS Auth";
71 break;
72 default:
73 label = "UNKNOWN";
74 break;
75 }
76 return label;
77 }
78
79 static ResponseType
80 toResponseType(const std::string& str)
81 {
82 ResponseType atype;
83 if (str == "NDNS Resp"){
84 atype = NDNS_Resp;
85 }
86 else if (str == "NDNS Nack") {
87 atype = NDNS_Nack;
88 }
89 else if (str == "NDNS Auth")
90 {
91 atype = NDNS_Auth;
92 }
93 else {
94 atype = UNKNOWN;
95 }
96 return atype;
97 }
98
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070099 Response();
100 virtual ~Response();
101
shockjianga5ae48c2014-07-27 23:21:41 -0700102 inline void addRr(const uint32_t rrId, const std::string rrData)
103 {
104 RR rr;
105 rr.setId(rrId);
106 rr.setRrdata(rrData);
107 this->m_rrs.push_back(rr);
108 }
109
110 const Block&
111 wireEncode() const;
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700112
113 void
shockjianga5ae48c2014-07-27 23:21:41 -0700114 wireDecode(const Block& wire);
115
116 template<bool T>
117 size_t
118 wireEncode(EncodingImpl<T> & block) const;
119
120
121 void
122 fromData(const Name& name, const Data& data);
123
124 void
125 fromData(const Data &data);
126
127 Data
128 toData() const;
129
130
131
132 const std::string
133 getStringRRs() const {
134 std::stringstream str;
135 str<<"[";
136 std::vector<RR>::const_iterator iter = m_rrs.begin();
137 while (iter != m_rrs.end())
138 {
139 str<<" "<<*iter;
140 iter ++;
141 }
142 str<<"]";
143 return str.str();
144 }
145
146 Query::QueryType getContentType() const {
147 return m_contentType;
148 }
149
150 void setContentType(Query::QueryType contentType) {
151 m_contentType = contentType;
152 }
153
154 time::milliseconds getFreshness() const {
155 return m_freshness;
156 }
157
158 void setFreshness(time::milliseconds freshness) {
159 m_freshness = freshness;
160 }
161
162 const Name& getQueryName() const {
163 return m_queryName;
164 }
165
166 void setQueryName(const Name& queryName) {
167 m_queryName = queryName;
168 }
169
170 ResponseType getResponseType() const {
171 return m_responseType;
172 }
173
174 void setResponseType(ResponseType responseType) {
175 m_responseType = responseType;
176 }
177
178 const std::vector<RR>& getRrs() const {
179 return m_rrs;
180 }
181
182 void setRrs(const std::vector<RR>& rrs) {
183 m_rrs = rrs;
184 }
185
186
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700187
188private:
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700189 time::milliseconds m_freshness;
shockjianga5ae48c2014-07-27 23:21:41 -0700190 Name m_queryName;
191 //std::string m_serial;
192 Query::QueryType m_contentType;
193 ResponseType m_responseType;
194 //unsigned int m_numberOfRR;
195 std::vector<RR> m_rrs;
196 mutable Block m_wire;
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700197
198};
199
shockjianga5ae48c2014-07-27 23:21:41 -0700200
201inline std::ostream&
202operator<<(std::ostream& os, const Response& response)
203{
204 os<<"Response: queryName="<<response.getQueryName().toUri()
205 <<" responseType="<<Response::toString(response.getResponseType())
206 <<" contentType="<<Query::toString(response.getContentType())
207 <<" [";
208 std::vector<RR>::const_iterator iter = response.getRrs().begin();
209 while (iter != response.getRrs().end())
210 {
211 os<<" "<<*iter;
212 iter ++;
213 }
214 os<<"]";
215 return os;
216}
217
218
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700219} // namespace ndns
220} // namespace ndn
221
222#endif // NDNS_RESPONSE_HPP