diff --git a/native/cpp/CommonCpp/grpc/Client.cpp b/native/cpp/CommonCpp/grpc/Client.cpp index cb474523b..b21e6a44e 100644 --- a/native/cpp/CommonCpp/grpc/Client.cpp +++ b/native/cpp/CommonCpp/grpc/Client.cpp @@ -1,131 +1,144 @@ #include "Client.h" #include "Logger.h" #include namespace comm { namespace network { Client::Client( std::string hostname, std::string port, std::shared_ptr credentials, const std::string id, const std::string deviceToken) : id(id), deviceToken(deviceToken) { std::shared_ptr channel = grpc::CreateChannel(hostname + ":" + port, credentials); this->stub_ = TunnelbrokerService::NewStub(channel); } tunnelbroker::CheckResponseType Client::checkIfPrimaryDeviceOnline() { grpc::ClientContext context; tunnelbroker::CheckRequest request; tunnelbroker::CheckResponse response; request.set_userid(this->id); request.set_devicetoken(this->deviceToken); grpc::Status status = stub_->CheckIfPrimaryDeviceOnline(&context, request, &response); if (!status.ok()) { throw std::runtime_error(status.error_message()); } return response.checkresponsetype(); } bool Client::becomeNewPrimaryDevice() { grpc::ClientContext context; tunnelbroker::NewPrimaryRequest request; tunnelbroker::NewPrimaryResponse response; request.set_userid(this->id); request.set_devicetoken(this->deviceToken); grpc::Status status = stub_->BecomeNewPrimaryDevice(&context, request, &response); if (!status.ok()) { throw std::runtime_error(status.error_message()); } return response.success(); } void Client::sendPong() { grpc::ClientContext context; tunnelbroker::PongRequest request; google::protobuf::Empty response; request.set_userid(this->id); request.set_devicetoken(this->deviceToken); Logger::log("Sending PONG"); grpc::Status status = this->stub_->SendPong(&context, request, &response); if (!status.ok()) { std::ostringstream errorMsg; errorMsg << "Sending PONG failed: " << status.error_message() << std::endl; Logger::log(errorMsg.str()); } } grpc::Status Client::send( std::string sessionID, std::string toDeviceID, std::string payload, std::vector blobHashes) { grpc::ClientContext context; tunnelbroker::SendRequest request; google::protobuf::Empty response; request.set_sessionid(sessionID); request.set_todeviceid(toDeviceID); request.set_payload(payload); for (const auto &blob : blobHashes) { request.add_blobhashes(blob); } return this->stub_->Send(&context, request, &response); } void Client::get(std::string sessionID) { this->clientGetReadReactor = std::make_unique(this->stub_.get(), sessionID); } void Client::setOnReadDoneCallback(std::function callback) { if (!this->clientGetReadReactor) { return; } this->clientGetReadReactor->setOnReadDoneCallback(callback); } void Client::setOnOpenCallback(std::function callback) { if (!this->clientGetReadReactor) { return; } this->clientGetReadReactor->setOnOpenCallback(callback); } void Client::setOnCloseCallback(std::function callback) { if (!this->clientGetReadReactor) { return; } this->clientGetReadReactor->setOnCloseCallback(callback); } void Client::closeGetStream() { if (!this->clientGetReadReactor) { return; } this->clientGetReadReactor->close(); } void Client::assignSetReadyStateCallback( std::function callback) { if (!this->clientGetReadReactor) { return; } this->clientGetReadReactor->assignSetReadyStateCallback(callback); } +std::string Client::sessionSignature(std::string deviceID) { + grpc::ClientContext context; + tunnelbroker::SessionSignatureRequest request; + tunnelbroker::SessionSignatureResponse response; + + request.set_deviceid(deviceID); + auto status{this->stub_->SessionSignature(&context, request, &response)}; + if (!status.ok()) { + return std::string{}; + } + return response.tosign(); +} + } // namespace network } // namespace comm diff --git a/native/cpp/CommonCpp/grpc/Client.h b/native/cpp/CommonCpp/grpc/Client.h index aeb37120a..e2b474482 100644 --- a/native/cpp/CommonCpp/grpc/Client.h +++ b/native/cpp/CommonCpp/grpc/Client.h @@ -1,52 +1,54 @@ #pragma once #include #include #include #include "ClientGetReadReactor.h" #include "_generated/tunnelbroker.grpc.pb.h" #include "_generated/tunnelbroker.pb.h" namespace comm { namespace network { using grpc::Channel; using tunnelbroker::CheckResponseType; using tunnelbroker::TunnelbrokerService; class Client { std::unique_ptr stub_; const std::string id; const std::string deviceToken; std::unique_ptr clientGetReadReactor; public: Client( std::string hostname, std::string port, std::shared_ptr credentials, const std::string id, const std::string deviceToken); CheckResponseType checkIfPrimaryDeviceOnline(); bool becomeNewPrimaryDevice(); void sendPong(); grpc::Status send( std::string sessionID, std::string toDeviceID, std::string payload, std::vector blobHashes); void get(std::string sessionID); void setOnReadDoneCallback(std::function callback); void setOnOpenCallback(std::function callback); void setOnCloseCallback(std::function callback); void closeGetStream(); void assignSetReadyStateCallback(std::function callback); + + std::string sessionSignature(std::string deviceID); }; } // namespace network } // namespace comm