2019-08-03 19:05:50 +02:00
|
|
|
package org.mercury_im.messenger.core;
|
2019-07-31 22:59:46 +02:00
|
|
|
|
2019-08-04 04:22:08 +02:00
|
|
|
import org.jivesoftware.smack.AbstractXMPPConnection;
|
|
|
|
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
|
|
|
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
|
|
|
import org.mercury_im.messenger.persistence.model.AccountModel;
|
|
|
|
|
2019-07-31 22:59:46 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2019-08-03 19:05:50 +02:00
|
|
|
import javax.inject.Singleton;
|
|
|
|
|
|
|
|
@Singleton
|
2019-07-31 22:59:46 +02:00
|
|
|
public class ConnectionCenter {
|
|
|
|
|
|
|
|
private static ConnectionCenter INSTANCE;
|
|
|
|
|
|
|
|
private final Map<Long, MercuryConnection> connectionMap = new HashMap<>();
|
|
|
|
|
|
|
|
private ConnectionCenter() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ConnectionCenter get() {
|
|
|
|
if (INSTANCE == null) {
|
|
|
|
INSTANCE = new ConnectionCenter();
|
|
|
|
}
|
|
|
|
return INSTANCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public MercuryConnection getConnection(long accountId) {
|
|
|
|
return connectionMap.get(accountId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void putConnection(MercuryConnection connection) {
|
|
|
|
connectionMap.put(connection.getAccountId(), connection);
|
|
|
|
}
|
2019-08-04 04:22:08 +02:00
|
|
|
|
|
|
|
public MercuryConnection createConnection(AccountModel accountModel) {
|
|
|
|
XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder()
|
|
|
|
.setHost(accountModel.getJid().getDomain().toString())
|
|
|
|
.setXmppAddressAndPassword(accountModel.getJid(), accountModel.getPassword())
|
|
|
|
.setConnectTimeout(2 * 60 * 1000)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
AbstractXMPPConnection tcpConnection = new XMPPTCPConnection(configuration);
|
|
|
|
return new MercuryConnection(tcpConnection, accountModel.getId());
|
|
|
|
}
|
2019-07-31 22:59:46 +02:00
|
|
|
}
|