Page MenuHomePhabricator

D3464.id10661.diff
No OneTemporary

D3464.id10661.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
@@ -51,6 +51,11 @@
./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/BackupServiceImpl.cpp b/services/backup/docker-server/contents/server/src/BackupServiceImpl.cpp
--- a/services/backup/docker-server/contents/server/src/BackupServiceImpl.cpp
+++ b/services/backup/docker-server/contents/server/src/BackupServiceImpl.cpp
@@ -1,7 +1,7 @@
#include "BackupServiceImpl.h"
-#include "BidiReactorBase.h"
#include "ReadReactorBase.h"
+#include "ServerBidiReactorBase.h"
#include <aws/core/Aws.h>
@@ -20,7 +20,7 @@
backup::CreateNewBackupRequest,
backup::CreateNewBackupResponse> *
BackupServiceImpl::CreateNewBackup(grpc::CallbackServerContext *context) {
- class CreateNewBackupReactor : public BidiReactorBase<
+ class CreateNewBackupReactor : public reactor::ServerBidiReactorBase<
backup::CreateNewBackupRequest,
backup::CreateNewBackupResponse> {
public:
@@ -60,7 +60,7 @@
backup::RecoverBackupKeyRequest,
backup::RecoverBackupKeyResponse> *
BackupServiceImpl::RecoverBackupKey(grpc::CallbackServerContext *context) {
- class RecoverBackupKeyReactor : public BidiReactorBase<
+ class RecoverBackupKeyReactor : public reactor::ServerBidiReactorBase<
backup::RecoverBackupKeyRequest,
backup::RecoverBackupKeyResponse> {
public:
@@ -78,7 +78,7 @@
grpc::ServerBidiReactor<backup::PullBackupRequest, backup::PullBackupResponse> *
BackupServiceImpl::PullBackup(grpc::CallbackServerContext *context) {
- class PullBackupReactor : public BidiReactorBase<
+ class PullBackupReactor : public reactor::ServerBidiReactorBase<
backup::PullBackupRequest,
backup::PullBackupResponse> {
public:
diff --git a/services/backup/docker-server/contents/server/src/Reactors/BidiReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/BidiReactorBase.h
deleted file mode 100644
--- a/services/backup/docker-server/contents/server/src/Reactors/BidiReactorBase.h
+++ /dev/null
@@ -1,68 +0,0 @@
-#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/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

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 24, 2:27 PM (17 h, 39 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2576308
Default Alt Text
D3464.id10661.diff (7 KB)

Event Timeline