Page MenuHomePhabricator

D3246.id10534.diff
No OneTemporary

D3246.id10534.diff

diff --git a/services/backup/docker-server/contents/server/CMakeLists.txt b/services/backup/docker-server/contents/server/CMakeLists.txt
--- a/services/backup/docker-server/contents/server/CMakeLists.txt
+++ b/services/backup/docker-server/contents/server/CMakeLists.txt
@@ -50,6 +50,7 @@
include_directories(
./src
./src/DatabaseEntities
+ ./src/Reactors
./_generated
${FOLLY_INCLUDES}
./lib/double-conversion
diff --git a/services/backup/docker-server/contents/server/src/Reactors/BidiReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/BidiReactorBase.h
new file mode 100644
--- /dev/null
+++ b/services/backup/docker-server/contents/server/src/Reactors/BidiReactorBase.h
@@ -0,0 +1,68 @@
+#pragma once
+
+#include <grpcpp/grpcpp.h>
+#include <iostream>
+#include <memory>
+#include <string>
+
+namespace comm {
+namespace network {
+
+template <class Request, class Response>
+class BidiReactorBase : public grpc::ServerBidiReactor<Request, Response> {
+ Request request;
+ Response response;
+
+public:
+ BidiReactorBase();
+
+ void OnDone() override;
+ void OnReadDone(bool ok) override;
+ void OnWriteDone(bool ok) override;
+
+ virtual std::unique_ptr<grpc::Status>
+ handleRequest(Request request, Response *response) = 0;
+};
+
+template <class Request, class Response>
+BidiReactorBase<Request, Response>::BidiReactorBase() {
+ this->StartRead(&this->request);
+}
+
+template <class Request, class Response>
+void BidiReactorBase<Request, Response>::OnDone() {
+ delete this;
+}
+
+template <class Request, class Response>
+void BidiReactorBase<Request, Response>::OnReadDone(bool ok) {
+ if (!ok) {
+ this->Finish(
+ grpc::Status(grpc::StatusCode::INTERNAL, "OnReadDone: reading error"));
+ return;
+ }
+ this->response = Response();
+ try {
+ std::unique_ptr<grpc::Status> status =
+ this->handleRequest(this->request, &this->response);
+ if (status != nullptr) {
+ this->Finish(*status);
+ return;
+ }
+ this->StartWrite(&this->response);
+ } catch (std::runtime_error &e) {
+ this->Finish(grpc::Status(grpc::StatusCode::INTERNAL, e.what()));
+ }
+}
+
+template <class Request, class Response>
+void BidiReactorBase<Request, Response>::OnWriteDone(bool ok) {
+ if (!ok) {
+ std::cout << "Server write failed" << std::endl;
+ return;
+ }
+ this->StartRead(&this->request);
+}
+
+} // namespace network
+} // namespace comm
diff --git a/services/backup/docker-server/contents/server/src/Reactors/ReadReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/ReadReactorBase.h
new file mode 100644
--- /dev/null
+++ b/services/backup/docker-server/contents/server/src/Reactors/ReadReactorBase.h
@@ -0,0 +1,59 @@
+#pragma once
+
+#include <grpcpp/grpcpp.h>
+#include <iostream>
+#include <memory>
+#include <string>
+
+namespace comm {
+namespace network {
+
+template <class Request, class Response>
+class ReadReactorBase : public grpc::ServerReadReactor<Request> {
+ Request request;
+
+protected:
+ Response *response;
+
+public:
+ ReadReactorBase(Response *response);
+
+ void OnDone() override;
+ void OnReadDone(bool ok) override;
+
+ virtual std::unique_ptr<grpc::Status> readRequest(Request request) = 0;
+};
+
+template <class Request, class Response>
+ReadReactorBase<Request, Response>::ReadReactorBase(Response *response)
+ : response(response) {
+ this->StartRead(&this->request);
+}
+
+template <class Request, class Response>
+void ReadReactorBase<Request, Response>::OnDone() {
+ delete this;
+}
+
+template <class Request, class Response>
+void ReadReactorBase<Request, Response>::OnReadDone(bool ok) {
+ if (!ok) {
+ this->Finish(
+ grpc::Status(grpc::StatusCode::INTERNAL, "OnReadDone: reading error"));
+ return;
+ }
+ try {
+ std::unique_ptr<grpc::Status> status = this->readRequest(this->request);
+ if (status != nullptr) {
+ this->Finish(*status);
+ return;
+ }
+ } catch (std::runtime_error &e) {
+ this->Finish(grpc::Status(grpc::StatusCode::INTERNAL, e.what()));
+ return;
+ }
+ this->StartRead(&this->request);
+}
+
+} // namespace network
+} // namespace comm
diff --git a/services/backup/docker-server/contents/server/src/Reactors/WriteReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/WriteReactorBase.h
new file mode 100644
--- /dev/null
+++ b/services/backup/docker-server/contents/server/src/Reactors/WriteReactorBase.h
@@ -0,0 +1,69 @@
+#pragma once
+
+#include <grpcpp/grpcpp.h>
+#include <iostream>
+#include <memory>
+#include <string>
+
+namespace comm {
+namespace network {
+
+template <class Request, class Response>
+class WriteReactorBase : public grpc::ServerWriteReactor<Response> {
+ Response response;
+
+protected:
+ // this is a const ref since it's not meant to be modified
+ const Request &request;
+
+public:
+ WriteReactorBase(const Request *request);
+
+ virtual void NextWrite();
+ void OnDone() override;
+ void OnWriteDone(bool ok) override;
+
+ virtual std::unique_ptr<grpc::Status> writeResponse(Response *response) = 0;
+};
+
+template <class Request, class Response>
+WriteReactorBase<Request, Response>::WriteReactorBase(const Request *request)
+ : request(*request) {
+ // we cannot call this->NextWrite() here because it's going to call it on
+ // the base class, not derived leading to the runtime error of calling
+ // a pure virtual function
+ // NextWrite has to be exposed as a public function and called explicitly
+ // to initialize writing
+}
+
+template <class Request, class Response>
+void WriteReactorBase<Request, Response>::NextWrite() {
+ this->response = Response();
+ std::unique_ptr<grpc::Status> status = this->writeResponse(&this->response);
+ if (status != nullptr) {
+ this->Finish(*status);
+ return;
+ }
+ this->StartWrite(&this->response);
+}
+
+template <class Request, class Response>
+void WriteReactorBase<Request, Response>::OnDone() {
+ delete this;
+}
+
+template <class Request, class Response>
+void WriteReactorBase<Request, Response>::OnWriteDone(bool ok) {
+ if (!ok) {
+ this->Finish(grpc::Status(grpc::StatusCode::INTERNAL, "writing error"));
+ return;
+ }
+ try {
+ this->NextWrite();
+ } catch (std::runtime_error &e) {
+ this->Finish(grpc::Status(grpc::StatusCode::INTERNAL, e.what()));
+ }
+}
+
+} // namespace network
+} // namespace comm

File Metadata

Mime Type
text/plain
Expires
Sun, Dec 1, 7:31 AM (18 h, 19 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2604210
Default Alt Text
D3246.id10534.diff (6 KB)

Event Timeline