Browse Source

Core: Replace NotifyEventToMany with TNotifyManyEvent & related changes

Herman Schoenfeld 7 years ago
parent
commit
712520c208

+ 38 - 2
src/core/UAccounts.pas

@@ -23,7 +23,8 @@ unit UAccounts;
 interface
 interface
 
 
 uses
 uses
-  Classes, UConst, UCrypto, SyncObjs, UThread, UBaseTypes;
+  Classes, SysUtils, UConst, UCrypto, SyncObjs, UThread, UBaseTypes;
+
 {$I config.inc}
 {$I config.inc}
 
 
 Type
 Type
@@ -287,8 +288,22 @@ Type
   TOrderedAccountList = Class;
   TOrderedAccountList = Class;
   TOrderedBlockAccountList = Class;
   TOrderedBlockAccountList = Class;
 
 
+  { TProgressNotify }
+
   TProgressNotify = procedure(sender : TObject; const message : AnsiString; curPos, totalCount : Int64) of object;
   TProgressNotify = procedure(sender : TObject; const message : AnsiString; curPos, totalCount : Int64) of object;
 
 
+  { TProgressNotifyMany }
+
+  TProgressNotifyMany = TArray<TProgressNotify>;
+
+  { TProgressNotifyManyHelper }
+
+  TProgressNotifyManyHelper = record helper for TProgressNotifyMany
+    procedure Add(listener : TProgressNotify);
+    procedure Remove(listener : TProgressNotify);
+    procedure Invoke(sender : TObject; const message : AnsiString; curPos, totalCount : Int64);
+  end;
+
   { TPCSafeBox }
   { TPCSafeBox }
 
 
   TAccountUpdateStyle = (aus_transaction_commit, aus_rollback, aus_commiting_from_otherchain);
   TAccountUpdateStyle = (aus_transaction_commit, aus_rollback, aus_commiting_from_otherchain);
@@ -523,7 +538,7 @@ Procedure Check_Safebox_Integrity(sb : TPCSafebox; title: String);
 implementation
 implementation
 
 
 uses
 uses
-  SysUtils, ULog, UOpenSSLdef, UOpenSSL, UAccountKeyStorage, math;
+  ULog, UOpenSSLdef, UOpenSSL, UAccountKeyStorage, math, UCommon;
 
 
 { This function is for testing purpose only.
 { This function is for testing purpose only.
   Will check if Account Names are well assigned and stored }
   Will check if Account Names are well assigned and stored }
@@ -1516,6 +1531,27 @@ begin
   list.Add(TObject(CT_NID_secp521r1)); // = 716
   list.Add(TObject(CT_NID_secp521r1)); // = 716
 end;
 end;
 
 
+{ TProgressNotifyManyHelper }
+
+procedure TProgressNotifyManyHelper.Add(listener : TProgressNotify);
+begin
+  if TArrayTool<TProgressNotify>.IndexOf(self, listener) = -1 then begin
+    TArrayTool<TProgressNotify>.Add(self, listener);
+  end;
+end;
+
+procedure TProgressNotifyManyHelper.Remove(listener : TProgressNotify);
+begin
+  TArrayTool<TProgressNotify>.Remove(self, listener);
+end;
+
+procedure TProgressNotifyManyHelper.Invoke(sender : TObject; const message : AnsiString; curPos, totalCount : Int64);
+var i : Integer;
+begin
+  for i := low(self) to high(self) do
+    self[i](sender, message, curPos, totalCount);
+end;
+
 { TPCSafeBox }
 { TPCSafeBox }
 
 
 // New on version 2: To reduce mem usage
 // New on version 2: To reduce mem usage

+ 3 - 66
src/core/UBaseTypes.pas

@@ -76,7 +76,8 @@ Type
     class function GetElapsedMilliseconds(Const previousTickCount : TTickCount) : Int64;
     class function GetElapsedMilliseconds(Const previousTickCount : TTickCount) : Int64;
   End;
   End;
 
 
-  TNotifyEventToMany = Class
+  //HS -- removed, TNotifyManyEvent in UCommon.pas
+  {TNotifyEventToMany = Class
   private
   private
     FList : Array of TNotifyEvent;
     FList : Array of TNotifyEvent;
   public
   public
@@ -87,7 +88,7 @@ Type
     function Count : Integer;
     function Count : Integer;
     procedure Delete(index : Integer);
     procedure Delete(index : Integer);
     Constructor Create;
     Constructor Create;
-  End;
+  End;}
 
 
 
 
 implementation
 implementation
