From ffc35299f80058f7d92d60cf2c586c3baf1df5f3 Mon Sep 17 00:00:00 2001 From: MurilloLazzaretti Date: Fri, 19 Feb 2021 19:09:02 -0300 Subject: [PATCH] General corretions for .net --- ZapMQ.dproj | 8 ++++---- src/ZapMQ.DataModule.dfm | 6 +++--- src/ZapMQ.DataModule.pas | 4 ++-- src/ZapMQ.Message.JSON.pas | 12 ++++++++++++ src/ZapMQ.Message.pas | 5 +++++ src/ZapMQ.Methods.pas | 34 ++++++++++++++++++++++++---------- 6 files changed, 50 insertions(+), 19 deletions(-) diff --git a/ZapMQ.dproj b/ZapMQ.dproj index cc27070..ce7073a 100755 --- a/ZapMQ.dproj +++ b/ZapMQ.dproj @@ -242,14 +242,14 @@ true - - + + ZapMQ.exe true - - + + ZapMQ.exe true diff --git a/src/ZapMQ.DataModule.dfm b/src/ZapMQ.DataModule.dfm index f102d1f..907fab1 100755 --- a/src/ZapMQ.DataModule.dfm +++ b/src/ZapMQ.DataModule.dfm @@ -1,7 +1,7 @@ object ZapDataModule: TZapDataModule OldCreateOrder = False - Height = 245 - Width = 176 + Height = 272 + Width = 220 object Server: TDSServer AutoStart = False ChannelQueueSize = 1000 @@ -21,6 +21,6 @@ object ZapDataModule: TZapDataModule DSPort = 5679 Filters = <> Left = 48 - Top = 152 + Top = 144 end end diff --git a/src/ZapMQ.DataModule.pas b/src/ZapMQ.DataModule.pas index 3090e33..86e30d0 100755 --- a/src/ZapMQ.DataModule.pas +++ b/src/ZapMQ.DataModule.pas @@ -4,7 +4,7 @@ interface uses System.SysUtils, System.Classes, Datasnap.DSCommonServer, Datasnap.DSServer, - IPPeerServer, Datasnap.DSHTTP; + IPPeerServer, Datasnap.DSHTTP, IdHTTPWebBrokerBridge, Datasnap.DSHTTPWebBroker; type TZapDataModule = class(TDataModule) @@ -14,7 +14,7 @@ TZapDataModule = class(TDataModule) procedure MethodsGetClass(DSServerClass: TDSServerClass; var PersistentClass: TPersistentClass); private - { Private declarations } + public { Public declarations } end; diff --git a/src/ZapMQ.Message.JSON.pas b/src/ZapMQ.Message.JSON.pas index 9670ac0..0d3b236 100755 --- a/src/ZapMQ.Message.JSON.pas +++ b/src/ZapMQ.Message.JSON.pas @@ -12,15 +12,18 @@ TZapJSONMessage = class FId: string; FRPC: boolean; FTTL: Word; + FResponse: TJSONObject; procedure SetBody(const Value: TJSONObject); procedure SetId(const Value: string); procedure SetRPC(const Value: boolean); procedure SetTTL(const Value: Word); + procedure SetResponse(const Value: TJSONObject); public property Id : string read FId write SetId; property Body : TJSONObject read FBody write SetBody; property RPC : boolean read FRPC write SetRPC; property TTL : Word read FTTL write SetTTL; + property Response : TJSONObject read FResponse write SetResponse; function ToJSON : TJSONObject; destructor Destroy; override; class function FromJSON(const pJSONString : string) : TZapJSONMessage; @@ -37,6 +40,8 @@ destructor TZapJSONMessage.Destroy; begin if Assigned(Body) then Body.Free; + if Assigned(Response) then + Response.Free; inherited; end; @@ -69,6 +74,11 @@ procedure TZapJSONMessage.SetId(const Value: string); FId := Value; end; +procedure TZapJSONMessage.SetResponse(const Value: TJSONObject); +begin + FResponse := Value; +end; + procedure TZapJSONMessage.SetRPC(const Value: boolean); begin FRPC := Value; @@ -87,6 +97,8 @@ function TZapJSONMessage.ToJSON: TJSONObject; TEncoding.ASCII.GetBytes(Body.ToString), 0) as TJSONValue); Result.AddPair('RPC', TJSONBool.Create(FRPC)); Result.AddPair('TTL', TJSONNumber.Create(FTTL)); + Result.AddPair('Response', TJSONObject.ParseJSONValue( + TEncoding.ASCII.GetBytes(Response.ToString), 0) as TJSONValue); end; end. diff --git a/src/ZapMQ.Message.pas b/src/ZapMQ.Message.pas index e15f5de..c197871 100755 --- a/src/ZapMQ.Message.pas +++ b/src/ZapMQ.Message.pas @@ -112,6 +112,11 @@ function TZapMessage.Prepare: TZapJSONMessage; Result.Body := TJSONObject.ParseJSONValue( TEncoding.ASCII.GetBytes(FBody.ToString), 0) as TJSONObject; Result.RPC := FRPC; + if Assigned(FResponse) then + begin + Result.Response := TJSONObject.ParseJSONValue( + TEncoding.ASCII.GetBytes(FResponse.ToString), 0) as TJSONObject; + end; end; end. diff --git a/src/ZapMQ.Methods.pas b/src/ZapMQ.Methods.pas index 5fd45c0..43b67a9 100755 --- a/src/ZapMQ.Methods.pas +++ b/src/ZapMQ.Methods.pas @@ -4,7 +4,8 @@ interface uses System.Classes, - JSON; + JSON, + DataSnap.DSHTTPWebBroker; type {$METHODINFO ON} @@ -12,9 +13,9 @@ TZapMethods = class(TComponent) public function GetMessage(const pQueueName : string) : string; function GetRPCResponse(const pQueueName : string; const pIdMessage : string) : string; - function UpdateMessage(const pQueueName : string; pMessage : string) : string; - function UpdateRPCResponse(const pQueueName : string; - const pIdMessage : string; pResponse : string) : string; + function UpdateMessage(const pQueueName : string; const pMessage : string) : string; + function UpdateRPCResponse(const pQueueName : string; const pIdMessage : string; + const pMessage : string) : string; end; {$METHODINFO OFF} @@ -60,6 +61,8 @@ function TZapMethods.GetRPCResponse(const pQueueName, var Queue : TZapQueue; ZapMessage : TZapMessage; + ZapJSONMessage : TZapJSONMessage; + JSON : TJSONObject; begin Result := string.Empty; Queue := ZapMQ.Core.Context.Queues.Find(pQueueName); @@ -70,14 +73,24 @@ function TZapMethods.GetRPCResponse(const pQueueName, begin if ZapMessage.Status = zAnswered then begin - Result := ZapMessage.Response.ToString; - ZapMessage.Status := zSended; + ZapJSONMessage := ZapMessage.Prepare; + try + JSON := ZapJSONMessage.ToJSON; + try + Result := JSON.ToString; + ZapMessage.Status := zSended; + finally + JSON.Free; + end; + finally + ZapJSONMessage.Free; + end; end; end; end; end; -function TZapMethods.UpdateMessage(const pQueueName : string; pMessage : string) : string; +function TZapMethods.UpdateMessage(const pQueueName : string; const pMessage : string) : string; var ZapMessage : TZapMessage; ZapJSONMessage : TZapJSONMessage; @@ -90,6 +103,7 @@ function TZapMethods.UpdateMessage(const pQueueName : string; pMessage : string) ZapMessage.Body := TJSONObject.ParseJSONValue( TEncoding.ASCII.GetBytes(ZapJSONMessage.Body.ToString), 0) as TJSONObject; ZapMessage.RPC := ZapJSONMessage.RPC; + ZapMessage.Response := TJSONObject.Create; Result := ZapMessage.Id; ZapMQ.Core.Context.Queues.AddMessage(ZapMessage); finally @@ -97,8 +111,8 @@ function TZapMethods.UpdateMessage(const pQueueName : string; pMessage : string) end; end; -function TZapMethods.UpdateRPCResponse(const pQueueName : string; - const pIdMessage : string; pResponse : string): string; +function TZapMethods.UpdateRPCResponse(const pQueueName : string; const pIdMessage : string; + const pMessage : string): string; var ZapMessage : TZapMessage; Queue : TZapQueue; @@ -111,7 +125,7 @@ function TZapMethods.UpdateRPCResponse(const pQueueName : string; if Assigned(ZapMessage) then begin ZapMessage.Response := TJSONObject.ParseJSONValue( - TEncoding.ASCII.GetBytes(pResponse), 0) as TJSONObject; + TEncoding.ASCII.GetBytes(pMessage), 0) as TJSONObject; ZapMessage.Status := zAnswered; end; end;