GroupConfig.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. unit GroupConfig;
  2. interface
  3. uses
  4. Classes, SysUtils;
  5. type
  6. TGroupVersion = (gvUnkown, gvIntegration, gvIndependent);
  7. TGroupShareVersion = (gsvUnkown, gsvOld, gsvNew);
  8. TGroupAddress = class
  9. public
  10. Port: Integer;
  11. Enable: Boolean;
  12. IP: string;
  13. end;
  14. TGroupConfig = class
  15. private
  16. FIP,
  17. FImageIP,
  18. FGatewayIP: string;
  19. FPort,
  20. FImagePort,
  21. FGatewayPort: Integer;
  22. FGroupVersion: TGroupVersion;
  23. FGroupAddresses: TStringList;
  24. FImageAddresses: TStringList;
  25. FGatewayAddresses: TStringList;
  26. procedure RandomGroupServer;
  27. procedure RandomImageServer;
  28. function GetImageIP: string;
  29. function GetImagePort: Integer;
  30. function GetIP: string;
  31. function GetPort: Integer;
  32. function GetGatewayEnable: boolean;
  33. function GetImageHost: string;
  34. public
  35. constructor Create;
  36. destructor Destroy; override;
  37. class function Load: TGroupConfig;
  38. class function GetConfig: TGroupConfig;
  39. procedure RandomGatewayServer;
  40. property IP: string read GetIP write FIP;
  41. property ImageIP: string read GetImageIP write FImageIP;
  42. property GatewayIP: string read FGatewayIP write FGatewayIP;
  43. property Port: Integer read GetPort write FPort;
  44. property ImagePort: Integer read GetImagePort write FImagePort;
  45. property GatewayPort: Integer read FGatewayPort write FGatewayPort;
  46. property GroupVersion: TGroupVersion read FGroupVersion;
  47. property GatewayEnable: Boolean read GetGatewayEnable;
  48. property ImageHost: string read GetImageHost;
  49. end;
  50. TGroupShareConfig = class
  51. private
  52. FIP: string;
  53. FURL: string;
  54. FPort: Integer;
  55. FGroupShareVersion: TGroupShareVersion;
  56. public
  57. class function Load: TGroupShareConfig;
  58. class function GetConfig: TGroupShareConfig;
  59. property IP: string read FIP;
  60. property URL: string read FURL write FURL;
  61. property Port: Integer read FPort;
  62. property GroupShareVersion: TGroupShareVersion read FGroupShareVersion;
  63. end;
  64. implementation
  65. uses
  66. superobject, Dialogs, TypInfo, LoggerImport;
  67. const
  68. GROUP_CONFIG: string = 'Config\group.config';
  69. GROUP_SHARE_CONFIG: string = 'Config\groupShare.config';
  70. var
  71. config: TGroupConfig;
  72. groupShareConfig: TGroupShareConfig;
  73. constructor TGroupConfig.Create;
  74. begin
  75. FGroupAddresses := TStringList.Create;
  76. FImageAddresses := TStringList.Create;
  77. FGatewayAddresses := TStringList.Create;
  78. end;
  79. destructor TGroupConfig.Destroy;
  80. var
  81. AGroupAddress: TGroupAddress;
  82. begin
  83. while FGroupAddresses.Count > 0 do
  84. begin
  85. AGroupAddress := FGroupAddresses.Objects[0] as TGroupAddress;
  86. FGroupAddresses.Delete(0);
  87. FreeAndNil(AGroupAddress);
  88. end;
  89. while FImageAddresses.Count > 0 do
  90. begin
  91. AGroupAddress := FImageAddresses.Objects[0] as TGroupAddress;
  92. FImageAddresses.Delete(0);
  93. FreeAndNil(AGroupAddress);
  94. end;
  95. while FGatewayAddresses.Count > 0 do
  96. begin
  97. AGroupAddress := FGatewayAddresses.Objects[0] as TGroupAddress;
  98. FGatewayAddresses.Delete(0);
  99. FreeAndNil(AGroupAddress);
  100. end;
  101. inherited;
  102. end;
  103. class function TGroupConfig.GetConfig: TGroupConfig;
  104. begin
  105. if config <> nil then
  106. Result := config
  107. else
  108. Result := TGroupConfig.Load;
  109. end;
  110. function TGroupConfig.GetGatewayEnable: boolean;
  111. begin
  112. Result := FGatewayAddresses.Count <> 0
  113. end;
  114. function TGroupConfig.GetImageHost: string;
  115. const
  116. UPLOAD_URL: string = 'http://%s:%d/file/upload';
  117. begin
  118. Result := Format(UPLOAD_URL, [FImageIP, FImagePort]);
  119. end;
  120. function TGroupConfig.GetImageIP: string;
  121. begin
  122. RandomImageServer;
  123. Result := FImageIP;
  124. end;
  125. function TGroupConfig.GetImagePort: Integer;
  126. begin
  127. Result := FImagePort;
  128. end;
  129. function TGroupConfig.GetIP: string;
  130. begin
  131. RandomGroupServer;
  132. Result := FIP;
  133. end;
  134. function TGroupConfig.GetPort: Integer;
  135. begin
  136. Result := FPort;
  137. end;
  138. class function TGroupConfig.Load: TGroupConfig;
  139. var
  140. AFullFileName: string;
  141. jo, AServer: ISuperObject;
  142. AGroupServerAddresses: TSuperArray;
  143. iLoop: Integer;
  144. AServerAddress: TGroupAddress;
  145. begin
  146. if config <> nil then
  147. FreeAndNil(config);
  148. config := TGroupConfig.Create;
  149. Result := config;
  150. Result.FGroupVersion := gvUnkown;
  151. AFullFileName := ExtractFilePath(ParamStr(0)) + GROUP_CONFIG;
  152. if not FileExists(AFullFileName) then
  153. begin
  154. Result.FGroupVersion := gvUnkown;
  155. Exit;
  156. end;
  157. //(const FileName: string; partial: boolean = true; const this: ISuperObject = nil; options: TSuperFindOptions = [];
  158. // const put: ISuperObject = nil; dt: TSuperType = stNull):
  159. try
  160. jo := TSuperObject.ParseFile(AFullFileName, False);
  161. Result.FGroupVersion := TGroupVersion(GetEnumValue(TypeInfo(TGroupVersion), jo['version'].AsString()));
  162. if jo.O['gatewayServerAddress'] <> nil then
  163. begin
  164. AGroupServerAddresses := jo.O['gatewayServerAddress'].AsArray();
  165. for iLoop := 0 to AGroupServerAddresses.Length - 1 do
  166. begin
  167. AServer := AGroupServerAddresses.O[iLoop];
  168. if not AServer['enable'].AsBoolean then
  169. Continue;
  170. AServerAddress := TGroupAddress.Create;
  171. AServerAddress.IP := AServer['ip'].AsString;
  172. AServerAddress.Port := AServer['port'].AsInteger;
  173. AServerAddress.Enable := True;
  174. Result.FGatewayAddresses.AddObject(IntToStr(iLoop), AServerAddress);
  175. end;
  176. end;
  177. if (Result.FGatewayAddresses.Count = 0) and (jo.O['groupServerAddress'] <> nil) then
  178. begin
  179. AGroupServerAddresses := jo.O['groupServerAddress'].AsArray();
  180. for iLoop := 0 to AGroupServerAddresses.Length - 1 do
  181. begin
  182. AServer := AGroupServerAddresses.O[iLoop];
  183. if not AServer['enable'].AsBoolean then
  184. Continue;
  185. AServerAddress := TGroupAddress.Create;
  186. AServerAddress.IP := AServer['ip'].AsString;
  187. AServerAddress.Port := AServer['port'].AsInteger;
  188. AServerAddress.Enable := True;
  189. Result.FGroupAddresses.AddObject(IntToStr(iLoop), AServerAddress);
  190. end;
  191. end;
  192. if (Result.FGatewayAddresses.Count = 0) and (jo.O['imageServerAddress'] <> nil) then
  193. begin
  194. AGroupServerAddresses := jo.O['imageServerAddress'].AsArray();
  195. for iLoop := 0 to AGroupServerAddresses.Length - 1 do
  196. begin
  197. AServer := AGroupServerAddresses.O[iLoop];
  198. if not AServer['enable'].AsBoolean then
  199. Continue;
  200. AServerAddress := TGroupAddress.Create;
  201. AServerAddress.IP := AServer['ip'].AsString;
  202. AServerAddress.Port := AServer['port'].AsInteger;
  203. AServerAddress.Enable := True;
  204. Result.FImageAddresses.AddObject(IntToStr(iLoop), AServerAddress);
  205. end;
  206. end;
  207. except
  208. on E: Exception do
  209. begin
  210. Result.FGroupVersion := gvUnkown;
  211. Dialogs.ShowMessage(E.Message);
  212. end;
  213. end;
  214. end;
  215. procedure TGroupConfig.RandomGatewayServer;
  216. var
  217. i: Integer;
  218. AGroupAddress: TGroupAddress;
  219. begin
  220. if FGatewayAddresses.Count = 0 then
  221. Exit;
  222. i := Random(FGatewayAddresses.Count);
  223. AGroupAddress := FGatewayAddresses.Objects[i] as TGroupAddress;
  224. FGatewayIP := AGroupAddress.IP;
  225. FGatewayPort := AGroupAddress.Port;
  226. end;
  227. procedure TGroupConfig.RandomGroupServer;
  228. var
  229. i: Integer;
  230. AGroupAddress: TGroupAddress;
  231. begin
  232. if FGroupAddresses.Count = 0 then
  233. Exit;
  234. i := Random(FGroupAddresses.Count);
  235. AGroupAddress := FGroupAddresses.Objects[i] as TGroupAddress;
  236. FIP := AGroupAddress.IP;
  237. FPort := AGroupAddress.Port;
  238. end;
  239. procedure TGroupConfig.RandomImageServer;
  240. var
  241. i: Integer;
  242. AGroupAddress: TGroupAddress;
  243. begin
  244. if FImageAddresses.Count = 0 then
  245. Exit;
  246. i := Random(FImageAddresses.Count);
  247. AGroupAddress := FImageAddresses.Objects[i] as TGroupAddress;
  248. FImageIP := AGroupAddress.IP;
  249. FImagePort := AGroupAddress.Port;
  250. end;
  251. { TGroupShareConfig }
  252. class function TGroupShareConfig.GetConfig: TGroupShareConfig;
  253. begin
  254. if groupShareConfig <> nil then
  255. Result := groupShareConfig
  256. else
  257. Result := TGroupShareConfig.Load;
  258. end;
  259. class function TGroupShareConfig.Load: TGroupShareConfig;
  260. var
  261. AFullFileName: string;
  262. jo: ISuperObject;
  263. begin
  264. if groupShareConfig <> nil then
  265. FreeAndNil(groupShareConfig);
  266. groupShareConfig := TGroupShareConfig.Create;
  267. Result := groupShareConfig;
  268. Result.FGroupShareVersion := gsvUnkown;
  269. AFullFileName := ExtractFilePath(ParamStr(0)) + GROUP_SHARE_CONFIG;
  270. if not FileExists(AFullFileName) then
  271. begin
  272. Result.FGroupShareVersion := gsvUnkown;
  273. Exit;
  274. end;
  275. try
  276. jo := TSuperObject.ParseFile(AFullFileName, False);
  277. Result.FGroupShareVersion := TGroupShareVersion(GetEnumValue(TypeInfo(TGroupShareVersion), jo['version'].AsString()));
  278. Result.FIP := jo['ip'].AsString();
  279. // Result.FURL := jo['url'].AsString();
  280. Result.FPort := jo['port'].AsInteger();
  281. except
  282. on E: Exception do
  283. begin
  284. Result.FGroupShareVersion := gsvUnkown;
  285. Debug(E.Message, 'TGroupShareConfig.Load');
  286. end;
  287. end;
  288. end;
  289. initialization
  290. finalization
  291. if config <> nil then
  292. FreeAndNil(config);
  293. if groupShareConfig <> nil then
  294. FreeAndNil(groupShareConfig);
  295. end.