Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(283)

Side by Side Diff: tools/android/forwarder2/host_forwarder_main.cc

Issue 15008004: Add device port unmapping support to forwarder2. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address David's comments Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/android/forwarder2/host_controller.cc ('k') | tools/android/forwarder2/socket.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <cstdio> 9 #include <cstdio>
10 #include <cstring> 10 #include <cstring>
11 #include <string> 11 #include <string>
12 #include <utility>
12 #include <vector> 13 #include <vector>
13 14
14 #include "base/command_line.h" 15 #include "base/command_line.h"
15 #include "base/compiler_specific.h" 16 #include "base/compiler_specific.h"
16 #include "base/file_util.h" 17 #include "base/file_util.h"
17 #include "base/files/file_path.h" 18 #include "base/files/file_path.h"
19 #include "base/hash_tables.h"
18 #include "base/logging.h" 20 #include "base/logging.h"
21 #include "base/memory/linked_ptr.h"
19 #include "base/memory/scoped_vector.h" 22 #include "base/memory/scoped_vector.h"
20 #include "base/posix/eintr_wrapper.h" 23 #include "base/posix/eintr_wrapper.h"
21 #include "base/safe_strerror_posix.h" 24 #include "base/safe_strerror_posix.h"
22 #include "base/string_number_conversions.h" 25 #include "base/string_number_conversions.h"
23 #include "base/string_util.h" 26 #include "base/string_util.h"
24 #include "base/stringprintf.h" 27 #include "base/stringprintf.h"
25 #include "base/strings/string_piece.h" 28 #include "base/strings/string_piece.h"
26 #include "base/strings/string_split.h" 29 #include "base/strings/string_split.h"
27 #include "tools/android/forwarder2/common.h" 30 #include "tools/android/forwarder2/common.h"
28 #include "tools/android/forwarder2/daemon.h" 31 #include "tools/android/forwarder2/daemon.h"
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 DCHECK(!g_notifier); 118 DCHECK(!g_notifier);
116 g_notifier = new PipeNotifier(); 119 g_notifier = new PipeNotifier();
117 signal(SIGTERM, KillHandler); 120 signal(SIGTERM, KillHandler);
118 signal(SIGINT, KillHandler); 121 signal(SIGINT, KillHandler);
119 } 122 }
120 123
121 virtual void OnClientConnected(scoped_ptr<Socket> client_socket) OVERRIDE { 124 virtual void OnClientConnected(scoped_ptr<Socket> client_socket) OVERRIDE {
122 char buf[kBufSize]; 125 char buf[kBufSize];
123 const int bytes_read = client_socket->Read(buf, sizeof(buf)); 126 const int bytes_read = client_socket->Read(buf, sizeof(buf));
124 if (bytes_read <= 0) { 127 if (bytes_read <= 0) {
125 if (client_socket->exited()) 128 if (client_socket->DidReceiveEvent())
126 return; 129 return;
127 PError("Read()"); 130 PError("Read()");
128 has_failed_ = true; 131 has_failed_ = true;
129 return; 132 return;
130 } 133 }
131 const std::string command(buf, bytes_read); 134 const std::string command(buf, bytes_read);
132 int adb_port = 0; 135 int adb_port = 0;
133 int device_port = 0; 136 int device_port = 0;
134 std::string forward_to_host; 137 std::string forward_to_host;
135 int forward_to_port = 0; 138 int forward_to_port = 0;
136 const bool succeeded = ParseForwardCommand( 139 const bool succeeded = ParseForwardCommand(
137 command, &adb_port, &device_port, &forward_to_host, &forward_to_port); 140 command, &adb_port, &device_port, &forward_to_host, &forward_to_port);
138 if (!succeeded) { 141 if (!succeeded) {
139 has_failed_ = true; 142 has_failed_ = true;
140 client_socket->WriteString( 143 const std::string msg = base::StringPrintf(
141 base::StringPrintf("ERROR: Could not parse forward command '%s'", 144 "ERROR: Could not parse forward command '%s'", command.c_str());
142 command.c_str())); 145 SendMessage(msg, client_socket.get());
143 return; 146 return;
144 } 147 }
148 if (device_port < 0) {
149 // Remove the previously created host controller.
150 const std::string controller_key = MakeHostControllerMapKey(
151 adb_port, -device_port);
152 const HostControllerMap::size_type removed_elements = controllers_.erase(
153 controller_key);
154 SendMessage(
155 !removed_elements ? "ERROR: could not unmap port" : "OK",
156 client_socket.get());
157 return;
158 }
159 // Create a new host controller.
145 scoped_ptr<HostController> host_controller( 160 scoped_ptr<HostController> host_controller(
146 new HostController(device_port, forward_to_host, forward_to_port, 161 new HostController(device_port, forward_to_host, forward_to_port,
147 adb_port, GetExitNotifierFD())); 162 adb_port, GetExitNotifierFD()));
148 if (!host_controller->Connect()) { 163 if (!host_controller->Connect()) {
149 has_failed_ = true; 164 has_failed_ = true;
150 client_socket->WriteString("ERROR: Connection to device failed."); 165 SendMessage("ERROR: Connection to device failed.", client_socket.get());
151 return; 166 return;
152 } 167 }
153 // Get the current allocated port. 168 // Get the current allocated port.
154 device_port = host_controller->device_port(); 169 device_port = host_controller->device_port();
155 LOG(INFO) << "Forwarding device port " << device_port << " to host " 170 LOG(INFO) << "Forwarding device port " << device_port << " to host "
156 << forward_to_host << ":" << forward_to_port; 171 << forward_to_host << ":" << forward_to_port;
157 if (!client_socket->WriteString( 172 const std::string msg = base::StringPrintf(
158 base::StringPrintf("%d:%d", device_port, forward_to_port))) { 173 "%d:%d", device_port, forward_to_port);
159 has_failed_ = true; 174 if (!SendMessage(msg, client_socket.get()))
160 return; 175 return;
161 }
162 host_controller->Start(); 176 host_controller->Start();
163 controllers_.push_back(host_controller.release()); 177 const std::string controller_key = MakeHostControllerMapKey(
178 adb_port, device_port);
179 controllers_.insert(
180 std::make_pair(controller_key,
181 linked_ptr<HostController>(host_controller.release())));
164 } 182 }
165 183
166 virtual void OnServerExited() OVERRIDE { 184 virtual void OnServerExited() OVERRIDE {
167 for (int i = 0; i < controllers_.size(); ++i) 185 for (HostControllerMap::iterator it = controllers_.begin();
168 controllers_[i]->Join(); 186 it != controllers_.end(); ++it) {
187 linked_ptr<HostController> host_controller = it->second;
188 host_controller->Join();
189 }
169 if (controllers_.size() == 0) { 190 if (controllers_.size() == 0) {
170 LOG(ERROR) << "No forwarder servers could be started. Exiting."; 191 LOG(ERROR) << "No forwarder servers could be started. Exiting.";
171 has_failed_ = true; 192 has_failed_ = true;
172 } 193 }
173 } 194 }
174 195
175 private: 196 private:
176 ScopedVector<HostController> controllers_; 197 typedef base::hash_map<
198 std::string, linked_ptr<HostController> > HostControllerMap;
199
200 static std::string MakeHostControllerMapKey(int adb_port, int device_port) {
201 return base::StringPrintf("%d:%d", adb_port, device_port);
202 }
203
204 bool SendMessage(const std::string& msg, Socket* client_socket) {
205 bool result = client_socket->WriteString(msg);
206 DCHECK(result);
207 if (!result)
208 has_failed_ = true;
209 return result;
210 }
211
212 HostControllerMap controllers_;
177 bool has_failed_; 213 bool has_failed_;
178 214
179 DISALLOW_COPY_AND_ASSIGN(ServerDelegate); 215 DISALLOW_COPY_AND_ASSIGN(ServerDelegate);
180 }; 216 };
181 217
182 class ClientDelegate : public Daemon::ClientDelegate { 218 class ClientDelegate : public Daemon::ClientDelegate {
183 public: 219 public:
184 ClientDelegate(const std::string& forward_command) 220 ClientDelegate(const std::string& forward_command)
185 : forward_command_(forward_command), 221 : forward_command_(forward_command),
186 has_failed_(false) { 222 has_failed_(false) {
(...skipping 19 matching lines...) Expand all
206 } 242 }
207 printf("%s\n", buf); 243 printf("%s\n", buf);
208 } 244 }
209 245
210 private: 246 private:
211 const std::string forward_command_; 247 const std::string forward_command_;
212 bool has_failed_; 248 bool has_failed_;
213 }; 249 };
214 250
215 void PrintUsage(const char* program_name) { 251 void PrintUsage(const char* program_name) {
216 LOG(ERROR) << program_name << " adb_port:from_port:to_port:to_host\n" 252 LOG(ERROR) << program_name
217 "<adb port> is the TCP port Adb is configured to forward to."; 253 << " adb_port:from_port:to_port:to_host\n"
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
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 }
OLDNEW
« no previous file with comments | « tools/android/forwarder2/host_controller.cc ('k') | tools/android/forwarder2/socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698