| Index: tools/android/forwarder2/device_forwarder_main.cc
|
| diff --git a/tools/android/forwarder2/device_forwarder_main.cc b/tools/android/forwarder2/device_forwarder_main.cc
|
| index f822d285a1080905a6d0c4f5a841af46aeffb866..26904b4e8c38575edd1cbd42df2b41eb86d34790 100644
|
| --- a/tools/android/forwarder2/device_forwarder_main.cc
|
| +++ b/tools/android/forwarder2/device_forwarder_main.cc
|
| @@ -3,9 +3,9 @@
|
| // found in the LICENSE file.
|
|
|
| #include <signal.h>
|
| -#include <stdio.h>
|
| #include <stdlib.h>
|
|
|
| +#include <iostream>
|
| #include <string>
|
|
|
| #include "base/at_exit.h"
|
| @@ -29,11 +29,9 @@ forwarder2::PipeNotifier* g_notifier = NULL;
|
|
|
| const int kBufSize = 256;
|
|
|
| +const char kUnixDomainSocketPath[] = "chrome_device_forwarder";
|
| const char kDaemonIdentifier[] = "chrome_device_forwarder_daemon";
|
|
|
| -const char kKillServerCommand[] = "kill-server";
|
| -const char kStartCommand[] = "start";
|
| -
|
| void KillHandler(int /* unused */) {
|
| CHECK(g_notifier);
|
| if (!g_notifier->Notify())
|
| @@ -48,6 +46,8 @@ int GetExitNotifierFD() {
|
|
|
| class ServerDelegate : public Daemon::ServerDelegate {
|
| public:
|
| + ServerDelegate() : initialized_(false) {}
|
| +
|
| // Daemon::ServerDelegate:
|
| virtual void Init() OVERRIDE {
|
| DCHECK(!g_notifier);
|
| @@ -59,45 +59,28 @@ class ServerDelegate : public Daemon::ServerDelegate {
|
| }
|
|
|
| virtual void OnClientConnected(scoped_ptr<Socket> client_socket) OVERRIDE {
|
| - char buf[kBufSize];
|
| - const int bytes_read = client_socket->Read(buf, sizeof(buf));
|
| - if (bytes_read <= 0) {
|
| - if (client_socket->DidReceiveEvent())
|
| - return;
|
| - PError("Read()");
|
| - return;
|
| - }
|
| - const std::string adb_socket_path(buf, bytes_read);
|
| - if (adb_socket_path == adb_socket_path_) {
|
| + if (initialized_) {
|
| client_socket->WriteString("OK");
|
| return;
|
| }
|
| - if (!adb_socket_path_.empty()) {
|
| - client_socket->WriteString(
|
| - base::StringPrintf(
|
| - "ERROR: Device controller already running (adb_socket_path=%s)",
|
| - adb_socket_path_.c_str()));
|
| - return;
|
| - }
|
| - adb_socket_path_ = adb_socket_path;
|
| controller_thread_->message_loop()->PostTask(
|
| FROM_HERE,
|
| - base::Bind(&ServerDelegate::StartController, adb_socket_path,
|
| - GetExitNotifierFD(), base::Passed(&client_socket)));
|
| + base::Bind(&ServerDelegate::StartController, GetExitNotifierFD(),
|
| + base::Passed(&client_socket)));
|
| + initialized_ = true;
|
| }
|
|
|
| virtual void OnServerExited() OVERRIDE {}
|
|
|
| private:
|
| - static void StartController(const std::string& adb_socket_path,
|
| - int exit_notifier_fd,
|
| + static void StartController(int exit_notifier_fd,
|
| scoped_ptr<Socket> client_socket) {
|
| forwarder2::DeviceController controller(exit_notifier_fd);
|
| - if (!controller.Init(adb_socket_path)) {
|
| + if (!controller.Init(kUnixDomainSocketPath)) {
|
| client_socket->WriteString(
|
| base::StringPrintf("ERROR: Could not initialize device controller "
|
| "with ADB socket path: %s",
|
| - adb_socket_path.c_str()));
|
| + kUnixDomainSocketPath));
|
| return;
|
| }
|
| client_socket->WriteString("OK");
|
| @@ -110,23 +93,17 @@ class ServerDelegate : public Daemon::ServerDelegate {
|
|
|
| base::AtExitManager at_exit_manager_; // Used by base::Thread.
|
| scoped_ptr<base::Thread> controller_thread_;
|
| - std::string adb_socket_path_;
|
| + bool initialized_;
|
| };
|
|
|
| class ClientDelegate : public Daemon::ClientDelegate {
|
| public:
|
| - ClientDelegate(const std::string& adb_socket)
|
| - : adb_socket_(adb_socket),
|
| - has_failed_(false) {
|
| - }
|
| + ClientDelegate() : has_failed_(false) {}
|
|
|
| bool has_failed() const { return has_failed_; }
|
|
|
| // Daemon::ClientDelegate:
|
| virtual void OnDaemonReady(Socket* daemon_socket) OVERRIDE {
|
| - // Send the adb socket path to the daemon.
|
| - CHECK(daemon_socket->Write(adb_socket_.c_str(),
|
| - adb_socket_.length()));
|
| char buf[kBufSize];
|
| const int bytes_read = daemon_socket->Read(
|
| buf, sizeof(buf) - 1 /* leave space for null terminator */);
|
| @@ -142,31 +119,26 @@ class ClientDelegate : public Daemon::ClientDelegate {
|
| }
|
|
|
| private:
|
| - const std::string adb_socket_;
|
| bool has_failed_;
|
| };
|
|
|
| int RunDeviceForwarder(int argc, char** argv) {
|
| - if (argc != 2) {
|
| - fprintf(stderr,
|
| - "Usage: %s kill-server|<adb_socket>\n"
|
| - " <adb_socket> is the abstract Unix Domain Socket path "
|
| - "where Adb is configured to forward from.\n", argv[0]);
|
| + CommandLine::Init(argc, argv); // Needed by logging.
|
| + const bool kill_server = CommandLine::ForCurrentProcess()->HasSwitch(
|
| + "kill-server");
|
| + if ((kill_server && argc != 2) || (!kill_server && argc != 1)) {
|
| + std::cerr << "Usage: device_forwarder [--kill-server]" << std::endl;
|
| return 1;
|
| }
|
| - CommandLine::Init(argc, argv); // Needed by logging.
|
| - const char* const command =
|
| - !strcmp(argv[1], kKillServerCommand) ? kKillServerCommand : kStartCommand;
|
| - ClientDelegate client_delegate(argv[1]);
|
| + ClientDelegate client_delegate;
|
| ServerDelegate daemon_delegate;
|
| const char kLogFilePath[] = ""; // Log to logcat.
|
| Daemon daemon(kLogFilePath, kDaemonIdentifier, &client_delegate,
|
| &daemon_delegate, &GetExitNotifierFD);
|
|
|
| - if (command == kKillServerCommand)
|
| + if (kill_server)
|
| return !daemon.Kill();
|
|
|
| - DCHECK(command == kStartCommand);
|
| if (!daemon.SpawnIfNeeded())
|
| return 1;
|
| return client_delegate.has_failed();
|
|
|