OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "remoting/host/simple_host.h" | 5 #include "remoting/host/chromoting_host.h" |
6 | 6 |
7 #include "base/stl_util-inl.h" | 7 #include "base/stl_util-inl.h" |
8 #include "base/waitable_event.h" | 8 #include "base/waitable_event.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "remoting/base/constants.h" | 10 #include "remoting/base/constants.h" |
11 #include "remoting/base/protocol_decoder.h" | 11 #include "remoting/base/protocol_decoder.h" |
12 #include "remoting/host/session_manager.h" | 12 #include "remoting/host/session_manager.h" |
13 #include "remoting/jingle_glue/jingle_channel.h" | 13 #include "remoting/jingle_glue/jingle_channel.h" |
14 | 14 |
15 namespace remoting { | 15 namespace remoting { |
16 | 16 |
17 SimpleHost::SimpleHost(const std::string& username, | 17 ChromotingHost::ChromotingHost(const std::string& username, |
18 const std::string& auth_token, | 18 const std::string& auth_token, |
19 Capturer* capturer, | 19 Capturer* capturer, |
20 Encoder* encoder, | 20 Encoder* encoder, |
21 EventExecutor* executor, | 21 EventExecutor* executor, |
22 base::WaitableEvent* host_done) | 22 base::WaitableEvent* host_done) |
23 : main_thread_("MainThread"), | 23 : main_thread_("MainThread"), |
24 capture_thread_("CaptureThread"), | 24 capture_thread_("CaptureThread"), |
25 encode_thread_("EncodeThread"), | 25 encode_thread_("EncodeThread"), |
26 username_(username), | 26 username_(username), |
27 auth_token_(auth_token), | 27 auth_token_(auth_token), |
28 capturer_(capturer), | 28 capturer_(capturer), |
29 encoder_(encoder), | 29 encoder_(encoder), |
30 executor_(executor), | 30 executor_(executor), |
31 host_done_(host_done) { | 31 host_done_(host_done) { |
32 // TODO(ajwong): The thread injection and object ownership is odd here. | 32 // TODO(ajwong): The thread injection and object ownership is odd here. |
33 // Fix so we do not start this thread in the constructor, so we only | 33 // Fix so we do not start this thread in the constructor, so we only |
34 // take in a session manager, don't let session manager own the | 34 // take in a session manager, don't let session manager own the |
35 // capturer/encoder, and then associate the capturer and encoder threads with | 35 // capturer/encoder, and then associate the capturer and encoder threads with |
36 // the capturer and encoder objects directly. This will require a | 36 // the capturer and encoder objects directly. This will require a |
37 // non-refcounted NewRunnableMethod. | 37 // non-refcounted NewRunnableMethod. |
38 main_thread_.StartWithOptions( | 38 main_thread_.StartWithOptions( |
39 base::Thread::Options(MessageLoop::TYPE_UI, 0)); | 39 base::Thread::Options(MessageLoop::TYPE_UI, 0)); |
40 network_thread_.Start(); | 40 network_thread_.Start(); |
41 } | 41 } |
42 | 42 |
43 SimpleHost::~SimpleHost() { | 43 ChromotingHost::~ChromotingHost() { |
44 // TODO(ajwong): We really need to inject these threads and get rid of these | 44 // TODO(ajwong): We really need to inject these threads and get rid of these |
45 // start/stops. | 45 // start/stops. |
46 main_thread_.Stop(); | 46 main_thread_.Stop(); |
47 network_thread_.Stop(); | 47 network_thread_.Stop(); |
48 DCHECK(!encode_thread_.IsRunning()); | 48 DCHECK(!encode_thread_.IsRunning()); |
49 DCHECK(!capture_thread_.IsRunning()); | 49 DCHECK(!capture_thread_.IsRunning()); |
50 } | 50 } |
51 | 51 |
52 void SimpleHost::Run() { | 52 void ChromotingHost::Run() { |
53 // Submit a task to perform host registration. We'll also start | 53 // Submit a task to perform host registration. We'll also start |
54 // listening to connection if registration is done. | 54 // listening to connection if registration is done. |
55 message_loop()->PostTask(FROM_HERE, | 55 message_loop()->PostTask( |
56 NewRunnableMethod(this, &SimpleHost::RegisterHost)); | 56 FROM_HERE, |
| 57 NewRunnableMethod(this, &ChromotingHost::RegisterHost)); |
57 } | 58 } |
58 | 59 |
59 // This method is called when we need to destroy the host process. | 60 // This method is called when we need to destroy the host process. |
60 void SimpleHost::DestroySession() { | 61 void ChromotingHost::DestroySession() { |
61 DCHECK_EQ(message_loop(), MessageLoop::current()); | 62 DCHECK_EQ(message_loop(), MessageLoop::current()); |
62 | 63 |
63 // First we tell the session to pause and then we wait until all | 64 // First we tell the session to pause and then we wait until all |
64 // the tasks are done. | 65 // the tasks are done. |
65 if (session_.get()) { | 66 if (session_.get()) { |
66 session_->Pause(); | 67 session_->Pause(); |
67 | 68 |
68 // TODO(hclam): Revise the order. | 69 // TODO(hclam): Revise the order. |
69 DCHECK(encode_thread_.IsRunning()); | 70 DCHECK(encode_thread_.IsRunning()); |
70 encode_thread_.Stop(); | 71 encode_thread_.Stop(); |
71 | 72 |
72 DCHECK(capture_thread_.IsRunning()); | 73 DCHECK(capture_thread_.IsRunning()); |
73 capture_thread_.Stop(); | 74 capture_thread_.Stop(); |
74 } | 75 } |
75 } | 76 } |
76 | 77 |
77 // This method talks to the cloud to register the host process. If | 78 // This method talks to the cloud to register the host process. If |
78 // successful we will start listening to network requests. | 79 // successful we will start listening to network requests. |
79 void SimpleHost::RegisterHost() { | 80 void ChromotingHost::RegisterHost() { |
80 DCHECK_EQ(message_loop(), MessageLoop::current()); | 81 DCHECK_EQ(message_loop(), MessageLoop::current()); |
81 DCHECK(!jingle_client_); | 82 DCHECK(!jingle_client_); |
82 | 83 |
83 // Connect to the talk network with a JingleClient. | 84 // Connect to the talk network with a JingleClient. |
84 jingle_client_ = new JingleClient(&network_thread_); | 85 jingle_client_ = new JingleClient(&network_thread_); |
85 jingle_client_->Init(username_, auth_token_, | 86 jingle_client_->Init(username_, auth_token_, |
86 kChromotingTokenServiceName, this); | 87 kChromotingTokenServiceName, this); |
87 } | 88 } |
88 | 89 |
89 // This method is called if a client is connected to this object. | 90 // This method is called if a client is connected to this object. |
90 void SimpleHost::OnClientConnected(ClientConnection* client) { | 91 void ChromotingHost::OnClientConnected(ClientConnection* client) { |
91 DCHECK_EQ(message_loop(), MessageLoop::current()); | 92 DCHECK_EQ(message_loop(), MessageLoop::current()); |
92 | 93 |
93 // Create a new RecordSession if there was none. | 94 // Create a new RecordSession if there was none. |
94 if (!session_.get()) { | 95 if (!session_.get()) { |
95 // The first we need to make sure capture and encode thread are | 96 // The first we need to make sure capture and encode thread are |
96 // running. | 97 // running. |
97 capture_thread_.Start(); | 98 capture_thread_.Start(); |
98 encode_thread_.Start(); | 99 encode_thread_.Start(); |
99 | 100 |
100 // Then we create a SessionManager passing the message loops that | 101 // Then we create a SessionManager passing the message loops that |
(...skipping 11 matching lines...) Expand all Loading... |
112 // Immediately add the client and start the session. | 113 // Immediately add the client and start the session. |
113 session_->AddClient(client); | 114 session_->AddClient(client); |
114 session_->Start(); | 115 session_->Start(); |
115 LOG(INFO) << "Session manager started"; | 116 LOG(INFO) << "Session manager started"; |
116 } else { | 117 } else { |
117 // If a session manager already exists we simply add the new client. | 118 // If a session manager already exists we simply add the new client. |
118 session_->AddClient(client); | 119 session_->AddClient(client); |
119 } | 120 } |
120 } | 121 } |
121 | 122 |
122 void SimpleHost::OnClientDisconnected(ClientConnection* client) { | 123 void ChromotingHost::OnClientDisconnected(ClientConnection* client) { |
123 DCHECK_EQ(message_loop(), MessageLoop::current()); | 124 DCHECK_EQ(message_loop(), MessageLoop::current()); |
124 | 125 |
125 // Remove the client from the session manager. | 126 // Remove the client from the session manager. |
126 if (session_.get()) | 127 if (session_.get()) |
127 session_->RemoveClient(client); | 128 session_->RemoveClient(client); |
128 | 129 |
129 // Also remove reference to ClientConnection from this object. | 130 // Also remove reference to ClientConnection from this object. |
130 client_ = NULL; | 131 client_ = NULL; |
131 | 132 |
132 // TODO(hclam): If the last client has disconnected we need to destroy | 133 // TODO(hclam): If the last client has disconnected we need to destroy |
133 // the session manager and shutdown the capture and encode threads. | 134 // the session manager and shutdown the capture and encode threads. |
134 // Right now we assume that there's only one client. | 135 // Right now we assume that there's only one client. |
135 DestroySession(); | 136 DestroySession(); |
136 } | 137 } |
137 | 138 |
138 //////////////////////////////////////////////////////////////////////////// | 139 //////////////////////////////////////////////////////////////////////////// |
139 // ClientConnection::EventHandler implementations | 140 // ClientConnection::EventHandler implementations |
140 void SimpleHost::HandleMessages(ClientConnection* client, | 141 void ChromotingHost::HandleMessages(ClientConnection* client, |
141 ClientMessageList* messages) { | 142 ClientMessageList* messages) { |
142 DCHECK_EQ(message_loop(), MessageLoop::current()); | 143 DCHECK_EQ(message_loop(), MessageLoop::current()); |
143 | 144 |
144 // Delegate the messages to EventExecutor and delete the unhandled | 145 // Delegate the messages to EventExecutor and delete the unhandled |
145 // messages. | 146 // messages. |
146 DCHECK(executor_.get()); | 147 DCHECK(executor_.get()); |
147 executor_->HandleInputEvents(messages); | 148 executor_->HandleInputEvents(messages); |
148 STLDeleteElements<ClientMessageList>(messages); | 149 STLDeleteElements<ClientMessageList>(messages); |
149 } | 150 } |
150 | 151 |
151 void SimpleHost::OnConnectionOpened(ClientConnection* client) { | 152 void ChromotingHost::OnConnectionOpened(ClientConnection* client) { |
152 DCHECK_EQ(message_loop(), MessageLoop::current()); | 153 DCHECK_EQ(message_loop(), MessageLoop::current()); |
153 | 154 |
154 // Completes the client connection. | 155 // Completes the client connection. |
155 LOG(INFO) << "Connection to client established."; | 156 LOG(INFO) << "Connection to client established."; |
156 OnClientConnected(client_.get()); | 157 OnClientConnected(client_.get()); |
157 } | 158 } |
158 | 159 |
159 void SimpleHost::OnConnectionClosed(ClientConnection* client) { | 160 void ChromotingHost::OnConnectionClosed(ClientConnection* client) { |
160 DCHECK_EQ(message_loop(), MessageLoop::current()); | 161 DCHECK_EQ(message_loop(), MessageLoop::current()); |
161 | 162 |
162 // Completes the client connection. | 163 // Completes the client connection. |
163 LOG(INFO) << "Connection to client closed."; | 164 LOG(INFO) << "Connection to client closed."; |
164 OnClientDisconnected(client_.get()); | 165 OnClientDisconnected(client_.get()); |
165 } | 166 } |
166 | 167 |
167 void SimpleHost::OnConnectionFailed(ClientConnection* client) { | 168 void ChromotingHost::OnConnectionFailed(ClientConnection* client) { |
168 DCHECK_EQ(message_loop(), MessageLoop::current()); | 169 DCHECK_EQ(message_loop(), MessageLoop::current()); |
169 | 170 |
170 // The client has disconnected. | 171 // The client has disconnected. |
171 LOG(ERROR) << "Connection failed unexpectedly."; | 172 LOG(ERROR) << "Connection failed unexpectedly."; |
172 OnClientDisconnected(client_.get()); | 173 OnClientDisconnected(client_.get()); |
173 } | 174 } |
174 | 175 |
175 //////////////////////////////////////////////////////////////////////////// | 176 //////////////////////////////////////////////////////////////////////////// |
176 // JingleClient::Callback implementations | 177 // JingleClient::Callback implementations |
177 void SimpleHost::OnStateChange(JingleClient* jingle_client, | 178 void ChromotingHost::OnStateChange(JingleClient* jingle_client, |
178 JingleClient::State state) { | 179 JingleClient::State state) { |
179 DCHECK_EQ(jingle_client_.get(), jingle_client); | 180 DCHECK_EQ(jingle_client_.get(), jingle_client); |
180 | 181 |
181 if (state == JingleClient::CONNECTED) { | 182 if (state == JingleClient::CONNECTED) { |
182 LOG(INFO) << "Host connected as " | 183 LOG(INFO) << "Host connected as " |
183 << jingle_client->GetFullJid() << "." << std::endl; | 184 << jingle_client->GetFullJid() << "." << std::endl; |
184 | 185 |
185 // Start heartbeating after we connected | 186 // Start heartbeating after we connected |
186 heartbeat_sender_ = new HeartbeatSender(); | 187 heartbeat_sender_ = new HeartbeatSender(); |
187 // TODO(sergeyu): where do we get host id? | 188 // TODO(sergeyu): where do we get host id? |
188 heartbeat_sender_->Start(jingle_client_.get(), "HostID"); | 189 heartbeat_sender_->Start(jingle_client_.get(), "HostID"); |
189 } else if (state == JingleClient::CLOSED) { | 190 } else if (state == JingleClient::CLOSED) { |
190 LOG(INFO) << "Host disconnected from talk network." << std::endl; | 191 LOG(INFO) << "Host disconnected from talk network." << std::endl; |
191 heartbeat_sender_ = NULL; | 192 heartbeat_sender_ = NULL; |
192 | 193 |
193 // Quit the message loop if disconected. | 194 // Quit the message loop if disconected. |
194 message_loop()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 195 message_loop()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
195 host_done_->Signal(); | 196 host_done_->Signal(); |
196 } | 197 } |
197 } | 198 } |
198 | 199 |
199 bool SimpleHost::OnAcceptConnection( | 200 bool ChromotingHost::OnAcceptConnection( |
200 JingleClient* jingle_client, const std::string& jid, | 201 JingleClient* jingle_client, const std::string& jid, |
201 JingleChannel::Callback** channel_callback) { | 202 JingleChannel::Callback** channel_callback) { |
202 DCHECK_EQ(jingle_client_.get(), jingle_client); | 203 DCHECK_EQ(jingle_client_.get(), jingle_client); |
203 | 204 |
204 // TODO(hclam): Allow multiple clients to connect to the host. | 205 // TODO(hclam): Allow multiple clients to connect to the host. |
205 if (client_.get()) | 206 if (client_.get()) |
206 return false; | 207 return false; |
207 | 208 |
208 LOG(INFO) << "Client connected: " << jid << std::endl; | 209 LOG(INFO) << "Client connected: " << jid << std::endl; |
209 | 210 |
210 // If we accept the connected then create a client object and set the | 211 // If we accept the connected then create a client object and set the |
211 // callback. | 212 // callback. |
212 client_ = new ClientConnection(message_loop(), new ProtocolDecoder(), this); | 213 client_ = new ClientConnection(message_loop(), new ProtocolDecoder(), this); |
213 *channel_callback = client_.get(); | 214 *channel_callback = client_.get(); |
214 return true; | 215 return true; |
215 } | 216 } |
216 | 217 |
217 void SimpleHost::OnNewConnection(JingleClient* jingle_client, | 218 void ChromotingHost::OnNewConnection(JingleClient* jingle_client, |
218 scoped_refptr<JingleChannel> channel) { | 219 scoped_refptr<JingleChannel> channel) { |
219 DCHECK_EQ(jingle_client_.get(), jingle_client); | 220 DCHECK_EQ(jingle_client_.get(), jingle_client); |
220 | 221 |
221 // Since the session manager has not started, it is still safe to access | 222 // Since the session manager has not started, it is still safe to access |
222 // the client directly. Note that we give the ownership of the channel | 223 // the client directly. Note that we give the ownership of the channel |
223 // to the client. | 224 // to the client. |
224 client_->set_jingle_channel(channel); | 225 client_->set_jingle_channel(channel); |
225 } | 226 } |
226 | 227 |
227 MessageLoop* SimpleHost::message_loop() { | 228 MessageLoop* ChromotingHost::message_loop() { |
228 return main_thread_.message_loop(); | 229 return main_thread_.message_loop(); |
229 } | 230 } |
230 | 231 |
231 } // namespace remoting | 232 } // namespace remoting |
OLD | NEW |