fix connection pool not handling removes

This commit is contained in:
Daniel Gultsch 2023-03-02 15:43:56 +01:00
parent 0c4771e2a8
commit b2414434dc
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
4 changed files with 27 additions and 3 deletions

View file

@ -34,7 +34,8 @@ public class Account extends AccountIdentifier {
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), randomSeed);
// careful with hashCode and equals for byte arrays
return Objects.hashCode(super.hashCode(), Arrays.hashCode(randomSeed));
}
public boolean isOnion() {

View file

@ -15,6 +15,7 @@ import im.conversations.android.database.model.AccountIdentifier;
import im.conversations.android.database.model.Connection;
import im.conversations.android.tls.ScopeFingerprint;
import im.conversations.android.xmpp.ConnectionPool;
import im.conversations.android.xmpp.ConnectionState;
import im.conversations.android.xmpp.XmppConnection;
import im.conversations.android.xmpp.manager.RegistrationManager;
import java.io.IOException;
@ -129,6 +130,13 @@ public class AccountRepository extends AbstractRepository {
return database.accountDao().getAccounts();
}
public ConnectionState getConnectionState(final Account account) {
return ConnectionPool.getInstance(context)
.get(account)
.transform(XmppConnection::getStatus)
.orNull();
}
public LiveData<Boolean> hasNoAccounts() {
return database.accountDao().hasNoAccounts();
}

View file

@ -263,7 +263,13 @@ public class SetupViewModel extends AndroidViewModel {
private void setAccount(@NonNull final Account account) {
this.account = account;
this.registerTrustDecisionCallback();
// TODO if the connection is already TLS_ERROR then do a quick reconnect
final var state = this.accountRepository.getConnectionState(account);
if (Arrays.asList(ConnectionState.TLS_ERROR).contains(state)) {
LOGGER.info(
"Connection had already failed when trust decision callback was registered."
+ " reconnecting");
this.accountRepository.reconnect(account);
}
this.decideNextStep(Target.ENTER_ADDRESS, account);
}

View file

@ -138,6 +138,11 @@ public class ConnectionPool {
final Set<Account> current = getAccounts();
final Set<Account> removed = Sets.difference(current, accounts);
final Set<Account> added = Sets.difference(accounts, current);
LOGGER.info(
"reconfiguring. current {} added {} removed {}",
current.size(),
added.size(),
removed.size());
for (final Account account : added) {
this.setupXmppConnection(context, account);
}
@ -145,10 +150,14 @@ public class ConnectionPool {
final Optional<XmppConnection> connectionOptional =
Iterables.tryFind(connections, c -> c.getAccount().equals(account));
if (connectionOptional.isPresent()) {
final XmppConnection connection = connectionOptional.get();
final var connection = connectionOptional.get();
if (connections.remove(connection)) {
LOGGER.info("Removed connection for {}", account.address);
}
disconnect(connection, false);
}
}
LOGGER.info("now managing {} connections", connections.size());
return null;
}