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;
|
2019-08-12 00:34:19 +02:00
|
|
|
import org.jivesoftware.smackx.caps.EntityCapsManager;
|
2019-08-04 04:22:08 +02:00
|
|
|
import org.mercury_im.messenger.persistence.model.AccountModel;
|
2019-08-12 00:34:19 +02:00
|
|
|
import org.mercury_im.messenger.persistence.repository.AccountRepository;
|
2019-08-04 04:22:08 +02:00
|
|
|
|
2019-08-12 00:34:19 +02:00
|
|
|
import java.util.Collections;
|
2019-07-31 22:59:46 +02:00
|
|
|
import java.util.HashMap;
|
2019-08-12 00:34:19 +02:00
|
|
|
import java.util.List;
|
2019-07-31 22:59:46 +02:00
|
|
|
import java.util.Map;
|
2019-08-12 00:34:19 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2019-07-31 22:59:46 +02:00
|
|
|
|
2019-08-10 21:50:03 +02:00
|
|
|
import javax.inject.Inject;
|
2019-08-03 19:05:50 +02:00
|
|
|
import javax.inject.Singleton;
|
|
|
|
|
2019-08-12 00:34:19 +02:00
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
|
|
import io.reactivex.functions.Consumer;
|
|
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
|
2019-08-03 19:05:50 +02:00
|
|
|
@Singleton
|
2019-07-31 22:59:46 +02:00
|
|
|
public class ConnectionCenter {
|
|
|
|
|
2019-08-12 00:34:19 +02:00
|
|
|
// Injected
|
|
|
|
private final AccountRepository accountRepository;
|
|
|
|
private final EntityCapsStore entityCapsStore;
|
2019-07-31 22:59:46 +02:00
|
|
|
|
2019-08-12 00:34:19 +02:00
|
|
|
// Connections
|
|
|
|
private final Map<Long, MercuryConnection> connectionMap =
|
|
|
|
Collections.synchronizedMap(new HashMap<>());
|
2019-07-31 22:59:46 +02:00
|
|
|
|
2019-08-12 00:34:19 +02:00
|
|
|
// Disposable for rx
|
|
|
|
private final CompositeDisposable disposable = new CompositeDisposable();
|
2019-08-10 21:50:03 +02:00
|
|
|
|
2019-08-12 00:34:19 +02:00
|
|
|
private final AtomicBoolean started = new AtomicBoolean(false);
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
public ConnectionCenter(EntityCapsStore capsStore, AccountRepository accountRepository) {
|
|
|
|
this.accountRepository = accountRepository;
|
|
|
|
this.entityCapsStore = capsStore;
|
2019-07-31 22:59:46 +02:00
|
|
|
|
2019-08-12 00:34:19 +02:00
|
|
|
EntityCapsManager.setPersistentCache(capsStore);
|
2019-07-31 22:59:46 +02:00
|
|
|
}
|
|
|
|
|
2019-08-12 00:34:19 +02:00
|
|
|
/**
|
|
|
|
* Start up the center by subscribing to changes of the {@link AccountModel accounts} in the
|
|
|
|
* database. For each new {@link AccountModel} it creates a {@link MercuryConnection} and
|
|
|
|
* stores it in the {@link #connectionMap}.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public synchronized void startUp() {
|
|
|
|
|
|
|
|
if (started.getAndSet(true)) {
|
|
|
|
// already started.
|
|
|
|
return;
|
2019-07-31 22:59:46 +02:00
|
|
|
}
|
2019-08-12 00:34:19 +02:00
|
|
|
|
|
|
|
// otherwise subscribe to accounts and create connections.
|
|
|
|
disposable.add(accountRepository.getAllAccounts()
|
|
|
|
.observeOn(Schedulers.io())
|
|
|
|
.subscribeOn(Schedulers.computation())
|
|
|
|
.subscribe((Consumer<List<? extends AccountModel>>) accounts -> {
|
|
|
|
for (AccountModel account : accounts) {
|
|
|
|
if (connectionMap.get(account.getId()) != null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
MercuryConnection connection = createConnection(account);
|
|
|
|
connectionMap.put(account.getId(), connection);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
public MercuryConnection getConnection(AccountModel account) {
|
|
|
|
return getConnection(account.getId());
|
2019-07-31 22:59:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-08-12 00:34:19 +02:00
|
|
|
.setEnabledSSLCiphers(MercuryConfiguration.enabledCiphers)
|
|
|
|
.setEnabledSSLProtocols(MercuryConfiguration.enabledProtocols)
|
2019-08-04 04:22:08 +02:00
|
|
|
.build();
|
|
|
|
|
|
|
|
AbstractXMPPConnection tcpConnection = new XMPPTCPConnection(configuration);
|
|
|
|
return new MercuryConnection(tcpConnection, accountModel.getId());
|
|
|
|
}
|
2019-07-31 22:59:46 +02:00
|
|
|
}
|