@@ -310,69 +311,5 @@ begin
   Result := {$IFDEF CPU64}GetTickCount64{$ELSE}{$IFNDEF FPC}Windows.{$ELSE}SysUtils.{$ENDIF}GetTickCount{$ENDIF};
   Result := {$IFDEF CPU64}GetTickCount64{$ELSE}{$IFNDEF FPC}Windows.{$ELSE}SysUtils.{$ENDIF}GetTickCount{$ENDIF};
 end;
 end;
 
 
-{ TNotifyEventToMany }
-
-procedure TNotifyEventToMany.Add(newNotifyEvent: TNotifyEvent);
-begin
-  if IndexOf(newNotifyEvent)>=0 then exit;
-  SetLength(FList,length(FList)+1);
-  FList[high(FList)] := newNotifyEvent;
-end;
-
-function TNotifyEventToMany.Count: Integer;
-begin
-  Result := Length(FList);
-end;
-
-constructor TNotifyEventToMany.Create;
-begin
-  SetLength(FList,0);
-end;
-
-procedure TNotifyEventToMany.Delete(index: Integer);
-Var i : Integer;
-begin
-  if (index<0) Or (index>High(FList)) then raise Exception.Create('Invalid index '+Inttostr(index)+' in '+Self.ClassName+'.Delete');
-  for i := index+1 to high(FList) do begin
-    FList[i-1] := FList[i];
-  end;
-  SetLength(FList,length(FList)-1);
-end;
-
-function TNotifyEventToMany.IndexOf(search: TNotifyEvent): Integer;
-begin
-  for Result := low(FList) to high(FList) do begin
-    if (TMethod(FList[Result]).Code = TMethod(search).Code) And
-       (TMethod(FList[Result]).Data = TMethod(search).Data) then Exit;
-  end;
-  Result := -1;
-end;
-
-procedure TNotifyEventToMany.Invoke(sender: TObject);
-Var i,j : Integer;
-begin
-  j := -1;
-  Try
-    for i := low(FList) to high(FList) do begin
-      j := i;
-      FList[i](sender);
-    end;
-  Except
-    On E:Exception do begin
-      E.Message := Format('Error TNotifyManyEventHelper.Invoke %d/%d (%s) %s',[j+1,length(FList),E.ClassType,E.Message]);
-      Raise;
-    end;
-  End;
-end;
-
-procedure TNotifyEventToMany.Remove(removeNotifyEvent: TNotifyEvent);
-Var i : Integer;
-begin
-  i := IndexOf(removeNotifyEvent);
-  if (i>=0) then begin
-    Delete(i);
-  end;
-end;
-
 end.
 end.
 
 

+ 4 - 4
src/core/UNetProtocol.pas

@@ -29,7 +29,7 @@ Uses
   {LCLIntf, LCLType, LMessages,}
   {LCLIntf, LCLType, LMessages,}
 {$ENDIF}
 {$ENDIF}
   UBlockChain, Classes, SysUtils, UAccounts, UThread,
   UBlockChain, Classes, SysUtils, UAccounts, UThread,
-  UCrypto, UTCPIP, SyncObjs, UBaseTypes;
+  UCrypto, UTCPIP, SyncObjs, UBaseTypes, UCommon;
 
 
 {$I config.inc}
 {$I config.inc}
 
 
@@ -256,7 +256,7 @@ Type
     FRegisteredRequests : TPCThreadList;
     FRegisteredRequests : TPCThreadList;
     FIsDiscoveringServers : Boolean;
     FIsDiscoveringServers : Boolean;
     FIsGettingNewBlockChainFromClient : Boolean;
     FIsGettingNewBlockChainFromClient : Boolean;
-    FOnConnectivityChanged : TNotifyEventToMany;
+    FOnConnectivityChanged : TNotifyManyEvent;
     FOnNetConnectionsUpdated: TNotifyEvent;
     FOnNetConnectionsUpdated: TNotifyEvent;
     FOnNodeServersUpdated: TNotifyEvent;
     FOnNodeServersUpdated: TNotifyEvent;
     FOnBlackListUpdated: TNotifyEvent;
     FOnBlackListUpdated: TNotifyEvent;
@@ -319,7 +319,7 @@ Type
     Property IsGettingNewBlockChainFromClient : Boolean read FIsGettingNewBlockChainFromClient;
     Property IsGettingNewBlockChainFromClient : Boolean read FIsGettingNewBlockChainFromClient;
     Property MaxRemoteOperationBlock : TOperationBlock read FMaxRemoteOperationBlock;
     Property MaxRemoteOperationBlock : TOperationBlock read FMaxRemoteOperationBlock;
     Property NodePrivateKey : TECPrivateKey read FNodePrivateKey;
     Property NodePrivateKey : TECPrivateKey read FNodePrivateKey;
