rpc.txt 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. library fisjsonrpc;
  2. import 'dart:collection';
  3. import 'package:fiscommon/extensions/type.dart';
  4. import 'client_base.dart';
  5. import 'services/index.dart';
  6. export 'services/index.dart';
  7. typedef T ServiceBuilder<T extends JsonRpcClientBase>();
  8. /// 默认地址标识
  9. const String _defaultAddressSign = "FLYINSONO";
  10. /// JSON-RPC 代理
  11. class JsonRpcProxy {
  12. JsonRpcProxy({
  13. this.defaultServerHost = "cloud.fis.plus",
  14. this.platformHost = "platform.fis.plus",
  15. }) {
  16. _currentHost = this.defaultServerHost;
  17. }
  18. /// 默认主机地址
  19. final String defaultServerHost;
  20. /// 宿主平台代理地址
  21. final String platformHost;
  22. late String _currentHost;
  23. /// 当前服务主机地址
  24. String get currentHostAddress => "http://$_currentHost";
  25. String get defaultAddressSign => _defaultAddressSign;
  26. HashMap<Type, dynamic> _serviceCache = HashMap();
  27. static PlatformService? _platformService;
  28. /* 服务代理设置 Start */
  29. /// 平台服务
  30. PlatformService get platform {
  31. if (_platformService == null)
  32. _platformService = PlatformService(platformHost);
  33. return _platformService!;
  34. }
  35. *******Separator******
  36. /* 服务代理设置 End */
  37. /// 设置服务主机地址
  38. void setServerHost(String address) {
  39. if (address == _defaultAddressSign) {
  40. address = defaultServerHost;
  41. }
  42. _currentHost = address;
  43. }
  44. /// 清空缓存
  45. void clearCache() => _serviceCache.clear();
  46. /// 查找Service实例
  47. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  48. Type serviceType = typeOf<T>();
  49. if (!_serviceCache.containsKey(serviceType)) {
  50. _serviceCache[serviceType] = builder.call();
  51. }
  52. return _serviceCache[serviceType] as T;
  53. }
  54. }