diff --git a/services/backup/src/Reactors/server/SendLogReactor.cpp b/services/backup/src/Reactors/server/SendLogReactor.cpp --- a/services/backup/src/Reactors/server/SendLogReactor.cpp +++ b/services/backup/src/Reactors/server/SendLogReactor.cpp @@ -148,8 +148,9 @@ // as there may be multiple threads from the pool taking over here const std::lock_guard lock(this->reactorStateMutex); // TODO implement - std::cout << "receive logs done " << this->status.error_code() << "/" - << this->status.error_message() << std::endl; + std::cout << "receive logs done " + << this->getUtility()->getStatus().error_code() << "/" + << this->getUtility()->getStatus().error_message() << std::endl; } } // namespace reactor diff --git a/services/backup/src/Reactors/server/base-reactors/ServerBidiReactorBase.h b/services/backup/src/Reactors/server/base-reactors/ServerBidiReactorBase.h --- a/services/backup/src/Reactors/server/base-reactors/ServerBidiReactorBase.h +++ b/services/backup/src/Reactors/server/base-reactors/ServerBidiReactorBase.h @@ -1,5 +1,7 @@ #pragma once +#include "BaseReactor.h" + #include #include @@ -22,11 +24,11 @@ }; template -class ServerBidiReactorBase - : public grpc::ServerBidiReactor { +class ServerBidiReactorBase : public grpc::ServerBidiReactor, + public BaseReactor { + std::shared_ptr utility; Request request; Response response; - std::atomic finished = false; protected: ServerBidiReactorStatus status; @@ -35,28 +37,39 @@ public: ServerBidiReactorBase(); + void terminate(const grpc::Status &status) override; + void validate() override{}; + void doneCallback() override{}; + void terminateCallback() override{}; + void OnDone() override; void OnReadDone(bool ok) override; void OnWriteDone(bool ok) override; + std::shared_ptr getUtility() override; void terminate(ServerBidiReactorStatus status); + ServerBidiReactorStatus getStatus() const; + void setStatus(const ServerBidiReactorStatus &status); virtual std::unique_ptr handleRequest(Request request, Response *response) = 0; - virtual void initialize(){}; - virtual void validate(){}; - virtual void doneCallback(){}; - virtual void terminateCallback(){}; }; template ServerBidiReactorBase::ServerBidiReactorBase() { - this->initialize(); + this->utility->state = ReactorState::RUNNING; this->StartRead(&this->request); } +template +void ServerBidiReactorBase::terminate( + const grpc::Status &status) { + this->terminate(ServerBidiReactorStatus(status)); +} + template void ServerBidiReactorBase::OnDone() { + this->utility->state = ReactorState::DONE; this->doneCallback(); // This looks weird but apparently it is okay to do this. More information: // https://phabricator.ashoat.com/D3246#87890 @@ -66,24 +79,36 @@ template void ServerBidiReactorBase::terminate( ServerBidiReactorStatus status) { - this->status = status; + this->setStatus(status); try { this->terminateCallback(); this->validate(); } catch (std::runtime_error &e) { - this->status = ServerBidiReactorStatus( - grpc::Status(grpc::StatusCode::INTERNAL, e.what())); + this->setStatus(ServerBidiReactorStatus( + grpc::Status(grpc::StatusCode::INTERNAL, e.what()))); } - if (this->finished) { + if (this->utility->state != ReactorState::RUNNING) { return; } - if (this->status.sendLastResponse) { + if (this->getStatus().sendLastResponse) { this->StartWriteAndFinish( - &this->response, grpc::WriteOptions(), this->status.status); + &this->response, grpc::WriteOptions(), this->getStatus().status); } else { - this->Finish(this->status.status); + this->Finish(this->getStatus().status); } - this->finished = true; + this->utility->state = ReactorState::TERMINATED; +} + +template +ServerBidiReactorStatus +ServerBidiReactorBase::getStatus() const { + return this->status; +} + +template +void ServerBidiReactorBase::setStatus( + const ServerBidiReactorStatus &status) { + this->status = status; } template @@ -121,6 +146,12 @@ this->StartRead(&this->request); } +template +std::shared_ptr +ServerBidiReactorBase::getUtility() { + return this->utility; +} + } // namespace reactor } // namespace network } // namespace comm diff --git a/services/backup/src/Reactors/server/base-reactors/ServerReadReactorBase.h b/services/backup/src/Reactors/server/base-reactors/ServerReadReactorBase.h --- a/services/backup/src/Reactors/server/base-reactors/ServerReadReactorBase.h +++ b/services/backup/src/Reactors/server/base-reactors/ServerReadReactorBase.h @@ -1,5 +1,7 @@ #pragma once +#include "BaseReactor.h" + #include #include @@ -12,64 +14,37 @@ namespace reactor { template -class ServerReadReactorBase : public grpc::ServerReadReactor { +class ServerReadReactorBase : public grpc::ServerReadReactor, + public BaseReactor { + std::shared_ptr utility; Request request; - std::atomic finished = false; - - void terminate(grpc::Status status); protected: Response *response; - grpc::Status status; public: ServerReadReactorBase(Response *response); - void OnDone() override; + void validate() override{}; + void doneCallback() override{}; + void terminateCallback() override{}; + void OnReadDone(bool ok) override; + void terminate(const grpc::Status &status) override; + void OnDone() override; + std::shared_ptr getUtility() override; virtual std::unique_ptr readRequest(Request request) = 0; - virtual void initialize(){}; - virtual void validate(){}; - virtual void doneCallback(){}; - virtual void terminateCallback(){}; }; -template -void ServerReadReactorBase::terminate(grpc::Status status) { - this->status = status; - try { - this->terminateCallback(); - this->validate(); - } catch (std::runtime_error &e) { - this->status = grpc::Status(grpc::StatusCode::INTERNAL, e.what()); - } - if (!this->status.ok()) { - std::cout << "error: " << this->status.error_message() << std::endl; - } - if (this->finished) { - return; - } - this->Finish(this->status); - this->finished = true; -} - template ServerReadReactorBase::ServerReadReactorBase( Response *response) : response(response) { - this->initialize(); + this->utility->state = ReactorState::RUNNING; this->StartRead(&this->request); } -template -void ServerReadReactorBase::OnDone() { - this->doneCallback(); - // This looks weird but apparently it is okay to do this. More information: - // https://phabricator.ashoat.com/D3246#87890 - delete this; -} - template void ServerReadReactorBase::OnReadDone(bool ok) { if (!ok) { @@ -92,6 +67,43 @@ this->StartRead(&this->request); } +template +void ServerReadReactorBase::terminate( + const grpc::Status &status) { + this->utility->setStatus(status); + try { + this->terminateCallback(); + this->validate(); + } catch (std::runtime_error &e) { + this->utility->setStatus( + grpc::Status(grpc::StatusCode::INTERNAL, e.what())); + } + if (!this->utility->getStatus().ok()) { + std::cout << "error: " << this->utility->getStatus().error_message() + << std::endl; + } + if (this->utility->state != ReactorState::RUNNING) { + return; + } + this->Finish(this->utility->getStatus()); + this->utility->state = ReactorState::TERMINATED; +} + +template +void ServerReadReactorBase::OnDone() { + this->utility->state = ReactorState::DONE; + this->doneCallback(); + // This looks weird but apparently it is okay to do this. More information: + // https://phabricator.ashoat.com/D3246#87890 + delete this; +} + +template +std::shared_ptr +ServerReadReactorBase::getUtility() { + return this->utility; +} + } // namespace reactor } // namespace network } // namespace comm diff --git a/services/backup/src/Reactors/server/base-reactors/ServerWriteReactorBase.h b/services/backup/src/Reactors/server/base-reactors/ServerWriteReactorBase.h --- a/services/backup/src/Reactors/server/base-reactors/ServerWriteReactorBase.h +++ b/services/backup/src/Reactors/server/base-reactors/ServerWriteReactorBase.h @@ -1,5 +1,7 @@ #pragma once +#include "BaseReactor.h" + #include #include @@ -12,50 +14,56 @@ namespace reactor { template -class ServerWriteReactorBase : public grpc::ServerWriteReactor { +class ServerWriteReactorBase : public grpc::ServerWriteReactor, + public BaseReactor { + std::shared_ptr utility; Response response; bool initialized = false; - std::atomic finished = false; - void terminate(grpc::Status status); void nextWrite(); protected: // this is a const ref since it's not meant to be modified const Request &request; - grpc::Status status; public: ServerWriteReactorBase(const Request *request); void start(); - void OnDone() override; + + void validate() override{}; + void doneCallback() override{}; + void terminateCallback() override{}; + + virtual void initialize(){}; void OnWriteDone(bool ok) override; + void terminate(const grpc::Status &status); + void OnDone() override; + std::shared_ptr getUtility() override; virtual std::unique_ptr writeResponse(Response *response) = 0; - virtual void initialize(){}; - virtual void validate(){}; - virtual void doneCallback(){}; - virtual void terminateCallback(){}; }; template -void ServerWriteReactorBase::terminate(grpc::Status status) { - this->status = status; +void ServerWriteReactorBase::terminate( + const grpc::Status &status) { + this->utility->setStatus(status); try { this->terminateCallback(); this->validate(); } catch (std::runtime_error &e) { - this->status = grpc::Status(grpc::StatusCode::INTERNAL, e.what()); + this->utility->setStatus( + grpc::Status(grpc::StatusCode::INTERNAL, e.what())); } - if (!this->status.ok()) { - std::cout << "error: " << this->status.error_message() << std::endl; + if (!this->utility->getStatus().ok()) { + std::cout << "error: " << this->utility->getStatus().error_message() + << std::endl; } - if (this->finished) { + if (this->utility->state != ReactorState::RUNNING) { return; } - this->Finish(this->status); - this->finished = true; + this->Finish(this->utility->getStatus()); + this->utility->state = ReactorState::TERMINATED; } template @@ -91,6 +99,7 @@ template void ServerWriteReactorBase::start() { + this->utility->state = ReactorState::RUNNING; this->nextWrite(); } @@ -102,6 +111,12 @@ delete this; } +template +std::shared_ptr +ServerWriteReactorBase::getUtility() { + return this->utility; +} + template void ServerWriteReactorBase::OnWriteDone(bool ok) { if (!ok) {