-    property OnConnectivityChanged : TNotifyEventToMany read FOnConnectivityChanged;
+    property OnConnectivityChanged : TNotifyManyEvent read FOnConnectivityChanged;
     Property OnNetConnectionsUpdated : TNotifyEvent read FOnNetConnectionsUpdated write FOnNetConnectionsUpdated;
     Property OnNetConnectionsUpdated : TNotifyEvent read FOnNetConnectionsUpdated write FOnNetConnectionsUpdated;
     Property OnNodeServersUpdated : TNotifyEvent read FOnNodeServersUpdated write FOnNodeServersUpdated;
     Property OnNodeServersUpdated : TNotifyEvent read FOnNodeServersUpdated write FOnNodeServersUpdated;
     Property OnBlackListUpdated : TNotifyEvent read FOnBlackListUpdated write FOnBlackListUpdated;
     Property OnBlackListUpdated : TNotifyEvent read FOnBlackListUpdated write FOnBlackListUpdated;
@@ -1146,7 +1146,7 @@ begin
   SetLength(FFixedServers,0);
   SetLength(FFixedServers,0);
   FMaxRemoteOperationBlock := CT_OperationBlock_NUL;
   FMaxRemoteOperationBlock := CT_OperationBlock_NUL;
   FNetStatistics := CT_TNetStatistics_NUL;
   FNetStatistics := CT_TNetStatistics_NUL;
-  FOnConnectivityChanged := TNotifyEventToMany.Create;
+  //FOnConnectivityChanged := TNotifyEventToMany.Create; //xxxxxxxxxxxxxxxxxx HS - removed TNotifyEventMany auto-collected
   FOnStatisticsChanged := Nil;
   FOnStatisticsChanged := Nil;
   FOnNetConnectionsUpdated := Nil;
   FOnNetConnectionsUpdated := Nil;
   FOnNodeServersUpdated := Nil;
   FOnNodeServersUpdated := Nil;

+ 38 - 2
src/core/UNode.pas

@@ -34,7 +34,7 @@ unit UNode;
 interface
 interface
 
 
 uses
 uses
-  Classes, UBlockChain, UNetProtocol, UAccounts, UCrypto, UThread, SyncObjs, ULog;
+  Classes, SysUtils, UBlockChain, UNetProtocol, UAccounts, UCrypto, UThread, SyncObjs, ULog;
 
 
 {$I config.inc}
 {$I config.inc}
 
 
@@ -117,7 +117,22 @@ Type
     Constructor Create(ANodeNotifyEvents : TNodeNotifyEvents);
     Constructor Create(ANodeNotifyEvents : TNodeNotifyEvents);
   End;
   End;
 
 
+  { TNodeMessage Event }
+
   TNodeMessageEvent = Procedure(NetConnection : TNetConnection; MessageData : TRawBytes) of object;
   TNodeMessageEvent = Procedure(NetConnection : TNetConnection; MessageData : TRawBytes) of object;
+
+  { TNodeMessageManyEvent }
+
+  TNodeMessageManyEvent = TArray<TNodeMessageEvent>;
+
+  { TNodeMessageManyEventHelper }
+
+  TNodeMessageManyEventHelper = record helper for TNodeMessageManyEvent
+    procedure Add(listener : TNodeMessageEvent);
+    procedure Remove(listener : TNodeMessageEvent);
+    procedure Invoke(NetConnection : TNetConnection; MessageData : TRawBytes);
+  end;
+
   { TNodeNotifyEvents is ThreadSafe and will only notify in the main thread }
   { TNodeNotifyEvents is ThreadSafe and will only notify in the main thread }
   TNodeNotifyEvents = Class(TComponent)
   TNodeNotifyEvents = Class(TComponent)
   private
   private
@@ -169,7 +184,7 @@ Type
 
 
 implementation
 implementation
 
 
-Uses UOpTransaction, SysUtils,  UConst, UTime;
+Uses UOpTransaction, UConst, UTime, UCommon;
 
 
 var _Node : TNode;
 var _Node : TNode;
 
 
@@ -1118,6 +1133,27 @@ begin
   FNodeLog.FileName := Value;
   FNodeLog.FileName := Value;
 end;
 end;
 
 
