| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/android/forwarder2/host_controller.h" | 5 #include "tools/android/forwarder2/host_controller.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "tools/android/forwarder2/command.h" | 11 #include "tools/android/forwarder2/command.h" |
| 12 #include "tools/android/forwarder2/forwarder.h" | 12 #include "tools/android/forwarder2/forwarder.h" |
| 13 #include "tools/android/forwarder2/socket.h" | 13 #include "tools/android/forwarder2/socket.h" |
| 14 | 14 |
| 15 namespace forwarder2 { | 15 namespace forwarder2 { |
| 16 | 16 |
| 17 HostController::HostController(int device_port, | 17 HostController::HostController(int device_port, |
| 18 const std::string& forward_to_host, | 18 const std::string& forward_to_host, |
| 19 int forward_to_host_port, | 19 int forward_to_host_port, |
| 20 int adb_port, | 20 int adb_port, |
| 21 int exit_notifier_fd) | 21 int exit_notifier_fd) |
| 22 : device_port_(device_port), | 22 : device_port_(device_port), |
| 23 forward_to_host_(forward_to_host), | 23 forward_to_host_(forward_to_host), |
| 24 forward_to_host_port_(forward_to_host_port), | 24 forward_to_host_port_(forward_to_host_port), |
| 25 adb_port_(adb_port), | 25 adb_port_(adb_port), |
| 26 exit_notifier_fd_(exit_notifier_fd), | 26 global_exit_notifier_fd_(exit_notifier_fd), |
| 27 ready_(false) { | 27 ready_(false) { |
| 28 adb_control_socket_.set_exit_notifier_fd(exit_notifier_fd); | 28 adb_control_socket_.AddEventFd(global_exit_notifier_fd_); |
| 29 adb_control_socket_.AddEventFd(delete_controller_notifier_.receiver_fd()); |
| 29 } | 30 } |
| 30 | 31 |
| 31 HostController::~HostController() { | 32 HostController::~HostController() { |
| 33 delete_controller_notifier_.Notify(); |
| 34 Join(); |
| 35 // Note that the Forwarder instance (that also received a delete notification) |
| 36 // might still be running on its own thread at this point. This is not a |
| 37 // problem since it will self-delete once the socket that it is operating on |
| 38 // is closed. |
| 32 } | 39 } |
| 33 | 40 |
| 34 void HostController::StartForwarder( | 41 void HostController::StartForwarder( |
| 35 scoped_ptr<Socket> host_server_data_socket) { | 42 scoped_ptr<Socket> host_server_data_socket) { |
| 36 scoped_ptr<Socket> adb_data_socket(new Socket); | 43 scoped_ptr<Socket> adb_data_socket(CreateSocket()); |
| 37 if (!adb_data_socket->ConnectTcp("", adb_port_)) { | 44 if (!adb_data_socket->ConnectTcp("", adb_port_)) { |
| 38 LOG(ERROR) << "Could not connect AdbDataSocket on port: " | 45 LOG(ERROR) << "Could not connect AdbDataSocket on port: " << adb_port_; |
| 39 << adb_port_; | |
| 40 return; | 46 return; |
| 41 } | 47 } |
| 42 // Open the Adb data connection, and send a command with the | 48 // Open the Adb data connection, and send a command with the |
| 43 // |device_forward_port| as a way for the device to identify the connection. | 49 // |device_forward_port| as a way for the device to identify the connection. |
| 44 SendCommand(command::DATA_CONNECTION, | 50 SendCommand(command::DATA_CONNECTION, |
| 45 device_port_, | 51 device_port_, |
| 46 adb_data_socket.get()); | 52 adb_data_socket.get()); |
| 47 | 53 |
| 48 // Check that the device received the new Adb Data Connection. Observe that | 54 // Check that the device received the new Adb Data Connection. Observe that |
| 49 // this check is done through the |adb_control_socket_| that is handled in the | 55 // this check is done through the |adb_control_socket_| that is handled in the |
| 50 // DeviceListener thread just after the call to WaitForAdbDataSocket(). | 56 // DeviceListener thread just after the call to WaitForAdbDataSocket(). |
| 51 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, | 57 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, |
| 52 &adb_control_socket_)) { | 58 &adb_control_socket_)) { |
| 53 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; | 59 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; |
| 54 host_server_data_socket->Close(); | 60 host_server_data_socket->Close(); |
| 55 adb_data_socket->Close(); | 61 adb_data_socket->Close(); |
| 56 return; | 62 return; |
| 57 } | 63 } |
| 58 Forwarder* forwarder = new Forwarder(host_server_data_socket.Pass(), | 64 Forwarder* forwarder = new Forwarder(host_server_data_socket.Pass(), |
| 59 adb_data_socket.Pass()); | 65 adb_data_socket.Pass()); |
| 60 // Forwarder object will self delete after returning. | 66 // Forwarder object will self delete after returning. |
| 61 forwarder->Start(); | 67 forwarder->Start(); |
| 62 } | 68 } |
| 63 | 69 |
| 70 scoped_ptr<Socket> HostController::CreateSocket() { |
| 71 scoped_ptr<Socket> socket(new Socket()); |
| 72 socket->AddEventFd(global_exit_notifier_fd_); |
| 73 socket->AddEventFd(delete_controller_notifier_.receiver_fd()); |
| 74 return socket.Pass(); |
| 75 } |
| 76 |
| 64 bool HostController::Connect() { | 77 bool HostController::Connect() { |
| 65 if (!adb_control_socket_.ConnectTcp("", adb_port_)) { | 78 if (!adb_control_socket_.ConnectTcp("", adb_port_)) { |
| 66 LOG(ERROR) << "Could not connect HostController socket on port: " | 79 LOG(ERROR) << "Could not connect HostController socket on port: " |
| 67 << adb_port_; | 80 << adb_port_; |
| 68 return false; | 81 return false; |
| 69 } | 82 } |
| 70 // Send the command to the device start listening to the "device_forward_port" | 83 // Send the command to the device start listening to the "device_forward_port" |
| 71 bool send_command_success = SendCommand( | 84 bool send_command_success = SendCommand( |
| 72 command::LISTEN, device_port_, &adb_control_socket_); | 85 command::LISTEN, device_port_, &adb_control_socket_); |
| 73 CHECK(send_command_success); | 86 CHECK(send_command_success); |
| 74 int device_port_allocated; | 87 int device_port_allocated; |
| 75 command::Type command; | 88 command::Type command; |
| 76 if (!ReadCommand(&adb_control_socket_, &device_port_allocated, &command) || | 89 if (!ReadCommand(&adb_control_socket_, &device_port_allocated, &command) || |
| 77 command != command::BIND_SUCCESS) { | 90 command != command::BIND_SUCCESS) { |
| 78 LOG(ERROR) << "Device binding error using port " << device_port_; | 91 LOG(ERROR) << "Device binding error using port " << device_port_; |
| 79 adb_control_socket_.Close(); | 92 adb_control_socket_.Close(); |
| 80 return false; | 93 return false; |
| 81 } | 94 } |
| 82 // When doing dynamically allocation of port, we get the port from the | 95 // When doing dynamically allocation of port, we get the port from the |
| 83 // BIND_SUCCESS command we received above. | 96 // BIND_SUCCESS command we received above. |
| 84 device_port_ = device_port_allocated; | 97 device_port_ = device_port_allocated; |
| 85 ready_ = true; | 98 ready_ = true; |
| 86 return true; | 99 return true; |
| 87 } | 100 } |
| 88 | 101 |
| 89 void HostController::Run() { | 102 void HostController::Run() { |
| 90 CHECK(ready_) << "HostController not ready. Must call Connect() first."; | 103 CHECK(ready_) << "HostController not ready. Must call Connect() first."; |
| 91 while (true) { | 104 while (true) { |
| 92 if (!ReceivedCommand(command::ACCEPT_SUCCESS, | 105 if (!ReceivedCommand(command::ACCEPT_SUCCESS, &adb_control_socket_)) { |
| 93 &adb_control_socket_)) { | 106 if (adb_control_socket_.DidReceiveEventOnFd( |
| 107 delete_controller_notifier_.receiver_fd())) { |
| 108 // The instance is being deleted. The control socket will be closed and |
| 109 // the device controller will see that as an error (that it will recover |
| 110 // from properly). TODO(pliard): tell the device controller that this is |
| 111 // a graceful (i.e. user-intended) shutdown rather than an error. |
| 112 break; |
| 113 } |
| 94 // TODO(pliard): This can also happen if device_forwarder was | 114 // TODO(pliard): This can also happen if device_forwarder was |
| 95 // intentionally killed before host_forwarder. In that case, | 115 // intentionally killed before host_forwarder. In that case, |
| 96 // device_forwarder should send a notification to the host. Currently the | 116 // device_forwarder should send a notification to the host. Currently the |
| 97 // error message below is always emitted to the log file although this is | 117 // error message below is always emitted to the log file although this is |
| 98 // not necessarily an error. | 118 // not necessarily an error. |
| 99 LOG(ERROR) << "Device socket error on accepting using port " | 119 LOG(ERROR) << "Device socket error on accepting using port " |
| 100 << device_port_; | 120 << device_port_; |
| 101 break; | 121 break; |
| 102 } | 122 } |
| 103 // Try to connect to host server. | 123 // Try to connect to host server. |
| 104 scoped_ptr<Socket> host_server_data_socket(new Socket); | 124 scoped_ptr<Socket> host_server_data_socket(CreateSocket()); |
| 105 host_server_data_socket->set_exit_notifier_fd(exit_notifier_fd_); | |
| 106 if (!host_server_data_socket->ConnectTcp( | 125 if (!host_server_data_socket->ConnectTcp( |
| 107 forward_to_host_, forward_to_host_port_)) { | 126 forward_to_host_, forward_to_host_port_)) { |
| 108 LOG(ERROR) << "Could not Connect HostServerData socket on port: " | 127 LOG(ERROR) << "Could not Connect HostServerData socket on port: " |
| 109 << forward_to_host_port_; | 128 << forward_to_host_port_; |
| 110 SendCommand(command::HOST_SERVER_ERROR, | 129 SendCommand(command::HOST_SERVER_ERROR, |
| 111 device_port_, | 130 device_port_, |
| 112 &adb_control_socket_); | 131 &adb_control_socket_); |
| 113 if (ReceivedCommand(command::ACK, &adb_control_socket_)) { | 132 if (ReceivedCommand(command::ACK, &adb_control_socket_)) { |
| 114 // It can continue if the host forwarder could not connect to the host | 133 // It can continue if the host forwarder could not connect to the host |
| 115 // server but the device acknowledged that, so that the device could | 134 // server but the device acknowledged that, so that the device could |
| 116 // re-try later. | 135 // re-try later. |
| 117 continue; | 136 continue; |
| 118 } else { | 137 } else { |
| 119 break; | 138 break; |
| 120 } | 139 } |
| 121 } | 140 } |
| 122 SendCommand(command::HOST_SERVER_SUCCESS, | 141 SendCommand(command::HOST_SERVER_SUCCESS, |
| 123 device_port_, | 142 device_port_, |
| 124 &adb_control_socket_); | 143 &adb_control_socket_); |
| 125 StartForwarder(host_server_data_socket.Pass()); | 144 StartForwarder(host_server_data_socket.Pass()); |
| 126 } | 145 } |
| 127 adb_control_socket_.Close(); | 146 adb_control_socket_.Close(); |
| 128 } | 147 } |
| 129 | 148 |
| 130 } // namespace forwarder2 | 149 } // namespace forwarder2 |
| OLD | NEW |