Skip to content

Commit

Permalink
Champs Patch (#193)
Browse files Browse the repository at this point in the history
* Added disconnect on connect (should resolve reconnections)

* Patched common nice to haves

* Added more try catches for networking

* Added close 1000 to main websocket disconnect
  • Loading branch information
CJBuchel authored Dec 1, 2024
1 parent e5a30b5 commit 89b806a
Show file tree
Hide file tree
Showing 21 changed files with 260 additions and 98 deletions.
8 changes: 6 additions & 2 deletions echo_tree_flutter/lib/client/network_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,13 @@ class EchoTreeNetworkService extends EchoTreeSubscriptionManager {
}

Future<void> disconnect() async {
try {
// https://datatracker.ietf.org/doc/html/rfc6455#section-7.4 (i'm being proper XD )
_channel?.sink.close(1000);
} catch (e) {
EchoTreeLogger().e("Error disconnecting from server: $e");
}
_state.value = EchoTreeConnection.disconnected;
// https://datatracker.ietf.org/doc/html/rfc6455#section-7.4 (i'm being proper XD )
_channel?.sink.close(1000);
}

void sendMessage(EchoTreeClientSocketMessage message) {
Expand Down
5 changes: 0 additions & 5 deletions echo_tree_flutter/lib/widgets/echo_tree_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ class EchoTreeProvider<K, V> extends ChangeNotifier {
void _populateData() {
final rawData = managedTree.getDataMap();

// check if the lists are equal (return if they are)
if (mapEquals(rawData, items)) {
return;
}

// Update existing items and add new items
rawData.forEach((key, value) {
V? entry = _entryFromString(value);
Expand Down
15 changes: 12 additions & 3 deletions tms_client/lib/network/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,22 @@ class DbController {
TmsLocalStorageProvider().authRoles.map((e) => MapEntry(e.roleId, e.password)),
);

await EchoTreeClient().connect(TmsLocalStorageProvider().serverAddress, roles: roles);
try {
await EchoTreeClient().connect(TmsLocalStorageProvider().serverAddress, roles: roles);
} catch (e) {
TmsLogger().e("Error connecting to EchoTree DB: $e");
}

var state = _stateParser();
TmsLogger().i("State: $state");
}

Future<void> disconnect() async {
TmsLogger().d("Disconnecting from EchoTree DB...");
await EchoTreeClient().disconnect();
try {
TmsLogger().d("Disconnecting from EchoTree DB...");
await EchoTreeClient().disconnect();
} catch (e) {
TmsLogger().e("Error disconnecting from EchoTree DB: $e");
}
}
}
39 changes: 22 additions & 17 deletions tms_client/lib/network/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,32 @@ class HttpController {
}
}

Future<bool> disconnect() async {
Future<void> disconnect() async {
String addr = TmsLocalStorageProvider().serverAddress;
final response = await _client.delete(
Uri.parse("$addr/register/${TmsLocalStorageProvider().uuid}"),
headers: {
...authHeaders,
"Content-Type": "application/json",
},
);

if (response.statusCode == HttpStatus.ok) {
TmsLogger().d("Unregistered with TMS server");
_connectivity.state = NetworkConnectionState.disconnected;
_client.close();
return true;
} else {
var m = HttpStatusToMessage().getMessage(response.statusCode);
TmsLogger().w("Failed to unregister with TMS server: $m");
try {
final response = await _client.delete(
Uri.parse("$addr/register/${TmsLocalStorageProvider().uuid}"),
headers: {
...authHeaders,
"Content-Type": "application/json",
},
);

if (response.statusCode == HttpStatus.ok) {
TmsLogger().d("Unregistered with TMS server");
_connectivity.state = NetworkConnectionState.disconnected;
_client.close();
} else {
var m = HttpStatusToMessage().getMessage(response.statusCode);
TmsLogger().w("Failed to unregister with TMS server: $m");
_connectivity.state = NetworkConnectionState.disconnected;
_client.close();
}
} catch (e) {
TmsLogger().e("Caught failure. Failed to unregister with TMS server: $e");
_connectivity.state = NetworkConnectionState.disconnected;
_client.close();
return false;
}
}

Expand Down
38 changes: 29 additions & 9 deletions tms_client/lib/network/network_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ class NetworkController {
}

Future<bool> _websocketConnect() async {
return await _websocketController.connect(TmsLocalStorageProvider().wsConnectionString);
try {
return await _websocketController.connect(TmsLocalStorageProvider().wsConnectionString);
} catch (e) {
TmsLogger().e("Error connecting to websocket: $e");
return false;
}
}

Future<void> init() async {
Expand All @@ -113,21 +118,36 @@ class NetworkController {
}

Future<bool> connect() async {
if (await _heartbeat()) {
if (await _httpController.connect()) {
if (await _websocketConnect()) {
await _dbController.connect();
return true;
// make sure we are disconnected first
try {
await disconnect();
} catch (e) {
TmsLogger().e("Error disconnecting: $e");
}

try {
if (await _heartbeat()) {
if (await _httpController.connect()) {
if (await _websocketConnect()) {
await _dbController.connect();
return true;
}
}
}
} catch (e) {
TmsLogger().e("Error connecting: $e");
}
return false;
}

Future<void> disconnect() async {
await _dbController.disconnect();
await _httpController.disconnect();
await _websocketController.disconnect();
try {
await _dbController.disconnect();
await _httpController.disconnect();
await _websocketController.disconnect();
} catch (e) {
TmsLogger().e("Error disconnecting: $e");
}
}

// http
Expand Down
9 changes: 7 additions & 2 deletions tms_client/lib/network/ws.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,20 @@ class WebsocketController {
});
} catch (e) {
_connectivity.state = NetworkConnectionState.disconnected;
TmsLogger().e("Error connecting to TMS server websocket: $e");
return false;
}
return true;
}

Future<void> disconnect() async {
TmsLogger().i("Disconnecting from TMS server...");
try {
TmsLogger().i("Disconnecting from TMS server...");
_channel?.sink.close(1000);
} catch (e) {
TmsLogger().e("Error disconnecting from TMS server: $e");
}
_connectivity.state = NetworkConnectionState.disconnected;
_channel?.sink.close();
}

void subscribe(TmsServerSocketEvent event, TmsEventHandler handler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class IntegrityMessageTile extends StatelessWidget {
style: const TextStyle(
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
overflow: TextOverflow.ellipsis,
),
),
],
Expand All @@ -51,6 +52,7 @@ class IntegrityMessageTile extends StatelessWidget {
style: const TextStyle(
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
overflow: TextOverflow.ellipsis,
),
),
],
Expand All @@ -62,6 +64,7 @@ class IntegrityMessageTile extends StatelessWidget {
style: const TextStyle(
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
overflow: TextOverflow.ellipsis,
),
),
],
Expand Down Expand Up @@ -93,14 +96,19 @@ class IntegrityMessageTile extends StatelessWidget {
error: (_) => Colors.red,
warning: (_) => Colors.orange,
),
overflow: TextOverflow.ellipsis,
),
),
const SizedBox(width: 10),
Text(
messages.first.integrityCode.getMessage(),
style: const TextStyle(fontSize: 16),
Expanded(
child: Text(
messages.first.integrityCode.getMessage(),
style: const TextStyle(
fontSize: 16,
overflow: TextOverflow.ellipsis,
),
),
),
const Spacer(),
ValueListenableBuilder(
valueListenable: _expansionController,
builder: (context, isExpanded, _) {
Expand Down
1 change: 1 addition & 0 deletions tms_client/lib/views/match_announcer/next_match_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class NextMatchInfo extends StatelessWidget {
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 18,
overflow: TextOverflow.ellipsis,
// color: Colors.black,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import 'package:dropdown_search/dropdown_search.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tms/generated/infra/database_schemas/game_match.dart';
import 'package:tms/generated/infra/database_schemas/team.dart';
import 'package:tms/providers/robot_game_providers/game_match_provider.dart';
import 'package:tms/providers/robot_game_providers/game_table_provider.dart';
import 'package:tms/providers/teams_provider.dart';
import 'package:tms/widgets/dialogs/confirm_future_dialog.dart';
import 'package:collection/collection.dart';
import 'package:tms/widgets/dialogs/dialog_style.dart';
Expand All @@ -22,24 +24,27 @@ class RescheduleButton extends StatelessWidget {
final ValueNotifier<String?> _selectedTable = ValueNotifier(null);

Widget _buildDialogMessage() {
return Selector2<GameMatchProvider, GameTableProvider, ({List<GameMatch> matches, List<String> tables})>(
selector: (context, matchProvider, tableProvider) {
return Selector3<GameMatchProvider, GameTableProvider, TeamsProvider,
({List<GameMatch> matches, List<String> tables, List<Team> teams})>(
selector: (context, matchProvider, tableProvider, teamsProvider) {
// get matches which are not completed
List<GameMatch> matches = matchProvider.matches.where((m) => !m.completed).toList();
List<String> tables = tableProvider.tables.map((table) => table.tableName).toList();
return (matches: matches, tables: tables);
return (matches: matches, tables: tables, teams: teamsProvider.teams);
},
builder: (context, data, _) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
// dropdown for teams
DropdownSearch<String>(
DropdownSearch<Team?>(
popupProps: const PopupProps.menu(
showSearchBox: true,
),
items: match.gameMatchTables.map((table) => table.teamNumber).toList(),
itemAsString: (team) => "${team}",
items: match.gameMatchTables
.map((table) => data.teams.firstWhereOrNull((t) => t.teamNumber == table.teamNumber))
.toList(),
itemAsString: (team) => "${team?.teamNumber} | ${team?.name}",
dropdownDecoratorProps: const DropDownDecoratorProps(
dropdownSearchDecoration: InputDecoration(
border: OutlineInputBorder(),
Expand All @@ -48,7 +53,7 @@ class RescheduleButton extends StatelessWidget {
hintText: "Select Team",
),
),
onChanged: (value) => _selectedTeamNumber.value = value,
onChanged: (value) => _selectedTeamNumber.value = value?.teamNumber,
),

// down arrow
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tms/generated/infra/database_schemas/game_match.dart';
import 'package:tms/providers/teams_provider.dart';
import 'package:tms/utils/color_modifiers.dart';

class TableItem extends StatelessWidget {
Expand Down Expand Up @@ -44,12 +46,24 @@ class TableItem extends StatelessWidget {
children: [
Text(
table.table,
style: TextStyle(fontSize: 12, color: fontColor),
style: TextStyle(fontSize: 12, color: fontColor, fontWeight: FontWeight.bold),
),
const SizedBox(height: 10),
Text(
table.teamNumber,
style: TextStyle(fontSize: 12, color: fontColor),
const SizedBox(height: 5),
Selector<TeamsProvider, String>(
selector: (_, provider) => provider.getTeam(table.teamNumber).name,
builder: (context, teamName, _) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 5),
child: Text(
"${table.teamNumber} | $teamName",
style: TextStyle(
fontSize: 10,
color: fontColor,
overflow: TextOverflow.ellipsis,
),
),
);
},
),
],
),
Expand Down
7 changes: 6 additions & 1 deletion tms_client/lib/views/referee_scoring/referee_scoring.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:tms/providers/robot_game_providers/game_scoring_provider.dart';
import 'package:tms/providers/robot_game_providers/game_table_provider.dart';
import 'package:tms/views/referee_scoring/referee_scoring_footer/floating_score.dart';
import 'package:tms/views/referee_scoring/referee_scoring_footer/referee_scoring_footer.dart';
import 'package:tms/views/referee_scoring/referee_scoring_header/floating_timer.dart';
import 'package:tms/views/referee_scoring/referee_scoring_header/referee_scoring_header.dart';
import 'package:tms/widgets/game_scoring/game_scoring_widget/game_scoring_widget.dart';
import 'package:tms/widgets/game_scoring/with_next_game_scoring.dart';
Expand Down Expand Up @@ -66,7 +67,11 @@ class RefereeScoring extends StatelessWidget {
],
),
),
// floating schedule timer
// floating timer
FloatingTimer(
top: headerHeight + 10,
right: 10,
),
// floating score
Selector<GameScoringProvider, int>(
selector: (context, gs) => gs.score,
Expand Down
Loading

0 comments on commit 89b806a

Please sign in to comment.