60 lines
1.9 KiB
Java
60 lines
1.9 KiB
Java
package org.mercury_im.xmpp.connection;
|
|
|
|
import org.jivesoftware.smack.AbstractXMPPConnection;
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
import org.mercury_im.core.Messenger;
|
|
import org.mercury_im.core.transport.ConnectionType;
|
|
import org.mercury_im.core.transport.connection.AbstractConnectionMethod;
|
|
import org.mercury_im.messenger.core.entity.Account;
|
|
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
import io.reactivex.Completable;
|
|
|
|
public class XmppTcpConnectionMethod extends AbstractConnectionMethod {
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(XmppTcpConnectionMethod.class.getName());
|
|
|
|
private XMPPConnection connection;
|
|
|
|
public XmppTcpConnectionMethod(Account account, Messenger messenger, XMPPConnection connection) {
|
|
super(account, messenger);
|
|
this.connection = connection;
|
|
}
|
|
|
|
@Override
|
|
public Completable connect() {
|
|
if (connection.isConnected()) {
|
|
return Completable.complete();
|
|
}
|
|
|
|
return Completable.fromAction(
|
|
() -> {
|
|
AbstractXMPPConnection con = (AbstractXMPPConnection) connection;
|
|
try {
|
|
con.connect();
|
|
} catch (Exception e) {
|
|
LOGGER.log(Level.WARNING, "Exception while connecting to XMPP account " + account.getId(), e);
|
|
throw e;
|
|
}
|
|
|
|
try {
|
|
con.login();
|
|
} catch (Exception e) {
|
|
LOGGER.log(Level.WARNING, "Exception while logging into XMPP account " + account.getId(), e);
|
|
throw e;
|
|
}
|
|
});
|
|
}
|
|
|
|
public XMPPConnection getConnection() {
|
|
return connection;
|
|
}
|
|
|
|
@Override
|
|
public ConnectionType getConnectionType() {
|
|
return ConnectionType.SMACK_TCP;
|
|
}
|
|
}
|