library fis_jsonrpc; import 'dart:collection'; import 'package:fis_common/extensions/type.dart'; import 'package:fis_common/logger/logger.dart'; import 'client_base.dart'; import 'interceptor.dart'; import 'services/index.dart'; export 'services/index.dart'; export 'request.dart'; export 'exception.dart'; export 'interceptor.dart'; export 'notifications/index.dart'; typedef T ServiceBuilder(); const C_SHELL_RPC_DEFAULT_HOST = 'platform.fis.plus'; /// JSON-RPC 代理 class JsonRpcProxy { JsonRpcProxy({ String? host, this.platformHost = C_SHELL_RPC_DEFAULT_HOST, }) { _currentHost = host ?? "unknown"; } /// 宿主平台代理地址 final String platformHost; /// 服务主机地址 late String _currentHost; /// 服务主机协议 late String _currentProtocol; /// 当前服务主机地址 String get currentHostAddress => "$_currentProtocol://$_currentHost"; HashMap _serviceCache = HashMap(); static PlatformService? _platformService; /* 服务代理设置 Start */ /// 平台服务 PlatformService get platform { if (_platformService == null) _platformService = PlatformService("http://$platformHost", timeout: 15000); return _platformService!; } *******Separator****** /* 服务代理设置 End */ /// 设置服务主机地址 void setServerHost(String address, [bool useSSL = false]) { logger.i('JsonRpcProxy setServerHost :' + address); _currentProtocol = useSSL ? "https" : "http"; _currentHost = address; } /// 添加拦截器 void addInterceptor(JsonRpcInterceptor interceptor) => jsonRpcInterceptHost.addInterceptor(interceptor); /// 清空缓存 void clearCache() => _serviceCache.clear(); /// 查找Service实例 T findService(ServiceBuilder builder) { Type serviceType = typeOf(); if (!_serviceCache.containsKey(serviceType)) { _serviceCache[serviceType] = builder.call(); } return _serviceCache[serviceType] as T; } }