diff --git a/services/backup/docker-server/contents/server/src/BackupServiceImpl.cpp b/services/backup/docker-server/contents/server/src/BackupServiceImpl.cpp index 897622274..2548d81fd 100644 --- a/services/backup/docker-server/contents/server/src/BackupServiceImpl.cpp +++ b/services/backup/docker-server/contents/server/src/BackupServiceImpl.cpp @@ -1,98 +1,99 @@ #include "BackupServiceImpl.h" -#include "ReadReactorBase.h" #include "ServerBidiReactorBase.h" +#include "ServerReadReactorBase.h" #include namespace comm { namespace network { BackupServiceImpl::BackupServiceImpl() { Aws::InitAPI({}); } BackupServiceImpl::~BackupServiceImpl() { Aws::ShutdownAPI({}); } grpc::ServerBidiReactor< backup::CreateNewBackupRequest, backup::CreateNewBackupResponse> * BackupServiceImpl::CreateNewBackup(grpc::CallbackServerContext *context) { class CreateNewBackupReactor : public reactor::ServerBidiReactorBase< backup::CreateNewBackupRequest, backup::CreateNewBackupResponse> { public: std::unique_ptr handleRequest( backup::CreateNewBackupRequest request, backup::CreateNewBackupResponse *response) override { // TODO handle request return std::make_unique( grpc::Status(grpc::StatusCode::UNIMPLEMENTED, "unimplemented")); } }; return new CreateNewBackupReactor(); } grpc::ServerReadReactor *BackupServiceImpl::SendLog( grpc::CallbackServerContext *context, google::protobuf::Empty *response) { - class SendLogReactor : public ReadReactorBase< + class SendLogReactor : public reactor::ServerReadReactorBase< backup::SendLogRequest, google::protobuf::Empty> { public: - using ReadReactorBase:: - ReadReactorBase; + using ServerReadReactorBase< + backup::SendLogRequest, + google::protobuf::Empty>::ServerReadReactorBase; std::unique_ptr readRequest(backup::SendLogRequest request) override { // TODO handle request return std::make_unique( grpc::StatusCode::UNIMPLEMENTED, "unimplemented"); } }; return new SendLogReactor(response); } grpc::ServerBidiReactor< backup::RecoverBackupKeyRequest, backup::RecoverBackupKeyResponse> * BackupServiceImpl::RecoverBackupKey(grpc::CallbackServerContext *context) { class RecoverBackupKeyReactor : public reactor::ServerBidiReactorBase< backup::RecoverBackupKeyRequest, backup::RecoverBackupKeyResponse> { public: std::unique_ptr handleRequest( backup::RecoverBackupKeyRequest request, backup::RecoverBackupKeyResponse *response) override { // TODO handle request return std::make_unique( grpc::Status(grpc::StatusCode::UNIMPLEMENTED, "unimplemented")); } }; return new RecoverBackupKeyReactor(); } grpc::ServerBidiReactor * BackupServiceImpl::PullBackup(grpc::CallbackServerContext *context) { class PullBackupReactor : public reactor::ServerBidiReactorBase< backup::PullBackupRequest, backup::PullBackupResponse> { public: std::unique_ptr handleRequest( backup::PullBackupRequest request, backup::PullBackupResponse *response) override { // TODO handle request return std::make_unique( grpc::Status(grpc::StatusCode::UNIMPLEMENTED, "unimplemented")); } }; return new PullBackupReactor(); } } // 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/server/base-reactors/ServerReadReactorBase.h similarity index 50% rename from services/backup/docker-server/contents/server/src/Reactors/ReadReactorBase.h rename to services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerReadReactorBase.h index 4b8776d01..9d690f26e 100644 --- a/services/backup/docker-server/contents/server/src/Reactors/ReadReactorBase.h +++ b/services/backup/docker-server/contents/server/src/Reactors/server/base-reactors/ServerReadReactorBase.h @@ -1,59 +1,75 @@ #pragma once #include + #include #include #include namespace comm { namespace network { +namespace reactor { template -class ReadReactorBase : public grpc::ServerReadReactor { +class ServerReadReactorBase : public grpc::ServerReadReactor { Request request; + void terminate(grpc::Status status); + protected: Response *response; + grpc::Status status; public: - ReadReactorBase(Response *response); + ServerReadReactorBase(Response *response); void OnDone() override; void OnReadDone(bool ok) override; virtual std::unique_ptr readRequest(Request request) = 0; + virtual void initialize(){}; + virtual void doneCallback(){}; }; template -ReadReactorBase::ReadReactorBase(Response *response) +void ServerReadReactorBase::terminate(grpc::Status status) { + this->status = status; + this->Finish(status); +} + +template +ServerReadReactorBase::ServerReadReactorBase( + Response *response) : response(response) { + this->initialize(); this->StartRead(&this->request); } template -void ReadReactorBase::OnDone() { +void ServerReadReactorBase::OnDone() { + this->doneCallback(); delete this; } template -void ReadReactorBase::OnReadDone(bool ok) { +void ServerReadReactorBase::OnReadDone(bool ok) { if (!ok) { - this->Finish( - grpc::Status(grpc::StatusCode::INTERNAL, "OnReadDone: reading error")); + this->terminate(grpc::Status(grpc::StatusCode::INTERNAL, "reading error")); return; } try { std::unique_ptr status = this->readRequest(this->request); if (status != nullptr) { - this->Finish(*status); + this->terminate(*status); return; } } catch (std::runtime_error &e) { - this->Finish(grpc::Status(grpc::StatusCode::INTERNAL, e.what())); + this->terminate(grpc::Status(grpc::StatusCode::INTERNAL, e.what())); return; } this->StartRead(&this->request); } +} // namespace reactor } // namespace network } // namespace comm