diff --git a/native/cpp/CommonCpp/grpc/Client.cpp b/native/cpp/CommonCpp/grpc/Client.cpp index b21e6a44e..44ea03c91 100644 --- a/native/cpp/CommonCpp/grpc/Client.cpp +++ b/native/cpp/CommonCpp/grpc/Client.cpp @@ -1,144 +1,171 @@ #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(); } +std::string Client::newSession( + std::string deviceID, + std::string publicKey, + std::string signature, + std::string notifyToken, + tunnelbroker::NewSessionRequest_DeviceTypes deviceType, + std::string deviceAppVersion, + std::string deviceOS) { + grpc::ClientContext context; + tunnelbroker::NewSessionRequest request; + tunnelbroker::NewSessionResponse response; + + request.set_deviceid(deviceID); + request.set_publickey(publicKey); + request.set_signature(signature); + request.set_notifytoken(notifyToken); + request.set_devicetype(deviceType); + request.set_deviceappversion(deviceAppVersion); + request.set_deviceos(deviceOS); + + auto status{this->stub_->NewSession(&context, request, &response)}; + if (!status.ok()) { + return std::string{}; + } + return response.sessionid(); +} + } // namespace network } // namespace comm diff --git a/native/cpp/CommonCpp/grpc/Client.h b/native/cpp/CommonCpp/grpc/Client.h index e2b474482..728db715d 100644 --- a/native/cpp/CommonCpp/grpc/Client.h +++ b/native/cpp/CommonCpp/grpc/Client.h @@ -1,54 +1,62 @@ #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); + std::string newSession( + std::string deviceID, + std::string publicKey, + std::string signature, + std::string notifyToken, + tunnelbroker::NewSessionRequest_DeviceTypes deviceType, + std::string deviceAppVersion, + std::string deviceOS); }; } // namespace network } // namespace comm