2020-07-18 12:47:52 +02:00
|
|
|
package org.mercury_im.messenger.core.connection;
|
2020-06-06 18:54:56 +02:00
|
|
|
|
|
|
|
import org.jivesoftware.smack.AbstractXMPPConnection;
|
2020-07-30 19:52:58 +02:00
|
|
|
import org.jivesoftware.smack.packet.id.StanzaIdSource;
|
|
|
|
import org.jivesoftware.smack.packet.id.StanzaIdSourceFactory;
|
2020-06-06 18:54:56 +02:00
|
|
|
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
|
|
|
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
|
|
|
import org.jxmpp.stringprep.XmppStringprepException;
|
|
|
|
import org.mercury_im.messenger.entity.Account;
|
|
|
|
|
2020-07-30 19:52:58 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2020-06-06 18:54:56 +02:00
|
|
|
public class XmppTcpConnectionFactory implements XmppConnectionFactory {
|
|
|
|
|
|
|
|
private static final int CONNECTION_TIMEOUT = 30 * 1000;
|
|
|
|
|
2020-07-30 19:52:58 +02:00
|
|
|
private final StanzaIdSourceFactory stanzaIdSourceFactory;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
public XmppTcpConnectionFactory(StanzaIdSourceFactory stanzaIdSourceFactory) {
|
|
|
|
this.stanzaIdSourceFactory = stanzaIdSourceFactory;
|
|
|
|
}
|
|
|
|
|
2020-06-06 18:54:56 +02:00
|
|
|
public AbstractXMPPConnection createConnection(Account account) {
|
|
|
|
try {
|
|
|
|
XMPPTCPConnectionConfiguration.Builder configBuilder =
|
|
|
|
XMPPTCPConnectionConfiguration.builder()
|
2020-07-30 19:52:58 +02:00
|
|
|
.setStanzaIdSourceFactory(stanzaIdSourceFactory)
|
2020-06-06 18:54:56 +02:00
|
|
|
.setConnectTimeout(CONNECTION_TIMEOUT)
|
|
|
|
.setXmppAddressAndPassword(account.getAddress(), account.getPassword());
|
|
|
|
if (account.getHost() != null) {
|
|
|
|
configBuilder.setHost(account.getHost());
|
|
|
|
}
|
|
|
|
if (account.getPort() != 0) {
|
|
|
|
configBuilder.setPort(account.getPort());
|
|
|
|
}
|
|
|
|
return new XMPPTCPConnection(configBuilder.build());
|
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
throw new AssertionError("Account has invalid address: " + account.getAddress(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|