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