rpc.txt 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. String get currentHostAddress => "http://$_currentHost";
  29. HashMap<Type, dynamic> _serviceCache = HashMap();
  30. static PlatformService? _platformService;
  31. /* 服务代理设置 Start */
  32. /// 平台服务
  33. PlatformService get platform {
  34. if (_platformService == null)
  35. _platformService = PlatformService("http://$platformHost");
  36. return _platformService!;
  37. }
  38. *******Separator******
  39. /* 服务代理设置 End */
  40. /// 设置服务主机地址
  41. void setServerHost(String address) {
  42. logger.i('JsonRpcProxy setServerHost :' + address);
  43. _currentHost = address;
  44. }
  45. /// 添加拦截器
  46. void addInterceptor(JsonRpcInterceptor interceptor) =>
  47. jsonRpcInterceptHost.addInterceptor(interceptor);
  48. /// 清空缓存
  49. void clearCache() => _serviceCache.clear();
  50. /// 查找Service实例
  51. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  52. Type serviceType = typeOf<T>();
  53. if (!_serviceCache.containsKey(serviceType)) {
  54. _serviceCache[serviceType] = builder.call();
  55. }
  56. return _serviceCache[serviceType] as T;
  57. }
  58. }