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 <signal.h> | 5 #include <signal.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 | 7 |
8 #include <iostream> | 8 #include <iostream> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 controller_thread_->Start(); | 58 controller_thread_->Start(); |
59 } | 59 } |
60 | 60 |
61 virtual void OnClientConnected(scoped_ptr<Socket> client_socket) OVERRIDE { | 61 virtual void OnClientConnected(scoped_ptr<Socket> client_socket) OVERRIDE { |
62 if (initialized_) { | 62 if (initialized_) { |
63 client_socket->WriteString("OK"); | 63 client_socket->WriteString("OK"); |
64 return; | 64 return; |
65 } | 65 } |
66 controller_thread_->message_loop()->PostTask( | 66 controller_thread_->message_loop()->PostTask( |
67 FROM_HERE, | 67 FROM_HERE, |
68 base::Bind(&ServerDelegate::StartController, GetExitNotifierFD(), | 68 base::Bind(&ServerDelegate::StartController, base::Unretained(this), |
69 base::Passed(&client_socket))); | 69 GetExitNotifierFD(), base::Passed(&client_socket))); |
70 initialized_ = true; | 70 initialized_ = true; |
71 } | 71 } |
72 | 72 |
73 private: | 73 private: |
74 static void StartController(int exit_notifier_fd, | 74 void StartController(int exit_notifier_fd, scoped_ptr<Socket> client_socket) { |
75 scoped_ptr<Socket> client_socket) { | 75 controller_.reset(new DeviceController(exit_notifier_fd)); |
76 forwarder2::DeviceController controller(exit_notifier_fd); | 76 if (!controller_->Init(kUnixDomainSocketPath)) { |
77 if (!controller.Init(kUnixDomainSocketPath)) { | |
78 client_socket->WriteString( | 77 client_socket->WriteString( |
79 base::StringPrintf("ERROR: Could not initialize device controller " | 78 base::StringPrintf("ERROR: Could not initialize device controller " |
80 "with ADB socket path: %s", | 79 "with ADB socket path: %s", |
81 kUnixDomainSocketPath)); | 80 kUnixDomainSocketPath)); |
82 return; | 81 return; |
83 } | 82 } |
| 83 controller_->Start(); |
84 client_socket->WriteString("OK"); | 84 client_socket->WriteString("OK"); |
85 client_socket->Close(); | 85 client_socket->Close(); |
86 // Note that the following call is blocking which explains why the device | |
87 // controller has to live on a separate thread (so that the daemon command | |
88 // server is not blocked). | |
89 controller.Start(); | |
90 } | 86 } |
91 | 87 |
92 base::AtExitManager at_exit_manager_; // Used by base::Thread. | 88 scoped_ptr<DeviceController> controller_; |
93 scoped_ptr<base::Thread> controller_thread_; | 89 scoped_ptr<base::Thread> controller_thread_; |
94 bool initialized_; | 90 bool initialized_; |
95 }; | 91 }; |
96 | 92 |
97 class ClientDelegate : public Daemon::ClientDelegate { | 93 class ClientDelegate : public Daemon::ClientDelegate { |
98 public: | 94 public: |
99 ClientDelegate() : has_failed_(false) {} | 95 ClientDelegate() : has_failed_(false) {} |
100 | 96 |
101 bool has_failed() const { return has_failed_; } | 97 bool has_failed() const { return has_failed_; } |
102 | 98 |
(...skipping 18 matching lines...) Expand all Loading... |
121 }; | 117 }; |
122 | 118 |
123 int RunDeviceForwarder(int argc, char** argv) { | 119 int RunDeviceForwarder(int argc, char** argv) { |
124 CommandLine::Init(argc, argv); // Needed by logging. | 120 CommandLine::Init(argc, argv); // Needed by logging. |
125 const bool kill_server = CommandLine::ForCurrentProcess()->HasSwitch( | 121 const bool kill_server = CommandLine::ForCurrentProcess()->HasSwitch( |
126 "kill-server"); | 122 "kill-server"); |
127 if ((kill_server && argc != 2) || (!kill_server && argc != 1)) { | 123 if ((kill_server && argc != 2) || (!kill_server && argc != 1)) { |
128 std::cerr << "Usage: device_forwarder [--kill-server]" << std::endl; | 124 std::cerr << "Usage: device_forwarder [--kill-server]" << std::endl; |
129 return 1; | 125 return 1; |
130 } | 126 } |
| 127 base::AtExitManager at_exit_manager; // Used by base::Thread. |
131 ClientDelegate client_delegate; | 128 ClientDelegate client_delegate; |
132 ServerDelegate daemon_delegate; | 129 ServerDelegate daemon_delegate; |
133 const char kLogFilePath[] = ""; // Log to logcat. | 130 const char kLogFilePath[] = ""; // Log to logcat. |
134 Daemon daemon(kLogFilePath, kDaemonIdentifier, &client_delegate, | 131 Daemon daemon(kLogFilePath, kDaemonIdentifier, &client_delegate, |
135 &daemon_delegate, &GetExitNotifierFD); | 132 &daemon_delegate, &GetExitNotifierFD); |
136 | 133 |
137 if (kill_server) | 134 if (kill_server) |
138 return !daemon.Kill(); | 135 return !daemon.Kill(); |
139 | 136 |
140 if (!daemon.SpawnIfNeeded()) | 137 if (!daemon.SpawnIfNeeded()) |
141 return 1; | 138 return 1; |
142 return client_delegate.has_failed(); | 139 return client_delegate.has_failed(); |
143 } | 140 } |
144 | 141 |
145 } // namespace | 142 } // namespace |
146 } // namespace forwarder2 | 143 } // namespace forwarder2 |
147 | 144 |
148 int main(int argc, char** argv) { | 145 int main(int argc, char** argv) { |
149 return forwarder2::RunDeviceForwarder(argc, argv); | 146 return forwarder2::RunDeviceForwarder(argc, argv); |
150 } | 147 } |
OLD | NEW |