unit GroupConfig; interface uses Classes, SysUtils; type TGroupVersion = (gvUnkown, gvIntegration, gvIndependent); TGroupShareVersion = (gsvUnkown, gsvOld, gsvNew); TGroupAddress = class public Port: Integer; Enable: Boolean; IP: string; end; TGroupConfig = class private FIP, FImageIP: string; FPort, FImagePort: Integer; FGroupVersion: TGroupVersion; FGroupAddresses: TStringList; FImageAddresses: TStringList; procedure RandomGroupServer; procedure RandomImageServer; function GetImageIP: string; function GetImagePort: Integer; function GetIP: string; function GetPort: Integer; public constructor Create; destructor Destroy; override; class function Load: TGroupConfig; class function GetConfig: TGroupConfig; property IP: string read GetIP; property ImageIP: string read GetImageIP; property Port: Integer read GetPort; property ImagePort: Integer read GetImagePort; property GroupVersion: TGroupVersion read FGroupVersion; end; TGroupShareConfig = class private FIP: string; FURL: string; FPort: Integer; FGroupShareVersion: TGroupShareVersion; public class function Load: TGroupShareConfig; class function GetConfig: TGroupShareConfig; property IP: string read FIP; property URL: string read FURL; property Port: Integer read FPort; property GroupShareVersion: TGroupShareVersion read FGroupShareVersion; end; implementation uses superobject, Dialogs, TypInfo, LoggerImport; const GROUP_CONFIG: string = 'Config\group.config'; GROUP_SHARE_CONFIG: string = 'Config\groupShare.config'; var config: TGroupConfig; groupShareConfig: TGroupShareConfig; constructor TGroupConfig.Create; begin FGroupAddresses := TStringList.Create; FImageAddresses := TStringList.Create; end; destructor TGroupConfig.Destroy; var AGroupAddress: TGroupAddress; begin while FGroupAddresses.Count > 0 do begin AGroupAddress := FGroupAddresses.Objects[0] as TGroupAddress; FGroupAddresses.Delete(0); FreeAndNil(AGroupAddress); end; while FImageAddresses.Count > 0 do begin AGroupAddress := FImageAddresses.Objects[0] as TGroupAddress; FImageAddresses.Delete(0); FreeAndNil(AGroupAddress); end; inherited; end; class function TGroupConfig.GetConfig: TGroupConfig; begin if config <> nil then Result := config else Result := TGroupConfig.Load; end; function TGroupConfig.GetImageIP: string; begin RandomImageServer; Result := FImageIP; end; function TGroupConfig.GetImagePort: Integer; begin Result := FImagePort; end; function TGroupConfig.GetIP: string; begin RandomGroupServer; Result := FIP; end; function TGroupConfig.GetPort: Integer; begin Result := FPort; end; class function TGroupConfig.Load: TGroupConfig; var AFullFileName: string; jo, AServer: ISuperObject; AGroupServerAddresses: TSuperArray; iLoop: Integer; AServerAddress: TGroupAddress; begin if config <> nil then FreeAndNil(config); config := TGroupConfig.Create; Result := config; Result.FGroupVersion := gvUnkown; AFullFileName := ExtractFilePath(ParamStr(0)) + GROUP_CONFIG; if not FileExists(AFullFileName) then begin Result.FGroupVersion := gvUnkown; Exit; end; //(const FileName: string; partial: boolean = true; const this: ISuperObject = nil; options: TSuperFindOptions = []; // const put: ISuperObject = nil; dt: TSuperType = stNull): try jo := TSuperObject.ParseFile(AFullFileName, False); Result.FGroupVersion := TGroupVersion(GetEnumValue(TypeInfo(TGroupVersion), jo['version'].AsString())); if jo.O['groupServerAddress'] <> nil then begin AGroupServerAddresses := jo.O['groupServerAddress'].AsArray(); for iLoop := 0 to AGroupServerAddresses.Length - 1 do begin AServer := AGroupServerAddresses.O[iLoop]; if not AServer['enable'].AsBoolean then Continue; AServerAddress := TGroupAddress.Create; AServerAddress.IP := AServer['ip'].AsString; AServerAddress.Port := AServer['port'].AsInteger; AServerAddress.Enable := True; Result.FGroupAddresses.AddObject(IntToStr(iLoop), AServerAddress); end; end; if jo.O['imageServerAddress'] <> nil then begin AGroupServerAddresses := jo.O['imageServerAddress'].AsArray(); for iLoop := 0 to AGroupServerAddresses.Length - 1 do begin AServer := AGroupServerAddresses.O[iLoop]; if not AServer['enable'].AsBoolean then Continue; AServerAddress := TGroupAddress.Create; AServerAddress.IP := AServer['ip'].AsString; AServerAddress.Port := AServer['port'].AsInteger; AServerAddress.Enable := True; Result.FImageAddresses.AddObject(IntToStr(iLoop), AServerAddress); end; end; except on E: Exception do begin Result.FGroupVersion := gvUnkown; Dialogs.ShowMessage(E.Message); end; end; end; procedure TGroupConfig.RandomGroupServer; var i: Integer; AGroupAddress: TGroupAddress; begin if FGroupAddresses.Count = 0 then Exit; i := Random(FGroupAddresses.Count); AGroupAddress := FGroupAddresses.Objects[i] as TGroupAddress; FIP := AGroupAddress.IP; FPort := AGroupAddress.Port; end; procedure TGroupConfig.RandomImageServer; var i: Integer; AGroupAddress: TGroupAddress; begin if FImageAddresses.Count = 0 then Exit; i := Random(FImageAddresses.Count); AGroupAddress := FImageAddresses.Objects[i] as TGroupAddress; FImageIP := AGroupAddress.IP; FImagePort := AGroupAddress.Port; end; { TGroupShareConfig } class function TGroupShareConfig.GetConfig: TGroupShareConfig; begin if groupShareConfig <> nil then Result := groupShareConfig else Result := TGroupShareConfig.Load; end; class function TGroupShareConfig.Load: TGroupShareConfig; var AFullFileName: string; jo: ISuperObject; begin if groupShareConfig <> nil then FreeAndNil(groupShareConfig); groupShareConfig := TGroupShareConfig.Create; Result := groupShareConfig; Result.FGroupShareVersion := gsvUnkown; AFullFileName := ExtractFilePath(ParamStr(0)) + GROUP_SHARE_CONFIG; if not FileExists(AFullFileName) then begin Result.FGroupShareVersion := gsvUnkown; Exit; end; try jo := TSuperObject.ParseFile(AFullFileName, False); Result.FGroupShareVersion := TGroupShareVersion(GetEnumValue(TypeInfo(TGroupShareVersion), jo['version'].AsString())); Result.FIP := jo['ip'].AsString(); // Result.FURL := jo['url'].AsString(); Result.FPort := jo['port'].AsInteger(); except on E: Exception do begin Result.FGroupShareVersion := gsvUnkown; Debug(E.Message, 'TGroupShareConfig.Load'); end; end; end; initialization finalization if config <> nil then FreeAndNil(config); if groupShareConfig <> nil then FreeAndNil(groupShareConfig); end.