Skip to content

Commit

Permalink
New feature: TProvider.Socket - When connecting receive the latest logs!
Browse files Browse the repository at this point in the history
  • Loading branch information
dliocode committed Jun 30, 2023
1 parent 140ba65 commit 0ad3587
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
4 changes: 3 additions & 1 deletion Samples/Socket/UProviderSocket.pas
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ procedure TForm2.FormCreate(Sender: TObject);

.InitSSL(nil)
.Port(8080)
.AutoStart(True)
.MaxConnection(0) // 0 - unlimited
.AutoStart(True)
.CustomMessage(nil) // Send log custom
.LogCacheSize(0) // When connecting receive the latest logs
;

Logger.AddProvider(FSocket);
Expand Down
72 changes: 60 additions & 12 deletions src/Providers/DataLogger.Provider.Socket.pas
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface
uses
DataLogger.Provider, DataLogger.Types,
IdTCPServer, IdHashSHA, IdSSLOpenSSL, IdContext, IdSSL, IdIOHandler, IdGlobal, IdCoderMIME, IdComponent,
System.SysUtils, System.Generics.Collections, System.JSON, System.Threading, System.SyncObjs, System.Classes, System.Types;
System.SysUtils, System.Generics.Collections, System.JSON, System.SyncObjs, System.Classes, System.Types;

type
TProviderSocketCustomMessage = reference to function(const AItem: TLoggerItem): string;
Expand All @@ -53,10 +53,15 @@ TProviderSocket = class(TDataLoggerProvider<TProviderSocket>)
FAutoStart: Boolean;
FCustomMessage: TProviderSocketCustomMessage;

FLogCacheSize: Integer;
FLogCache: TList<TLoggerItem>;

procedure SendMessages(const ACache: TArray<TLoggerItem>; const AContextID: string = '');
procedure DoOnConnect(AContext: TIdContext);
procedure DoOnExecute(AContext: TIdContext);
procedure DoOnDisconnect(AContext: TIdContext);
procedure CheckConnection(const AContext: TIdContext);
procedure AddLogCache(const ACache: TArray<TLoggerItem>);
protected
procedure Save(const ACache: TArray<TLoggerItem>); override;
public
Expand All @@ -70,6 +75,8 @@ TProviderSocket = class(TDataLoggerProvider<TProviderSocket>)
function AutoStart(const AValue: Boolean): TProviderSocket;
function CustomMessage(const AMessage: TProviderSocketCustomMessage): TProviderSocket;

function LogCacheSize(const ALogCacheSize: Integer): TProviderSocket;

function Start: TProviderSocket;
function Stop: TProviderSocket;

Expand Down Expand Up @@ -185,6 +192,9 @@ constructor TProviderSocket.Create;
AutoStart(True);
MaxConnection(0);
CustomMessage(nil);
LogCacheSize(0);

FLogCache := TList<TLoggerItem>.Create;
end;

procedure TProviderSocket.AfterConstruction;
Expand All @@ -198,6 +208,7 @@ destructor TProviderSocket.Destroy;
begin
Stop;

FLogCache.Free;
FListConnection.Free;
FIdTCPServer.Free;

Expand Down Expand Up @@ -255,6 +266,12 @@ function TProviderSocket.CustomMessage(const AMessage: TProviderSocketCustomMess
FCustomMessage := AMessage;
end;

function TProviderSocket.LogCacheSize(const ALogCacheSize: Integer): TProviderSocket;
begin
Result := Self;
FLogCacheSize := ALogCacheSize;
end;

function TProviderSocket.Start: TProviderSocket;
begin
Result := Self;
Expand Down Expand Up @@ -363,10 +380,6 @@ function TProviderSocket.ToJSON(const AFormat: Boolean): string;
end;

procedure TProviderSocket.Save(const ACache: TArray<TLoggerItem>);
var
LContexts: TArray<TDataLoggerSocketConnection>;
LItem: TLoggerItem;
LLog: string;
begin
if (Length(ACache) = 0) then
Exit;
Expand All @@ -377,9 +390,23 @@ procedure TProviderSocket.Save(const ACache: TArray<TLoggerItem>);
if not IsActive then
Exit;

if (FLogCacheSize > 0) then
AddLogCache(ACache);

if (TDataLoggerSocketListConnection(FListConnection).Count = 0) then
Exit;

SendMessages(ACache, '');
end;

procedure TProviderSocket.SendMessages(const ACache: TArray<TLoggerItem>; const AContextID: string = '');
var
LContexts: TArray<TDataLoggerSocketConnection>;
LItem: TLoggerItem;
LLog: string;
LContext: TDataLoggerSocketConnection;
LRetriesCount: Integer;
begin
LContexts := TDataLoggerSocketListConnection(FListConnection).ToArray;

for LItem in ACache do
Expand All @@ -392,13 +419,11 @@ procedure TProviderSocket.Save(const ACache: TArray<TLoggerItem>);
else
LLog := SerializeItem.LogItem(LItem).ToJSON;

TParallel.For(Low(LContexts), High(LContexts),
procedure(I: Integer)
var
LContext: TDataLoggerSocketConnection;
LRetriesCount: Integer;
for LContext in LContexts do
begin
LContext := LContexts[I];
if not AContextID.Trim.IsEmpty then
if (LContext.ID <> AContextID) then
Continue;

LRetriesCount := 0;

Expand Down Expand Up @@ -426,7 +451,10 @@ procedure TProviderSocket.Save(const ACache: TArray<TLoggerItem>);
Break;
end;
end;
end);

if not AContextID.Trim.IsEmpty then
Break;
end;
end;
end;

Expand All @@ -446,6 +474,9 @@ procedure TProviderSocket.DoOnConnect(AContext: TIdContext);

if Assigned(FOnConnection) then
FOnConnection(LConnection.ID);

if (FLogCacheSize > 0) and (FLogCache.Count > 0) then
SendMessages(FLogCache.ToArray, LConnection.ID);
end;

procedure TProviderSocket.DoOnExecute(AContext: TIdContext);
Expand Down Expand Up @@ -551,6 +582,23 @@ procedure TProviderSocket.CheckConnection(const AContext: TIdContext);
end;
end;

procedure TProviderSocket.AddLogCache(const ACache: TArray<TLoggerItem>);
var
I: Integer;
LCount: Integer;
LRemoveCount: Integer;
begin
FLogCache.AddRange(ACache);

LCount := FLogCache.Count;
if (LCount < FLogCacheSize) then
Exit;

LRemoveCount := (LCount - FLogCacheSize);
for I := 0 to Pred(LRemoveCount) do
FLogCache.Delete(0);
end;

{ TDataLoggerSocketConnection }

constructor TDataLoggerSocketConnection.Create(const AIdTCPServer: TIdTCPServer; const AIdContext: TIdContext);
Expand Down

0 comments on commit 0ad3587

Please sign in to comment.