OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 |
| 5 #include "tools/android/forwarder2/host_controller.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "tools/android/forwarder2/command.h" |
| 12 #include "tools/android/forwarder2/forwarder.h" |
| 13 #include "tools/android/forwarder2/socket.h" |
| 14 |
| 15 namespace forwarder { |
| 16 |
| 17 HostController::HostController(int device_port, |
| 18 const string& forward_to_host, |
| 19 int forward_to_host_port, |
| 20 int adb_port, |
| 21 int notifier_fd) |
| 22 : device_port_(device_port), |
| 23 forward_to_host_(forward_to_host), |
| 24 forward_to_host_port_(forward_to_host_port), |
| 25 adb_port_(adb_port), |
| 26 notifier_fd_(notifier_fd) { |
| 27 adb_control_socket_.set_notifier_fd(notifier_fd); |
| 28 } |
| 29 |
| 30 HostController::~HostController() { |
| 31 } |
| 32 |
| 33 bool HostController::StartForwarder( |
| 34 scoped_ptr<Socket> host_server_data_socket) { |
| 35 scoped_ptr<Socket> adb_data_socket(new Socket); |
| 36 if (!adb_data_socket->ConnectTcp("", adb_port_)) { |
| 37 LOG(ERROR) << "Could not Connect AdbDataSocket on port: " |
| 38 << adb_port_; |
| 39 return false; |
| 40 } |
| 41 // Open the Adb data connection, and send a command with the |
| 42 // |device_forward_port| as a way for the device to identify the connection. |
| 43 SendCommand(command::DATA_CONNECTION, |
| 44 device_port_, |
| 45 adb_data_socket.get()); |
| 46 |
| 47 // Check that the device received the new Adb Data Connection. Observe that |
| 48 // this check is done through the |adb_control_socket_| that is handled in the |
| 49 // DeviceListener thread just after the call to WaitForAdbDataSocket(). |
| 50 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, |
| 51 &adb_control_socket_)) { |
| 52 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; |
| 53 host_server_data_socket->Close(); |
| 54 adb_data_socket->Close(); |
| 55 return false; |
| 56 } |
| 57 Forwarder* forwarder = new Forwarder(host_server_data_socket.Pass(), |
| 58 adb_data_socket.Pass()); |
| 59 // Forwarder object will self delete after returning from the Run() call. |
| 60 forwarder->Start(); |
| 61 return true; |
| 62 } |
| 63 |
| 64 void HostController::Run() { |
| 65 if (!adb_control_socket_.ConnectTcp("", adb_port_)) { |
| 66 LOG(ERROR) << "Could not Connect HostController socket on port: " |
| 67 << adb_port_; |
| 68 return; |
| 69 } |
| 70 // Send the command to the device start listening to the "device_forward_port" |
| 71 SendCommand(command::LISTEN, device_port_, &adb_control_socket_); |
| 72 if (!ReceivedCommand(command::BIND_SUCCESS, &adb_control_socket_)) { |
| 73 LOG(ERROR) << "Device binding error using port " << device_port_; |
| 74 adb_control_socket_.Close(); |
| 75 return; |
| 76 } |
| 77 while (true) { |
| 78 if (!ReceivedCommand(command::ACCEPT_SUCCESS, |
| 79 &adb_control_socket_)) { |
| 80 LOG(ERROR) << "Device socket accepting error using port " << device_port_; |
| 81 break; |
| 82 } |
| 83 // Try to connect to host server. |
| 84 scoped_ptr<Socket> host_server_data_socket(new Socket); |
| 85 if (!host_server_data_socket->ConnectTcp( |
| 86 forward_to_host_, forward_to_host_port_)) { |
| 87 LOG(ERROR) << "Could not Connect HostServerData socket on port: " |
| 88 << forward_to_host_port_; |
| 89 SendCommand(command::HOST_SERVER_ERROR, |
| 90 device_port_, |
| 91 &adb_control_socket_); |
| 92 if (ReceivedCommand(command::ACK, &adb_control_socket_)) { |
| 93 // It can continue if the host forwarder could not connect to the host |
| 94 // server but the device acknowledged that, so that the device could |
| 95 // re-try later. |
| 96 continue; |
| 97 } else { |
| 98 break; |
| 99 } |
| 100 } |
| 101 SendCommand(command::HOST_SERVER_SUCCESS, |
| 102 device_port_, |
| 103 &adb_control_socket_); |
| 104 StartForwarder(host_server_data_socket.Pass()); |
| 105 } |
| 106 adb_control_socket_.Close(); |
| 107 } |
| 108 |
| 109 } // namespace forwarder |
OLD | NEW |