OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "build/build_config.h" | |
6 | |
7 #if !defined(OS_WIN) | |
8 extern "C" { | |
9 #include <unistd.h> | |
10 } | |
11 #endif // !defined(OS_WIN) | |
12 | |
13 #include <iostream> | |
14 #include <list> | |
15 | |
16 #include "base/at_exit.h" | |
17 #include "base/bind.h" | |
18 #include "base/command_line.h" | |
19 #include "base/message_loop_proxy.h" | |
20 #include "base/test/mock_chrome_application_mac.h" | |
21 #include "base/time.h" | |
22 #include "crypto/nss_util.h" | |
23 #include "net/base/completion_callback.h" | |
24 #include "net/base/io_buffer.h" | |
25 #include "net/base/net_errors.h" | |
26 #include "net/socket/socket.h" | |
27 #include "remoting/base/constants.h" | |
28 #include "remoting/jingle_glue/jingle_thread.h" | |
29 #include "remoting/jingle_glue/xmpp_signal_strategy.h" | |
30 #include "remoting/protocol/jingle_session_manager.h" | |
31 | |
32 namespace remoting { | |
33 namespace protocol { | |
34 | |
35 namespace { | |
36 const int kBufferSize = 4096; | |
37 const char kDummyAuthToken[] = ""; | |
38 } // namespace | |
39 | |
40 class ProtocolTestClient; | |
41 | |
42 class ProtocolTestConnection | |
43 : public base::RefCountedThreadSafe<ProtocolTestConnection> { | |
44 public: | |
45 ProtocolTestConnection(ProtocolTestClient* client) | |
46 : client_(client), | |
47 message_loop_(MessageLoop::current()), | |
48 session_(NULL), | |
49 ALLOW_THIS_IN_INITIALIZER_LIST( | |
50 write_cb_(this, &ProtocolTestConnection::OnWritten)), | |
51 pending_write_(false), | |
52 ALLOW_THIS_IN_INITIALIZER_LIST( | |
53 read_cb_(this, &ProtocolTestConnection::OnRead)) { | |
54 } | |
55 | |
56 virtual ~ProtocolTestConnection() {} | |
57 | |
58 void Init(Session* session); | |
59 void Write(const std::string& str); | |
60 void Read(); | |
61 void Close(); | |
62 | |
63 // Session::Callback interface. | |
64 virtual void OnStateChange(Session::State state); | |
65 private: | |
66 void DoWrite(scoped_refptr<net::IOBuffer> buf, int size); | |
67 void DoRead(); | |
68 | |
69 void HandleReadResult(int result); | |
70 | |
71 void OnWritten(int result); | |
72 void OnRead(int result); | |
73 | |
74 ProtocolTestClient* client_; | |
75 MessageLoop* message_loop_; | |
76 scoped_ptr<Session> session_; | |
77 net::OldCompletionCallbackImpl<ProtocolTestConnection> write_cb_; | |
78 bool pending_write_; | |
79 net::OldCompletionCallbackImpl<ProtocolTestConnection> read_cb_; | |
80 scoped_refptr<net::IOBuffer> read_buffer_; | |
81 }; | |
82 | |
83 class ProtocolTestClient | |
84 : public SignalStrategy::StatusObserver, | |
85 public SessionManager::Listener, | |
86 public base::RefCountedThreadSafe<ProtocolTestClient> { | |
87 public: | |
88 ProtocolTestClient() { | |
89 } | |
90 | |
91 virtual ~ProtocolTestClient() {} | |
92 | |
93 void Run(const std::string& username, const std::string& auth_token, | |
94 const std::string& auth_service, const std::string& host_jid); | |
95 | |
96 void OnConnectionClosed(ProtocolTestConnection* connection); | |
97 | |
98 // SignalStrategy::StatusObserver interface. | |
99 virtual void OnStateChange( | |
100 SignalStrategy::StatusObserver::State state) OVERRIDE; | |
101 virtual void OnJidChange(const std::string& full_jid) OVERRIDE; | |
102 | |
103 // SessionManager::Listener interface. | |
104 virtual void OnSessionManagerInitialized() OVERRIDE; | |
105 virtual void OnIncomingSession( | |
106 Session* session, | |
107 SessionManager::IncomingSessionResponse* response) OVERRIDE; | |
108 | |
109 private: | |
110 typedef std::list<scoped_refptr<ProtocolTestConnection> > ConnectionsList; | |
111 | |
112 std::string host_jid_; | |
113 scoped_ptr<SignalStrategy> signal_strategy_; | |
114 std::string local_jid_; | |
115 scoped_ptr<JingleSessionManager> session_manager_; | |
116 ConnectionsList connections_; | |
117 base::Lock connections_lock_; | |
118 }; | |
119 | |
120 | |
121 void ProtocolTestConnection::Init(Session* session) { | |
122 session_.reset(session); | |
123 } | |
124 | |
125 void ProtocolTestConnection::Write(const std::string& str) { | |
126 if (str.empty()) | |
127 return; | |
128 | |
129 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(str.length())); | |
130 memcpy(buf->data(), str.c_str(), str.length()); | |
131 message_loop_->PostTask( | |
132 FROM_HERE, base::Bind(&ProtocolTestConnection::DoWrite, | |
133 this, buf, str.length())); | |
134 } | |
135 | |
136 void ProtocolTestConnection::DoWrite( | |
137 scoped_refptr<net::IOBuffer> buf, int size) { | |
138 if (pending_write_) { | |
139 LOG(ERROR) << "Cannot write because there is another pending write."; | |
140 return; | |
141 } | |
142 | |
143 net::Socket* channel = session_->event_channel(); | |
144 if (channel != NULL) { | |
145 int result = channel->Write(buf, size, &write_cb_); | |
146 if (result < 0) { | |
147 if (result == net::ERR_IO_PENDING) | |
148 pending_write_ = true; | |
149 else | |
150 LOG(ERROR) << "Write() returned error " << result; | |
151 } | |
152 } else { | |
153 LOG(ERROR) << "Cannot write because the channel isn't intialized yet."; | |
154 } | |
155 } | |
156 | |
157 void ProtocolTestConnection::Read() { | |
158 message_loop_->PostTask( | |
159 FROM_HERE, base::Bind(&ProtocolTestConnection::DoRead, this)); | |
160 } | |
161 | |
162 void ProtocolTestConnection::DoRead() { | |
163 read_buffer_ = new net::IOBuffer(kBufferSize); | |
164 while (true) { | |
165 int result = session_->event_channel()->Read( | |
166 read_buffer_, kBufferSize, &read_cb_); | |
167 if (result < 0) { | |
168 if (result != net::ERR_IO_PENDING) | |
169 LOG(ERROR) << "Read failed: " << result; | |
170 break; | |
171 } else { | |
172 HandleReadResult(result); | |
173 } | |
174 } | |
175 } | |
176 | |
177 void ProtocolTestConnection::Close() { | |
178 session_->Close(); | |
179 } | |
180 | |
181 void ProtocolTestConnection::OnStateChange(Session::State state) { | |
182 LOG(INFO) << "State of " << session_->jid() << " changed to " << state; | |
183 if (state == Session::CONNECTED) { | |
184 // Start reading after we've connected. | |
185 Read(); | |
186 } else if (state == Session::CLOSED) { | |
187 std::cerr << "Connection to " << session_->jid() | |
188 << " closed" << std::endl; | |
189 client_->OnConnectionClosed(this); | |
190 } | |
191 } | |
192 | |
193 void ProtocolTestConnection::OnWritten(int result) { | |
194 pending_write_ = false; | |
195 if (result < 0) | |
196 LOG(ERROR) << "Write() returned error " << result; | |
197 } | |
198 | |
199 void ProtocolTestConnection::OnRead(int result) { | |
200 HandleReadResult(result); | |
201 DoRead(); | |
202 } | |
203 | |
204 void ProtocolTestConnection::HandleReadResult(int result) { | |
205 if (result > 0) { | |
206 std::string str(reinterpret_cast<const char*>(read_buffer_->data()), | |
207 result); | |
208 std::cout << "(" << session_->jid() << "): " << str << std::endl; | |
209 } else { | |
210 LOG(ERROR) << "Read() returned error " << result; | |
211 } | |
212 } | |
213 | |
214 void ProtocolTestClient::Run(const std::string& username, | |
215 const std::string& auth_token, | |
216 const std::string& auth_service, | |
217 const std::string& host_jid) { | |
218 remoting::JingleThread jingle_thread; | |
219 jingle_thread.Start(); | |
220 | |
221 signal_strategy_.reset( | |
222 new XmppSignalStrategy(&jingle_thread, username, auth_token, | |
223 auth_service)); | |
224 signal_strategy_->Init(this); | |
225 session_manager_.reset(new JingleSessionManager( | |
226 jingle_thread.message_loop_proxy())); | |
227 | |
228 host_jid_ = host_jid; | |
229 | |
230 while (true) { | |
231 std::string line; | |
232 std::getline(std::cin, line); | |
233 | |
234 { | |
235 base::AutoLock auto_lock(connections_lock_); | |
236 | |
237 // Broadcast message to all clients. | |
238 for (ConnectionsList::iterator it = connections_.begin(); | |
239 it != connections_.end(); ++it) { | |
240 (*it)->Write(line); | |
241 } | |
242 } | |
243 | |
244 if (line == "exit") | |
245 break; | |
246 } | |
247 | |
248 while (!connections_.empty()) { | |
249 connections_.front()->Close(); | |
250 connections_.pop_front(); | |
251 } | |
252 | |
253 if (session_manager_.get()) { | |
254 session_manager_->Close(); | |
255 session_manager_.reset(); | |
256 } | |
257 | |
258 signal_strategy_->Close(); | |
259 jingle_thread.Stop(); | |
260 } | |
261 | |
262 void ProtocolTestClient::OnConnectionClosed( | |
263 ProtocolTestConnection* connection) { | |
264 connection->Close(); | |
265 base::AutoLock auto_lock(connections_lock_); | |
266 for (ConnectionsList::iterator it = connections_.begin(); | |
267 it != connections_.end(); ++it) { | |
268 if ((*it) == connection) { | |
269 connections_.erase(it); | |
270 return; | |
271 } | |
272 } | |
273 } | |
274 | |
275 void ProtocolTestClient::OnStateChange( | |
276 SignalStrategy::StatusObserver::State state) { | |
277 if (state == SignalStrategy::StatusObserver::CONNECTED) { | |
278 std::cerr << "Connected as " << local_jid_ << std::endl; | |
279 | |
280 session_manager_->Init( | |
281 local_jid_, signal_strategy_.get(), this, NULL, "", true); | |
282 session_manager_->set_allow_local_ips(true); | |
283 } else if (state == SignalStrategy::StatusObserver::CLOSED) { | |
284 std::cerr << "Connection closed" << std::endl; | |
285 } | |
286 } | |
287 | |
288 void ProtocolTestClient::OnJidChange(const std::string& full_jid) { | |
289 local_jid_ = full_jid; | |
290 } | |
291 | |
292 void ProtocolTestClient::OnSessionManagerInitialized() { | |
293 if (host_jid_ != "") { | |
294 ProtocolTestConnection* connection = new ProtocolTestConnection(this); | |
295 connection->Init(session_manager_->Connect( | |
296 host_jid_, "", kDummyAuthToken, | |
297 CandidateSessionConfig::CreateDefault(), | |
298 base::Bind(&ProtocolTestConnection::OnStateChange, connection))); | |
299 connections_.push_back(make_scoped_refptr(connection)); | |
300 } | |
301 } | |
302 | |
303 void ProtocolTestClient::OnIncomingSession( | |
304 Session* session, | |
305 SessionManager::IncomingSessionResponse* response) { | |
306 std::cerr << "Accepting connection from " << session->jid() << std::endl; | |
307 | |
308 session->set_config(SessionConfig::GetDefault()); | |
309 *response = SessionManager::ACCEPT; | |
310 | |
311 ProtocolTestConnection* test_connection = new ProtocolTestConnection(this); | |
312 session->SetStateChangeCallback( | |
313 base::Bind(&ProtocolTestConnection::OnStateChange, test_connection)); | |
314 test_connection->Init(session); | |
315 base::AutoLock auto_lock(connections_lock_); | |
316 connections_.push_back(make_scoped_refptr(test_connection)); | |
317 } | |
318 | |
319 } // namespace protocol | |
320 } // namespace remoting | |
321 | |
322 using remoting::protocol::ProtocolTestClient; | |
323 | |
324 void usage(char* command) { | |
325 std::cerr << "Usage: " << command << "--username=<username>" << std::endl | |
326 << "\t--auth_token=<auth_token>" << std::endl | |
327 << "\t[--host_jid=<host_jid>]" << std::endl; | |
328 exit(1); | |
329 } | |
330 | |
331 int main(int argc, char** argv) { | |
332 CommandLine::Init(argc, argv); | |
333 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
334 | |
335 if (!cmd_line->GetArgs().empty() || cmd_line->HasSwitch("help")) | |
336 usage(argv[0]); | |
337 | |
338 base::AtExitManager exit_manager; | |
339 | |
340 crypto::EnsureNSPRInit(); | |
341 crypto::EnsureNSSInit(); | |
342 | |
343 #if defined(OS_MACOSX) | |
344 mock_cr_app::RegisterMockCrApp(); | |
345 #endif // OS_MACOSX | |
346 | |
347 std::string host_jid(cmd_line->GetSwitchValueASCII("host_jid")); | |
348 | |
349 if (!cmd_line->HasSwitch("username")) | |
350 usage(argv[0]); | |
351 std::string username(cmd_line->GetSwitchValueASCII("username")); | |
352 | |
353 if (!cmd_line->HasSwitch("auth_token")) | |
354 usage(argv[0]); | |
355 std::string auth_token(cmd_line->GetSwitchValueASCII("auth_token")); | |
356 | |
357 // Default to OAuth2 for the auth token. | |
358 std::string auth_service("oauth2"); | |
359 if (cmd_line->HasSwitch("auth_service")) | |
360 auth_service = cmd_line->GetSwitchValueASCII("auth_service"); | |
361 | |
362 scoped_refptr<ProtocolTestClient> client(new ProtocolTestClient()); | |
363 | |
364 client->Run(username, auth_token, host_jid, auth_service); | |
365 | |
366 return 0; | |
367 } | |
OLD | NEW |