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,6 +1,6 @@ #include "BackupServiceImpl.h" -#include "ReadReactorBase.h" +#include "ServerReadReactorBase.h" #include "ServerBidiReactorBase.h" #include @@ -39,12 +39,13 @@ grpc::ServerReadReactor *BackupServiceImpl::SendLog( grpc::CallbackServerContext *context, google::protobuf::Empty *response) { - class SendLogReactor - : public reactor:: - 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 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 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 --- 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