opt.: server provider

This commit is contained in:
lollipopkit
2023-11-02 13:41:28 +08:00
parent 9000228698
commit 220f4c6723
4 changed files with 56 additions and 56 deletions

View File

@@ -22,6 +22,12 @@ class Server implements TagPickable {
@override @override
String get tagName => spi.id; String get tagName => spi.id;
bool get needGenClient => state < ServerState.connecting;
bool get canViewDetails => state == ServerState.finished;
String get id => spi.id;
} }
enum ServerState { enum ServerState {
@@ -38,9 +44,5 @@ enum ServerState {
/// Status parsing finished /// Status parsing finished
finished; finished;
bool get shouldConnect => this < ServerState.connecting;
bool get canViewDetails => this == ServerState.finished;
operator <(ServerState other) => index < other.index; operator <(ServerState other) => index < other.index;
} }

View File

@@ -118,35 +118,25 @@ class ServerProvider extends ChangeNotifier {
return; return;
} }
Future<void> connectFn(Server s) async { await Future.wait(_servers.values.map((s) => _connectFn(s, onlyFailed)));
if (onlyFailed) { }
if (s.state != ServerState.failed) return;
_limiter.reset(s.spi.id);
}
/// If [spi.autoConnect] is false and server is disconnected, then skip. Future<void> _connectFn(Server s, bool onlyFailed) async {
/// if (onlyFailed) {
/// If [spi.autoConnect] is false and server is connected, then refresh. if (s.state != ServerState.failed) return;
/// If no this, the server will only refresh once by clicking refresh button. _limiter.reset(s.spi.id);
///
/// If [spi.autoConnect] is true, then refresh.
if (!(s.spi.autoConnect ?? true) && s.state == ServerState.disconnected) {
return;
}
return await _getData(s.spi);
} }
final directServers = <Server>[]; /// If [spi.autoConnect] is false and server is disconnected, then skip.
final proxyServers = <Server>[]; ///
for (final s in _servers.values) { /// If [spi.autoConnect] is false and server is connected, then refresh.
if (s.spi.jumpId == null) { /// If no this, the server will only refresh once by clicking refresh button.
directServers.add(s); ///
} else { /// If [spi.autoConnect] is true, then refresh.
proxyServers.add(s); if (!(s.spi.autoConnect ?? true) && s.state == ServerState.disconnected) {
} return;
} }
await Future.wait(directServers.map(connectFn)); return await _getData(s.spi);
await Future.wait(proxyServers.map(connectFn));
} }
Future<void> startAutoRefresh() async { Future<void> startAutoRefresh() async {
@@ -236,6 +226,8 @@ class ServerProvider extends ChangeNotifier {
// Only reconnect if neccessary // Only reconnect if neccessary
if (newSpi.shouldReconnect(old)) { if (newSpi.shouldReconnect(old)) {
// Use [newSpi.id] instead of [old.id] because [old.id] may be changed
_limiter.reset(newSpi.id);
refreshData(spi: newSpi); refreshData(spi: newSpi);
} }
@@ -271,21 +263,31 @@ class ServerProvider extends ChangeNotifier {
return; return;
} }
s.status.failedInfo = null;
/// If busy, it may be because of network reasons that the last request /// If busy, it may be because of network reasons that the last request
/// has not been completed, and the request should not be made again at this time. /// has not been completed, and the request should not be made again at this time.
if (s.isBusy) return; if (s.isBusy) return;
s.isBusy = true; s.isBusy = true;
if (s.state.shouldConnect || (s.client?.isClosed ?? true)) { if (s.needGenClient || (s.client?.isClosed ?? true)) {
_setServerState(s, ServerState.connecting); _setServerState(s, ServerState.connecting);
final time1 = DateTime.now();
try { try {
final time1 = DateTime.now();
s.client = await genClient( s.client = await genClient(
spi, spi,
timeout: Stores.setting.timeoutD, timeout: Stores.setting.timeoutD,
); );
final time2 = DateTime.now();
final spentTime = time2.difference(time1).inMilliseconds;
if (spi.jumpId == null) {
Loggers.app.info('Connected to ${spi.name} in $spentTime ms.');
} else {
Loggers.app.info(
'Connected to ${spi.name} via jump server in $spentTime ms.',
);
}
} catch (e) { } catch (e) {
_limiter.inc(sid); _limiter.inc(sid);
s.status.failedInfo = e.toString(); s.status.failedInfo = e.toString();
@@ -296,15 +298,6 @@ class ServerProvider extends ChangeNotifier {
return; return;
} }
final time2 = DateTime.now();
final spentTime = time2.difference(time1).inMilliseconds;
if (spi.jumpId == null) {
Loggers.app.info('Connected to ${spi.name} in $spentTime ms.');
} else {
Loggers.app
.info('Connected to ${spi.name} via jump server in $spentTime ms.');
}
_setServerState(s, ServerState.connected); _setServerState(s, ServerState.connected);
// Write script to server // Write script to server

View File

@@ -113,6 +113,7 @@ void _setupLogger() {
Logger.root.onRecord.listen((record) { Logger.root.onRecord.listen((record) {
Pros.debug.addLog(record); Pros.debug.addLog(record);
print(record); print(record);
if (record.error != null) print(record.error);
if (record.stackTrace != null) print(record.stackTrace); if (record.stackTrace != null) print(record.stackTrace);
}); });
} }

View File

@@ -173,37 +173,37 @@ class _ServerPageState extends State<ServerPage>
); );
} }
Widget _buildEachServerCard(Server? si) { Widget _buildEachServerCard(Server? srv) {
if (si == null) { if (srv == null) {
return UIs.placeholder; return UIs.placeholder;
} }
return CardX( return CardX(
key: Key(si.spi.id + (_tag ?? '')), key: Key(srv.spi.id + (_tag ?? '')),
InkWell( InkWell(
onTap: () { onTap: () {
if (si.state.canViewDetails) { if (srv.canViewDetails) {
AppRoute.serverDetail(spi: si.spi).go(context); AppRoute.serverDetail(spi: srv.spi).go(context);
} else if (si.status.failedInfo != null) { } else if (srv.status.failedInfo != null) {
_showFailReason(si.status); _showFailReason(srv.status);
} }
}, },
onLongPress: () { onLongPress: () {
if (si.state == ServerState.finished) { if (srv.state == ServerState.finished) {
setState(() { setState(() {
if (_flipedCardIds.contains(si.spi.id)) { if (_flipedCardIds.contains(srv.spi.id)) {
_flipedCardIds.remove(si.spi.id); _flipedCardIds.remove(srv.spi.id);
} else { } else {
_flipedCardIds.add(si.spi.id); _flipedCardIds.add(srv.spi.id);
} }
}); });
} else { } else {
AppRoute.serverEdit(spi: si.spi).go(context); AppRoute.serverEdit(spi: srv.spi).go(context);
} }
}, },
child: Padding( child: Padding(
padding: const EdgeInsets.all(13), padding: const EdgeInsets.all(13),
child: _buildRealServerCard(si), child: _buildRealServerCard(srv),
), ),
), ),
); );
@@ -465,8 +465,12 @@ class _ServerPageState extends State<ServerPage>
); );
} }
Widget _buildIOData(String up, String down, Widget _buildIOData(
{void Function()? onTap, Key? key}) { String up,
String down, {
void Function()? onTap,
Key? key,
}) {
final child = Column( final child = Column(
children: [ children: [
const SizedBox(height: 5), const SizedBox(height: 5),