Let the limiter know whether connections closed cleanly.

This commit is contained in:
akwizgran
2020-05-06 14:59:36 +01:00
parent bda3b2100a
commit bd86ff2d5f
5 changed files with 12 additions and 9 deletions

View File

@@ -50,7 +50,7 @@ class AndroidBluetoothTransportConnection
try { try {
socket.close(); socket.close();
} finally { } finally {
connectionLimiter.connectionClosed(this); connectionLimiter.connectionClosed(this, exception);
} }
} }
} }

View File

@@ -45,11 +45,13 @@ interface BluetoothConnectionLimiter {
/** /**
* Informs the limiter that the given connection has been closed. * Informs the limiter that the given connection has been closed.
*
* @param exception True if the connection was closed due to an exception.
*/ */
void connectionClosed(DuplexTransportConnection conn); void connectionClosed(DuplexTransportConnection conn, boolean exception);
/** /**
* Informs the limiter that all connections have been closed. * Informs the limiter that the Bluetooth adapter has been disabled.
*/ */
void allConnectionsClosed(); void bluetoothDisabled();
} }

View File

@@ -126,7 +126,8 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
} }
@Override @Override
public void connectionClosed(DuplexTransportConnection conn) { public void connectionClosed(DuplexTransportConnection conn,
boolean exception) {
synchronized (lock) { synchronized (lock) {
Iterator<ConnectionRecord> it = connections.iterator(); Iterator<ConnectionRecord> it = connections.iterator();
while (it.hasNext()) { while (it.hasNext()) {
@@ -141,10 +142,10 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
} }
@Override @Override
public void allConnectionsClosed() { public void bluetoothDisabled() {
synchronized (lock) { synchronized (lock) {
connections.clear(); connections.clear();
LOG.info("All connections closed"); LOG.info("Bluetooth disabled");
} }
} }

View File

@@ -130,7 +130,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
void onAdapterDisabled() { void onAdapterDisabled() {
LOG.info("Bluetooth disabled"); LOG.info("Bluetooth disabled");
tryToClose(socket); tryToClose(socket);
connectionLimiter.allConnectionsClosed(); connectionLimiter.bluetoothDisabled();
callback.transportDisabled(); callback.transportDisabled();
} }

View File

@@ -45,7 +45,7 @@ class JavaBluetoothTransportConnection
try { try {
stream.close(); stream.close();
} finally { } finally {
connectionLimiter.connectionClosed(this); connectionLimiter.connectionClosed(this, exception);
} }
} }
} }