From 406e6e22b82ef80e8bc44f45d85efa68799bc552 Mon Sep 17 00:00:00 2001 From: "garykac@chromium.org" Date: Mon, 28 Feb 2011 23:35:19 +0000 Subject: Fold jingle_connection_to_host into connection_to_host. BUG=none TEST=unittests Review URL: http://codereview.chromium.org/6532099 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@76306 0039d316-1c4b-4281-b951-d872f2087c98 --- remoting/protocol/connection_to_host.cc | 160 ++++++++++++++++++++- remoting/protocol/connection_to_host.h | 70 ++++++++-- remoting/protocol/jingle_connection_to_host.cc | 185 ------------------------- remoting/protocol/jingle_connection_to_host.h | 111 --------------- 4 files changed, 221 insertions(+), 305 deletions(-) delete mode 100644 remoting/protocol/jingle_connection_to_host.cc delete mode 100644 remoting/protocol/jingle_connection_to_host.h (limited to 'remoting/protocol') diff --git a/remoting/protocol/connection_to_host.cc b/remoting/protocol/connection_to_host.cc index 162dfdf..67719f4 100644 --- a/remoting/protocol/connection_to_host.cc +++ b/remoting/protocol/connection_to_host.cc @@ -8,7 +8,11 @@ #include "base/message_loop.h" #include "remoting/base/constants.h" #include "remoting/jingle_glue/jingle_thread.h" +#include "remoting/proto/auth.pb.h" +#include "remoting/protocol/client_message_dispatcher.h" #include "remoting/protocol/client_stub.h" +#include "remoting/protocol/host_control_sender.h" +#include "remoting/protocol/input_sender.h" #include "remoting/protocol/jingle_session_manager.h" #include "remoting/protocol/video_reader.h" #include "remoting/protocol/video_stub.h" @@ -17,7 +21,10 @@ namespace remoting { namespace protocol { -ConnectionToHost::ConnectionToHost() { +ConnectionToHost::ConnectionToHost(JingleThread* thread) + : thread_(thread), + event_callback_(NULL), + dispatcher_(new ClientMessageDispatcher()) { } ConnectionToHost::~ConnectionToHost() { @@ -31,5 +38,156 @@ HostStub* ConnectionToHost::host_stub() { return host_stub_.get(); } +MessageLoop* ConnectionToHost::message_loop() { + return thread_->message_loop(); +} + +void ConnectionToHost::Connect(const std::string& username, + const std::string& auth_token, + const std::string& host_jid, + HostEventCallback* event_callback, + ClientStub* client_stub, + VideoStub* video_stub) { + event_callback_ = event_callback; + client_stub_ = client_stub; + video_stub_ = video_stub; + + // Initialize |jingle_client_|. + jingle_client_ = new JingleClient(thread_); + jingle_client_->Init(username, auth_token, + kChromotingTokenServiceName, this); + + // Save jid of the host. The actual connection is created later after + // |jingle_client_| is connected. + host_jid_ = host_jid; +} + +void ConnectionToHost::Disconnect() { + if (MessageLoop::current() != message_loop()) { + message_loop()->PostTask( + FROM_HERE, NewRunnableMethod(this, + &ConnectionToHost::Disconnect)); + return; + } + + if (session_) { + session_->Close( + NewRunnableMethod(this, &ConnectionToHost::OnDisconnected)); + } else { + OnDisconnected(); + } +} + +void ConnectionToHost::InitSession() { + DCHECK_EQ(message_loop(), MessageLoop::current()); + + // Initialize chromotocol |session_manager_|. + JingleSessionManager* session_manager = + new JingleSessionManager(thread_); + // TODO(ajwong): Make this a command switch when we're more stable. + session_manager->set_allow_local_ips(true); + session_manager->Init( + jingle_client_->GetFullJid(), + jingle_client_->session_manager(), + NewCallback(this, &ConnectionToHost::OnNewSession), + NULL, NULL); + session_manager_ = session_manager; + + CandidateSessionConfig* candidate_config = + CandidateSessionConfig::CreateDefault(); + // TODO(sergeyu): Set resolution in the |candidate_config| to the desired + // resolution. + + ClientAuthToken auth_token_proto; + auth_token_proto.set_host_full_jid(host_jid_); + auth_token_proto.set_client_full_jid(jingle_client_->GetFullJid()); + // TODO(ajwong): Use real token. + auth_token_proto.set_client_oauth_token(""); + + // TODO(ajwong): We should encrypt this based on the host's public key. + std::string client_token = auth_token_proto.SerializeAsString(); + + // Initialize |session_|. + session_ = session_manager_->Connect( + host_jid_, client_token, candidate_config, + NewCallback(this, &ConnectionToHost::OnSessionStateChange)); +} + +void ConnectionToHost::OnDisconnected() { + session_ = NULL; + + if (session_manager_) { + session_manager_->Close( + NewRunnableMethod(this, &ConnectionToHost::OnServerClosed)); + } else { + OnServerClosed(); + } +} + +void ConnectionToHost::OnServerClosed() { + session_manager_ = NULL; + if (jingle_client_) { + jingle_client_->Close(); + jingle_client_ = NULL; + } +} + +const SessionConfig* ConnectionToHost::config() { + return session_->config(); +} + +// JingleClient::Callback interface. +void ConnectionToHost::OnStateChange(JingleClient* client, + JingleClient::State state) { + DCHECK_EQ(message_loop(), MessageLoop::current()); + DCHECK(client); + DCHECK(event_callback_); + + if (state == JingleClient::CONNECTED) { + VLOG(1) << "Connected as: " << client->GetFullJid(); + InitSession(); + } else if (state == JingleClient::CLOSED) { + VLOG(1) << "Connection closed."; + event_callback_->OnConnectionClosed(this); + } +} + +void ConnectionToHost::OnNewSession(Session* session, + SessionManager::IncomingSessionResponse* response) { + DCHECK_EQ(message_loop(), MessageLoop::current()); + // Client always rejects incoming sessions. + *response = SessionManager::DECLINE; +} + +void ConnectionToHost::OnSessionStateChange( + Session::State state) { + DCHECK_EQ(message_loop(), MessageLoop::current()); + DCHECK(event_callback_); + + switch (state) { + case Session::FAILED: + event_callback_->OnConnectionFailed(this); + break; + + case Session::CLOSED: + event_callback_->OnConnectionClosed(this); + break; + + case Session::CONNECTED: + // Initialize reader and writer. + video_reader_.reset(VideoReader::Create(session_->config())); + video_reader_->Init(session_, video_stub_); + input_stub_.reset(new InputSender(session_->event_channel())); + host_stub_.reset(new HostControlSender(session_->control_channel())); + dispatcher_->Initialize(session_.get(), client_stub_); + event_callback_->OnConnectionOpened(this); + break; + + default: + // Ignore the other states by default. + break; + } +} + } // namespace protocol } // namespace remoting diff --git a/remoting/protocol/connection_to_host.h b/remoting/protocol/connection_to_host.h index c3b2c54..515b89a 100644 --- a/remoting/protocol/connection_to_host.h +++ b/remoting/protocol/connection_to_host.h @@ -9,18 +9,32 @@ #include "base/ref_counted.h" #include "base/scoped_ptr.h" +#include "base/task.h" +#include "remoting/jingle_glue/jingle_client.h" #include "remoting/proto/internal.pb.h" +#include "remoting/protocol/connection_to_host.h" #include "remoting/protocol/host_stub.h" #include "remoting/protocol/input_stub.h" +#include "remoting/protocol/message_reader.h" +#include "remoting/protocol/session.h" +#include "remoting/protocol/session_manager.h" + +class MessageLoop; namespace remoting { + +class JingleThread; +class VideoPacket; + namespace protocol { +class ClientMessageDispatcher; class ClientStub; class SessionConfig; +class VideoReader; class VideoStub; -class ConnectionToHost { +class ConnectionToHost : public JingleClient::Callback { public: class HostEventCallback { public: @@ -36,6 +50,8 @@ class ConnectionToHost { virtual void OnConnectionFailed(ConnectionToHost* conn) = 0; }; + // TODO(sergeyu): Constructor shouldn't need thread here. + explicit ConnectionToHost(JingleThread* thread); virtual ~ConnectionToHost(); // TODO(ajwong): We need to generalize this API. @@ -44,20 +60,56 @@ class ConnectionToHost { const std::string& host_jid, HostEventCallback* event_callback, ClientStub* client_stub, - VideoStub* video_stub) = 0; - virtual void Disconnect() = 0; + VideoStub* video_stub); + virtual void Disconnect(); - virtual const SessionConfig* config() = 0; + virtual const SessionConfig* config(); virtual InputStub* input_stub(); virtual HostStub* host_stub(); - protected: - ConnectionToHost(); + // JingleClient::Callback interface. + virtual void OnStateChange(JingleClient* client, JingleClient::State state); + + // Callback for chromotocol SessionManager. + void OnNewSession( + Session* connection, + SessionManager::IncomingSessionResponse* response); + + // Callback for chromotocol Session. + void OnSessionStateChange(Session::State state); + + private: + // The message loop for the jingle thread this object works on. + MessageLoop* message_loop(); + + // Called on the jingle thread after we've successfully to XMPP server. Starts + // P2P connection to the host. + void InitSession(); + + // Callback for |video_reader_|. + void OnVideoPacket(VideoPacket* packet); + + // Used by Disconnect() to disconnect chromoting connection, stop chromoting + // server, and then disconnect XMPP connection. + void OnDisconnected(); + void OnServerClosed(); + + JingleThread* thread_; + + scoped_refptr jingle_client_; + scoped_refptr session_manager_; + scoped_refptr session_; + + scoped_ptr video_reader_; + + HostEventCallback* event_callback_; + + std::string host_jid_; + + scoped_ptr dispatcher_; - //private: TODO(garykac). Merge jingle_connection_to_host up into this class - // and then make these private again. //////////////////////////////////////////////////////////////////////////// // User input event channel interface @@ -86,4 +138,6 @@ class ConnectionToHost { } // namespace protocol } // namespace remoting +DISABLE_RUNNABLE_METHOD_REFCOUNT(remoting::protocol::ConnectionToHost); + #endif // REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_ diff --git a/remoting/protocol/jingle_connection_to_host.cc b/remoting/protocol/jingle_connection_to_host.cc deleted file mode 100644 index 2191a19..0000000 --- a/remoting/protocol/jingle_connection_to_host.cc +++ /dev/null @@ -1,185 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "remoting/protocol/jingle_connection_to_host.h" - -#include "base/callback.h" -#include "base/message_loop.h" -#include "remoting/base/constants.h" -#include "remoting/jingle_glue/jingle_thread.h" -#include "remoting/proto/auth.pb.h" -#include "remoting/protocol/client_message_dispatcher.h" -#include "remoting/protocol/client_stub.h" -#include "remoting/protocol/host_control_sender.h" -#include "remoting/protocol/input_sender.h" -#include "remoting/protocol/jingle_session_manager.h" -#include "remoting/protocol/video_reader.h" -#include "remoting/protocol/video_stub.h" -#include "remoting/protocol/util.h" - -namespace remoting { -namespace protocol { - -JingleConnectionToHost::JingleConnectionToHost(JingleThread* thread) - : thread_(thread), - event_callback_(NULL), - dispatcher_(new ClientMessageDispatcher()) { -} - -JingleConnectionToHost::~JingleConnectionToHost() { -} - -void JingleConnectionToHost::Connect(const std::string& username, - const std::string& auth_token, - const std::string& host_jid, - HostEventCallback* event_callback, - ClientStub* client_stub, - VideoStub* video_stub) { - event_callback_ = event_callback; - client_stub_ = client_stub; - video_stub_ = video_stub; - - // Initialize |jingle_client_|. - jingle_client_ = new JingleClient(thread_); - jingle_client_->Init(username, auth_token, - kChromotingTokenServiceName, this); - - // Save jid of the host. The actual connection is created later after - // |jingle_client_| is connected. - host_jid_ = host_jid; -} - -void JingleConnectionToHost::Disconnect() { - if (MessageLoop::current() != message_loop()) { - message_loop()->PostTask( - FROM_HERE, NewRunnableMethod(this, - &JingleConnectionToHost::Disconnect)); - return; - } - - if (session_) { - session_->Close( - NewRunnableMethod(this, &JingleConnectionToHost::OnDisconnected)); - } else { - OnDisconnected(); - } -} - -void JingleConnectionToHost::InitSession() { - DCHECK_EQ(message_loop(), MessageLoop::current()); - - // Initialize chromotocol |session_manager_|. - JingleSessionManager* session_manager = - new JingleSessionManager(thread_); - // TODO(ajwong): Make this a command switch when we're more stable. - session_manager->set_allow_local_ips(true); - session_manager->Init( - jingle_client_->GetFullJid(), - jingle_client_->session_manager(), - NewCallback(this, &JingleConnectionToHost::OnNewSession), - NULL, NULL); - session_manager_ = session_manager; - - CandidateSessionConfig* candidate_config = - CandidateSessionConfig::CreateDefault(); - // TODO(sergeyu): Set resolution in the |candidate_config| to the desired - // resolution. - - ClientAuthToken auth_token_proto; - auth_token_proto.set_host_full_jid(host_jid_); - auth_token_proto.set_client_full_jid(jingle_client_->GetFullJid()); - // TODO(ajwong): Use real token. - auth_token_proto.set_client_oauth_token(""); - - // TODO(ajwong): We should encrypt this based on the host's public key. - std::string client_token = auth_token_proto.SerializeAsString(); - - // Initialize |session_|. - session_ = session_manager_->Connect( - host_jid_, client_token, candidate_config, - NewCallback(this, &JingleConnectionToHost::OnSessionStateChange)); -} - -void JingleConnectionToHost::OnDisconnected() { - session_ = NULL; - - if (session_manager_) { - session_manager_->Close( - NewRunnableMethod(this, &JingleConnectionToHost::OnServerClosed)); - } else { - OnServerClosed(); - } -} - -void JingleConnectionToHost::OnServerClosed() { - session_manager_ = NULL; - if (jingle_client_) { - jingle_client_->Close(); - jingle_client_ = NULL; - } -} - -const SessionConfig* JingleConnectionToHost::config() { - return session_->config(); -} - -// JingleClient::Callback interface. -void JingleConnectionToHost::OnStateChange(JingleClient* client, - JingleClient::State state) { - DCHECK_EQ(message_loop(), MessageLoop::current()); - DCHECK(client); - DCHECK(event_callback_); - - if (state == JingleClient::CONNECTED) { - VLOG(1) << "Connected as: " << client->GetFullJid(); - InitSession(); - } else if (state == JingleClient::CLOSED) { - VLOG(1) << "Connection closed."; - event_callback_->OnConnectionClosed(this); - } -} - -void JingleConnectionToHost::OnNewSession(Session* session, - SessionManager::IncomingSessionResponse* response) { - DCHECK_EQ(message_loop(), MessageLoop::current()); - // Client always rejects incoming sessions. - *response = SessionManager::DECLINE; -} - -void JingleConnectionToHost::OnSessionStateChange( - Session::State state) { - DCHECK_EQ(message_loop(), MessageLoop::current()); - DCHECK(event_callback_); - - switch (state) { - case Session::FAILED: - event_callback_->OnConnectionFailed(this); - break; - - case Session::CLOSED: - event_callback_->OnConnectionClosed(this); - break; - - case Session::CONNECTED: - // Initialize reader and writer. - video_reader_.reset(VideoReader::Create(session_->config())); - video_reader_->Init(session_, video_stub_); - input_stub_.reset(new InputSender(session_->event_channel())); - host_stub_.reset(new HostControlSender(session_->control_channel())); - dispatcher_->Initialize(session_.get(), client_stub_); - event_callback_->OnConnectionOpened(this); - break; - - default: - // Ignore the other states by default. - break; - } -} - -MessageLoop* JingleConnectionToHost::message_loop() { - return thread_->message_loop(); -} - -} // namespace protocol -} // namespace remoting diff --git a/remoting/protocol/jingle_connection_to_host.h b/remoting/protocol/jingle_connection_to_host.h deleted file mode 100644 index 5c24e40..0000000 --- a/remoting/protocol/jingle_connection_to_host.h +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// JingleConnectionToHost implements the ConnectionToHost interface using -// libjingle as the transport protocol. -// -// Much of this class focuses on translating JingleClient and -// ChromotingConnection callbacks into ConnectionToHost::HostEventCallback -// messages. -// -// The public API of this class is designed to be asynchronous, and thread -// safe for invocation from other threads. -// -// Internally though, all work delegeated to the |network_thread| given -// during construction. Any event handlers running on the |network_thread| -// should not block. - -#ifndef REMOTING_PROTOCOL_JINGLE_CONNECTION_TO_HOST_H_ -#define REMOTING_PROTOCOL_JINGLE_CONNECTION_TO_HOST_H_ - -#include "base/ref_counted.h" -#include "base/scoped_ptr.h" -#include "base/task.h" -#include "remoting/jingle_glue/jingle_client.h" -#include "remoting/protocol/connection_to_host.h" -#include "remoting/protocol/message_reader.h" -#include "remoting/protocol/session.h" -#include "remoting/protocol/session_manager.h" - -class MessageLoop; - -namespace remoting { - -class JingleThread; -class VideoPacket; - -namespace protocol { - -class ClientMessageDispatcher; -class VideoReader; -class VideoStub; - -class JingleConnectionToHost : public ConnectionToHost, - public JingleClient::Callback { - public: - // TODO(sergeyu): Constructor shouldn't need thread here. - explicit JingleConnectionToHost(JingleThread* thread); - virtual ~JingleConnectionToHost(); - - virtual void Connect(const std::string& username, - const std::string& auth_token, - const std::string& host_jid, - HostEventCallback* event_callback, - ClientStub* client_stub, - VideoStub* video_stub); - virtual void Disconnect(); - - virtual const SessionConfig* config(); - - // JingleClient::Callback interface. - virtual void OnStateChange(JingleClient* client, JingleClient::State state); - - // Callback for chromotocol SessionManager. - void OnNewSession( - Session* connection, - SessionManager::IncomingSessionResponse* response); - - // Callback for chromotocol Session. - void OnSessionStateChange(Session::State state); - - private: - // The message loop for the jingle thread this object works on. - MessageLoop* message_loop(); - - // Called on the jingle thread after we've successfully to XMPP server. Starts - // P2P connection to the host. - void InitSession(); - - // Callback for |video_reader_|. - void OnVideoPacket(VideoPacket* packet); - - // Used by Disconnect() to disconnect chromoting connection, stop chromoting - // server, and then disconnect XMPP connection. - void OnDisconnected(); - void OnServerClosed(); - - JingleThread* thread_; - - scoped_refptr jingle_client_; - scoped_refptr session_manager_; - scoped_refptr session_; - - scoped_ptr video_reader_; - - HostEventCallback* event_callback_; - VideoStub* video_stub_; - - std::string host_jid_; - - scoped_ptr dispatcher_; - - DISALLOW_COPY_AND_ASSIGN(JingleConnectionToHost); -}; - -} // namespace protocol -} // namespace remoting - -DISABLE_RUNNABLE_METHOD_REFCOUNT(remoting::protocol::JingleConnectionToHost); - -#endif // REMOTING_PROTOCOL_JINGLE_CONNECTION_TO_HOST_H_ -- cgit v1.1