2019-12-21 00:27:48 +01:00
|
|
|
package org.mercury_im.messenger.xmpp;
|
|
|
|
|
2020-05-05 22:08:04 +02:00
|
|
|
import org.jivesoftware.smack.AbstractXMPPConnection;
|
2020-01-05 15:54:44 +01:00
|
|
|
import org.jivesoftware.smack.ConnectionListener;
|
2020-05-05 22:08:04 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2019-12-21 00:27:48 +01:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2020-05-05 22:08:04 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2020-01-05 15:54:44 +01:00
|
|
|
import org.mercury_im.messenger.data.repository.AccountRepository;
|
2019-12-21 00:27:48 +01:00
|
|
|
import org.mercury_im.messenger.entity.Account;
|
2020-01-05 15:54:44 +01:00
|
|
|
import org.mercury_im.messenger.util.Optional;
|
2020-05-05 22:08:04 +02:00
|
|
|
import org.mercury_im.messenger.xmpp.state.ConnectionState;
|
|
|
|
import org.mercury_im.messenger.xmpp.state.ConnectivityState;
|
2020-01-05 15:54:44 +01:00
|
|
|
|
2020-05-05 22:08:04 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.UUID;
|
2020-01-06 01:27:11 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2020-01-05 23:13:58 +01:00
|
|
|
import io.reactivex.Observable;
|
2020-01-05 15:54:44 +01:00
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
|
|
import io.reactivex.subjects.BehaviorSubject;
|
2020-05-05 22:08:04 +02:00
|
|
|
import lombok.Getter;
|
2019-12-21 00:27:48 +01:00
|
|
|
|
|
|
|
public class MercuryConnection {
|
|
|
|
|
2020-01-06 03:41:37 +01:00
|
|
|
private static final Logger LOGGER = Logger.getLogger("MercuryConnection");
|
2019-12-21 00:27:48 +01:00
|
|
|
|
2020-05-05 22:08:04 +02:00
|
|
|
@Getter
|
2019-12-21 00:27:48 +01:00
|
|
|
private XMPPConnection connection;
|
|
|
|
|
2020-05-05 22:08:04 +02:00
|
|
|
@Getter
|
|
|
|
private final UUID accountId;
|
2020-01-05 23:13:58 +01:00
|
|
|
|
2020-05-05 22:08:04 +02:00
|
|
|
private final BehaviorSubject<ConnectionState> state;
|
2020-01-05 15:54:44 +01:00
|
|
|
|
2020-05-05 22:08:04 +02:00
|
|
|
public MercuryConnection(XMPPConnection connection, Account account) {
|
|
|
|
this.connection = connection;
|
|
|
|
this.accountId = account.getId();
|
2020-01-05 15:54:44 +01:00
|
|
|
|
2020-05-05 22:08:04 +02:00
|
|
|
this.state = BehaviorSubject.createDefault(new ConnectionState(accountId,
|
|
|
|
ConnectivityState.disconnected, false, false));
|
2020-01-05 15:54:44 +01:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
|
2020-05-05 22:08:04 +02:00
|
|
|
public Observable<ConnectionState> observeConnection() {
|
|
|
|
return state;
|
2019-12-21 00:27:48 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:08:04 +02:00
|
|
|
public void connect() throws InterruptedException, XMPPException, SmackException, IOException {
|
|
|
|
if (getConnection().isConnected()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
((AbstractXMPPConnection) getConnection()).connect();
|
2019-12-21 00:27:48 +01:00
|
|
|
}
|
2020-01-05 15:54:44 +01:00
|
|
|
|
2020-05-05 22:08:04 +02:00
|
|
|
public void login() throws InterruptedException, IOException, SmackException, XMPPException {
|
|
|
|
if (getConnection().isAuthenticated()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
connect();
|
|
|
|
((AbstractXMPPConnection) getConnection()).login();
|
2020-01-05 15:54:44 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:08:04 +02:00
|
|
|
private final ConnectionListener connectionListener = new ConnectionListener() {
|
|
|
|
@Override
|
|
|
|
public void connected(XMPPConnection connection) {
|
|
|
|
state.onNext(state.getValue()
|
|
|
|
.withConnectivity(ConnectivityState.connected)
|
|
|
|
.withAuthenticated(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void authenticated(XMPPConnection connection, boolean resumed) {
|
|
|
|
state.onNext(state.getValue()
|
|
|
|
.withConnectivity(ConnectivityState.connected)
|
|
|
|
.withAuthenticated(true)
|
|
|
|
.withResumed(resumed));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void connectionClosed() {
|
|
|
|
state.onNext(state.getValue()
|
|
|
|
.withConnectivity(ConnectivityState.disconnected)
|
|
|
|
.withAuthenticated(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void connectionClosedOnError(Exception e) {
|
|
|
|
state.onNext(state.getValue()
|
|
|
|
.withConnectivity(ConnectivityState.disconnected)
|
|
|
|
.withAuthenticated(false));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public void shutdown() {
|
|
|
|
if (connection.isConnected()) {
|
|
|
|
((AbstractXMPPConnection) getConnection()).disconnect();
|
|
|
|
} else {
|
|
|
|
((AbstractXMPPConnection) getConnection()).instantShutdown();
|
|
|
|
}
|
2020-01-05 15:54:44 +01:00
|
|
|
}
|
2019-12-21 00:27:48 +01:00
|
|
|
}
|