Componentes.Terceros.RemObj.../internal/6.0.43.801/1/RemObjects Samples/RemObjects SDK for Delphi/Super TCP Channel Chat/AproovedClientsStorage.pas
2010-01-29 16:17:43 +00:00

137 lines
3.0 KiB
ObjectPascal

unit ApprovedClientsStorage;
interface
uses Classes, SyncObjs;
type
TApprovedClientsStorage = class(TObject)
private
fStorageFile: string;
fGuidList: TStringList;
fLock: TCriticalSection;
procedure SetStorageFile(aName: string);
protected
procedure Initialize;
procedure FlushStorage;
public
constructor Create;
destructor Destroy; override;
procedure AddClient(aClientGuid: string);
function IsClientKnown(aClientGuid: string): boolean;
property StorageFile: string read fStorageFile write SetStorageFile;
end;
var
StorageInstance: TApprovedClientsStorage;
implementation
uses SysUtils, uROClasses;
{ TApprovedClientsStorage }
procedure TApprovedClientsStorage.AddClient(aClientGuid: string);
var s: AnsiString;
begin
s := AnsiUpperCase({$IFDEF UNICODE}WideStringToAnsiString{$ENDIF}(aClientGuid));
if fGuidList.IndexOf(s) = -1 then begin
fLock.Enter;
try
if fGuidList.IndexOf(s) = -1 then begin
fGuidList.Add(s);
FlushStorage;
end;
finally
fLock.Leave;
end;
end;
end;
constructor TApprovedClientsStorage.Create;
begin
inherited;
fGuidList := TStringList.Create;
fLock := TCriticalSection.Create;
end;
destructor TApprovedClientsStorage.Destroy;
begin
fGuidList.Free;
fLock.Free;
inherited;
end;
procedure TApprovedClientsStorage.FlushStorage;
var
f: Text;
s: AnsiString;
i: integer;
begin
{$I-}
AssignFile(f, fStorageFile);
try
Rewrite(f);
for i := 0 to fGuidList.Count - 1 do begin
s := {$IFDEF UNICODE}WideStringToAnsiString{$ENDIF}(fGuidList[i]);
WriteLn(f, s);
if IOResult <> 0 then raise Exception.Create('Approved clients file write error.');
end;
finally
CloseFile(f);
end;
end;
procedure TApprovedClientsStorage.Initialize;
var
f: Text;
s: AnsiString;
begin
{$I-}
AssignFile(f, fStorageFile);
try
fGuidList.Clear;
Reset(f);
if IOResult <> 0 then Rewrite(f)
else
while not Eof(f) do begin
ReadLn(f, s);
if IOResult <> 0 then raise Exception.Create('Approved clients file read error.');
fGuidList.Append({$IFDEF UNICODE}AnsiStringToWideString{$ENDIF}(s));
end;
finally
CloseFile(f);
end;
end;
function TApprovedClientsStorage.IsClientKnown(aClientGuid: string): boolean;
var s: AnsiString;
begin
s := AnsiUpperCase({$IFDEF UNICODE}WideStringToAnsiString{$ENDIF}(aClientGuid));
fLock.Enter;
try
Result := fGuidList.IndexOf(s) > -1;
finally
fLock.Leave;
end;
end;
procedure TApprovedClientsStorage.SetStorageFile(aName: string);
var needInit: boolean;
begin
fLock.Enter;
try
needInit := fStorageFile = '';
fStorageFile := aName;
if needInit then Initialize;
finally
fLock.Leave;
end;
end;
initialization
StorageInstance := TApprovedClientsStorage.Create;
finalization
StorageInstance.Free;
end.