Commit 575c32ee authored by Yuwei Huang's avatar Yuwei Huang Committed by Commit Bot

[Remoting] Make FtlClient async

This CL makes FtlClient async such that it makes the RPC call
asynchronously and runs the callback after the response is received.

Bug: 927962
Change-Id: Id1399f3edc381f0acca7780aca2959a4aa1a3737
Reviewed-on: https://chromium-review.googlesource.com/c/1487960
Commit-Queue: Yuwei Huang <yuweih@chromium.org>
Reviewed-by: default avatarJoe Downing <joedow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#635877}
parent 2ba63532
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include "remoting/signaling/ftl_client.h" #include "remoting/signaling/ftl_client.h"
#include <utility>
#include "base/bind.h"
#include "base/guid.h" #include "base/guid.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "google_apis/google_api_keys.h" #include "google_apis/google_api_keys.h"
...@@ -26,21 +29,23 @@ FtlClient::FtlClient() { ...@@ -26,21 +29,23 @@ FtlClient::FtlClient() {
peer_to_peer_stub_ = PeerToPeer::NewStub(channel); peer_to_peer_stub_ = PeerToPeer::NewStub(channel);
} }
FtlClient::~FtlClient() {} FtlClient::~FtlClient() = default;
grpc::Status FtlClient::GetIceServer(ftl::GetICEServerResponse* response) {
grpc::ClientContext context;
FillClientContextMetadata(&context);
void FtlClient::GetIceServer(RpcCallback<ftl::GetICEServerResponse> callback) {
ftl::GetICEServerRequest request; ftl::GetICEServerRequest request;
request.set_allocated_header(BuildRequestHeader().release()); request.set_allocated_header(BuildRequestHeader().release());
return peer_to_peer_stub_->GetICEServer(&context, request, response); dispatcher_.ExecuteAsyncRpc(
base::BindOnce(&PeerToPeer::Stub::AsyncGetICEServer,
base::Unretained(peer_to_peer_stub_.get())),
CreateClientContext(), request, std::move(callback));
} }
// static // static
void FtlClient::FillClientContextMetadata(grpc::ClientContext* context) { std::unique_ptr<grpc::ClientContext> FtlClient::CreateClientContext() {
auto context = std::make_unique<grpc::ClientContext>();
context->AddMetadata("x-goog-api-key", google_apis::GetRemotingFtlAPIKey()); context->AddMetadata("x-goog-api-key", google_apis::GetRemotingFtlAPIKey());
return context;
} }
// static // static
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "remoting/signaling/ftl_services.grpc.pb.h" #include "remoting/signaling/ftl_services.grpc.pb.h"
#include "remoting/signaling/grpc_async_dispatcher.h"
#include "third_party/grpc/src/include/grpcpp/support/status.h" #include "third_party/grpc/src/include/grpcpp/support/status.h"
namespace remoting { namespace remoting {
...@@ -16,21 +17,25 @@ namespace remoting { ...@@ -16,21 +17,25 @@ namespace remoting {
// This is the class that makes RPC calls to the FTL signaling backend. // This is the class that makes RPC calls to the FTL signaling backend.
class FtlClient { class FtlClient {
public: public:
template <typename ResponseType>
using RpcCallback =
base::OnceCallback<void(grpc::Status, const ResponseType&)>;
FtlClient(); FtlClient();
~FtlClient(); ~FtlClient();
// Retrieves the ice server configs and write output to |response|. // Retrieves the ice server configs.
// TODO(yuweih): Change this to async interface. void GetIceServer(RpcCallback<ftl::GetICEServerResponse> callback);
grpc::Status GetIceServer(ftl::GetICEServerResponse* response);
private: private:
using PeerToPeer = using PeerToPeer =
google::internal::communications::instantmessaging::v1::PeerToPeer; google::internal::communications::instantmessaging::v1::PeerToPeer;
static void FillClientContextMetadata(grpc::ClientContext* context); static std::unique_ptr<grpc::ClientContext> CreateClientContext();
static std::unique_ptr<ftl::RequestHeader> BuildRequestHeader(); static std::unique_ptr<ftl::RequestHeader> BuildRequestHeader();
std::unique_ptr<PeerToPeer::Stub> peer_to_peer_stub_; std::unique_ptr<PeerToPeer::Stub> peer_to_peer_stub_;
GrpcAsyncDispatcher dispatcher_;
DISALLOW_COPY_AND_ASSIGN(FtlClient); DISALLOW_COPY_AND_ASSIGN(FtlClient);
}; };
......
...@@ -3,22 +3,62 @@ ...@@ -3,22 +3,62 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "base/at_exit.h" #include "base/at_exit.h"
#include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "remoting/signaling/ftl_client.h" #include "remoting/signaling/ftl_client.h"
using namespace remoting; namespace remoting {
int main(int argc, char const* argv[]) { class FtlSignalingPlayground {
base::AtExitManager exitManager; public:
base::CommandLine::Init(argc, argv); FtlSignalingPlayground();
~FtlSignalingPlayground();
void GetIceServer(base::OnceClosure on_done);
private:
static void OnGetIceServerResponse(base::OnceClosure on_done,
grpc::Status status,
const ftl::GetICEServerResponse& response);
FtlClient client; FtlClient client_;
ftl::GetICEServerResponse response; DISALLOW_COPY_AND_ASSIGN(FtlSignalingPlayground);
grpc::Status status = client.GetIceServer(&response); };
if (!status.ok()) { FtlSignalingPlayground::FtlSignalingPlayground() = default;
FtlSignalingPlayground::~FtlSignalingPlayground() = default;
void FtlSignalingPlayground::GetIceServer(base::OnceClosure on_done) {
client_.GetIceServer(base::BindOnce(
&FtlSignalingPlayground::OnGetIceServerResponse, std::move(on_done)));
VLOG(0) << "Running GetIceServer...";
}
// static
void FtlSignalingPlayground::OnGetIceServerResponse(
base::OnceClosure on_done,
grpc::Status status,
const ftl::GetICEServerResponse& response) {
if (status.ok()) {
VLOG(0) << "Ice transport policy: "
<< response.ice_config().ice_transport_policy();
for (const ftl::ICEServerList& server :
response.ice_config().ice_servers()) {
VLOG(0) << "ICE server:";
VLOG(0) << " hostname=" << server.hostname();
VLOG(0) << " username=" << server.username();
VLOG(0) << " credential=" << server.credential();
VLOG(0) << " max_rate_kbps=" << server.max_rate_kbps();
for (const std::string& url : server.urls()) {
VLOG(0) << " url=" << url;
}
}
} else {
LOG(ERROR) << "RPC failed. Code=" << status.error_code() << ", " LOG(ERROR) << "RPC failed. Code=" << status.error_code() << ", "
<< "Message=" << status.error_message(); << "Message=" << status.error_message();
if (status.error_code() == grpc::StatusCode::UNAVAILABLE) { if (status.error_code() == grpc::StatusCode::UNAVAILABLE) {
...@@ -27,20 +67,22 @@ int main(int argc, char const* argv[]) { ...@@ -27,20 +67,22 @@ int main(int argc, char const* argv[]) {
<< "to third_party/grpc/src/etc/roots.pem if gRPC cannot locate the " << "to third_party/grpc/src/etc/roots.pem if gRPC cannot locate the "
<< "root certificates."; << "root certificates.";
} }
return 1;
} }
std::move(on_done).Run();
}
} // namespace remoting
int main(int argc, char const* argv[]) {
base::AtExitManager exitManager;
base::CommandLine::Init(argc, argv);
base::MessageLoopForIO message_loop;
remoting::FtlSignalingPlayground playground;
base::RunLoop run_loop;
playground.GetIceServer(run_loop.QuitClosure());
run_loop.Run();
VLOG(0) << "Ice transport policy: "
<< response.ice_config().ice_transport_policy();
for (const ftl::ICEServerList& server : response.ice_config().ice_servers()) {
VLOG(0) << "ICE server:";
VLOG(0) << " hostname=" << server.hostname();
VLOG(0) << " username=" << server.username();
VLOG(0) << " credential=" << server.credential();
VLOG(0) << " max_rate_kbps=" << server.max_rate_kbps();
for (const std::string& url : server.urls()) {
VLOG(0) << " url=" << url;
}
}
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment