Reuse socket of failed connection
This commit is contained in:
parent
4a11723a61
commit
c564cc2214
3 changed files with 30 additions and 16 deletions
|
@ -266,7 +266,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab
|
||||||
recoveryTimer.interrupt();
|
recoveryTimer.interrupt();
|
||||||
recoveryTimer = null;
|
recoveryTimer = null;
|
||||||
setState(InsightState.DISCONNECTED);
|
setState(InsightState.DISCONNECTED);
|
||||||
cleanup();
|
cleanup(true);
|
||||||
} else if (state != InsightState.DISCONNECTED) {
|
} else if (state != InsightState.DISCONNECTED) {
|
||||||
long disconnectTimeout = SP.getInt("insight_disconnect_delay", 5);
|
long disconnectTimeout = SP.getInt("insight_disconnect_delay", 5);
|
||||||
disconnectTimeout = Math.min(disconnectTimeout, 15);
|
disconnectTimeout = Math.min(disconnectTimeout, 15);
|
||||||
|
@ -281,7 +281,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab
|
||||||
return connectionRequests.contains(lock);
|
return connectionRequests.contains(lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cleanup() {
|
private void cleanup(boolean closeSocket) {
|
||||||
messageQueue.completeActiveRequest(new ConnectionLostException());
|
messageQueue.completeActiveRequest(new ConnectionLostException());
|
||||||
messageQueue.completePendingRequests(new ConnectionLostException());
|
messageQueue.completePendingRequests(new ConnectionLostException());
|
||||||
if (recoveryTimer != null) {
|
if (recoveryTimer != null) {
|
||||||
|
@ -301,10 +301,12 @@ public class InsightConnectionService extends Service implements ConnectionEstab
|
||||||
outputStreamWriter = null;
|
outputStreamWriter = null;
|
||||||
}
|
}
|
||||||
if (connectionEstablisher != null) {
|
if (connectionEstablisher != null) {
|
||||||
connectionEstablisher.close();
|
if (closeSocket) {
|
||||||
|
connectionEstablisher.close(closeSocket);
|
||||||
|
bluetoothSocket = null;
|
||||||
|
}
|
||||||
connectionEstablisher = null;
|
connectionEstablisher = null;
|
||||||
}
|
}
|
||||||
bluetoothSocket = null;
|
|
||||||
if (timeoutTimer != null) {
|
if (timeoutTimer != null) {
|
||||||
timeoutTimer.interrupt();
|
timeoutTimer.interrupt();
|
||||||
timeoutTimer = null;
|
timeoutTimer = null;
|
||||||
|
@ -331,7 +333,9 @@ public class InsightConnectionService extends Service implements ConnectionEstab
|
||||||
log.info("Exception occurred: " + e.getClass().getSimpleName());
|
log.info("Exception occurred: " + e.getClass().getSimpleName());
|
||||||
if (pairingDataStorage.isPaired()) {
|
if (pairingDataStorage.isPaired()) {
|
||||||
setState(connectionRequests.size() != 0 ? InsightState.RECOVERING : InsightState.DISCONNECTED);
|
setState(connectionRequests.size() != 0 ? InsightState.RECOVERING : InsightState.DISCONNECTED);
|
||||||
cleanup();
|
if (e instanceof ConnectionFailedException) {
|
||||||
|
cleanup(((ConnectionFailedException) e).getDurationOfConnectionAttempt() <= 1000);
|
||||||
|
} else cleanup(true);
|
||||||
messageQueue.completeActiveRequest(e);
|
messageQueue.completeActiveRequest(e);
|
||||||
messageQueue.completePendingRequests(e);
|
messageQueue.completePendingRequests(e);
|
||||||
if (connectionRequests.size() != 0) {
|
if (connectionRequests.size() != 0) {
|
||||||
|
@ -351,7 +355,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setState(InsightState.NOT_PAIRED);
|
setState(InsightState.NOT_PAIRED);
|
||||||
cleanup();
|
cleanup(true);
|
||||||
}
|
}
|
||||||
for (ExceptionCallback exceptionCallback : exceptionCallbacks)
|
for (ExceptionCallback exceptionCallback : exceptionCallbacks)
|
||||||
exceptionCallback.onExceptionOccur(e);
|
exceptionCallback.onExceptionOccur(e);
|
||||||
|
@ -362,7 +366,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab
|
||||||
sendAppLayerMessage(new DisconnectMessage());
|
sendAppLayerMessage(new DisconnectMessage());
|
||||||
sendSatlMessageAndWait(new info.nightscout.androidaps.plugins.PumpInsightLocal.satl.DisconnectMessage());
|
sendSatlMessageAndWait(new info.nightscout.androidaps.plugins.PumpInsightLocal.satl.DisconnectMessage());
|
||||||
}
|
}
|
||||||
cleanup();
|
cleanup(true);
|
||||||
setState(pairingDataStorage.isPaired() ? InsightState.DISCONNECTED : InsightState.NOT_PAIRED);
|
setState(pairingDataStorage.isPaired() ? InsightState.DISCONNECTED : InsightState.NOT_PAIRED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -377,7 +381,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab
|
||||||
if (connectionRequests.size() == 0)
|
if (connectionRequests.size() == 0)
|
||||||
throw new IllegalStateException("A connection lock must be hold for pairing");
|
throw new IllegalStateException("A connection lock must be hold for pairing");
|
||||||
log.info("Pairing initiated");
|
log.info("Pairing initiated");
|
||||||
cleanup();
|
cleanup(true);
|
||||||
pairingDataStorage.setMacAddress(macAddress);
|
pairingDataStorage.setMacAddress(macAddress);
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
|
@ -743,8 +747,8 @@ public class InsightConnectionService extends Service implements ConnectionEstab
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void onConnectionFail(Exception e) {
|
public synchronized void onConnectionFail(Exception e, long duration) {
|
||||||
handleException(new ConnectionFailedException());
|
handleException(new ConnectionFailedException(duration));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -2,4 +2,13 @@ package info.nightscout.androidaps.plugins.PumpInsightLocal.exceptions;
|
||||||
|
|
||||||
public class ConnectionFailedException extends InsightException {
|
public class ConnectionFailedException extends InsightException {
|
||||||
|
|
||||||
|
private long durationOfConnectionAttempt;
|
||||||
|
|
||||||
|
public ConnectionFailedException(long durationOfConnectionAttempt) {
|
||||||
|
this.durationOfConnectionAttempt = durationOfConnectionAttempt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDurationOfConnectionAttempt() {
|
||||||
|
return durationOfConnectionAttempt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ public class ConnectionEstablisher extends Thread {
|
||||||
Method removeBond = bluetoothDevice.getClass().getMethod("removeBond", (Class[]) null);
|
Method removeBond = bluetoothDevice.getClass().getMethod("removeBond", (Class[]) null);
|
||||||
removeBond.invoke(bluetoothDevice, (Object[]) null);
|
removeBond.invoke(bluetoothDevice, (Object[]) null);
|
||||||
} catch (ReflectiveOperationException e) {
|
} catch (ReflectiveOperationException e) {
|
||||||
if (!isInterrupted()) callback.onConnectionFail(e);
|
if (!isInterrupted()) callback.onConnectionFail(e, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,21 +49,22 @@ public class ConnectionEstablisher extends Thread {
|
||||||
callback.onSocketCreated(socket);
|
callback.onSocketCreated(socket);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (!isInterrupted()) callback.onConnectionFail(e);
|
if (!isInterrupted()) callback.onConnectionFail(e, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
long connectionStart = System.currentTimeMillis();
|
||||||
try {
|
try {
|
||||||
socket.connect();
|
socket.connect();
|
||||||
if (!isInterrupted()) callback.onConnectionSucceed();
|
if (!isInterrupted()) callback.onConnectionSucceed();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (!isInterrupted()) callback.onConnectionFail(e);
|
if (!isInterrupted()) callback.onConnectionFail(e, System.currentTimeMillis() - connectionStart);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close(boolean closeSocket) {
|
||||||
try {
|
try {
|
||||||
interrupt();
|
interrupt();
|
||||||
if (socket != null && socket.isConnected()) socket.close();
|
if (closeSocket && socket != null && socket.isConnected()) socket.close();
|
||||||
} catch (IOException ignored) {
|
} catch (IOException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,6 +74,6 @@ public class ConnectionEstablisher extends Thread {
|
||||||
|
|
||||||
void onConnectionSucceed();
|
void onConnectionSucceed();
|
||||||
|
|
||||||
void onConnectionFail(Exception e);
|
void onConnectionFail(Exception e, long duration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue