| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- 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,
- FGatewayIP: string;
- FPort,
- FImagePort,
- FGatewayPort: Integer;
- FGroupVersion: TGroupVersion;
- FGroupAddresses: TStringList;
- FImageAddresses: TStringList;
- FGatewayAddresses: TStringList;
- procedure RandomGroupServer;
- procedure RandomImageServer;
- function GetImageIP: string;
- function GetImagePort: Integer;
- function GetIP: string;
- function GetPort: Integer;
- function GetGatewayEnable: boolean;
- function GetImageHost: string;
- public
- constructor Create;
- destructor Destroy; override;
- class function Load: TGroupConfig;
- class function GetConfig: TGroupConfig;
- procedure RandomGatewayServer;
- property IP: string read GetIP write FIP;
- property ImageIP: string read GetImageIP write FImageIP;
- property GatewayIP: string read FGatewayIP write FGatewayIP;
- property Port: Integer read GetPort write FPort;
- property ImagePort: Integer read GetImagePort write FImagePort;
- property GatewayPort: Integer read FGatewayPort write FGatewayPort;
- property GroupVersion: TGroupVersion read FGroupVersion;
- property GatewayEnable: Boolean read GetGatewayEnable;
- property ImageHost: string read GetImageHost;
- 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 write 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;
- FGatewayAddresses := 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;
- while FGatewayAddresses.Count > 0 do
- begin
- AGroupAddress := FGatewayAddresses.Objects[0] as TGroupAddress;
- FGatewayAddresses.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.GetGatewayEnable: boolean;
- begin
- Result := FGatewayAddresses.Count <> 0
- end;
- function TGroupConfig.GetImageHost: string;
- const
- UPLOAD_URL: string = 'http://%s:%d/file/upload';
- begin
- RandomImageServer;
- Result := Format(UPLOAD_URL, [FImageIP, FImagePort]);
- 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['gatewayServerAddress'] <> nil then
- begin
- AGroupServerAddresses := jo.O['gatewayServerAddress'].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.FGatewayAddresses.AddObject(IntToStr(iLoop), AServerAddress);
- end;
- end;
- if (Result.FGatewayAddresses.Count = 0) and (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 (Result.FGatewayAddresses.Count = 0) and (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.RandomGatewayServer;
- var
- i: Integer;
- AGroupAddress: TGroupAddress;
- begin
- if FGatewayAddresses.Count = 0 then
- Exit;
- i := Random(FGatewayAddresses.Count);
- AGroupAddress := FGatewayAddresses.Objects[i] as TGroupAddress;
- FGatewayIP := AGroupAddress.IP;
- FGatewayPort := AGroupAddress.Port;
- 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.
|