This source file includes following definitions.
- connectToHost
- hostIncomplete
- reloadHostListAndConnect
- onConnectionState
- onHostListReceived
- onError
package org.chromium.chromoting;
import org.chromium.chromoting.jni.JniInterface;
public class SessionConnector implements JniInterface.ConnectionListener,
HostListLoader.Callback {
private JniInterface.ConnectionListener mConnectionCallback;
private HostListLoader.Callback mHostListCallback;
private HostListLoader mHostListLoader;
private String mAccountName;
private String mAuthToken;
private String mHostId;
private String mHostJabberId;
public SessionConnector(JniInterface.ConnectionListener connectionCallback,
HostListLoader.Callback hostListCallback, HostListLoader hostListLoader) {
mConnectionCallback = connectionCallback;
mHostListCallback = hostListCallback;
mHostListLoader = hostListLoader;
}
public void connectToHost(String accountName, String authToken, HostInfo host) {
mAccountName = accountName;
mAuthToken = authToken;
mHostId = host.id;
mHostJabberId = host.jabberId;
if (hostIncomplete(host)) {
reloadHostListAndConnect();
return;
}
JniInterface.connectToHost(accountName, authToken, host.jabberId, host.id, host.publicKey,
this);
}
private static boolean hostIncomplete(HostInfo host) {
return host.jabberId.isEmpty() || host.publicKey.isEmpty();
}
private void reloadHostListAndConnect() {
mHostListLoader.retrieveHostList(mAuthToken, this);
}
@Override
public void onConnectionState(JniInterface.ConnectionListener.State state,
JniInterface.ConnectionListener.Error error) {
if (state == JniInterface.ConnectionListener.State.FAILED &&
error == JniInterface.ConnectionListener.Error.PEER_IS_OFFLINE) {
reloadHostListAndConnect();
} else {
mConnectionCallback.onConnectionState(state, error);
}
}
@Override
public void onHostListReceived(HostInfo[] hosts) {
mHostListCallback.onHostListReceived(hosts);
HostInfo foundHost = null;
for (HostInfo host : hosts) {
if (host.id.equals(mHostId)) {
foundHost = host;
break;
}
}
if (foundHost == null || foundHost.jabberId.equals(mHostJabberId)
|| hostIncomplete(foundHost)) {
mConnectionCallback.onConnectionState(JniInterface.ConnectionListener.State.FAILED,
JniInterface.ConnectionListener.Error.PEER_IS_OFFLINE);
} else {
JniInterface.connectToHost(mAccountName, mAuthToken, foundHost.jabberId,
foundHost.id, foundHost.publicKey, mConnectionCallback);
}
}
@Override
public void onError(HostListLoader.Error error) {
mConnectionCallback.onConnectionState(JniInterface.ConnectionListener.State.FAILED,
JniInterface.ConnectionListener.Error.PEER_IS_OFFLINE);
mHostListCallback.onError(error);
}
}