Page MenuHomePhabricator

D3464.id10540.diff
No OneTemporary

D3464.id10540.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,7 +50,11 @@
include_directories(
./src
./src/DatabaseEntities
- ./src/Reactors
+ ./src/Reactors/server
+ ./src/Reactors/server/base-reactors
+ ./src/Reactors/client
+ ./src/Reactors/client/blob
+ ./src/Reactors/client/base-reactors
./_generated
${FOLLY_INCLUDES}
./lib/double-conversion
diff --git a/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerBidiReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerBidiReactorBase.h
new file mode 100644
--- /dev/null
+++ b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerBidiReactorBase.h
@@ -0,0 +1,93 @@
+#pragma once
+
+#include <grpcpp/grpcpp.h>
+
+#include <iostream>
+#include <memory>
+#include <string>
+
+namespace comm {
+namespace network {
+namespace reactor {
+
+template <class Request, class Response>
+class ServerBidiReactorBase
+ : public grpc::ServerBidiReactor<Request, Response> {
+ Request request;
+ Response response;
+
+protected:
+ grpc::Status status;
+ bool readingAborted = false;
+ bool sendLastResponse = false;
+
+public:
+ ServerBidiReactorBase();
+
+ void OnDone() override;
+ void OnReadDone(bool ok) override;
+ void OnWriteDone(bool ok) override;
+
+ void terminate(grpc::Status status);
+
+ virtual std::unique_ptr<grpc::Status>
+ handleRequest(Request request, Response *response) = 0;
+ virtual void initialize(){};
+ virtual void doneCallback(){};
+};
+
+template <class Request, class Response>
+ServerBidiReactorBase<Request, Response>::ServerBidiReactorBase() {
+ this->initialize();
+ this->StartRead(&this->request);
+}
+
+template <class Request, class Response>
+void ServerBidiReactorBase<Request, Response>::OnDone() {
+ this->doneCallback();
+ delete this;
+}
+
+template <class Request, class Response>
+void ServerBidiReactorBase<Request, Response>::terminate(grpc::Status status) {
+ this->status = status;
+ if (this->sendLastResponse) {
+ this->StartWriteAndFinish(&this->response, grpc::WriteOptions(), status);
+ } else {
+ this->Finish(status);
+ }
+}
+
+template <class Request, class Response>
+void ServerBidiReactorBase<Request, Response>::OnReadDone(bool ok) {
+ if (!ok) {
+ this->readingAborted = true;
+ this->terminate(grpc::Status(grpc::StatusCode::ABORTED, "no more reads"));
+ return;
+ }
+ try {
+ this->response = Response();
+ std::unique_ptr<grpc::Status> status =
+ this->handleRequest(this->request, &this->response);
+ if (status != nullptr) {
+ this->terminate(*status);
+ return;
+ }
+ this->StartWrite(&this->response);
+ } catch (std::runtime_error &e) {
+ this->terminate(grpc::Status(grpc::StatusCode::INTERNAL, e.what()));
+ }
+}
+
+template <class Request, class Response>
+void ServerBidiReactorBase<Request, Response>::OnWriteDone(bool ok) {
+ if (!ok) {
+ std::cout << "Server write failed" << std::endl;
+ return;
+ }
+ this->StartRead(&this->request);
+}
+
+} // namespace reactor
+} // namespace network
+} // namespace comm
diff --git a/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerReadReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerReadReactorBase.h
new file mode 100644
--- /dev/null
+++ b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerReadReactorBase.h
@@ -0,0 +1,75 @@
+#pragma once
+
+#include <grpcpp/grpcpp.h>
+
+#include <iostream>
+#include <memory>
+#include <string>
+
+namespace comm {
+namespace network {
+namespace reactor {
+
+template <class Request, class Response>
+class ServerReadReactorBase : public grpc::ServerReadReactor<Request> {
+ Request request;
+
+ void terminate(grpc::Status status);
+
+protected:
+ Response *response;
+ grpc::Status status;
+
+public:
+ ServerReadReactorBase(Response *response);
+
+ void OnDone() override;
+ void OnReadDone(bool ok) override;
+
+ virtual std::unique_ptr<grpc::Status> readRequest(Request request) = 0;
+ virtual void initialize(){};
+ virtual void doneCallback(){};
+};
+
+template <class Request, class Response>
+void ServerReadReactorBase<Request, Response>::terminate(grpc::Status status) {
+ this->status = status;
+ this->Finish(status);
+}
+
+template <class Request, class Response>
+ServerReadReactorBase<Request, Response>::ServerReadReactorBase(
+ Response *response)
+ : response(response) {
+ this->initialize();
+ this->StartRead(&this->request);
+}
+
+template <class Request, class Response>
+void ServerReadReactorBase<Request, Response>::OnDone() {
+ this->doneCallback();
+ delete this;
+}
+
+template <class Request, class Response>
+void ServerReadReactorBase<Request, Response>::OnReadDone(bool ok) {
+ if (!ok) {
+ this->terminate(grpc::Status(grpc::StatusCode::INTERNAL, "reading error"));
+ return;
+ }
+ try {
+ std::unique_ptr<grpc::Status> status = this->readRequest(this->request);
+ if (status != nullptr) {
+ this->terminate(*status);
+ return;
+ }
+ } catch (std::runtime_error &e) {
+ this->terminate(grpc::Status(grpc::StatusCode::INTERNAL, e.what()));
+ return;
+ }
+ this->StartRead(&this->request);
+}
+
+} // namespace reactor
+} // namespace network
+} // namespace comm
diff --git a/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerWriteReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerWriteReactorBase.h
new file mode 100644
--- /dev/null
+++ b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerWriteReactorBase.h
@@ -0,0 +1,86 @@
+#pragma once
+
+#include <grpcpp/grpcpp.h>
+
+#include <iostream>
+#include <memory>
+#include <string>
+
+namespace comm {
+namespace network {
+namespace reactor {
+
+template <class Request, class Response>
+class ServerWriteReactorBase : public grpc::ServerWriteReactor<Response> {
+ Response response;
+ bool initialized = false;
+
+protected:
+ // this is a const ref since it's not meant to be modified
+ const Request &request;
+
+public:
+ ServerWriteReactorBase(const Request *request);
+
+ virtual void NextWrite();
+ void OnDone() override;
+ void OnWriteDone(bool ok) override;
+
+ virtual std::unique_ptr<grpc::Status> writeResponse(Response *response) = 0;
+ virtual void initialize(){};
+ virtual void doneCallback(){};
+};
+
+template <class Request, class Response>
+ServerWriteReactorBase<Request, Response>::ServerWriteReactorBase(
+ 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 ServerWriteReactorBase<Request, Response>::NextWrite() {
+ try {
+ if (!this->initialized) {
+ this->initialize();
+ this->initialized = true;
+ }
+ this->response = Response();
+ std::unique_ptr<grpc::Status> status = this->writeResponse(&this->response);
+ if (status != nullptr) {
+ this->Finish(*status);
+ return;
+ }
+ this->StartWrite(&this->response);
+ } catch (std::runtime_error &e) {
+ std::cout << "error: " << e.what() << std::endl;
+ this->Finish(grpc::Status(grpc::StatusCode::INTERNAL, e.what()));
+ }
+}
+
+template <class Request, class Response>
+void ServerWriteReactorBase<Request, Response>::OnDone() {
+ this->doneCallback();
+ delete this;
+}
+
+template <class Request, class Response>
+void ServerWriteReactorBase<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 reactor
+} // namespace network
+} // namespace comm

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 24, 2:18 PM (17 h, 29 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2576265
Default Alt Text
D3464.id10540.diff (8 KB)

Event Timeline