Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 5 | #include "remoting/test/ftl_services_playground.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 6 | |
Yuwei Huang | 30bac9e | 2019-03-01 21:28:14 | [diff] [blame] | 7 | #include <inttypes.h> |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 8 | #include <string> |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 9 | #include <utility> |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 10 | #include <vector> |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 11 | |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 12 | #include "base/base64.h" |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 13 | #include "base/bind.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 14 | #include "base/bind_helpers.h" |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 15 | #include "base/command_line.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 17 | #include "base/guid.h" |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 18 | #include "base/logging.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 19 | #include "base/path_service.h" |
Yuwei Huang | 22e0b56 | 2019-03-27 20:54:29 | [diff] [blame] | 20 | #include "base/run_loop.h" |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 21 | #include "base/task/post_task.h" |
Yuwei Huang | 84b92a7c | 2019-04-16 22:25:00 | [diff] [blame] | 22 | #include "remoting/base/grpc_support/grpc_async_unary_request.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 23 | #include "remoting/base/oauth_token_getter_impl.h" |
Yuwei Huang | 8dd1b728 | 2019-05-03 02:12:40 | [diff] [blame] | 24 | #include "remoting/proto/ftl/v1/ftl_services.grpc.pb.h" |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 25 | #include "remoting/signaling/ftl_grpc_context.h" |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 26 | #include "remoting/test/cli_util.h" |
Yuwei Huang | 1cf6806 | 2019-04-11 23:28:42 | [diff] [blame] | 27 | #include "remoting/test/test_device_id_provider.h" |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 28 | #include "remoting/test/test_oauth_token_getter.h" |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 29 | #include "remoting/test/test_token_storage.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | |
| 33 | constexpr char kSwitchNameHelp[] = "help"; |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 34 | constexpr char kSwitchNameUsername[] = "username"; |
| 35 | constexpr char kSwitchNameStoragePath[] = "storage-path"; |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 36 | constexpr char kSwitchNameNoAutoSignin[] = "no-auto-signin"; |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 37 | |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 38 | bool NeedsManualSignin() { |
| 39 | return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 40 | kSwitchNameNoAutoSignin); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 41 | } |
| 42 | |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 43 | } // namespace |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 44 | |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 45 | namespace remoting { |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 46 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 47 | FtlServicesPlayground::FtlServicesPlayground() : weak_factory_(this) {} |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 48 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 49 | FtlServicesPlayground::~FtlServicesPlayground() = default; |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 50 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 51 | bool FtlServicesPlayground::ShouldPrintHelp() { |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 52 | return base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchNameHelp); |
| 53 | } |
| 54 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 55 | void FtlServicesPlayground::PrintHelp() { |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 56 | printf( |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 57 | "Usage: %s [--no-auto-signin] [--auth-code=<auth-code>] " |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 58 | "[--storage-path=<storage-path>] [--username=<[email protected]>]\n", |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 59 | base::CommandLine::ForCurrentProcess() |
| 60 | ->GetProgram() |
| 61 | .MaybeAsASCII() |
| 62 | .c_str()); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 63 | } |
| 64 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 65 | void FtlServicesPlayground::StartAndAuthenticate() { |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 66 | DCHECK(!storage_); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 67 | DCHECK(!token_getter_); |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 68 | DCHECK(!executor_); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 69 | |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 70 | base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 71 | std::string username = cmd_line->GetSwitchValueASCII(kSwitchNameUsername); |
| 72 | base::FilePath storage_path = |
| 73 | cmd_line->GetSwitchValuePath(kSwitchNameStoragePath); |
| 74 | storage_ = test::TestTokenStorage::OnDisk(username, storage_path); |
| 75 | |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 76 | token_getter_ = std::make_unique<test::TestOAuthTokenGetter>(storage_.get()); |
| 77 | |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 78 | base::RunLoop run_loop; |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 79 | token_getter_->Initialize( |
| 80 | base::BindOnce(&FtlServicesPlayground::ResetServices, |
| 81 | weak_factory_.GetWeakPtr(), run_loop.QuitClosure())); |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 82 | run_loop.Run(); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 83 | |
| 84 | StartLoop(); |
| 85 | } |
| 86 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 87 | void FtlServicesPlayground::StartLoop() { |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 88 | std::vector<test::CommandOption> options{ |
| 89 | {"GetIceServer", base::BindRepeating(&FtlServicesPlayground::GetIceServer, |
| 90 | weak_factory_.GetWeakPtr())}, |
| 91 | {"PullMessages", base::BindRepeating(&FtlServicesPlayground::PullMessages, |
| 92 | weak_factory_.GetWeakPtr())}, |
| 93 | {"ReceiveMessages", |
| 94 | base::BindRepeating(&FtlServicesPlayground::StartReceivingMessages, |
| 95 | weak_factory_.GetWeakPtr())}, |
| 96 | {"SendMessage", base::BindRepeating(&FtlServicesPlayground::SendMessage, |
| 97 | weak_factory_.GetWeakPtr())}}; |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 98 | |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 99 | if (NeedsManualSignin()) { |
| 100 | options.insert( |
| 101 | options.begin(), |
| 102 | {"SignInGaia", base::BindRepeating(&FtlServicesPlayground::SignInGaia, |
| 103 | weak_factory_.GetWeakPtr())}); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 104 | } |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 105 | |
| 106 | test::RunCommandOptionsLoop(options); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 107 | } |
| 108 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 109 | void FtlServicesPlayground::ResetServices(base::OnceClosure on_done) { |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 110 | executor_ = std::make_unique<GrpcAuthenticatedExecutor>(token_getter_.get()); |
| 111 | peer_to_peer_stub_ = PeerToPeer::NewStub(FtlGrpcContext::CreateChannel()); |
| 112 | |
| 113 | registration_manager_ = std::make_unique<FtlRegistrationManager>( |
| 114 | token_getter_.get(), |
Yuwei Huang | 1cf6806 | 2019-04-11 23:28:42 | [diff] [blame] | 115 | std::make_unique<test::TestDeviceIdProvider>(storage_.get())); |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 116 | |
| 117 | message_subscription_.reset(); |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 118 | messaging_client_ = std::make_unique<FtlMessagingClient>( |
| 119 | token_getter_.get(), registration_manager_.get()); |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 120 | message_subscription_ = messaging_client_->RegisterMessageCallback( |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 121 | base::BindRepeating(&FtlServicesPlayground::OnMessageReceived, |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 122 | weak_factory_.GetWeakPtr())); |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 123 | |
| 124 | if (NeedsManualSignin()) { |
| 125 | std::move(on_done).Run(); |
| 126 | } else { |
| 127 | SignInGaia(std::move(on_done)); |
| 128 | } |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 129 | } |
| 130 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 131 | void FtlServicesPlayground::GetIceServer(base::OnceClosure on_done) { |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 132 | DCHECK(peer_to_peer_stub_); |
| 133 | VLOG(0) << "Running GetIceServer..."; |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 134 | ftl::GetICEServerRequest request; |
| 135 | *request.mutable_header() = FtlGrpcContext::CreateRequestHeader(); |
| 136 | auto grpc_request = CreateGrpcAsyncUnaryRequest( |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 137 | base::BindOnce(&PeerToPeer::Stub::AsyncGetICEServer, |
| 138 | base::Unretained(peer_to_peer_stub_.get())), |
Yuwei Huang | 2071aeb | 2019-05-10 21:10:59 | [diff] [blame] | 139 | request, |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 140 | base::BindOnce(&FtlServicesPlayground::OnGetIceServerResponse, |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 141 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | 2071aeb | 2019-05-10 21:10:59 | [diff] [blame] | 142 | FtlGrpcContext::FillClientContext(grpc_request->context()); |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 143 | executor_->ExecuteRpc(std::move(grpc_request)); |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 144 | } |
| 145 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 146 | void FtlServicesPlayground::OnGetIceServerResponse( |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 147 | base::OnceClosure on_done, |
Yuwei Huang | 81bbc1a | 2019-03-08 20:46:06 | [diff] [blame] | 148 | const grpc::Status& status, |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 149 | const ftl::GetICEServerResponse& response) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 150 | if (!status.ok()) { |
| 151 | HandleGrpcStatusError(std::move(on_done), status); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | printf("Ice transport policy: %s\n", |
| 156 | response.ice_config().ice_transport_policy().c_str()); |
| 157 | for (const ftl::ICEServerList& server : response.ice_config().ice_servers()) { |
| 158 | printf( |
| 159 | "ICE server:\n" |
| 160 | " hostname=%s\n" |
| 161 | " username=%s\n" |
| 162 | " credential=%s\n" |
| 163 | " max_rate_kbps=%" PRId64 "\n", |
| 164 | server.hostname().c_str(), server.username().c_str(), |
| 165 | server.credential().c_str(), server.max_rate_kbps()); |
| 166 | for (const std::string& url : server.urls()) { |
| 167 | printf(" url=%s\n", url.c_str()); |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 168 | } |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 169 | } |
| 170 | std::move(on_done).Run(); |
| 171 | } |
| 172 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 173 | void FtlServicesPlayground::SignInGaia(base::OnceClosure on_done) { |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame] | 174 | DCHECK(registration_manager_); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 175 | VLOG(0) << "Running SignInGaia..."; |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame] | 176 | registration_manager_->SignInGaia( |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 177 | base::BindOnce(&FtlServicesPlayground::OnSignInGaiaResponse, |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 178 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 179 | } |
| 180 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 181 | void FtlServicesPlayground::OnSignInGaiaResponse(base::OnceClosure on_done, |
| 182 | const grpc::Status& status) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 183 | if (!status.ok()) { |
| 184 | HandleGrpcStatusError(std::move(on_done), status); |
| 185 | return; |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 186 | } |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 187 | |
| 188 | std::string registration_id_base64; |
| 189 | base::Base64Encode(registration_manager_->GetRegistrationId(), |
| 190 | ®istration_id_base64); |
| 191 | printf("Service signed in. registration_id(base64)=%s\n", |
| 192 | registration_id_base64.c_str()); |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 193 | std::move(on_done).Run(); |
| 194 | } |
| 195 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 196 | void FtlServicesPlayground::PullMessages(base::OnceClosure on_done) { |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 197 | DCHECK(messaging_client_); |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 198 | VLOG(0) << "Running PullMessages..."; |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 199 | |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 200 | messaging_client_->PullMessages( |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 201 | base::BindOnce(&FtlServicesPlayground::OnPullMessagesResponse, |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 202 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
| 203 | } |
| 204 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 205 | void FtlServicesPlayground::OnPullMessagesResponse(base::OnceClosure on_done, |
| 206 | const grpc::Status& status) { |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 207 | if (!status.ok()) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 208 | HandleGrpcStatusError(std::move(on_done), status); |
| 209 | return; |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 210 | } |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 211 | std::move(on_done).Run(); |
| 212 | } |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 213 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 214 | void FtlServicesPlayground::SendMessage(base::OnceClosure on_done) { |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 215 | DCHECK(messaging_client_); |
| 216 | VLOG(0) << "Running SendMessage..."; |
| 217 | |
| 218 | printf("Receiver ID: "); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 219 | std::string receiver_id = test::ReadString(); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 220 | |
| 221 | printf("Receiver registration ID (base64, optional): "); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 222 | std::string registration_id_base64 = test::ReadString(); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 223 | |
| 224 | std::string registration_id; |
| 225 | bool success = base::Base64Decode(registration_id_base64, ®istration_id); |
| 226 | if (!success) { |
| 227 | fprintf(stderr, "Your input can't be base64 decoded.\n"); |
| 228 | std::move(on_done).Run(); |
| 229 | return; |
| 230 | } |
| 231 | DoSendMessage(receiver_id, registration_id, std::move(on_done), true); |
| 232 | } |
| 233 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 234 | void FtlServicesPlayground::DoSendMessage(const std::string& receiver_id, |
| 235 | const std::string& registration_id, |
| 236 | base::OnceClosure on_done, |
| 237 | bool should_keep_running) { |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 238 | if (!should_keep_running) { |
| 239 | std::move(on_done).Run(); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | printf("Message (enter nothing to quit): "); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 244 | std::string message = test::ReadString(); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 245 | |
| 246 | if (message.empty()) { |
| 247 | std::move(on_done).Run(); |
| 248 | return; |
| 249 | } |
| 250 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 251 | auto on_continue = base::BindOnce(&FtlServicesPlayground::DoSendMessage, |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 252 | weak_factory_.GetWeakPtr(), receiver_id, |
| 253 | registration_id, std::move(on_done)); |
| 254 | |
Yuwei Huang | 4c919d14 | 2019-04-05 17:05:49 | [diff] [blame] | 255 | ftl::ChromotingMessage crd_message; |
| 256 | crd_message.mutable_xmpp()->set_stanza(message); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 257 | messaging_client_->SendMessage( |
Yuwei Huang | 4c919d14 | 2019-04-05 17:05:49 | [diff] [blame] | 258 | receiver_id, registration_id, crd_message, |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 259 | base::BindOnce(&FtlServicesPlayground::OnSendMessageResponse, |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 260 | weak_factory_.GetWeakPtr(), std::move(on_continue))); |
| 261 | } |
| 262 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 263 | void FtlServicesPlayground::OnSendMessageResponse( |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 264 | base::OnceCallback<void(bool)> on_continue, |
| 265 | const grpc::Status& status) { |
| 266 | if (!status.ok()) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 267 | HandleGrpcStatusError(base::BindOnce(std::move(on_continue), false), |
| 268 | status); |
| 269 | return; |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 270 | } |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 271 | |
| 272 | printf("Message successfully sent.\n"); |
| 273 | std::move(on_continue).Run(true); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 274 | } |
| 275 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 276 | void FtlServicesPlayground::StartReceivingMessages(base::OnceClosure on_done) { |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 277 | VLOG(0) << "Running StartReceivingMessages..."; |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 278 | receive_messages_done_callback_ = std::move(on_done); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 279 | messaging_client_->StartReceivingMessages( |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 280 | base::BindOnce(&FtlServicesPlayground::OnReceiveMessagesStreamReady, |
| 281 | weak_factory_.GetWeakPtr()), |
| 282 | base::BindOnce(&FtlServicesPlayground::OnReceiveMessagesStreamClosed, |
| 283 | weak_factory_.GetWeakPtr())); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 284 | } |
| 285 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 286 | void FtlServicesPlayground::StopReceivingMessages(base::OnceClosure on_done) { |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 287 | messaging_client_->StopReceivingMessages(); |
| 288 | std::move(on_done).Run(); |
| 289 | } |
| 290 | |
Yuwei Huang | c9bb46f | 2019-04-04 02:19:00 | [diff] [blame] | 291 | void FtlServicesPlayground::OnMessageReceived( |
Yuwei Huang | 4ef50e73 | 2019-05-30 04:30:25 | [diff] [blame^] | 292 | const ftl::Id& sender_id, |
Yuwei Huang | c9bb46f | 2019-04-04 02:19:00 | [diff] [blame] | 293 | const std::string& sender_registration_id, |
Yuwei Huang | 4c919d14 | 2019-04-05 17:05:49 | [diff] [blame] | 294 | const ftl::ChromotingMessage& message) { |
| 295 | std::string message_text = message.xmpp().stanza(); |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 296 | printf( |
| 297 | "Received message:\n" |
| 298 | " Sender ID=%s\n" |
Yuwei Huang | c9bb46f | 2019-04-04 02:19:00 | [diff] [blame] | 299 | " Sender Registration ID=%s\n" |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 300 | " Message=%s\n", |
Yuwei Huang | 4ef50e73 | 2019-05-30 04:30:25 | [diff] [blame^] | 301 | sender_id.id().c_str(), sender_registration_id.c_str(), |
| 302 | message_text.c_str()); |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 303 | } |
| 304 | |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 305 | void FtlServicesPlayground::OnReceiveMessagesStreamReady() { |
| 306 | printf("Started receiving messages. Press enter to stop streaming...\n"); |
| 307 | test::WaitForEnterKey(base::BindOnce( |
| 308 | &FtlServicesPlayground::StopReceivingMessages, weak_factory_.GetWeakPtr(), |
| 309 | std::move(receive_messages_done_callback_))); |
| 310 | } |
| 311 | |
| 312 | void FtlServicesPlayground::OnReceiveMessagesStreamClosed( |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 313 | const grpc::Status& status) { |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 314 | base::OnceClosure callback = std::move(receive_messages_done_callback_); |
| 315 | bool is_callback_null = callback.is_null(); |
| 316 | if (is_callback_null) { |
| 317 | callback = base::DoNothing::Once(); |
| 318 | } |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 319 | if (status.error_code() == grpc::StatusCode::CANCELLED) { |
| 320 | printf("ReceiveMessages stream canceled by client.\n"); |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 321 | std::move(callback).Run(); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 322 | return; |
| 323 | } |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 324 | |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 325 | if (!status.ok()) { |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 326 | HandleGrpcStatusError(std::move(callback), status); |
| 327 | } else { |
| 328 | printf("Stream closed by server.\n"); |
| 329 | std::move(callback).Run(); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 330 | } |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 331 | |
| 332 | if (is_callback_null) { |
| 333 | // Stream had been started and callback has been passed to wait for the |
| 334 | // enter key. |
| 335 | printf("Please press enter to continue...\n"); |
| 336 | } |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 337 | } |
| 338 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 339 | void FtlServicesPlayground::HandleGrpcStatusError(base::OnceClosure on_done, |
| 340 | const grpc::Status& status) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 341 | DCHECK(!status.ok()); |
| 342 | if (status.error_code() == grpc::StatusCode::UNAUTHENTICATED) { |
| 343 | if (NeedsManualSignin()) { |
| 344 | printf( |
| 345 | "Request is unauthenticated. You should run SignInGaia first if " |
| 346 | "you haven't done so, otherwise your OAuth token might be expired. \n" |
| 347 | "Request for new OAuth token? [y/N]: "); |
Yuwei Huang | dc86bdf4 | 2019-04-18 19:14:10 | [diff] [blame] | 348 | if (!test::ReadYNBool()) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 349 | std::move(on_done).Run(); |
| 350 | return; |
| 351 | } |
| 352 | } |
| 353 | VLOG(0) << "Grpc request failed to authenticate. " |
| 354 | << "Trying to reauthenticate..."; |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 355 | token_getter_->ResetWithAuthenticationFlow( |
| 356 | base::BindOnce(&FtlServicesPlayground::ResetServices, |
| 357 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
| 361 | fprintf(stderr, "RPC failed. Code=%d, Message=%s\n", status.error_code(), |
| 362 | status.error_message().c_str()); |
| 363 | std::move(on_done).Run(); |
| 364 | } |
| 365 | |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 366 | } // namespace remoting |