rpc.txt 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. library fis_jsonrpc;
  2. import 'dart:collection';
  3. import 'package:fis_common/extensions/type.dart';
  4. import 'client_base.dart';
  5. import 'interceptor.dart';
  6. import 'services/index.dart';
  7. export 'services/index.dart';
  8. typedef T ServiceBuilder<T extends JsonRpcClientBase>();
  9. /// 默认地址标识
  10. const String _defaultAddressSign = "FLYINSONO";
  11. /// JSON-RPC 代理
  12. class JsonRpcProxy {
  13. JsonRpcProxy({
  14. this.defaultServerHost = "cloud.fis.plus",
  15. this.platformHost = "platform.fis.plus",
  16. }) {
  17. _currentHost = this.defaultServerHost;
  18. }
  19. /// 默认主机地址
  20. final String defaultServerHost;
  21. /// 宿主平台代理地址
  22. final String platformHost;
  23. late String _currentHost;
  24. /// 当前服务主机地址
  25. String get currentHostAddress => "http://$_currentHost";
  26. String get defaultAddressSign => _defaultAddressSign;
  27. HashMap<Type, dynamic> _serviceCache = HashMap();
  28. static PlatformService? _platformService;
  29. /* 服务代理设置 Start */
  30. /// 平台服务
  31. PlatformService get platform {
  32. if (_platformService == null)
  33. _platformService = PlatformService("http://$platformHost");
  34. return _platformService!;
  35. }
  36. *******Separator******
  37. /* 服务代理设置 End */
  38. /// 设置服务主机地址
  39. void setServerHost(String address) {
  40. if (address == _defaultAddressSign) {
  41. address = defaultServerHost;
  42. }
  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. }