GroupConfig.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. RandomImageServer;
  119. Result := Format(UPLOAD_URL, [FImageIP, FImagePort]);
  120. end;
  121. function TGroupConfig.GetImageIP: string;
  122. begin
  123. RandomImageServer;
  124. Result := FImageIP;
  125. end;
  126. function TGroupConfig.GetImagePort: Integer;
  127. begin
  128. Result := FImagePort;
  129. end;
  130. function TGroupConfig.GetIP: string;
  131. begin
  132. RandomGroupServer;
  133. Result := FIP;
  134. end;
  135. function TGroupConfig.GetPort: Integer;
  136. begin
  137. Result := FPort;
  138. end;
  139. class function TGroupConfig.Load: TGroupConfig;
  140. var
  141. AFullFileName: string;
  142. jo, AServer: ISuperObject;
  143. AGroupServerAddresses: TSuperArray;
  144. iLoop: Integer;
  145. AServerAddress: TGroupAddress;
  146. begin
  147. if config <> nil then
  148. FreeAndNil(config);
  149. config := TGroupConfig.Create;
  150. Result := config;
  151. Result.FGroupVersion := gvUnkown;
  152. AFullFileName := ExtractFilePath(ParamStr(0)) + GROUP_CONFIG;
  153. if not FileExists(AFullFileName) then
  154. begin
  155. Result.FGroupVersion := gvUnkown;
  156. Exit;
  157. end;
  158. //(const FileName: string; partial: boolean = true; const this: ISuperObject = nil; options: TSuperFindOptions = [];
  159. // const put: ISuperObject = nil; dt: TSuperType = stNull):
  160. try
  161. jo := TSuperObject.ParseFile(AFullFileName, False);
  162. Result.FGroupVersion := TGroupVersion(GetEnumValue(TypeInfo(TGroupVersion), jo['version'].AsString()));
  163. if jo.O['gatewayServerAddress'] <> nil then
  164. begin
  165. AGroupServerAddresses := jo.O['gatewayServerAddress'].AsArray();
  166. for iLoop := 0 to AGroupServerAddresses.Length - 1 do
  167. begin
  168. AServer := AGroupServerAddresses.O[iLoop];
  169. if not AServer['enable'].AsBoolean then
  170. Continue;
  171. AServerAddress := TGroupAddress.Create;
  172. AServerAddress.IP := AServer['ip'].AsString;
  173. AServerAddress.Port := AServer['port'].AsInteger;
  174. AServerAddress.Enable := True;
  175. Result.FGatewayAddresses.AddObject(IntToStr(iLoop), AServerAddress);
  176. end;
  177. end;
  178. if (Result.FGatewayAddresses.Count = 0) and (jo.O['groupServerAddress'] <> nil) then
  179. begin
  180. AGroupServerAddresses := jo.O['groupServerAddress'].AsArray();
  181. for iLoop := 0 to AGroupServerAddresses.Length - 1 do
  182. begin
  183. AServer := AGroupServerAddresses.O[iLoop];
  184. if not AServer['enable'].AsBoolean then
  185. Continue;
  186. AServerAddress := TGroupAddress.Create;
  187. AServerAddress.IP := AServer['ip'].AsString;
  188. AServerAddress.Port := AServer['port'].AsInteger;
  189. AServerAddress.Enable := True;
  190. Result.FGroupAddresses.AddObject(IntToStr(iLoop), AServerAddress);
  191. end;
  192. end;
  193. if (Result.FGatewayAddresses.Count = 0) and (jo.O['imageServerAddress'] <> nil) then
  194. begin
  195. AGroupServerAddresses := jo.O['imageServerAddress'].AsArray();
  196. for iLoop := 0 to AGroupServerAddresses.Length - 1 do
  197. begin
  198. AServer := AGroupServerAddresses.O[iLoop];
  199. if not AServer['enable'].AsBoolean then
  200. Continue;
  201. AServerAddress := TGroupAddress.Create;
  202. AServerAddress.IP := AServer['ip'].AsString;
  203. AServerAddress.Port := AServer['port'].AsInteger;
  204. AServerAddress.Enable := True;
  205. Result.FImageAddresses.AddObject(IntToStr(iLoop), AServerAddress);
  206. end;
  207. end;
  208. except
  209. on E: Exception do
  210. begin
  211. Result.FGroupVersion := gvUnkown;
  212. Dialogs.ShowMessage(E.Message);
  213. end;
  214. end;
  215. end;
  216. procedure TGroupConfig.RandomGatewayServer;
  217. var
  218. i: Integer;
  219. AGroupAddress: TGroupAddress;
  220. begin
  221. if FGatewayAddresses.Count = 0 then
  222. Exit;
  223. i := Random(FGatewayAddresses.Count);
  224. AGroupAddress := FGatewayAddresses.Objects[i] as TGroupAddress;
  225. FGatewayIP := AGroupAddress.IP;
  226. FGatewayPort := AGroupAddress.Port;
  227. end;
  228. procedure TGroupConfig.RandomGroupServer;
  229. var
  230. i: Integer;
  231. AGroupAddress: TGroupAddress;
  232. begin
  233. if FGroupAddresses.Count = 0 then
  234. Exit;
  235. i := Random(FGroupAddresses.Count);
  236. AGroupAddress := FGroupAddresses.Objects[i] as TGroupAddress;
  237. FIP := AGroupAddress.IP;
  238. FPort := AGroupAddress.Port;
  239. end;
  240. procedure TGroupConfig.RandomImageServer;
  241. var
  242. i: Integer;
  243. AGroupAddress: TGroupAddress;
  244. begin
  245. if FImageAddresses.Count = 0 then
  246. Exit;
  247. i := Random(FImageAddresses.Count);
  248. AGroupAddress := FImageAddresses.Objects[i] as TGroupAddress;
  249. FImageIP := AGroupAddress.IP;
  250. FImagePort := AGroupAddress.Port;
  251. end;
  252. { TGroupShareConfig }
  253. class function TGroupShareConfig.GetConfig: TGroupShareConfig;
  254. begin
  255. if groupShareConfig <> nil then
  256. Result := groupShareConfig
  257. else
  258. Result := TGroupShareConfig.Load;
  259. end;
  260. class function TGroupShareConfig.Load: TGroupShareConfig;
  261. var
  262. AFullFileName: string;
  263. jo: ISuperObject;
  264. begin
  265. if groupShareConfig <> nil then
  266. FreeAndNil(groupShareConfig);
  267. groupShareConfig := TGroupShareConfig.Create;
  268. Result := groupShareConfig;
  269. Result.FGroupShareVersion := gsvUnkown;
  270. AFullFileName := ExtractFilePath(ParamStr(0)) + GROUP_SHARE_CONFIG;
  271. if not FileExists(AFullFileName) then
  272. begin
  273. Result.FGroupShareVersion := gsvUnkown;
  274. Exit;
  275. end;
  276. try
  277. jo := TSuperObject.ParseFile(AFullFileName, False);
  278. Result.FGroupShareVersion := TGroupShareVersion(GetEnumValue(TypeInfo(TGroupShareVersion), jo['version'].AsString()));
  279. Result.FIP := jo['ip'].AsString();
  280. // Result.FURL := jo['url'].AsString();
  281. Result.FPort := jo['port'].AsInteger();
  282. except
  283. on E: Exception do
  284. begin
  285. Result.FGroupShareVersion := gsvUnkown;
  286. Debug(E.Message, 'TGroupShareConfig.Load');
  287. end;
  288. end;
  289. end;
  290. initialization
  291. finalization
  292. if config <> nil then
  293. FreeAndNil(config);
  294. if groupShareConfig <> nil then
  295. FreeAndNil(groupShareConfig);
  296. end.