Skip to content

Commit

Permalink
handle connection timeouts on solc api sever #188
Browse files Browse the repository at this point in the history
  • Loading branch information
DevKevYT committed Oct 7, 2022
1 parent 2f37618 commit 14fa3ed
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
30 changes: 19 additions & 11 deletions lib/core/excel/solc_api_manager.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

Expand All @@ -20,6 +21,7 @@ class SOLCApiManager {

String _inetAddress;
int _port;
static const int timeoutSeconds = 5; //Throw a timeout if a response does not come within x seconds

static final Version buildRequired = Version.of("2.1.5");

Expand Down Expand Up @@ -105,23 +107,25 @@ class SOLCApiManager {
try {
_activeSockets = _activeSockets + 1;
final socket = await Socket.connect(_inetAddress, _port);

//Sende den Befehl
socket.writeln(command);
await socket.flush();
bool awaitFileStream = false;

if (downloadBytes != null) {
downloadBytes.clear();
}


void throwException(Exception e) {
exception = e;
socket.close();
}

bool awaitFileStream = false;

if (downloadBytes != null) {
downloadBytes.clear();
}

var subscription = socket.listen(
(event) async {

if (awaitFileStream) {
if (downloadBytes == null) {
throwException(DownloadFileNotFoundException("Kein Ziel zum Download angegeben"));
Expand Down Expand Up @@ -183,17 +187,21 @@ class SOLCApiManager {
},
);

await subscription.asFuture<void>();
await subscription.asFuture<void>().timeout(const Duration(seconds: timeoutSeconds), onTimeout: () {
throwException(SOLCServerResponseTimeoutException("Timeout after $timeoutSeconds seconds"));
});

await socket.close();
await subscription.cancel();

_activeSockets--;
//log.d("Socket closed naturally. " + _activeSockets.toString() + " active sockets.");
log.d("Connection for SOLC command '$command' closed. $_activeSockets active connections.");

} on Exception catch (error) {
_activeSockets--;
throw FailedToEstablishSOLCServerConnection(
throw FailedToEstablishSOLCServerConnection (
"Konnte keine Verbindung zum Konvertierungsserver $_inetAddress herstellen: $error");
}

if (exception != null) {
throw exception;
}
Expand Down
10 changes: 10 additions & 0 deletions lib/core/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ class FailedToEstablishSOLCServerConnection implements Exception {
}
}

class SOLCServerResponseTimeoutException implements Exception {
String cause = "";
SOLCServerResponseTimeoutException(this.cause);

@override
String toString() {
return "$runtimeType: $cause";
}
}

class ExcelConversionConnectionError implements Exception {
String cause = "";
ExcelConversionConnectionError(this.cause);
Expand Down
14 changes: 12 additions & 2 deletions lib/ui/screens/settings/settings.screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ class SettingsScreen extends ConsumerWidget {
ScaffoldMessenger.of(context).showSnackBar(
createSnackbar(
"Überprüfe, ob eine Phasierung verfügbar ist ...", theme.colors.elementBackground,
duration: const Duration(seconds: 10)),
duration: const Duration(seconds: SOLCApiManager.timeoutSeconds)),
);

log.d("Checking file status on server ...");
try {
PhaseStatus? status = await manager.getSchoolClassInfo(schoolClassId: schoolClassId);

if (DateTime.now().millisecondsSinceEpoch >= status!.blockEnd.millisecondsSinceEpoch) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
Expand Down Expand Up @@ -159,7 +161,15 @@ class SettingsScreen extends ConsumerWidget {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
createSnackbar(
"Bitte überprüfe deine Internetverbindung", theme.colors.errorBackground),
"Fehler beim Verbindungsaufbau zum Server", theme.colors.errorBackground),
);
working = false;
return;
} on SOLCServerResponseTimeoutException {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
createSnackbar(
"Zeitüberschreitung bei der Serververbindung", theme.colors.errorBackground),
);
working = false;
return;
Expand Down

0 comments on commit 14fa3ed

Please sign in to comment.