+{ TNodeMessageManyEventHelper }
+
+procedure TNodeMessageManyEventHelper.Add(listener : TNodeMessageEvent);
+begin
+  if TArrayTool<TNodeMessageEvent>.IndexOf(self, listener) = -1 then begin
+    TArrayTool<TNodeMessageEvent>.Add(self, listener);
+  end;
+end;
+
+procedure TNodeMessageManyEventHelper.Remove(listener : TNodeMessageEvent);
+begin
+  TArrayTool<TNodeMessageEvent>.Remove(self, listener);
+end;
+
+procedure TNodeMessageManyEventHelper.Invoke(NetConnection : TNetConnection; MessageData : TRawBytes);
+var i : Integer;
+begin
+  for i := low(self) to high(self) do
+    self[i](NetConnection, MessageData);
+end;
+
 { TNodeNotifyEvents }
 { TNodeNotifyEvents }
 
 
 constructor TNodeNotifyEvents.Create(AOwner: TComponent);
 constructor TNodeNotifyEvents.Create(AOwner: TComponent);

+ 3 - 4
src/core/USettings.pas

@@ -25,7 +25,7 @@ unit USettings;
 interface
 interface
 
 
 uses
 uses
-  UAppParams, UBaseTypes;
+  UAppParams, UBaseTypes, UCommon;
 
 
 const
 const
   // App Params
   // App Params
@@ -62,7 +62,7 @@ type
 
 
   TSettings = class
   TSettings = class
     private
     private
-      class var FOnChanged : TNotifyEventToMany;
+      class var FOnChanged : TNotifyManyEvent;
       class var FAppParams : TAppParams;
       class var FAppParams : TAppParams;
       class function GetInternetServerPort : Integer; static;
       class function GetInternetServerPort : Integer; static;
       class procedure SetInternetServerPort(AInt:Integer); static;
       class procedure SetInternetServerPort(AInt:Integer); static;
@@ -101,7 +101,7 @@ type
     public
     public
       class procedure Load;
       class procedure Load;
       class procedure Save;
       class procedure Save;
-      class property OnChanged : TNotifyEventToMany read FOnChanged;
+      class property OnChanged : TNotifyManyEvent read FOnChanged;
       class property InternetServerPort : Integer read GetInternetServerPort write SetInternetServerPort;
       class property InternetServerPort : Integer read GetInternetServerPort write SetInternetServerPort;
       class property RpcPortEnabled : boolean read GetRpcPortEnabled write SetRpcPortEnabled;
       class property RpcPortEnabled : boolean read GetRpcPortEnabled write SetRpcPortEnabled;
       class property RpcAllowedIPs : string read GetRpcAllowedIPs write SetRpcAllowedIPs;
       class property RpcAllowedIPs : string read GetRpcAllowedIPs write SetRpcAllowedIPs;
@@ -345,7 +345,6 @@ end;
 
 
 initialization
 initialization
   TSettings.FAppParams := nil;
   TSettings.FAppParams := nil;
-  TSettings.FOnChanged := TNotifyEventToMany.Create;
 
 
 finalization
 finalization
   if Assigned(TSettings.FAppParams) then
   if Assigned(TSettings.FAppParams) then

+ 3 - 5
src/core/UWallet.pas

@@ -23,7 +23,7 @@ unit UWallet;
 interface
 interface
 
 
 uses
 uses
-  Classes, USettings, UBlockChain, UAccounts, UCrypto, UBaseTypes;
+  Classes, USettings, UBlockChain, UAccounts, UCrypto, UBaseTypes, UCommon;
 
 
 Type
 Type
   TWalletKey = Record
   TWalletKey = Record
@@ -44,7 +44,7 @@ Type
     FIsValidPassword: Boolean;
     FIsValidPassword: Boolean;
     FWalletFileName: AnsiString;
     FWalletFileName: AnsiString;
     FIsReadingStream : Boolean;
     FIsReadingStream : Boolean;
-    FOnChanged: TNotifyEventToMany;
+    FOnChanged: TNotifyManyEvent;
     function GetHasPassword : boolean;
     function GetHasPassword : boolean;
     function GetKey(index: Integer): TWalletKey;
     function GetKey(index: Integer): TWalletKey;
     procedure SetWalletPassword(const Value: AnsiString);
     procedure SetWalletPassword(const Value: AnsiString);
@@ -67,7 +67,7 @@ Type
     Procedure Clear; virtual;
     Procedure Clear; virtual;
     Function Count : Integer;
     Function Count : Integer;
     Property WalletFileName : AnsiString read FWalletFileName write SetWalletFileName;
     Property WalletFileName : AnsiString read FWalletFileName write SetWalletFileName;
