clients: add query

Change-Id: I59da2d9894ad35de56a11a9f063a19773d4cecad
diff --git a/src/clients/query.cpp b/src/clients/query.cpp
new file mode 100644
index 0000000..f890ad1
--- /dev/null
+++ b/src/clients/query.cpp
@@ -0,0 +1,92 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NDNS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "query.hpp"
+
+namespace ndn {
+namespace ndns {
+
+Query::Query()
+  : m_interestLifetime(ndn::DEFAULT_INTEREST_LIFETIME)
+{
+}
+
+Query::Query(const Name& hint, const Name& zone, const name::Component& queryType)
+  : m_hint(hint)
+  , m_zone(zone)
+  , m_queryType(queryType)
+  , m_interestLifetime(ndn::DEFAULT_INTEREST_LIFETIME)
+{
+}
+
+
+bool
+Query::fromInterest(const Name& hint, const Name& zone, const Interest& interest)
+{
+  label::MatchResult re;
+  if (!matchName(interest, hint, zone, re))
+    return false;
+
+  m_rrLabel = re.rrLabel;
+  m_rrType = re.rrType;
+
+  m_hint = hint;
+  m_zone = zone;
+
+  size_t len = zone.size();
+  if (!hint.empty())
+    len += hint.size() + 1;
+  m_queryType = interest.getName().get(len);
+
+  return true;
+}
+
+Interest
+Query::toInterest() const
+{
+  // <hint> /xF0. <zone> [<KEY>|<NDNS>|<NDNS-R>] <rrLabel> <rrType>
+  Name name;
+  if (!m_hint.empty()) {
+    name.append(m_hint)
+        .append(ndn::ndns::label::FORWARDING_HINT_LABEL);
+  }
+
+  name.append(this->m_zone)
+      .append(this->m_queryType)
+      .append(this->m_rrLabel)
+      .append(this->m_rrType);
+
+  return Interest(name, m_interestLifetime);
+}
+
+std::ostream&
+operator<<(std::ostream& os, const Query& query)
+{
+  os << "Query: zone=" << query.getZone()
+     << " hint=" << query.getHint()
+     << " queryType=" << query.getQueryType()
+     << " rrLabel=" << query.getRrLabel()
+     << " rrType=" << query.getRrType()
+     << " Lifetime=" << query.getInterestLifetime()
+    ;
+  return os;
+}
+
+} // namespace ndns
+} // namespace ndn