Chromium Code Reviews| 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <signal.h> | 6 #include <signal.h> |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 | 8 |
| 9 #include <cmath> | |
| 9 #include <cstdio> | 10 #include <cstdio> |
| 10 #include <cstring> | 11 #include <cstring> |
| 11 #include <string> | 12 #include <string> |
| 13 #include <utility> | |
| 12 #include <vector> | 14 #include <vector> |
| 13 | 15 |
| 14 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 15 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
| 16 #include "base/file_util.h" | 18 #include "base/file_util.h" |
| 17 #include "base/files/file_path.h" | 19 #include "base/files/file_path.h" |
| 20 #include "base/hash_tables.h" | |
| 18 #include "base/logging.h" | 21 #include "base/logging.h" |
| 22 #include "base/memory/linked_ptr.h" | |
| 19 #include "base/memory/scoped_vector.h" | 23 #include "base/memory/scoped_vector.h" |
| 20 #include "base/posix/eintr_wrapper.h" | 24 #include "base/posix/eintr_wrapper.h" |
| 21 #include "base/safe_strerror_posix.h" | 25 #include "base/safe_strerror_posix.h" |
| 22 #include "base/string_number_conversions.h" | 26 #include "base/string_number_conversions.h" |
| 23 #include "base/string_util.h" | 27 #include "base/string_util.h" |
| 24 #include "base/stringprintf.h" | 28 #include "base/stringprintf.h" |
| 25 #include "base/strings/string_piece.h" | 29 #include "base/strings/string_piece.h" |
| 26 #include "base/strings/string_split.h" | 30 #include "base/strings/string_split.h" |
| 27 #include "tools/android/forwarder2/common.h" | 31 #include "tools/android/forwarder2/common.h" |
| 28 #include "tools/android/forwarder2/daemon.h" | 32 #include "tools/android/forwarder2/daemon.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 DCHECK(!g_notifier); | 119 DCHECK(!g_notifier); |
| 116 g_notifier = new PipeNotifier(); | 120 g_notifier = new PipeNotifier(); |
| 117 signal(SIGTERM, KillHandler); | 121 signal(SIGTERM, KillHandler); |
| 118 signal(SIGINT, KillHandler); | 122 signal(SIGINT, KillHandler); |
| 119 } | 123 } |
| 120 | 124 |
| 121 virtual void OnClientConnected(scoped_ptr<Socket> client_socket) OVERRIDE { | 125 virtual void OnClientConnected(scoped_ptr<Socket> client_socket) OVERRIDE { |
| 122 char buf[kBufSize]; | 126 char buf[kBufSize]; |
| 123 const int bytes_read = client_socket->Read(buf, sizeof(buf)); | 127 const int bytes_read = client_socket->Read(buf, sizeof(buf)); |
| 124 if (bytes_read <= 0) { | 128 if (bytes_read <= 0) { |
| 125 if (client_socket->exited()) | 129 if (client_socket->DidReceiveEvent()) |
| 126 return; | 130 return; |
| 127 PError("Read()"); | 131 PError("Read()"); |
| 128 has_failed_ = true; | 132 has_failed_ = true; |
| 129 return; | 133 return; |
| 130 } | 134 } |
| 131 const std::string command(buf, bytes_read); | 135 const std::string command(buf, bytes_read); |
| 132 int adb_port = 0; | 136 int adb_port = 0; |
| 133 int device_port = 0; | 137 int device_port = 0; |
| 134 std::string forward_to_host; | 138 std::string forward_to_host; |
| 135 int forward_to_port = 0; | 139 int forward_to_port = 0; |
| 136 const bool succeeded = ParseForwardCommand( | 140 const bool succeeded = ParseForwardCommand( |
| 137 command, &adb_port, &device_port, &forward_to_host, &forward_to_port); | 141 command, &adb_port, &device_port, &forward_to_host, &forward_to_port); |
| 138 if (!succeeded) { | 142 if (!succeeded) { |
| 139 has_failed_ = true; | 143 has_failed_ = true; |
| 140 client_socket->WriteString( | 144 const std::string msg = base::StringPrintf( |
| 141 base::StringPrintf("ERROR: Could not parse forward command '%s'", | 145 "ERROR: Could not parse forward command '%s'", command.c_str()); |
| 142 command.c_str())); | 146 SendMessage(msg, client_socket.get()); |
| 143 return; | 147 return; |
| 144 } | 148 } |
| 149 if (device_port < 0) { | |
| 150 // Remove the previously created host controller. | |
| 151 const std::string controller_key = MakeHostControllerMapKey( | |
| 152 adb_port, std::abs(device_port)); | |
|
digit1
2013/05/15 10:13:46
I believe -device_port might work as well since yo
Philippe
2013/05/15 11:40:50
Done.
| |
| 153 const HostControllerMap::size_type removed_elements = controllers_.erase( | |
| 154 controller_key); | |
| 155 SendMessage( | |
| 156 !removed_elements ? "ERROR: could not unmap port" : "OK", | |
| 157 client_socket.get()); | |
| 158 return; | |
| 159 } | |
| 160 // Create a new host controller. | |
| 145 scoped_ptr<HostController> host_controller( | 161 scoped_ptr<HostController> host_controller( |
| 146 new HostController(device_port, forward_to_host, forward_to_port, | 162 new HostController(device_port, forward_to_host, forward_to_port, |
| 147 adb_port, GetExitNotifierFD())); | 163 adb_port, GetExitNotifierFD())); |
| 148 if (!host_controller->Connect()) { | 164 if (!host_controller->Connect()) { |
| 149 has_failed_ = true; | 165 has_failed_ = true; |
| 150 client_socket->WriteString("ERROR: Connection to device failed."); | 166 SendMessage("ERROR: Connection to device failed.", client_socket.get()); |
| 151 return; | 167 return; |
| 152 } | 168 } |
| 153 // Get the current allocated port. | 169 // Get the current allocated port. |
| 154 device_port = host_controller->device_port(); | 170 device_port = host_controller->device_port(); |
| 155 LOG(INFO) << "Forwarding device port " << device_port << " to host " | 171 LOG(INFO) << "Forwarding device port " << device_port << " to host " |
| 156 << forward_to_host << ":" << forward_to_port; | 172 << forward_to_host << ":" << forward_to_port; |
| 157 if (!client_socket->WriteString( | 173 const std::string msg = base::StringPrintf( |
| 158 base::StringPrintf("%d:%d", device_port, forward_to_port))) { | 174 "%d:%d", device_port, forward_to_port); |
| 159 has_failed_ = true; | 175 if (!SendMessage(msg, client_socket.get())) |
| 160 return; | 176 return; |
| 161 } | |
| 162 host_controller->Start(); | 177 host_controller->Start(); |
| 163 controllers_.push_back(host_controller.release()); | 178 const std::string controller_key = MakeHostControllerMapKey( |
| 179 adb_port, device_port); | |
| 180 controllers_.insert( | |
| 181 std::make_pair(controller_key, | |
| 182 linked_ptr<HostController>(host_controller.release()))); | |
| 164 } | 183 } |
| 165 | 184 |
| 166 virtual void OnServerExited() OVERRIDE { | 185 virtual void OnServerExited() OVERRIDE { |
| 167 for (int i = 0; i < controllers_.size(); ++i) | 186 for (HostControllerMap::iterator it = controllers_.begin(); |
| 168 controllers_[i]->Join(); | 187 it != controllers_.end(); ++it) { |
| 188 linked_ptr<HostController> host_controller = it->second; | |
| 189 host_controller->Join(); | |
| 190 } | |
| 169 if (controllers_.size() == 0) { | 191 if (controllers_.size() == 0) { |
| 170 LOG(ERROR) << "No forwarder servers could be started. Exiting."; | 192 LOG(ERROR) << "No forwarder servers could be started. Exiting."; |
| 171 has_failed_ = true; | 193 has_failed_ = true; |
| 172 } | 194 } |
| 173 } | 195 } |
| 174 | 196 |
| 175 private: | 197 private: |
| 176 ScopedVector<HostController> controllers_; | 198 typedef base::hash_map< |
| 199 std::string, linked_ptr<HostController> > HostControllerMap; | |
| 200 | |
| 201 static std::string MakeHostControllerMapKey(int adb_port, int device_port) { | |
| 202 return base::StringPrintf("%d:%d", adb_port, device_port); | |
| 203 } | |
| 204 | |
| 205 bool SendMessage(const std::string& msg, Socket* client_socket) { | |
| 206 bool result = client_socket->WriteString(msg); | |
| 207 DCHECK(result); | |
| 208 if (!result) | |
| 209 has_failed_ = true; | |
| 210 return result; | |
| 211 } | |
| 212 | |
| 213 HostControllerMap controllers_; | |
| 177 bool has_failed_; | 214 bool has_failed_; |
| 178 | 215 |
| 179 DISALLOW_COPY_AND_ASSIGN(ServerDelegate); | 216 DISALLOW_COPY_AND_ASSIGN(ServerDelegate); |
| 180 }; | 217 }; |
| 181 | 218 |
| 182 class ClientDelegate : public Daemon::ClientDelegate { | 219 class ClientDelegate : public Daemon::ClientDelegate { |
| 183 public: | 220 public: |
| 184 ClientDelegate(const std::string& forward_command) | 221 ClientDelegate(const std::string& forward_command) |
| 185 : forward_command_(forward_command), | 222 : forward_command_(forward_command), |
| 186 has_failed_(false) { | 223 has_failed_(false) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 207 printf("%s\n", buf); | 244 printf("%s\n", buf); |
| 208 } | 245 } |
| 209 | 246 |
| 210 private: | 247 private: |
| 211 const std::string forward_command_; | 248 const std::string forward_command_; |
| 212 bool has_failed_; | 249 bool has_failed_; |
| 213 }; | 250 }; |
| 214 | 251 |
| 215 void PrintUsage(const char* program_name) { | 252 void PrintUsage(const char* program_name) { |
| 216 LOG(ERROR) << program_name << " adb_port:from_port:to_port:to_host\n" | 253 LOG(ERROR) << program_name << " adb_port:from_port:to_port:to_host\n" |
| 217 "<adb port> is the TCP port Adb is configured to forward to."; | 254 "<adb port> is the TCP port Adb is configured to forward to.\n" |
| 255 "Note that <from_port> can be unmapped by making it negative."; | |
| 218 } | 256 } |
| 219 | 257 |
| 220 int RunHostForwarder(int argc, char** argv) { | 258 int RunHostForwarder(int argc, char** argv) { |
| 221 if (!CommandLine::Init(argc, argv)) { | 259 if (!CommandLine::Init(argc, argv)) { |
| 222 LOG(ERROR) << "Could not initialize command line"; | 260 LOG(ERROR) << "Could not initialize command line"; |
| 223 return 1; | 261 return 1; |
| 224 } | 262 } |
| 225 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 263 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 226 const char* command = NULL; | 264 const char* command = NULL; |
| 227 int adb_port = 0; | 265 int adb_port = 0; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 254 | 292 |
| 255 return client_delegate.has_failed() || daemon_delegate.has_failed(); | 293 return client_delegate.has_failed() || daemon_delegate.has_failed(); |
| 256 } | 294 } |
| 257 | 295 |
| 258 } // namespace | 296 } // namespace |
| 259 } // namespace forwarder2 | 297 } // namespace forwarder2 |
| 260 | 298 |
| 261 int main(int argc, char** argv) { | 299 int main(int argc, char** argv) { |
| 262 return forwarder2::RunHostForwarder(argc, argv); | 300 return forwarder2::RunHostForwarder(argc, argv); |
| 263 } | 301 } |
| OLD | NEW |