mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 15:24:35 +01:00
opt.: server provider
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,7 +118,10 @@ class ServerProvider extends ChangeNotifier {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> connectFn(Server s) async {
|
await Future.wait(_servers.values.map((s) => _connectFn(s, onlyFailed)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _connectFn(Server s, bool onlyFailed) async {
|
||||||
if (onlyFailed) {
|
if (onlyFailed) {
|
||||||
if (s.state != ServerState.failed) return;
|
if (s.state != ServerState.failed) return;
|
||||||
_limiter.reset(s.spi.id);
|
_limiter.reset(s.spi.id);
|
||||||
@@ -136,19 +139,6 @@ class ServerProvider extends ChangeNotifier {
|
|||||||
return await _getData(s.spi);
|
return await _getData(s.spi);
|
||||||
}
|
}
|
||||||
|
|
||||||
final directServers = <Server>[];
|
|
||||||
final proxyServers = <Server>[];
|
|
||||||
for (final s in _servers.values) {
|
|
||||||
if (s.spi.jumpId == null) {
|
|
||||||
directServers.add(s);
|
|
||||||
} else {
|
|
||||||
proxyServers.add(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await Future.wait(directServers.map(connectFn));
|
|
||||||
await Future.wait(proxyServers.map(connectFn));
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> startAutoRefresh() async {
|
Future<void> startAutoRefresh() async {
|
||||||
final duration = Stores.setting.serverStatusUpdateInterval.fetch();
|
final duration = Stores.setting.serverStatusUpdateInterval.fetch();
|
||||||
stopAutoRefresh();
|
stopAutoRefresh();
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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),
|
||||||
|
|||||||
Reference in New Issue
Block a user