-    Property OnChanged : TNotifyEventToMany read FOnChanged;
+    Property OnChanged : TNotifyManyEvent read FOnChanged;
     Procedure SetName(index : Integer; Const newName : AnsiString);
     Procedure SetName(index : Integer; Const newName : AnsiString);
     Function LockWallet : Boolean;
     Function LockWallet : Boolean;
   End;
   End;
@@ -227,7 +227,6 @@ begin
   FWalletPassword := '';
   FWalletPassword := '';
   FSearchableKeys := TList.Create;
   FSearchableKeys := TList.Create;
   FIsReadingStream := false;
   FIsReadingStream := false;
-  FOnChanged := TNotifyEventToMany.Create;
 end;
 end;
 
 
 procedure TWalletKeys.Delete(index: Integer);
 procedure TWalletKeys.Delete(index: Integer);
@@ -243,7 +242,6 @@ end;
 
 
 destructor TWalletKeys.destroy;
 destructor TWalletKeys.destroy;
 begin
 begin
-  FreeAndNil(FOnChanged);
   FreeAndNil(FWalletFileStream);
   FreeAndNil(FWalletFileStream);
   Clear;
   Clear;
   FreeAndNil(FSearchableKeys);
   FreeAndNil(FSearchableKeys);

+ 3 - 1
src/gui-classic/UFRMOperation.pas

@@ -25,6 +25,7 @@ interface
 uses
 uses
 {$IFnDEF FPC}
 {$IFnDEF FPC}
   Windows,
   Windows,
+  System.Actions,
 {$ELSE}
 {$ELSE}
   LCLIntf, LCLType, LMessages,
   LCLIntf, LCLType, LMessages,
 {$ENDIF}
 {$ENDIF}
@@ -180,7 +181,8 @@ type
 implementation
 implementation
 
 
 uses
 uses
-  UECIES, UConst, UOpTransaction, UFRMNewPrivateKeyType, UAES, UFRMWalletKeys;
+  UECIES, UConst, UOpTransaction, UFRMNewPrivateKeyType, UAES, UFRMWalletKeys,
+  UCommon;
 
 
 {$IFnDEF FPC}
 {$IFnDEF FPC}
   {$R *.dfm}
   {$R *.dfm}

+ 2 - 1
src/gui-classic/UFRMWallet.pas

@@ -313,7 +313,8 @@ implementation
 Uses UFolderHelper, UOpenSSL, UOpenSSLdef, UTime, UFileStorage,
 Uses UFolderHelper, UOpenSSL, UOpenSSLdef, UTime, UFileStorage,
   UThread, UOpTransaction, UECIES, UFRMPascalCoinWalletConfig,
   UThread, UOpTransaction, UECIES, UFRMPascalCoinWalletConfig,
   UFRMOperationsExplorer,
   UFRMOperationsExplorer,
-  UFRMAbout, UFRMOperation, UFRMWalletKeys, UFRMPayloadDecoder, UFRMNodesIp, UFRMMemoText, USettings;
+  UFRMAbout, UFRMOperation, UFRMWalletKeys, UFRMPayloadDecoder, UFRMNodesIp, UFRMMemoText,
+  USettings, UCommon;
 
 
 Type
 Type
 
 

+ 1 - 1
src/gui-classic/UFRMWalletKeys.pas

@@ -95,7 +95,7 @@ uses
 {$ELSE}
 {$ELSE}
   LCLIntf, LCLType,
   LCLIntf, LCLType,
 {$ENDIF}
 {$ENDIF}
-  UCrypto, UAccounts, UFRMNewPrivateKeyType, UAES, UBaseTypes;
+  UCrypto, UAccounts, UFRMNewPrivateKeyType, UAES, UBaseTypes, UCommon;
 
 
 {$IFnDEF FPC}
 {$IFnDEF FPC}
   {$R *.dfm}
   {$R *.dfm}

+ 1 - 1
src/pascalcoin_wallet_classic.lpi

@@ -213,7 +213,7 @@
     </Target>
     </Target>
     <SearchPaths>
     <SearchPaths>
       <IncludeFiles Value="$(ProjOutDir)"/>
       <IncludeFiles Value="$(ProjOutDir)"/>
-      <OtherUnitFiles Value="core;gui-classic;libraries\synapse;libraries\pascalcoin"/>
+      <OtherUnitFiles Value="core;gui-classic;libraries\synapse;libraries\sphere10;libraries\hashlib4pascal;libraries\generics.collections;libraries\pascalcoin"/>
       <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
       <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
     </SearchPaths>
     </SearchPaths>
     <Parsing>
     <Parsing>