1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
// 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.
#include "base/message_loop.h"
#include "remoting/base/constants.h"
#include "remoting/client/jingle_host_connection.h"
#include "remoting/jingle_glue/jingle_thread.h"
namespace remoting {
JingleHostConnection::JingleHostConnection(JingleThread* network_thread)
: network_thread_(network_thread),
event_callback_(NULL) {
}
JingleHostConnection::~JingleHostConnection() {
}
void JingleHostConnection::Connect(const std::string& username,
const std::string& password,
const std::string& host_jid,
HostEventCallback* event_callback) {
message_loop()->PostTask(
FROM_HERE,
NewRunnableMethod(this, &JingleHostConnection::DoConnect,
username, password, host_jid,
event_callback));
}
void JingleHostConnection::Disconnect() {
message_loop()->PostTask(
FROM_HERE,
NewRunnableMethod(this, &JingleHostConnection::DoDisconnect));
}
void JingleHostConnection::OnStateChange(JingleChannel* channel,
JingleChannel::State state) {
DCHECK_EQ(message_loop(), MessageLoop::current());
DCHECK(event_callback_);
switch (state) {
case JingleChannel::FAILED:
event_callback_->OnConnectionFailed(this);
break;
case JingleChannel::CLOSED:
event_callback_->OnConnectionClosed(this);
break;
case JingleChannel::OPEN:
event_callback_->OnConnectionOpened(this);
break;
default:
// Ignore the other states by default.
break;
}
}
void JingleHostConnection::OnPacketReceived(
JingleChannel* channel,
scoped_refptr<media::DataBuffer> buffer) {
DCHECK_EQ(message_loop(), MessageLoop::current());
DCHECK(event_callback_);
HostMessageList list;
decoder_.ParseHostMessages(buffer, &list);
event_callback_->HandleMessages(this, &list);
}
// JingleClient::Callback interface.
void JingleHostConnection::OnStateChange(JingleClient* client,
JingleClient::State state) {
DCHECK_EQ(message_loop(), MessageLoop::current());
DCHECK(client);
DCHECK(event_callback_);
if (state == JingleClient::CONNECTED) {
LOG(INFO) << "Connected as: " << client->GetFullJid();
} else if (state == JingleClient::CLOSED) {
LOG(INFO) << "Connection closed.";
event_callback_->OnConnectionClosed(this);
}
}
bool JingleHostConnection::OnAcceptConnection(
JingleClient* client,
const std::string& jid,
JingleChannel::Callback** callback) {
DCHECK_EQ(message_loop(), MessageLoop::current());
// Client rejects all connection.
return false;
}
void JingleHostConnection::OnNewConnection(
JingleClient* client,
scoped_refptr<JingleChannel> channel) {
DCHECK_EQ(message_loop(), MessageLoop::current());
// TODO(ajwong): Should we log more aggressively on this and above? We
// shouldn't be getting any inbound connections.
NOTREACHED() << "Clients can't accept inbound connections.";
}
MessageLoop* JingleHostConnection::message_loop() {
return network_thread_->message_loop();
}
void JingleHostConnection::DoConnect(const std::string& username,
const std::string& auth_token,
const std::string& host_jid,
HostEventCallback* event_callback) {
DCHECK_EQ(message_loop(), MessageLoop::current());
event_callback_ = event_callback;
jingle_client_ = new JingleClient(network_thread_);
jingle_client_->Init(username, auth_token, kChromotingTokenServiceName, this);
jingle_channel_ = jingle_client_->Connect(host_jid, this);
}
void JingleHostConnection::DoDisconnect() {
DCHECK_EQ(message_loop(), MessageLoop::current());
if (jingle_channel_.get()) {
jingle_channel_->Close();
jingle_channel_ = NULL;
}
if (jingle_client_.get()) {
jingle_client_->Close();
jingle_client_ = NULL;
}
}
} // namespace remoting
|