diff --git a/services/backup/docker-server/contents/server/src/Reactors/client/base-reactors/ClientReadReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/client/base-reactors/ClientReadReactorBase.h new file mode 100644 --- /dev/null +++ b/services/backup/docker-server/contents/server/src/Reactors/client/base-reactors/ClientReadReactorBase.h @@ -0,0 +1,78 @@ +#include + +namespace comm { +namespace network { +namespace reactor { + +template +class ClientReadReactorBase : public grpc::ClientReadReactor { + Response response; + grpc::Status status; + bool done = false; + bool initialized = false; + + void terminate(const grpc::Status &status); + +public: + Request request; + grpc::ClientContext context; + + void start(); + void OnReadDone(bool ok) override; + void OnDone(const grpc::Status &status) override; + bool isDone(); + + virtual std::unique_ptr + readResponse(const Response &response) = 0; + virtual void doneCallback() { + } +}; + +template +void ClientReadReactorBase::terminate( + const grpc::Status &status) { + if (this->done) { + return; + } + this->status = status; + this->done = true; + this->doneCallback(); +} + +template +void ClientReadReactorBase::start() { + this->StartRead(&this->response); + if (!this->initialized) { + this->StartCall(); + this->initialized = true; + } +} + +template +void ClientReadReactorBase::OnReadDone(bool ok) { + if (!ok) { + this->terminate(grpc::Status(grpc::StatusCode::UNKNOWN, "read error")); + return; + } + std::unique_ptr status = this->readResponse(this->response); + if (status != nullptr) { + this->terminate(*status); + return; + } + this->StartRead(&this->response); +} + +template +void ClientReadReactorBase::OnDone( + const grpc::Status &status) { + this->terminate(status); +} + +template +bool ClientReadReactorBase::isDone() { + return this->done; +} + +} // namespace reactor +} // namespace network +} // namespace comm