Mercury-IM/domain/src/main/java/org/mercury_im/messenger/core/xmpp/MercuryConnection.java

158 lines
5.9 KiB
Java
Raw Normal View History

2020-06-06 18:45:20 +02:00
package org.mercury_im.messenger.core.xmpp;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionListener;
2020-06-01 10:24:49 +02:00
import org.jivesoftware.smack.ReconnectionListener;
import org.jivesoftware.smack.ReconnectionManager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
2020-05-11 16:31:07 +02:00
import org.jivesoftware.smack.sasl.SASLErrorException;
2020-06-06 18:45:20 +02:00
import org.mercury_im.messenger.core.xmpp.exception.InvalidCredentialsException;
import org.mercury_im.messenger.core.xmpp.exception.ServerUnreachableException;
import org.mercury_im.messenger.core.xmpp.state.ConnectivityState;
import org.mercury_im.messenger.entity.Account;
2020-06-06 18:45:20 +02:00
import org.mercury_im.messenger.core.xmpp.state.ConnectionState;
import java.io.IOException;
import java.util.UUID;
2020-05-31 22:32:33 +02:00
import java.util.logging.Level;
2020-01-06 01:27:11 +01:00
import java.util.logging.Logger;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;
import lombok.Getter;
public class MercuryConnection {
2020-01-06 03:41:37 +01:00
private static final Logger LOGGER = Logger.getLogger("MercuryConnection");
@Getter
private XMPPConnection connection;
@Getter
2020-05-16 15:53:54 +02:00
private final Account account;
private final BehaviorSubject<ConnectionState> state;
public MercuryConnection(XMPPConnection connection, Account account) {
this.connection = connection;
2020-05-16 15:53:54 +02:00
this.account = account;
2020-05-16 15:53:54 +02:00
this.state = BehaviorSubject.createDefault(new ConnectionState(account.getId(), this,
ConnectivityState.disconnected, false, false));
2020-05-23 12:01:31 +02:00
connection.addConnectionListener(connectionListener);
}
2020-05-16 15:53:54 +02:00
public UUID getAccountId() {
return getAccount().getId();
}
public Observable<ConnectionState> observeConnection() {
return state;
}
public Completable connect() {
2020-06-06 17:23:38 +02:00
return Completable.fromAction(this::doConnect)
.doOnError(error -> LOGGER.log(Level.WARNING, "Connection error for account " + account, error));
2020-05-11 16:31:07 +02:00
}
private void doConnect() throws ServerUnreachableException {
2020-05-31 22:32:33 +02:00
state.onNext(state.getValue().withConnectivity(ConnectivityState.connecting));
2020-05-11 16:31:07 +02:00
AbstractXMPPConnection connection = (AbstractXMPPConnection) getConnection();
2020-06-24 22:50:26 +02:00
if (connection.isConnected()) {
return;
}
2020-05-11 16:31:07 +02:00
try {
2020-06-24 22:50:26 +02:00
LOGGER.log(Level.INFO, "Connected!");
2020-05-11 16:31:07 +02:00
connection.connect();
} catch (SmackException.EndpointConnectionException e) {
connection.disconnect();
throw new ServerUnreachableException("Cannot connect to server " + connection.getXMPPServiceDomain().asUnescapedString(), e);
} catch (IOException | InterruptedException | XMPPException | SmackException e) {
throw new AssertionError("Unexpected exception.", e);
}
}
public Completable login() {
2020-06-06 17:23:38 +02:00
return Completable.fromAction(this::doLogin)
.doOnError(error -> LOGGER.log(Level.WARNING, "Login error for account " + account, error));
2020-05-11 16:31:07 +02:00
}
2020-05-23 12:01:31 +02:00
private void doLogin() throws InvalidCredentialsException {
2020-06-24 22:50:26 +02:00
if (connection.isAuthenticated()) {
return;
}
2020-05-11 16:31:07 +02:00
try {
((AbstractXMPPConnection) getConnection()).login();
} catch (SASLErrorException e) {
2020-05-16 15:53:54 +02:00
throw new InvalidCredentialsException("Credentials of account " + account.getId() + " are invalid.", e);
2020-05-11 16:31:07 +02:00
} catch (InterruptedException | XMPPException | SmackException | IOException e) {
throw new AssertionError("Unexpected exception.", e);
}
}
2020-05-31 22:32:33 +02:00
public Completable shutdown() {
2020-06-06 17:23:38 +02:00
return Completable.fromAction(this::doShutdown)
.doOnError(error -> LOGGER.log(Level.WARNING, "Shutdown error for account " + account, error));
2020-05-31 22:32:33 +02:00
}
public void doShutdown() {
if (connection.isConnected()) {
((AbstractXMPPConnection) getConnection()).disconnect();
} else {
((AbstractXMPPConnection) getConnection()).instantShutdown();
}
}
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));
2020-06-01 10:24:49 +02:00
if (!resumed) {
initialConnectionSetup();
}
}
@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));
}
};
2020-06-01 10:24:49 +02:00
private void initialConnectionSetup() {
ReconnectionManager.getInstanceFor((AbstractXMPPConnection) getConnection())
.addReconnectionListener(new ReconnectionListener() {
@Override
public void reconnectingIn(int seconds) {
LOGGER.log(Level.FINER, "Reconnecting connection " + getAccount().getAddress() + " in " + seconds + " seconds.");
}
@Override
public void reconnectionFailed(Exception e) {
LOGGER.log(Level.WARNING, "Reconnection of connection " + getAccount().getAddress() + " failed.", e);
}
});
}
}