| 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: " |
| 39 << adb_port_; | 46 << adb_port_; |
| 40 return; | 47 return; |
| 41 } | 48 } |
| 42 // Open the Adb data connection, and send a command with the | 49 // 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. | 50 // |device_forward_port| as a way for the device to identify the connection. |
| 44 SendCommand(command::DATA_CONNECTION, | 51 SendCommand(command::DATA_CONNECTION, |
| 45 device_port_, | 52 device_port_, |
| 46 adb_data_socket.get()); | 53 adb_data_socket.get()); |
| 47 | 54 |
| 48 // Check that the device received the new Adb Data Connection. Observe that | 55 // 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 | 56 // this check is done through the |adb_control_socket_| that is handled in the |
| 50 // DeviceListener thread just after the call to WaitForAdbDataSocket(). | 57 // DeviceListener thread just after the call to WaitForAdbDataSocket(). |
| 51 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, | 58 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, |
| 52 &adb_control_socket_)) { | 59 &adb_control_socket_)) { |
| 53 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; | 60 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; |
| 54 host_server_data_socket->Close(); | 61 host_server_data_socket->Close(); |
| 55 adb_data_socket->Close(); | 62 adb_data_socket->Close(); |
| 56 return; | 63 return; |
| 57 } | 64 } |
| 58 Forwarder* forwarder = new Forwarder(host_server_data_socket.Pass(), | 65 Forwarder* forwarder = new Forwarder(host_server_data_socket.Pass(), |
| 59 adb_data_socket.Pass()); | 66 adb_data_socket.Pass()); |
| 60 // Forwarder object will self delete after returning. | 67 // Forwarder object will self delete after returning. |
| 61 forwarder->Start(); | 68 forwarder->Start(); |
| 62 } | 69 } |
| 63 | 70 |
| 71 scoped_ptr<Socket> HostController::CreateSocket() { |
| 72 scoped_ptr<Socket> socket(new Socket()); |
| 73 socket->AddEventFd(global_exit_notifier_fd_); |
| 74 socket->AddEventFd(delete_controller_notifier_.receiver_fd()); |
| 75 return socket.Pass(); |
| 76 } |
| 77 |
| 64 bool HostController::Connect() { | 78 bool HostController::Connect() { |
| 65 if (!adb_control_socket_.ConnectTcp("", adb_port_)) { | 79 if (!adb_control_socket_.ConnectTcp("", adb_port_)) { |
| 66 LOG(ERROR) << "Could not connect HostController socket on port: " | 80 LOG(ERROR) << "Could not connect HostController socket on port: " |
| 67 << adb_port_; | 81 << adb_port_; |
| 68 return false; | 82 return false; |
| 69 } | 83 } |
| 70 // Send the command to the device start listening to the "device_forward_port" | 84 // Send the command to the device start listening to the "device_forward_port" |
| 71 bool send_command_success = SendCommand( | 85 bool send_command_success = SendCommand( |
| 72 command::LISTEN, device_port_, &adb_control_socket_); | 86 command::LISTEN, device_port_, &adb_control_socket_); |
| 73 CHECK(send_command_success); | 87 CHECK(send_command_success); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 84 device_port_ = device_port_allocated; | 98 device_port_ = device_port_allocated; |
| 85 ready_ = true; | 99 ready_ = true; |
| 86 return true; | 100 return true; |
| 87 } | 101 } |
| 88 | 102 |
| 89 void HostController::Run() { | 103 void HostController::Run() { |
| 90 CHECK(ready_) << "HostController not ready. Must call Connect() first."; | 104 CHECK(ready_) << "HostController not ready. Must call Connect() first."; |
| 91 while (true) { | 105 while (true) { |
| 92 if (!ReceivedCommand(command::ACCEPT_SUCCESS, | 106 if (!ReceivedCommand(command::ACCEPT_SUCCESS, |
| 93 &adb_control_socket_)) { | 107 &adb_control_socket_)) { |
| 108 if (adb_control_socket_.DidReceiveEventOnFd( |
| 109 delete_controller_notifier_.receiver_fd())) { |
| 110 // The instance is being deleted. The control socket will be closed and |
| 111 // the device controller will see that as an error (that it will recover |
| 112 // from properly). TODO(pliard): tell the device controller that this is |
| 113 // a graceful (i.e. user-intended) shutdown rather than an error. |
| 114 break; |
| 115 } |
| 94 // TODO(pliard): This can also happen if device_forwarder was | 116 // TODO(pliard): This can also happen if device_forwarder was |
| 95 // intentionally killed before host_forwarder. In that case, | 117 // intentionally killed before host_forwarder. In that case, |
| 96 // device_forwarder should send a notification to the host. Currently the | 118 // 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 | 119 // error message below is always emitted to the log file although this is |
| 98 // not necessarily an error. | 120 // not necessarily an error. |
| 99 LOG(ERROR) << "Device socket error on accepting using port " | 121 LOG(ERROR) << "Device socket error on accepting using port " |
| 100 << device_port_; | 122 << device_port_; |
| 101 break; | 123 break; |
| 102 } | 124 } |
| 103 // Try to connect to host server. | 125 // Try to connect to host server. |
| 104 scoped_ptr<Socket> host_server_data_socket(new Socket); | 126 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( | 127 if (!host_server_data_socket->ConnectTcp( |
| 107 forward_to_host_, forward_to_host_port_)) { | 128 forward_to_host_, forward_to_host_port_)) { |
| 108 LOG(ERROR) << "Could not Connect HostServerData socket on port: " | 129 LOG(ERROR) << "Could not Connect HostServerData socket on port: " |
| 109 << forward_to_host_port_; | 130 << forward_to_host_port_; |
| 110 SendCommand(command::HOST_SERVER_ERROR, | 131 SendCommand(command::HOST_SERVER_ERROR, |
| 111 device_port_, | 132 device_port_, |
| 112 &adb_control_socket_); | 133 &adb_control_socket_); |
| 113 if (ReceivedCommand(command::ACK, &adb_control_socket_)) { | 134 if (ReceivedCommand(command::ACK, &adb_control_socket_)) { |
| 114 // It can continue if the host forwarder could not connect to the host | 135 // 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 | 136 // server but the device acknowledged that, so that the device could |
| 116 // re-try later. | 137 // re-try later. |
| 117 continue; | 138 continue; |
| 118 } else { | 139 } else { |
| 119 break; | 140 break; |
| 120 } | 141 } |
| 121 } | 142 } |
| 122 SendCommand(command::HOST_SERVER_SUCCESS, | 143 SendCommand(command::HOST_SERVER_SUCCESS, |
| 123 device_port_, | 144 device_port_, |
| 124 &adb_control_socket_); | 145 &adb_control_socket_); |
| 125 StartForwarder(host_server_data_socket.Pass()); | 146 StartForwarder(host_server_data_socket.Pass()); |
| 126 } | 147 } |
| 127 adb_control_socket_.Close(); | 148 adb_control_socket_.Close(); |
| 128 } | 149 } |
| 129 | 150 |
| 130 } // namespace forwarder2 | 151 } // namespace forwarder2 |
| OLD | NEW |