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 | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 5 | #include "remoting/test/ftl_signaling_playground.h" |
| 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 | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 20 | #include "base/strings/string_number_conversions.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 21 | #include "base/strings/stringprintf.h" |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 22 | #include "base/task/post_task.h" |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 23 | #include "remoting/base/fake_oauth_token_getter.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 24 | #include "remoting/base/oauth_token_getter_impl.h" |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame^] | 25 | #include "remoting/signaling/ftl_device_id_provider.h" |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 26 | #include "remoting/signaling/ftl_services.grpc.pb.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 27 | #include "remoting/test/test_oauth_token_factory.h" |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 28 | #include "remoting/test/test_token_storage.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | |
| 32 | constexpr char kSwitchNameHelp[] = "help"; |
| 33 | constexpr char kSwitchNameAuthCode[] = "code"; |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 34 | constexpr char kSwitchNameUsername[] = "username"; |
| 35 | constexpr char kSwitchNameStoragePath[] = "storage-path"; |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 36 | |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 37 | // Reads a newline-terminated string from stdin. |
| 38 | std::string ReadString() { |
| 39 | const int kMaxLen = 1024; |
| 40 | std::string str(kMaxLen, 0); |
| 41 | char* result = fgets(&str[0], kMaxLen, stdin); |
| 42 | if (!result) |
| 43 | return std::string(); |
| 44 | size_t newline_index = str.find('\n'); |
| 45 | if (newline_index != std::string::npos) |
| 46 | str[newline_index] = '\0'; |
| 47 | str.resize(strlen(&str[0])); |
| 48 | return str; |
| 49 | } |
| 50 | |
| 51 | // Read the value of |switch_name| from command line if it exists, otherwise |
| 52 | // read from stdin. |
| 53 | std::string ReadStringFromCommandLineOrStdin(const std::string& switch_name, |
| 54 | const std::string& read_prompt) { |
| 55 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 56 | if (command_line->HasSwitch(switch_name)) { |
| 57 | return command_line->GetSwitchValueASCII(switch_name); |
| 58 | } |
| 59 | printf("%s", read_prompt.c_str()); |
| 60 | return ReadString(); |
| 61 | } |
| 62 | |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 63 | // Wait for the user to press enter key on an anonymous thread and calls |
| 64 | // |on_done| once it is done. |
| 65 | void WaitForEnterKey(base::OnceClosure on_done) { |
| 66 | base::PostTaskWithTraitsAndReply(FROM_HERE, {base::MayBlock()}, |
| 67 | base::BindOnce([]() { getchar(); }), |
| 68 | std::move(on_done)); |
| 69 | } |
| 70 | |
| 71 | void PrintGrpcStatusError(const grpc::Status& status) { |
| 72 | DCHECK(!status.ok()); |
| 73 | fprintf(stderr, "RPC failed. Code=%d, Message=%s\n", status.error_code(), |
| 74 | status.error_message().c_str()); |
| 75 | switch (status.error_code()) { |
| 76 | case grpc::StatusCode::UNAVAILABLE: |
| 77 | fprintf(stderr, |
| 78 | "Set the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable " |
| 79 | "to third_party/grpc/src/etc/roots.pem if gRPC cannot locate the " |
| 80 | "root certificates.\n"); |
| 81 | break; |
| 82 | case grpc::StatusCode::UNAUTHENTICATED: |
| 83 | fprintf( |
| 84 | stderr, |
| 85 | "Request is unauthenticated. Make sure you have run SignInGaia.\n"); |
| 86 | break; |
| 87 | default: |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 92 | } // namespace |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 93 | |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 94 | namespace remoting { |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 95 | |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 96 | FtlSignalingPlayground::FtlSignalingPlayground() : weak_factory_(this) {} |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 97 | |
| 98 | FtlSignalingPlayground::~FtlSignalingPlayground() = default; |
| 99 | |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 100 | bool FtlSignalingPlayground::ShouldPrintHelp() { |
| 101 | return base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchNameHelp); |
| 102 | } |
| 103 | |
| 104 | void FtlSignalingPlayground::PrintHelp() { |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 105 | printf( |
| 106 | "Usage: %s [--code=<auth-code>] [--storage-path=<storage-path>] " |
| 107 | "[--username=<[email protected]>]\n", |
| 108 | base::CommandLine::ForCurrentProcess() |
| 109 | ->GetProgram() |
| 110 | .MaybeAsASCII() |
| 111 | .c_str()); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void FtlSignalingPlayground::StartAndAuthenticate() { |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 115 | DCHECK(!storage_); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 116 | DCHECK(!token_getter_factory_); |
| 117 | DCHECK(!token_getter_); |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 118 | DCHECK(!ftl_context_); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 119 | |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 120 | token_getter_factory_ = std::make_unique<TestOAuthTokenGetterFactory>(); |
| 121 | |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 122 | base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 123 | std::string username = cmd_line->GetSwitchValueASCII(kSwitchNameUsername); |
| 124 | base::FilePath storage_path = |
| 125 | cmd_line->GetSwitchValuePath(kSwitchNameStoragePath); |
| 126 | storage_ = test::TestTokenStorage::OnDisk(username, storage_path); |
| 127 | |
| 128 | std::string access_token = storage_->FetchAccessToken(); |
| 129 | if (access_token.empty()) { |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 130 | AuthenticateAndResetServices(); |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 131 | } else { |
| 132 | VLOG(0) << "Reusing access token: " << access_token; |
| 133 | token_getter_ = std::make_unique<FakeOAuthTokenGetter>( |
| 134 | OAuthTokenGetter::Status::SUCCESS, "[email protected]", |
| 135 | access_token); |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 136 | ResetServices(); |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 137 | } |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 138 | |
| 139 | StartLoop(); |
| 140 | } |
| 141 | |
| 142 | void FtlSignalingPlayground::StartLoop() { |
| 143 | while (true) { |
| 144 | printf( |
| 145 | "\nOptions:\n" |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 146 | " 1. SignInGaia\n" |
| 147 | " 2. GetIceServer\n" |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 148 | " 3. PullMessages\n" |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 149 | " 4. ReceiveMessages\n" |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 150 | " 5. SendMessage\n" |
| 151 | " 6. Quit\n\n" |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 152 | "Your choice [number]: "); |
| 153 | int choice = 0; |
| 154 | base::StringToInt(ReadString(), &choice); |
| 155 | base::RunLoop run_loop; |
| 156 | switch (choice) { |
| 157 | case 1: |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 158 | SignInGaia(run_loop.QuitClosure()); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 159 | break; |
| 160 | case 2: |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 161 | GetIceServer(run_loop.QuitClosure()); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 162 | break; |
| 163 | case 3: |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 164 | PullMessages(run_loop.QuitClosure()); |
| 165 | break; |
| 166 | case 4: |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 167 | StartReceivingMessages(run_loop.QuitWhenIdleClosure()); |
| 168 | break; |
| 169 | case 5: |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 170 | SendMessage(run_loop.QuitClosure()); |
| 171 | break; |
| 172 | case 6: |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 173 | return; |
| 174 | default: |
| 175 | fprintf(stderr, "Unknown option\n"); |
| 176 | continue; |
| 177 | } |
| 178 | run_loop.Run(); |
| 179 | } |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 180 | } |
| 181 | |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 182 | void FtlSignalingPlayground::ResetServices() { |
| 183 | ftl_context_ = std::make_unique<FtlGrpcContext>(token_getter_.get()); |
| 184 | peer_to_peer_stub_ = PeerToPeer::NewStub(ftl_context_->channel()); |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 185 | |
| 186 | message_subscription_.reset(); |
| 187 | messaging_client_ = std::make_unique<FtlMessagingClient>(ftl_context_.get()); |
| 188 | message_subscription_ = messaging_client_->RegisterMessageCallback( |
| 189 | base::BindRepeating(&FtlSignalingPlayground::OnMessageReceived, |
| 190 | weak_factory_.GetWeakPtr())); |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame^] | 191 | registration_manager_ = std::make_unique<FtlRegistrationManager>( |
| 192 | ftl_context_.get(), |
| 193 | std::make_unique<FtlDeviceIdProvider>(storage_.get())); |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void FtlSignalingPlayground::AuthenticateAndResetServices() { |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 197 | static const std::string read_auth_code_prompt = base::StringPrintf( |
| 198 | "Please authenticate at:\n\n" |
| 199 | " %s\n\n" |
| 200 | "Enter the auth code: ", |
| 201 | TestOAuthTokenGetterFactory::GetAuthorizationCodeUri().c_str()); |
| 202 | std::string auth_code = ReadStringFromCommandLineOrStdin( |
| 203 | kSwitchNameAuthCode, read_auth_code_prompt); |
| 204 | |
| 205 | // Make sure we don't try to reuse an auth code. |
| 206 | base::CommandLine::ForCurrentProcess()->RemoveSwitch(kSwitchNameAuthCode); |
| 207 | |
| 208 | // We can't get back the refresh token since we have first-party scope, so |
| 209 | // we are not trying to store it. |
| 210 | token_getter_ = token_getter_factory_->CreateFromIntermediateCredentials( |
| 211 | auth_code, |
| 212 | base::DoNothing::Repeatedly<const std::string&, const std::string&>()); |
| 213 | |
| 214 | // Get the access token so that we can reuse it for next time. |
| 215 | base::OnceClosure on_access_token_done = base::DoNothing::Once(); |
| 216 | base::RunLoop run_loop; |
| 217 | if (!base::RunLoop::IsRunningOnCurrentThread()) { |
| 218 | on_access_token_done = run_loop.QuitClosure(); |
| 219 | } |
| 220 | token_getter_->CallWithToken(base::BindOnce( |
| 221 | &FtlSignalingPlayground::OnAccessToken, weak_factory_.GetWeakPtr(), |
| 222 | std::move(on_access_token_done))); |
| 223 | if (!base::RunLoop::IsRunningOnCurrentThread()) { |
| 224 | run_loop.Run(); |
| 225 | } |
| 226 | |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 227 | ResetServices(); |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void FtlSignalingPlayground::OnAccessToken(base::OnceClosure on_done, |
| 231 | OAuthTokenGetter::Status status, |
| 232 | const std::string& user_email, |
| 233 | const std::string& access_token) { |
| 234 | DCHECK(status == OAuthTokenGetter::Status::SUCCESS); |
| 235 | VLOG(0) << "Received access_token: " << access_token; |
| 236 | storage_->StoreAccessToken(access_token); |
| 237 | std::move(on_done).Run(); |
| 238 | } |
| 239 | |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 240 | void FtlSignalingPlayground::GetIceServer(base::OnceClosure on_done) { |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 241 | DCHECK(peer_to_peer_stub_); |
| 242 | VLOG(0) << "Running GetIceServer..."; |
| 243 | ftl_context_->ExecuteRpc( |
| 244 | base::BindOnce(&PeerToPeer::Stub::AsyncGetICEServer, |
| 245 | base::Unretained(peer_to_peer_stub_.get())), |
| 246 | ftl::GetICEServerRequest(), |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 247 | base::BindOnce(&FtlSignalingPlayground::OnGetIceServerResponse, |
| 248 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 249 | } |
| 250 | |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 251 | void FtlSignalingPlayground::OnGetIceServerResponse( |
| 252 | base::OnceClosure on_done, |
Yuwei Huang | 81bbc1a | 2019-03-08 20:46:06 | [diff] [blame] | 253 | const grpc::Status& status, |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 254 | const ftl::GetICEServerResponse& response) { |
| 255 | if (status.ok()) { |
Yuwei Huang | 30bac9e | 2019-03-01 21:28:14 | [diff] [blame] | 256 | printf("Ice transport policy: %s\n", |
| 257 | response.ice_config().ice_transport_policy().c_str()); |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 258 | for (const ftl::ICEServerList& server : |
| 259 | response.ice_config().ice_servers()) { |
Yuwei Huang | 30bac9e | 2019-03-01 21:28:14 | [diff] [blame] | 260 | printf( |
| 261 | "ICE server:\n" |
| 262 | " hostname=%s\n" |
| 263 | " username=%s\n" |
| 264 | " credential=%s\n" |
| 265 | " max_rate_kbps=%" PRId64 "\n", |
| 266 | server.hostname().c_str(), server.username().c_str(), |
| 267 | server.credential().c_str(), server.max_rate_kbps()); |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 268 | for (const std::string& url : server.urls()) { |
Yuwei Huang | 30bac9e | 2019-03-01 21:28:14 | [diff] [blame] | 269 | printf(" url=%s\n", url.c_str()); |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } else { |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 273 | PrintGrpcStatusError(status); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 274 | } |
| 275 | std::move(on_done).Run(); |
| 276 | } |
| 277 | |
| 278 | void FtlSignalingPlayground::SignInGaia(base::OnceClosure on_done) { |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame^] | 279 | DCHECK(registration_manager_); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 280 | VLOG(0) << "Running SignInGaia..."; |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame^] | 281 | registration_manager_->SignInGaia( |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 282 | base::BindOnce(&FtlSignalingPlayground::OnSignInGaiaResponse, |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 283 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 284 | } |
| 285 | |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame^] | 286 | void FtlSignalingPlayground::OnSignInGaiaResponse(base::OnceClosure on_done, |
| 287 | const grpc::Status& status) { |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 288 | if (status.ok()) { |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 289 | std::string registration_id_base64; |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame^] | 290 | base::Base64Encode(registration_manager_->registration_id(), |
| 291 | ®istration_id_base64); |
| 292 | printf("Service signed in. registration_id(base64)=%s\n", |
| 293 | registration_id_base64.c_str()); |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 294 | } else { |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 295 | if (status.error_code() == grpc::StatusCode::UNAUTHENTICATED) { |
| 296 | VLOG(0) << "Grpc request failed to authenticate. " |
| 297 | << "Trying to reauthenticate..."; |
| 298 | AuthenticateAndResetServices(); |
| 299 | } else { |
| 300 | PrintGrpcStatusError(status); |
| 301 | } |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 302 | } |
| 303 | std::move(on_done).Run(); |
| 304 | } |
| 305 | |
| 306 | void FtlSignalingPlayground::PullMessages(base::OnceClosure on_done) { |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 307 | DCHECK(messaging_client_); |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 308 | VLOG(0) << "Running PullMessages..."; |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 309 | |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 310 | messaging_client_->PullMessages( |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 311 | base::BindOnce(&FtlSignalingPlayground::OnPullMessagesResponse, |
| 312 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
| 313 | } |
| 314 | |
| 315 | void FtlSignalingPlayground::OnPullMessagesResponse( |
| 316 | base::OnceClosure on_done, |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 317 | const grpc::Status& status) { |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 318 | if (!status.ok()) { |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 319 | PrintGrpcStatusError(status); |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 320 | } |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 321 | std::move(on_done).Run(); |
| 322 | } |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 323 | |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 324 | void FtlSignalingPlayground::SendMessage(base::OnceClosure on_done) { |
| 325 | DCHECK(messaging_client_); |
| 326 | VLOG(0) << "Running SendMessage..."; |
| 327 | |
| 328 | printf("Receiver ID: "); |
| 329 | std::string receiver_id = ReadString(); |
| 330 | |
| 331 | printf("Receiver registration ID (base64, optional): "); |
| 332 | std::string registration_id_base64 = ReadString(); |
| 333 | |
| 334 | std::string registration_id; |
| 335 | bool success = base::Base64Decode(registration_id_base64, ®istration_id); |
| 336 | if (!success) { |
| 337 | fprintf(stderr, "Your input can't be base64 decoded.\n"); |
| 338 | std::move(on_done).Run(); |
| 339 | return; |
| 340 | } |
| 341 | DoSendMessage(receiver_id, registration_id, std::move(on_done), true); |
| 342 | } |
| 343 | |
| 344 | void FtlSignalingPlayground::DoSendMessage(const std::string& receiver_id, |
| 345 | const std::string& registration_id, |
| 346 | base::OnceClosure on_done, |
| 347 | bool should_keep_running) { |
| 348 | if (!should_keep_running) { |
| 349 | std::move(on_done).Run(); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | printf("Message (enter nothing to quit): "); |
| 354 | std::string message = ReadString(); |
| 355 | |
| 356 | if (message.empty()) { |
| 357 | std::move(on_done).Run(); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | auto on_continue = base::BindOnce(&FtlSignalingPlayground::DoSendMessage, |
| 362 | weak_factory_.GetWeakPtr(), receiver_id, |
| 363 | registration_id, std::move(on_done)); |
| 364 | |
| 365 | messaging_client_->SendMessage( |
| 366 | receiver_id, registration_id, message, |
| 367 | base::BindOnce(&FtlSignalingPlayground::OnSendMessageResponse, |
| 368 | weak_factory_.GetWeakPtr(), std::move(on_continue))); |
| 369 | } |
| 370 | |
| 371 | void FtlSignalingPlayground::OnSendMessageResponse( |
| 372 | base::OnceCallback<void(bool)> on_continue, |
| 373 | const grpc::Status& status) { |
| 374 | if (!status.ok()) { |
| 375 | PrintGrpcStatusError(status); |
| 376 | } else { |
| 377 | printf("Message successfully sent.\n"); |
| 378 | } |
| 379 | std::move(on_continue).Run(status.ok()); |
| 380 | } |
| 381 | |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 382 | void FtlSignalingPlayground::StartReceivingMessages(base::OnceClosure on_done) { |
| 383 | VLOG(0) << "Running StartReceivingMessages..."; |
| 384 | messaging_client_->StartReceivingMessages( |
| 385 | base::BindOnce(&FtlSignalingPlayground::OnStartReceivingMessagesDone, |
| 386 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
| 387 | } |
| 388 | |
| 389 | void FtlSignalingPlayground::StopReceivingMessages(base::OnceClosure on_done) { |
| 390 | messaging_client_->StopReceivingMessages(); |
| 391 | std::move(on_done).Run(); |
| 392 | } |
| 393 | |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 394 | void FtlSignalingPlayground::OnMessageReceived(const std::string& sender_id, |
| 395 | const std::string& message) { |
| 396 | printf( |
| 397 | "Received message:\n" |
| 398 | " Sender ID=%s\n" |
| 399 | " Message=%s\n", |
| 400 | sender_id.c_str(), message.c_str()); |
| 401 | } |
| 402 | |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 403 | void FtlSignalingPlayground::OnStartReceivingMessagesDone( |
| 404 | base::OnceClosure on_done, |
| 405 | const grpc::Status& status) { |
| 406 | if (status.error_code() == grpc::StatusCode::CANCELLED) { |
| 407 | printf("ReceiveMessages stream canceled by client.\n"); |
| 408 | std::move(on_done).Run(); |
| 409 | return; |
| 410 | } |
| 411 | if (!status.ok()) { |
| 412 | PrintGrpcStatusError(status); |
| 413 | std::move(on_done).Run(); |
| 414 | return; |
| 415 | } |
| 416 | printf("Started receiving messages. Press enter to stop streaming...\n"); |
| 417 | WaitForEnterKey(base::BindOnce(&FtlSignalingPlayground::StopReceivingMessages, |
| 418 | weak_factory_.GetWeakPtr(), |
| 419 | std::move(on_done))); |
| 420 | } |
| 421 | |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 422 | } // namespace remoting |