Add client module
Change-Id: If33b2f9fd4676e295b87bdfd1911304a4add27f9
diff --git a/src/client-module.hpp b/src/client-module.hpp
new file mode 100644
index 0000000..5293b9a
--- /dev/null
+++ b/src/client-module.hpp
@@ -0,0 +1,156 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert 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.
+ *
+ * ndncert 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 copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_CLIENT_MODULE_HPP
+#define NDNCERT_CLIENT_MODULE_HPP
+
+#include "client-config.hpp"
+#include "certificate-request.hpp"
+
+namespace ndn {
+namespace ndncert {
+
+class RequestState
+{
+public:
+ ClientCaItem m_ca;
+ security::Key m_key;
+
+ std::string m_requestId;
+ std::string m_status;
+ std::string m_challengeType;
+ std::list<std::string> m_challengeList;
+
+ bool m_isInstalled = false;
+};
+
+// TODO
+// For each CA item in Client.Conf, create a validator instance and initialize it with CA's cert
+// The validator instance should be in ClientCaItem
+
+class ClientModule : noncopyable
+{
+public:
+ /**
+ * @brief Error that can be thrown from ClientModule
+ */
+ class Error : public std::runtime_error
+ {
+ public:
+ using std::runtime_error::runtime_error;
+ };
+
+ using RequestCallback = function<void (const shared_ptr<RequestState>&)>;
+ using ErrorCallback = function<void (const std::string&)>;
+
+public:
+ explicit
+ ClientModule(Face& face, security::v2::KeyChain& keyChain, size_t retryTimes = 2);
+
+ ClientConfig&
+ getClientConf()
+ {
+ return m_config;
+ }
+
+ void
+ sendProbe(const ClientCaItem& ca, const std::string& probeInfo,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ void
+ handleProbeResponse(const Interest& request, const Data& reply, const ClientCaItem& ca,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ void
+ sendNew(const ClientCaItem& ca, const Name& identityName,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ void
+ handleNewResponse(const Interest& request, const Data& reply,
+ const shared_ptr<RequestState>& state,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ void
+ sendSelect(const shared_ptr<RequestState>& state, const std::string& challengeType,
+ const JsonSection& selectParams,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ void
+ handleSelectResponse(const Interest& request, const Data& reply,
+ const shared_ptr<RequestState>& state,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ void
+ sendValidate(const shared_ptr<RequestState>& state, const JsonSection& validateParams,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ void
+ handleValidateResponse(const Interest& request, const Data& reply,
+ const shared_ptr<RequestState>& state,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ void
+ requestStatus(const shared_ptr<RequestState>& state,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ void
+ handleStatusResponse(const Interest& request, const Data& reply,
+ const shared_ptr<RequestState>& state,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ void
+ requestDownload(const shared_ptr<RequestState>& state, const RequestCallback& requestCallback,
+ const ErrorCallback& errorCallback);
+
+ void
+ handleDownloadResponse(const Interest& request, const Data& reply,
+ const shared_ptr<RequestState>& state,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ // helper functions
+ static JsonSection
+ getJsonFromData(const Data& data);
+
+ static Block
+ nameBlockFromJson(const JsonSection& json);
+
+ static bool
+ checkStatus(const RequestState& state, const JsonSection& json, const ErrorCallback& errorCallback);
+
+protected:
+ virtual void
+ onTimeout(const Interest& interest, int nRetriesLeft, const DataCallback& dataCallback,
+ const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+
+ virtual void
+ onNack(const Interest& interest, const lp::Nack& nack, const ErrorCallback& errorCallback);
+
+
+protected:
+ ClientConfig m_config;
+ Face& m_face;
+ security::v2::KeyChain& m_keyChain;
+ size_t m_retryTimes;
+};
+
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_CLIENT_MODULE_HPP