helpers: RepoCommandParameter and RepoCommandResponse data structures
Change-Id: I9809e44b8216b026468fbf33265eff3009d87404
diff --git a/helpers/repo-command-parameter.hpp b/helpers/repo-command-parameter.hpp
new file mode 100644
index 0000000..5033f61
--- /dev/null
+++ b/helpers/repo-command-parameter.hpp
@@ -0,0 +1,331 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef REPO_HELPERS_REPO_COMMAND_PARAMETER_HPP
+#define REPO_HELPERS_REPO_COMMAND_PARAMETER_HPP
+
+#include <ndn-cpp-dev/encoding/encoding-buffer.hpp>
+#include <ndn-cpp-dev/name.hpp>
+#include <ndn-cpp-dev/selectors.hpp>
+#include "repo-tlv.hpp"
+
+namespace repo {
+
+using ndn::Name;
+using ndn::Block;
+using ndn::EncodingImpl;
+using ndn::Selectors;
+using ndn::EncodingEstimator;
+using ndn::EncodingBuffer;
+
+/**
+* @brief Class defining abstraction of parameter of command for NDN Repo Protocol
+* @sa link http://redmine.named-data.net/projects/repo-ng/wiki/Repo_Protocol_Specification#RepoCommandParameter
+**/
+
+class RepoCommandParameter
+{
+public:
+ class Error : public ndn::Tlv::Error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : ndn::Tlv::Error(what)
+ {
+ }
+ };
+
+ RepoCommandParameter()
+ : m_hasName(false)
+ , m_hasStartBlockId(false)
+ , m_hasEndBlockId(false)
+ , m_hasProcessId(false)
+ {
+ }
+
+ explicit
+ RepoCommandParameter(const Block& block)
+ {
+ wireDecode(block);
+ }
+
+ const Name&
+ getName() const
+ {
+ return m_name;
+ }
+
+ RepoCommandParameter&
+ setName(const Name& name)
+ {
+ m_name = name;
+ m_hasName = true;
+ m_wire.reset();
+ return *this;
+ }
+
+ bool
+ hasName() const
+ {
+ return m_hasName;
+ }
+
+ const Selectors&
+ getSelectors() const
+ {
+ return m_selectors;
+ }
+
+ RepoCommandParameter&
+ setSelectors(const Selectors& selectors)
+ {
+ m_selectors = selectors;
+ m_wire.reset();
+ return *this;
+ }
+
+ bool
+ hasSelectors() const
+ {
+ return !m_selectors.empty();
+ }
+
+ uint64_t
+ getStartBlockId() const
+ {
+ assert(hasStartBlockId());
+ return m_startBlockId;
+ }
+
+ RepoCommandParameter&
+ setStartBlockId(uint64_t startBlockId)
+ {
+ m_startBlockId = startBlockId;
+ m_hasStartBlockId = true;
+ m_wire.reset();
+ return *this;
+ }
+
+ bool
+ hasStartBlockId() const
+ {
+ return m_hasStartBlockId;
+ }
+
+ uint64_t
+ getEndBlockId() const
+ {
+ assert(hasEndBlockId());
+ return m_endBlockId;
+ }
+
+ RepoCommandParameter&
+ setEndBlockId(uint64_t endBlockId)
+ {
+ m_endBlockId = endBlockId;
+ m_hasEndBlockId = true;
+ m_wire.reset();
+ return *this;
+ }
+
+ bool
+ hasEndBlockId() const
+ {
+ return m_hasEndBlockId;
+ }
+
+ uint64_t
+ getProcessId() const
+ {
+ assert(hasProcessId());
+ return m_processId;
+ }
+
+ bool
+ hasProcessId() const
+ {
+ return m_hasProcessId;
+ }
+
+ RepoCommandParameter&
+ setProcessId(uint64_t processId)
+ {
+ m_processId = processId;
+ m_hasProcessId = true;
+ m_wire.reset();
+ return *this;
+ }
+
+ template<bool T>
+ size_t
+ wireEncode(EncodingImpl<T>& block) const;
+
+ const Block&
+ wireEncode() const;
+
+ void
+ wireDecode(const Block& wire);
+
+private:
+
+ Name m_name;
+ Selectors m_selectors;
+ uint64_t m_startBlockId;
+ uint64_t m_endBlockId;
+ uint64_t m_processId;
+
+ bool m_hasName;
+ bool m_hasStartBlockId;
+ bool m_hasEndBlockId;
+ bool m_hasProcessId;
+
+ mutable Block m_wire;
+};
+
+template<bool T>
+inline size_t
+RepoCommandParameter::wireEncode(EncodingImpl<T>& encoder) const
+{
+ size_t totalLength = 0;
+ size_t variableLength = 0;
+
+ if (m_hasProcessId) {
+ variableLength = encoder.prependNonNegativeInteger(m_processId);
+ totalLength += variableLength;
+ totalLength += encoder.prependVarNumber(variableLength);
+ totalLength += encoder.prependVarNumber(tlv::ProcessId);
+ }
+
+ if (m_hasEndBlockId) {
+ variableLength = encoder.prependNonNegativeInteger(m_endBlockId);
+ totalLength += variableLength;
+ totalLength += encoder.prependVarNumber(variableLength);
+ totalLength += encoder.prependVarNumber(tlv::EndBlockId);
+ }
+
+ if (m_hasStartBlockId) {
+ variableLength = encoder.prependNonNegativeInteger(m_startBlockId);
+ totalLength += variableLength;
+ totalLength += encoder.prependVarNumber(variableLength);
+ totalLength += encoder.prependVarNumber(tlv::StartBlockId);
+ }
+
+ if (!getSelectors().empty()) {
+ totalLength += getSelectors().wireEncode(encoder);
+ }
+
+ if (m_hasName) {
+ totalLength += getName().wireEncode(encoder);
+ }
+
+ totalLength += encoder.prependVarNumber(totalLength);
+ totalLength += encoder.prependVarNumber(tlv::RepoCommandParameter);
+ return totalLength;
+}
+
+inline const Block&
+RepoCommandParameter::wireEncode() const
+{
+ if (m_wire.hasWire())
+ return m_wire;
+
+ EncodingEstimator estimator;
+ size_t estimatedSize = wireEncode(estimator);
+
+ EncodingBuffer buffer(estimatedSize, 0);
+ wireEncode(buffer);
+
+ m_wire = buffer.block();
+ return m_wire;
+}
+
+inline void
+RepoCommandParameter::wireDecode(const Block& wire)
+{
+ m_hasName = false;
+ m_hasStartBlockId = false;
+ m_hasEndBlockId = false;
+ m_hasProcessId = false;
+
+ m_wire = wire;
+
+ m_wire.parse();
+
+ if (m_wire.type() != tlv::RepoCommandParameter)
+ throw Error("Requested decoding of RepoCommandParameter, but Block is of different type");
+
+ // Name
+ Block::element_const_iterator val = m_wire.find(tlv::Name);
+ if (val != m_wire.elements_end())
+ {
+ m_hasName = true;
+ m_name.wireDecode(m_wire.get(tlv::Name));
+ }
+
+ // Selectors
+ val = m_wire.find(tlv::Selectors);
+ if (val != m_wire.elements_end())
+ {
+ m_selectors.wireDecode(*val);
+ }
+ else
+ m_selectors = Selectors();
+
+ // StartBlockId
+ val = m_wire.find(tlv::StartBlockId);
+ if (val != m_wire.elements_end())
+ {
+ m_hasStartBlockId = true;
+ m_startBlockId = readNonNegativeInteger(*val);
+ }
+
+ // EndBlockId
+ val = m_wire.find(tlv::EndBlockId);
+ if (val != m_wire.elements_end())
+ {
+ m_hasEndBlockId = true;
+ m_endBlockId = readNonNegativeInteger(*val);
+ }
+
+ // ProcessId
+ val = m_wire.find(tlv::ProcessId);
+ if (val != m_wire.elements_end())
+ {
+ m_hasProcessId = true;
+ m_processId = readNonNegativeInteger(*val);
+ }
+
+}
+
+inline std::ostream&
+operator<<(std::ostream& os, const RepoCommandParameter& repoCommandParameter)
+{
+ os << "RepoCommandParameter(";
+
+ // Name
+ if (repoCommandParameter.hasName()) {
+ os << " Name: " << repoCommandParameter.getName();
+ }
+ if (repoCommandParameter.hasStartBlockId()) {
+ // StartBlockId
+ os << " StartBlockId: " << repoCommandParameter.getStartBlockId();
+ }
+ // EndBlockId
+ if (repoCommandParameter.hasEndBlockId()) {
+ os << " EndBlockId: " << repoCommandParameter.getEndBlockId();
+ }
+ //ProcessId
+ if (repoCommandParameter.hasProcessId()) {
+ os << " ProcessId: " << repoCommandParameter.getProcessId();
+ }
+ os << " )";
+ return os;
+}
+
+} //namespace repo
+
+#endif
diff --git a/helpers/repo-command-response.hpp b/helpers/repo-command-response.hpp
new file mode 100644
index 0000000..8c3a230
--- /dev/null
+++ b/helpers/repo-command-response.hpp
@@ -0,0 +1,382 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef REPO_HELPERS_REPO_COMMAND_RESPONSE_HPP
+#define REPO_HELPERS_REPO_COMMAND_RESPONSE_HPP
+
+#include <ndn-cpp-dev/encoding/block.hpp>
+#include <ndn-cpp-dev/encoding/encoding-buffer.hpp>
+#include <ndn-cpp-dev/encoding/tlv-nfd.hpp>
+#include "repo-tlv.hpp"
+
+namespace repo {
+
+using ndn::Block;
+using ndn::EncodingImpl;
+using ndn::EncodingEstimator;
+using ndn::EncodingBuffer;
+
+/**
+* @brief Class defining abstraction of Response for NDN Repo Protocol
+* @sa link http://redmine.named-data.net/projects/repo-ng/wiki/Repo_Protocol_Specification#Repo-Command-Response
+*/
+class RepoCommandResponse
+{
+public:
+ class Error : public ndn::Tlv::Error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : ndn::Tlv::Error(what)
+ {
+ }
+ };
+
+ RepoCommandResponse()
+ : m_hasStartBlockId(false)
+ , m_hasEndBlockId(false)
+ , m_hasProcessId(false)
+ , m_hasInsertNum(false)
+ , m_hasDeleteNum(false)
+ , m_hasStatusCode(false)
+ {
+ }
+
+ explicit
+ RepoCommandResponse(const Block& block)
+ {
+ wireDecode(block);
+ }
+
+ uint64_t
+ getStartBlockId() const
+ {
+ return m_startBlockId;
+ }
+
+ RepoCommandResponse&
+ setStartBlockId(uint64_t startBlockId)
+ {
+ m_startBlockId = startBlockId;
+ m_hasStartBlockId = true;
+ m_wire.reset();
+ return *this;
+ }
+
+ bool
+ hasStartBlockId() const
+ {
+ return m_hasStartBlockId;
+ }
+
+ uint64_t
+ getEndBlockId() const
+ {
+ assert(hasEndBlockId());
+ return m_endBlockId;
+ }
+
+ RepoCommandResponse&
+ setEndBlockId(uint64_t endBlockId)
+ {
+ m_endBlockId = endBlockId;
+ m_hasEndBlockId = true;
+ m_wire.reset();
+ return *this;
+ }
+
+ bool
+ hasEndBlockId() const
+ {
+ return m_hasEndBlockId;
+ }
+
+
+ uint64_t
+ getProcessId() const
+ {
+ return m_processId;
+ }
+
+ RepoCommandResponse&
+ setProcessId(uint64_t processId)
+ {
+ m_processId = processId;
+ m_hasProcessId = true;
+ m_wire.reset();
+ return *this;
+ }
+
+ bool
+ hasProcessId() const
+ {
+ return m_hasProcessId;
+ }
+
+ uint64_t
+ getStatusCode() const
+ {
+ return m_statusCode;
+ }
+
+ RepoCommandResponse&
+ setStatusCode(uint64_t statusCode)
+ {
+ m_statusCode = statusCode;
+ m_hasStatusCode = true;
+ m_wire.reset();
+ return *this;
+ }
+
+ bool
+ hasStatusCode() const
+ {
+ return m_hasStatusCode;
+ }
+
+ uint64_t
+ getInsertNum() const
+ {
+ return m_insertNum;
+ }
+
+ RepoCommandResponse&
+ setInsertNum(uint64_t insertNum)
+ {
+ m_insertNum = insertNum;
+ m_hasInsertNum = true;
+ m_wire.reset();
+ return *this;
+ }
+
+ bool
+ hasInsertNum() const
+ {
+ return m_hasInsertNum;
+ }
+
+ uint64_t
+ getDeleteNum() const
+ {
+ return m_deleteNum;
+ }
+
+ RepoCommandResponse&
+ setDeleteNum(uint64_t deleteNum)
+ {
+ m_deleteNum = deleteNum;
+ m_hasDeleteNum = true;
+ m_wire.reset();
+ return *this;
+ }
+
+ bool
+ hasDeleteNum() const
+ {
+ return m_hasDeleteNum;
+ }
+
+ template<bool T>
+ size_t
+ wireEncode(EncodingImpl<T>& block) const;
+
+ const Block&
+ wireEncode() const;
+
+ void
+ wireDecode(const Block& wire);
+
+private:
+ uint64_t m_statusCode;
+ uint64_t m_startBlockId;
+ uint64_t m_endBlockId;
+ uint64_t m_processId;
+ uint64_t m_insertNum;
+ uint64_t m_deleteNum;
+
+ bool m_hasStartBlockId;
+ bool m_hasEndBlockId;
+ bool m_hasProcessId;
+ bool m_hasInsertNum;
+ bool m_hasDeleteNum;
+ bool m_hasStatusCode;
+
+ mutable Block m_wire;
+};
+
+template<bool T>
+inline size_t
+RepoCommandResponse::wireEncode(EncodingImpl<T>& encoder) const
+{
+ size_t totalLength = 0;
+ size_t variableLength = 0;
+
+ if (m_hasDeleteNum) {
+ variableLength = encoder.prependNonNegativeInteger(m_deleteNum);
+ totalLength += variableLength;
+ totalLength += encoder.prependVarNumber(variableLength);
+ totalLength += encoder.prependVarNumber(tlv::DeleteNum);
+ }
+
+ if (m_hasInsertNum) {
+ variableLength = encoder.prependNonNegativeInteger(m_insertNum);
+ totalLength += variableLength;
+ totalLength += encoder.prependVarNumber(variableLength);
+ totalLength += encoder.prependVarNumber(tlv::InsertNum);
+ }
+
+ if (m_hasEndBlockId) {
+ variableLength = encoder.prependNonNegativeInteger(m_endBlockId);
+ totalLength += variableLength;
+ totalLength += encoder.prependVarNumber(variableLength);
+ totalLength += encoder.prependVarNumber(tlv::EndBlockId);
+ }
+
+ if (m_hasStartBlockId) {
+ variableLength = encoder.prependNonNegativeInteger(m_startBlockId);
+ totalLength += variableLength;
+ totalLength += encoder.prependVarNumber(variableLength);
+ totalLength += encoder.prependVarNumber(repo::tlv::StartBlockId);
+ }
+
+ if (m_hasStatusCode) {
+ variableLength = encoder.prependNonNegativeInteger(m_statusCode);
+ totalLength += variableLength;
+ totalLength += encoder.prependVarNumber(variableLength);
+ totalLength += encoder.prependVarNumber(tlv::StatusCode);
+ } else {
+ throw Error("required field StatusCode is missing");
+ }
+
+ if (m_hasProcessId) {
+ variableLength = encoder.prependNonNegativeInteger(m_processId);
+ totalLength += variableLength;
+ totalLength += encoder.prependVarNumber(variableLength);
+ totalLength += encoder.prependVarNumber(tlv::ProcessId);
+ }
+
+ totalLength += encoder.prependVarNumber(totalLength);
+ totalLength += encoder.prependVarNumber(tlv::RepoCommandResponse);
+ return totalLength;
+}
+
+inline const Block&
+RepoCommandResponse::wireEncode() const
+{
+ if (m_wire.hasWire())
+ return m_wire;
+
+ EncodingEstimator estimator;
+ size_t estimatedSize = wireEncode(estimator);
+
+ EncodingBuffer buffer(estimatedSize, 0);
+ wireEncode(buffer);
+
+ m_wire = buffer.block();
+ return m_wire;
+}
+
+inline void
+RepoCommandResponse::wireDecode(const Block& wire)
+{
+ m_hasStartBlockId = false;
+ m_hasEndBlockId = false;
+ m_hasProcessId = false;
+ m_hasStatusCode = false;
+ m_hasInsertNum = false;
+ m_hasDeleteNum = false;
+
+ m_wire = wire;
+
+ m_wire.parse();
+
+ Block::element_const_iterator val;
+
+ if (m_wire.type() != tlv::RepoCommandResponse)
+ throw Error("RepoCommandResponse malformed");
+
+ // StartBlockId
+ val = m_wire.find(tlv::StartBlockId);
+ if (val != m_wire.elements_end())
+ {
+ m_hasStartBlockId = true;
+ m_startBlockId = readNonNegativeInteger(*val);
+ }
+
+ // EndBlockId
+ val = m_wire.find(tlv::EndBlockId);
+ if (val != m_wire.elements_end())
+ {
+ m_hasEndBlockId = true;
+ m_endBlockId = readNonNegativeInteger(*val);
+ }
+
+ // ProcessId
+ val = m_wire.find(tlv::ProcessId);
+ if (val != m_wire.elements_end())
+ {
+ m_hasProcessId = true;
+ m_processId = readNonNegativeInteger(*val);
+ }
+
+ // StatusCode
+ val = m_wire.find(tlv::StatusCode);
+ if (val != m_wire.elements_end())
+ {
+ m_hasStatusCode = true;
+ m_statusCode = readNonNegativeInteger(*val);
+
+ } else {
+ throw Error("required field StatusCode is missing");
+ }
+
+ // InsertNum
+ val = m_wire.find(tlv::InsertNum);
+ if (val != m_wire.elements_end())
+ {
+ m_hasInsertNum = true;
+ m_insertNum = readNonNegativeInteger(*val);
+ }
+
+ // DeleteNum
+ val = m_wire.find(tlv::DeleteNum);
+ if (val != m_wire.elements_end())
+ {
+ m_hasDeleteNum = true;
+ m_deleteNum = readNonNegativeInteger(*val);
+ }
+}
+
+inline std::ostream&
+operator<<(std::ostream& os, const RepoCommandResponse& repoCommandResponse)
+{
+ os << "RepoCommandResponse(";
+
+ if (repoCommandResponse.hasProcessId()) {
+ os << " ProcessId: " << repoCommandResponse.getProcessId();
+ }
+ if (repoCommandResponse.hasStatusCode()) {
+ os << " StatusCode: " << repoCommandResponse.getStatusCode();
+ }
+ if (repoCommandResponse.hasStartBlockId()) {
+ os << " StartBlockId: " << repoCommandResponse.getStartBlockId();
+ }
+ if (repoCommandResponse.hasEndBlockId()) {
+ os << " EndBlockId: " << repoCommandResponse.getEndBlockId();
+ }
+ if (repoCommandResponse.hasInsertNum()) {
+ os << " InsertNum: " << repoCommandResponse.getInsertNum();
+ }
+ if (repoCommandResponse.hasDeleteNum()) {
+ os << " DeleteNum: " << repoCommandResponse.getDeleteNum();
+
+ }
+ os << " )";
+ return os;
+}
+} //namespace repo
+#endif
diff --git a/helpers/repo-tlv.hpp b/helpers/repo-tlv.hpp
new file mode 100644
index 0000000..d74bd22
--- /dev/null
+++ b/helpers/repo-tlv.hpp
@@ -0,0 +1,31 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef REPO_HELPERS_REPO_TLV_HPP
+#define REPO_HELPERS_REPO_TLV_HPP
+
+#include <ndn-cpp-dev/encoding/tlv.hpp>
+
+namespace repo {
+namespace tlv {
+
+using namespace ndn::Tlv;
+
+enum {
+ RepoCommandParameter = 201,
+ StartBlockId = 204,
+ EndBlockId = 205,
+ ProcessId = 206,
+ RepoCommandResponse = 207,
+ StatusCode = 208,
+ InsertNum = 209,
+ DeleteNum = 210
+};
+
+} // tlv
+} // repo
+
+#endif // REPO_HELPERS_REPO_TLV_HPP