summaryrefslogtreecommitdiffstats
path: root/remoting/client/jingle_host_connection.cc
blob: c48ea61ed4e7e50b29e51500d23c2b9744be5075 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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/callback.h"
#include "base/message_loop.h"
#include "remoting/base/constants.h"
// TODO(hclam): Remove this header once MessageDispatcher is used.
#include "remoting/base/multiple_array_input_stream.h"
#include "remoting/client/client_config.h"
#include "remoting/client/jingle_host_connection.h"
#include "remoting/jingle_glue/jingle_thread.h"
#include "remoting/protocol/jingle_chromotocol_server.h"
#include "remoting/protocol/video_stub.h"
#include "remoting/protocol/util.h"

namespace remoting {

JingleHostConnection::JingleHostConnection(ClientContext* context)
    : context_(context),
      event_callback_(NULL) {
}

JingleHostConnection::~JingleHostConnection() {
}

void JingleHostConnection::Connect(const ClientConfig& config,
                                   HostEventCallback* event_callback,
                                   VideoStub* video_stub) {
  event_callback_ = event_callback;
  video_stub_ = video_stub;

  // Initialize |jingle_client_|.
  jingle_client_ = new JingleClient(context_->jingle_thread());
  jingle_client_->Init(config.username, config.auth_token,
                       kChromotingTokenServiceName, this);

  // Save jid of the host. The actual connection is created later after
  // |jingle_client_| is connected.
  host_jid_ = config.host_jid;
}

void JingleHostConnection::Disconnect() {
  if (MessageLoop::current() != message_loop()) {
    message_loop()->PostTask(
        FROM_HERE, NewRunnableMethod(this,
                                     &JingleHostConnection::Disconnect));
    return;
  }

  control_reader_.Close();
  event_writer_.Close();
  video_reader_->Close();

  if (connection_) {
    connection_->Close(
        NewRunnableMethod(this, &JingleHostConnection::OnDisconnected));
  } else {
    OnDisconnected();
  }
}

void JingleHostConnection::OnControlMessage(ChromotingHostMessage* msg) {
  event_callback_->HandleMessage(this, msg);
}

void JingleHostConnection::InitConnection() {
  DCHECK_EQ(message_loop(), MessageLoop::current());

  // Initialize |chromotocol_server_|.
  JingleChromotocolServer* chromotocol_server =
      new JingleChromotocolServer(context_->jingle_thread());
  // TODO(ajwong): Make this a command switch when we're more stable.
  chromotocol_server->set_allow_local_ips(true);
  chromotocol_server->Init(
      jingle_client_->GetFullJid(),
      jingle_client_->session_manager(),
      NewCallback(this, &JingleHostConnection::OnNewChromotocolConnection));
  chromotocol_server_ = chromotocol_server;

  CandidateChromotocolConfig* candidate_config =
      CandidateChromotocolConfig::CreateDefault();
  // TODO(sergeyu): Set resolution in the |candidate_config| to the desired
  // resolution.

  // Initialize |connection_|.
  connection_ = chromotocol_server_->Connect(
      host_jid_, candidate_config,
      NewCallback(this, &JingleHostConnection::OnConnectionStateChange));
}

void JingleHostConnection::OnDisconnected() {
  connection_ = NULL;

  if (chromotocol_server_) {
    chromotocol_server_->Close(
        NewRunnableMethod(this, &JingleHostConnection::OnServerClosed));
  } else {
    OnServerClosed();
  }
}

void JingleHostConnection::OnServerClosed() {
  chromotocol_server_ = NULL;
  if (jingle_client_) {
    jingle_client_->Close();
    jingle_client_ = NULL;
  }
}

void JingleHostConnection::SendEvent(const ChromotingClientMessage& msg) {
  // This drops the message if we are not connected yet.
  event_writer_.SendMessage(msg);
}

// 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) {
    VLOG(1) << "Connected as: " << client->GetFullJid();
    InitConnection();
  } else if (state == JingleClient::CLOSED) {
    VLOG(1) << "Connection closed.";
    event_callback_->OnConnectionClosed(this);
  }
}

void JingleHostConnection::OnNewChromotocolConnection(
    ChromotocolConnection* connection,
    ChromotocolServer::IncomingConnectionResponse* response) {
  DCHECK_EQ(message_loop(), MessageLoop::current());
  // Client always rejects incoming connections.
  *response = ChromotocolServer::DECLINE;
}

void JingleHostConnection::OnConnectionStateChange(
    ChromotocolConnection::State state) {
  DCHECK_EQ(message_loop(), MessageLoop::current());
  DCHECK(event_callback_);

  switch (state) {
    case ChromotocolConnection::FAILED:
      event_callback_->OnConnectionFailed(this);
      break;

    case ChromotocolConnection::CLOSED:
      event_callback_->OnConnectionClosed(this);
      break;

    case ChromotocolConnection::CONNECTED:
      // Initialize reader and writer.
      control_reader_.Init<ChromotingHostMessage>(
          connection_->control_channel(),
          NewCallback(this, &JingleHostConnection::OnControlMessage));
      event_writer_.Init(connection_->event_channel());
      video_reader_.reset(VideoReader::Create(connection_->config()));
      video_reader_->Init(connection_, video_stub_);
      event_callback_->OnConnectionOpened(this);
      break;

    default:
      // Ignore the other states by default.
      break;
  }
}

MessageLoop* JingleHostConnection::message_loop() {
  return context_->jingle_thread()->message_loop();
}

}  // namespace remoting