rpcForUSMachine.txt 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. library fis_jsonrpc;
  2. import 'dart:collection';
  3. import 'package:fis_common/extensions/type.dart';
  4. import 'package:fis_common/logger/logger.dart';
  5. import 'client_base.dart';
  6. import 'interceptor.dart';
  7. import 'services/index.dart';
  8. export 'services/index.dart';
  9. export 'request.dart';
  10. export 'exception.dart';
  11. export 'interceptor.dart';
  12. export 'notifications/index.dart';
  13. typedef T ServiceBuilder<T extends JsonRpcClientBase>();
  14. const C_SHELL_RPC_DEFAULT_HOST = 'platform.fis.plus';
  15. /// JSON-RPC 代理
  16. class JsonRpcProxy {
  17. JsonRpcProxy({
  18. String? host,
  19. this.platformHost = C_SHELL_RPC_DEFAULT_HOST,
  20. }) {
  21. _currentHost = host ?? "unknown";
  22. }
  23. /// 宿主平台代理地址
  24. final String platformHost;
  25. /// 服务主机地址
  26. late String _currentHost;
  27. /// 服务主机协议
  28. late String _currentProtocol;
  29. /// 当前服务主机地址
  30. String get currentHostAddress => "$_currentProtocol://$_currentHost";
  31. HashMap<Type, dynamic> _serviceCache = HashMap();
  32. static PlatformService? _platformService;
  33. /* 服务代理设置 Start */
  34. /// 平台服务
  35. PlatformService get platform {
  36. if (_platformService == null)
  37. _platformService = PlatformService("http://$platformHost", timeout: 15000);
  38. return _platformService!;
  39. }
  40. *******Separator******
  41. /* 服务代理设置 End */
  42. /// 设置服务主机地址
  43. void setServerHost(String address, [bool useSSL = false]) {
  44. logger.i('JsonRpcProxy setServerHost :' + address);
  45. _currentProtocol = useSSL ? "https" : "http";
  46. _currentHost = address;
  47. }
  48. /// 添加拦截器
  49. void addInterceptor(JsonRpcInterceptor interceptor) =>
  50. jsonRpcInterceptHost.addInterceptor(interceptor);
  51. /// 清空缓存
  52. void clearCache() => _serviceCache.clear();
  53. /// 查找Service实例
  54. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  55. Type serviceType = typeOf<T>();
  56. if (!_serviceCache.containsKey(serviceType)) {
  57. _serviceCache[serviceType] = builder.call();
  58. }
  59. return _serviceCache[serviceType] as T;
  60. }
  61. }