OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/client/host_connection.h" |
| 6 |
| 7 namespace remoting { |
| 8 |
| 9 HostConnection::HostConnection(ProtocolDecoder* decoder, |
| 10 EventHandler* handler) |
| 11 : decoder_(decoder), handler_(handler) { |
| 12 } |
| 13 |
| 14 HostConnection::~HostConnection() { |
| 15 Disconnect(); |
| 16 } |
| 17 |
| 18 void HostConnection::Connect(const std::string& username, |
| 19 const std::string& password, |
| 20 const std::string& host_jid) { |
| 21 jingle_client_ = new JingleClient(); |
| 22 jingle_client_->Init(username, password, this); |
| 23 jingle_channel_ = jingle_client_->Connect(host_jid, this); |
| 24 } |
| 25 |
| 26 void HostConnection::Disconnect() { |
| 27 if (jingle_channel_.get()) |
| 28 jingle_channel_->Close(); |
| 29 |
| 30 if (jingle_client_.get()) |
| 31 jingle_client_->Close(); |
| 32 } |
| 33 |
| 34 void HostConnection::OnStateChange(JingleChannel* channel, |
| 35 JingleChannel::State state) { |
| 36 DCHECK(handler_); |
| 37 if (state == JingleChannel::FAILED) |
| 38 handler_->OnConnectionFailed(this); |
| 39 else if (state == JingleChannel::CLOSED) |
| 40 handler_->OnConnectionClosed(this); |
| 41 else if (state == JingleChannel::OPEN) |
| 42 handler_->OnConnectionOpened(this); |
| 43 } |
| 44 |
| 45 void HostConnection::OnPacketReceived(JingleChannel* channel, |
| 46 scoped_refptr<media::DataBuffer> buffer) { |
| 47 HostMessageList list; |
| 48 decoder_->ParseHostMessages(buffer, &list); |
| 49 DCHECK(handler_); |
| 50 handler_->HandleMessages(this, &list); |
| 51 } |
| 52 |
| 53 // JingleClient::Callback interface. |
| 54 void HostConnection::OnStateChange(JingleClient* client, |
| 55 JingleClient::State state) { |
| 56 DCHECK(client); |
| 57 DCHECK(handler_); |
| 58 if (state == JingleClient::CONNECTED) { |
| 59 LOG(INFO) << "Connected as: " << client->GetFullJid(); |
| 60 } else if (state == JingleClient::CLOSED) { |
| 61 LOG(INFO) << "Connection closed."; |
| 62 handler_->OnConnectionClosed(this); |
| 63 } |
| 64 } |
| 65 |
| 66 bool HostConnection::OnAcceptConnection(JingleClient* client, |
| 67 const std::string& jid, |
| 68 JingleChannel::Callback** callback) { |
| 69 // Client rejects all connection. |
| 70 return false; |
| 71 } |
| 72 |
| 73 void HostConnection::OnNewConnection(JingleClient* client, |
| 74 scoped_refptr<JingleChannel> channel) { |
| 75 NOTREACHED() << "SimpleClient can't accept connection."; |
| 76 } |
| 77 |
| 78 } // namespace remoting |
OLD | NEW |