2011-04-14 11:13:25 +02:00
|
|
|
// Stomp Client for Embarcadero Delphi & FreePascal
|
2013-10-15 10:14:36 +02:00
|
|
|
// Tested With ApacheMQ 5.2/5.3, Apache Apollo 1.2
|
|
|
|
// Copyright (c) 2009-2012 Daniele Teti
|
2011-04-14 11:13:25 +02:00
|
|
|
//
|
|
|
|
// Contributors:
|
|
|
|
// Daniel Gaspary: dgaspary@gmail.com
|
2013-10-10 17:08:06 +02:00
|
|
|
// Oliver Marr: oliver.sn@wmarr.de
|
2013-05-10 18:00:10 +02:00
|
|
|
// Marco Mottadelli: mottadelli75@gmail.com
|
2011-04-14 11:13:25 +02:00
|
|
|
// WebSite: www.danieleteti.it
|
|
|
|
// email:d.teti@bittime.it
|
|
|
|
// *******************************************************
|
2012-04-18 19:19:31 +02:00
|
|
|
|
|
|
|
unit StompClient;
|
|
|
|
|
|
|
|
// For FreePascal users:
|
|
|
|
// Automatically selected synapse tcp library
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE DELPHI}
|
|
|
|
{$DEFINE USESYNAPSE}
|
|
|
|
{$ENDIF}
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
// For Delphi users:
|
|
|
|
// Decomment following line to use synapse also in Delphi
|
|
|
|
{ .$DEFINE USESYNAPSE }
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
StompTypes,
|
|
|
|
SysUtils,
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$IFNDEF USESYNAPSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
IdTCPClient,
|
|
|
|
IdException,
|
|
|
|
IdExceptionCore,
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ELSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
synsock,
|
|
|
|
blcksock,
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
Classes;
|
|
|
|
|
|
|
|
type
|
|
|
|
{ TStompClient }
|
|
|
|
|
2013-10-10 17:08:06 +02:00
|
|
|
TSenderFrameEvent = procedure(AFrame: IStompFrame) of object;
|
2012-12-27 23:25:09 +01:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
TStompClient = class(TInterfacedObject, IStompClient)
|
|
|
|
private
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$IFDEF USESYNAPSE}
|
|
|
|
|
|
|
|
FSynapseTCP : TTCPBlockSocket;
|
2012-04-18 19:19:31 +02:00
|
|
|
FSynapseConnected: boolean;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ELSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
FTCP: TIdTCPClient;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
FHeaders : IStompHeaders;
|
|
|
|
FPassword : string;
|
|
|
|
FUserName : string;
|
|
|
|
FTimeout : Integer;
|
|
|
|
FSession : string;
|
|
|
|
FInTransaction : boolean;
|
|
|
|
FTransactions : TStringList;
|
|
|
|
FReceiptTimeout : Integer;
|
|
|
|
FServerProtocolVersion : string;
|
2012-04-18 19:19:31 +02:00
|
|
|
FClientAcceptProtocolVersion: TStompAcceptProtocol;
|
2013-10-10 17:08:06 +02:00
|
|
|
FServer : string;
|
|
|
|
FOnBeforeSendFrame : TSenderFrameEvent;
|
|
|
|
FOnAfterSendFrame : TSenderFrameEvent;
|
2012-04-18 19:19:31 +02:00
|
|
|
procedure SetReceiptTimeout(const Value: Integer);
|
|
|
|
|
|
|
|
protected
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$IFDEF USESYNAPSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
procedure SynapseSocketCallBack(Sender: TObject; Reason: THookSocketReason;
|
|
|
|
const Value: string);
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
procedure Init;
|
|
|
|
procedure DeInit;
|
|
|
|
procedure MergeHeaders(var AFrame: IStompFrame; var AHeaders: IStompHeaders);
|
|
|
|
procedure SendFrame(AFrame: IStompFrame);
|
|
|
|
|
|
|
|
public
|
|
|
|
function SetPassword(const Value: string): IStompClient;
|
|
|
|
function SetUserName(const Value: string): IStompClient;
|
|
|
|
function Receive(out StompFrame: IStompFrame; ATimeout: Integer): boolean; overload;
|
|
|
|
function Receive: IStompFrame; overload;
|
|
|
|
function Receive(ATimeout: Integer): IStompFrame; overload;
|
|
|
|
procedure Receipt(const ReceiptID: string);
|
|
|
|
procedure Connect(Host: string = '127.0.0.1'; Port: Integer = DEFAULT_STOMP_PORT;
|
|
|
|
ClientID: string = ''; AcceptVersion: TStompAcceptProtocol = STOMP_Version_1_0);
|
2013-10-15 10:14:36 +02:00
|
|
|
procedure Disconnect;
|
2012-04-18 19:19:31 +02:00
|
|
|
procedure Subscribe(QueueOrTopicName: string; Ack: TAckMode = amAuto;
|
|
|
|
Headers: IStompHeaders = nil);
|
|
|
|
procedure Unsubscribe(Queue: string);
|
|
|
|
procedure Send(QueueOrTopicName: string; TextMessage: string; Headers: IStompHeaders = nil);
|
|
|
|
overload;
|
|
|
|
procedure Send(QueueOrTopicName: string; TextMessage: string; TransactionIdentifier: string;
|
|
|
|
Headers: IStompHeaders = nil); overload;
|
|
|
|
procedure Ack(const MessageID: string; const TransactionIdentifier: string = '');
|
2012-12-27 23:25:09 +01:00
|
|
|
{ STOMP 1.1 }
|
|
|
|
procedure Nack(const MessageID: string; const TransactionIdentifier: string = '');
|
2012-04-18 19:19:31 +02:00
|
|
|
procedure BeginTransaction(const TransactionIdentifier: string);
|
|
|
|
procedure CommitTransaction(const TransactionIdentifier: string);
|
|
|
|
procedure AbortTransaction(const TransactionIdentifier: string);
|
|
|
|
/// ////////////
|
|
|
|
constructor Create; virtual;
|
|
|
|
destructor Destroy; override;
|
|
|
|
function Connected: boolean;
|
|
|
|
function SetReceiveTimeout(const AMilliSeconds: Cardinal): IStompClient;
|
2013-10-10 17:08:06 +02:00
|
|
|
function GetProtocolVersion: string;
|
|
|
|
function GetServer: string;
|
2012-04-18 19:19:31 +02:00
|
|
|
function GetSession: string;
|
|
|
|
property ReceiptTimeout: Integer read FReceiptTimeout write SetReceiptTimeout;
|
|
|
|
property Transactions: TStringList read FTransactions;
|
2013-10-10 17:08:06 +02:00
|
|
|
// * Manage Events
|
2012-12-27 23:25:09 +01:00
|
|
|
property OnBeforeSendFrame: TSenderFrameEvent read FOnBeforeSendFrame write FOnBeforeSendFrame;
|
|
|
|
property OnAfterSendFrame: TSenderFrameEvent read FOnAfterSendFrame write FOnAfterSendFrame;
|
2012-04-18 19:19:31 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{$IFDEF FPC}
|
|
|
|
|
|
|
|
|
|
|
|
const
|
|
|
|
CHAR0 = #0;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
{$ELSE}
|
|
|
|
|
|
|
|
|
|
|
|
uses
|
2013-05-10 18:00:10 +02:00
|
|
|
// Windows, // Remove windows unit for compiling on ios
|
2013-10-10 17:08:06 +02:00
|
|
|
IdGlobal,
|
2012-04-18 19:19:31 +02:00
|
|
|
Character;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
{$ENDIF}
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
{ TStompClient }
|
|
|
|
|
|
|
|
procedure TStompClient.AbortTransaction(const TransactionIdentifier: string);
|
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
2013-10-10 17:08:06 +02:00
|
|
|
if FTransactions.IndexOf(TransactionIdentifier) > - 1 then
|
2012-04-18 19:19:31 +02:00
|
|
|
begin
|
|
|
|
Frame := TStompFrame.Create;
|
|
|
|
Frame.SetCommand('ABORT');
|
|
|
|
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
|
|
|
|
SendFrame(Frame);
|
2013-10-15 10:14:36 +02:00
|
|
|
FInTransaction := False;
|
2012-04-18 19:19:31 +02:00
|
|
|
FTransactions.Delete(FTransactions.IndexOf(TransactionIdentifier));
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise EStomp.CreateFmt('Abort Transaction Error. Transaction [%s] not found',
|
|
|
|
[TransactionIdentifier]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClient.Ack(const MessageID: string; const TransactionIdentifier: string);
|
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
|
|
|
Frame := TStompFrame.Create;
|
|
|
|
Frame.SetCommand('ACK');
|
2013-10-15 10:14:36 +02:00
|
|
|
Frame.GetHeaders.Add(TStompHeaders.MESSAGE_ID, MessageID);
|
2012-04-18 19:19:31 +02:00
|
|
|
if TransactionIdentifier <> '' then
|
|
|
|
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
|
|
|
|
SendFrame(Frame);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClient.BeginTransaction(const TransactionIdentifier: string);
|
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
2013-10-10 17:08:06 +02:00
|
|
|
if FTransactions.IndexOf(TransactionIdentifier) = - 1 then
|
2012-04-18 19:19:31 +02:00
|
|
|
begin
|
|
|
|
Frame := TStompFrame.Create;
|
|
|
|
Frame.SetCommand('BEGIN');
|
|
|
|
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
|
|
|
|
SendFrame(Frame);
|
|
|
|
// CheckReceipt(Frame);
|
|
|
|
FInTransaction := True;
|
|
|
|
FTransactions.Add(TransactionIdentifier);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise EStomp.CreateFmt('Begin Transaction Error. Transaction [%s] still open',
|
|
|
|
[TransactionIdentifier]);
|
|
|
|
end;
|
|
|
|
|
2013-10-15 10:14:36 +02:00
|
|
|
// procedure TStompClient.CheckReceipt(Frame: TStompFrame);
|
|
|
|
// var
|
|
|
|
// ReceiptID: string;
|
|
|
|
// begin
|
|
|
|
// if FEnableReceipts then
|
|
|
|
// begin
|
|
|
|
// ReceiptID := inttostr(GetTickCount);
|
|
|
|
// Frame.GetHeaders.Add('receipt', ReceiptID);
|
|
|
|
// SendFrame(Frame);
|
|
|
|
// Receipt(ReceiptID);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// SendFrame(Frame);
|
|
|
|
// end;
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
procedure TStompClient.CommitTransaction(const TransactionIdentifier: string);
|
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
2013-10-10 17:08:06 +02:00
|
|
|
if FTransactions.IndexOf(TransactionIdentifier) > - 1 then
|
2012-04-18 19:19:31 +02:00
|
|
|
begin
|
|
|
|
Frame := TStompFrame.Create;
|
|
|
|
Frame.SetCommand('COMMIT');
|
|
|
|
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
|
|
|
|
SendFrame(Frame);
|
2013-10-15 10:14:36 +02:00
|
|
|
FInTransaction := False;
|
2012-04-18 19:19:31 +02:00
|
|
|
FTransactions.Delete(FTransactions.IndexOf(TransactionIdentifier));
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise EStomp.CreateFmt('Commit Transaction Error. Transaction [%s] not found',
|
|
|
|
[TransactionIdentifier]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClient.Connect(Host: string; Port: Integer; ClientID: string;
|
|
|
|
AcceptVersion: TStompAcceptProtocol);
|
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
Init;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$IFDEF USESYNAPSE}
|
|
|
|
|
2013-10-15 10:14:36 +02:00
|
|
|
FSynapseConnected := False;
|
2012-04-18 19:19:31 +02:00
|
|
|
FSynapseTCP.Connect(Host, intToStr(Port));
|
|
|
|
FSynapseConnected := True;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ELSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
FTCP.Connect(Host, Port);
|
|
|
|
FTCP.IOHandler.MaxLineLength := MaxInt;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
Frame := TStompFrame.Create;
|
|
|
|
Frame.SetCommand('CONNECT');
|
|
|
|
|
|
|
|
FClientAcceptProtocolVersion := AcceptVersion;
|
|
|
|
if STOMP_Version_1_1 in [FClientAcceptProtocolVersion] then
|
|
|
|
begin
|
|
|
|
Frame.GetHeaders.Add('heart-beat', '0,1000'); // stomp 1.1
|
|
|
|
Frame.GetHeaders.Add('accept-version', '1.1'); // stomp 1.1
|
|
|
|
end;
|
|
|
|
|
|
|
|
Frame.GetHeaders.Add('login', FUserName).Add('passcode', FPassword);
|
|
|
|
if ClientID <> '' then
|
|
|
|
Frame.GetHeaders.Add('client-id', ClientID);
|
|
|
|
SendFrame(Frame);
|
|
|
|
Frame := nil;
|
|
|
|
while Frame = nil do
|
|
|
|
Frame := Receive;
|
|
|
|
if Frame.GetCommand = 'ERROR' then
|
|
|
|
raise EStomp.Create(Frame.output);
|
|
|
|
if Frame.GetCommand = 'CONNECTED' then
|
|
|
|
begin
|
|
|
|
FSession := Frame.GetHeaders.Value('session');
|
|
|
|
FServerProtocolVersion := Frame.GetHeaders.Value('version'); // stomp 1.1
|
|
|
|
FServer := Frame.GetHeaders.Value('server'); // stomp 1.1
|
|
|
|
end;
|
|
|
|
{ todo: 'Call event?' }
|
|
|
|
except
|
|
|
|
on E: Exception do
|
|
|
|
begin
|
|
|
|
raise EStomp.Create(E.message);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompClient.Connected: boolean;
|
|
|
|
begin
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$IFDEF USESYNAPSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
Result := Assigned(FSynapseTCP) and FSynapseConnected;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ELSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
Result := Assigned(FTCP) and FTCP.Connected;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TStompClient.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
2013-10-15 10:14:36 +02:00
|
|
|
FInTransaction := False;
|
2012-04-18 19:19:31 +02:00
|
|
|
FSession := '';
|
|
|
|
FUserName := 'guest';
|
|
|
|
FPassword := 'guest';
|
|
|
|
FHeaders := TStompHeaders.Create;
|
|
|
|
FTimeout := 200;
|
|
|
|
FReceiptTimeout := FTimeout;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClient.DeInit;
|
|
|
|
begin
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$IFDEF USESYNAPSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
FreeAndNil(FSynapseTCP);
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ELSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
FreeAndNil(FTCP);
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
FreeAndNil(FTransactions);
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TStompClient.Destroy;
|
|
|
|
begin
|
2013-10-15 10:14:36 +02:00
|
|
|
Disconnect;
|
2012-04-18 19:19:31 +02:00
|
|
|
DeInit;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2013-10-15 10:14:36 +02:00
|
|
|
procedure TStompClient.Disconnect;
|
2012-04-18 19:19:31 +02:00
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
|
|
|
if Connected then
|
|
|
|
begin
|
2013-10-15 10:14:36 +02:00
|
|
|
Frame := TStompFrame.Create;
|
|
|
|
Frame.SetCommand('DISCONNECT');
|
|
|
|
SendFrame(Frame);
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$IFDEF USESYNAPSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
FSynapseTCP.CloseSocket;
|
2013-10-15 10:14:36 +02:00
|
|
|
FSynapseConnected := False;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ELSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
FTCP.Disconnect;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
end;
|
|
|
|
DeInit;
|
|
|
|
end;
|
|
|
|
|
2013-10-10 17:08:06 +02:00
|
|
|
function TStompClient.GetProtocolVersion: string;
|
2012-04-18 19:19:31 +02:00
|
|
|
begin
|
|
|
|
Result := FServerProtocolVersion;
|
|
|
|
end;
|
|
|
|
|
2013-10-10 17:08:06 +02:00
|
|
|
function TStompClient.GetServer: string;
|
2012-04-18 19:19:31 +02:00
|
|
|
begin
|
|
|
|
Result := FServer;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompClient.GetSession: string;
|
|
|
|
begin
|
|
|
|
Result := FSession;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClient.Init;
|
|
|
|
begin
|
|
|
|
DeInit;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$IFDEF USESYNAPSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
FSynapseTCP := TTCPBlockSocket.Create;
|
|
|
|
FSynapseTCP.OnStatus := SynapseSocketCallBack;
|
|
|
|
FSynapseTCP.RaiseExcept := True;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ELSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
FTCP := TIdTCPClient.Create(nil);
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
FTransactions := TStringList.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{$IFDEF USESYNAPSE}
|
|
|
|
|
|
|
|
|
|
|
|
procedure TStompClient.SynapseSocketCallBack(Sender: TObject;
|
|
|
|
Reason: THookSocketReason; const Value: string);
|
|
|
|
begin
|
|
|
|
// As seen at TBlockSocket.ExceptCheck procedure, it SEEMS safe to say
|
|
|
|
// when an error occurred and is not a Timeout, the connection is broken
|
|
|
|
if (Reason = HR_Error) and (FSynapseTCP.LastError <> WSAETIMEDOUT)
|
|
|
|
then
|
|
|
|
begin
|
2013-10-15 10:14:36 +02:00
|
|
|
FSynapseConnected := False;
|
2012-04-18 19:19:31 +02:00
|
|
|
end;
|
|
|
|
end;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
|
|
|
|
procedure TStompClient.MergeHeaders(var AFrame: IStompFrame; var AHeaders: IStompHeaders);
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
h: TKeyValue;
|
|
|
|
begin
|
|
|
|
if Assigned(AHeaders) then
|
|
|
|
if AHeaders.Count > 0 then
|
|
|
|
for i := 0 to AHeaders.Count - 1 do
|
|
|
|
begin
|
|
|
|
h := AHeaders.GetAt(i);
|
|
|
|
AFrame.GetHeaders.Add(h.Key, h.Value);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2012-12-27 23:25:09 +01:00
|
|
|
procedure TStompClient.Nack(const MessageID, TransactionIdentifier: string);
|
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
|
|
|
Frame := TStompFrame.Create;
|
|
|
|
Frame.SetCommand('NACK');
|
|
|
|
Frame.GetHeaders.Add('message-id', MessageID);
|
|
|
|
if TransactionIdentifier <> '' then
|
|
|
|
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
|
|
|
|
SendFrame(Frame);
|
|
|
|
end;
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
procedure TStompClient.Receipt(const ReceiptID: string);
|
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
|
|
|
if Receive(Frame, FReceiptTimeout) then
|
|
|
|
begin
|
|
|
|
if Frame.GetCommand <> 'RECEIPT' then
|
|
|
|
raise EStomp.Create('Receipt command error');
|
|
|
|
if Frame.GetHeaders.Value('receipt-id') <> ReceiptID then
|
|
|
|
raise EStomp.Create('Receipt receipt-id error');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompClient.Receive(out StompFrame: IStompFrame; ATimeout: Integer): boolean;
|
|
|
|
begin
|
|
|
|
StompFrame := nil;
|
|
|
|
StompFrame := Receive(ATimeout);
|
|
|
|
Result := Assigned(StompFrame);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompClient.Receive(ATimeout: Integer): IStompFrame;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
{$IFDEF USESYNAPSE}
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
function InternalReceiveSynapse(ATimeout: Integer): IStompFrame;
|
|
|
|
var
|
2013-10-10 17:08:06 +02:00
|
|
|
c : char;
|
|
|
|
s : string;
|
2012-04-18 19:19:31 +02:00
|
|
|
tout: boolean;
|
|
|
|
begin
|
2013-10-15 10:14:36 +02:00
|
|
|
tout := False;
|
2012-04-18 19:19:31 +02:00
|
|
|
Result := nil;
|
|
|
|
try
|
|
|
|
try
|
|
|
|
FSynapseTCP.SetRecvTimeout(ATimeout);
|
|
|
|
s := '';
|
|
|
|
try
|
|
|
|
while True do
|
|
|
|
begin
|
|
|
|
c := Chr(FSynapseTCP.RecvByte(ATimeout));
|
|
|
|
if c <> CHAR0 then
|
|
|
|
s := s + c // should be improved with a string buffer (daniele.teti)
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
c := Chr(FSynapseTCP.RecvByte(ATimeout));
|
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
on E: ESynapseError do
|
|
|
|
begin
|
|
|
|
if E.ErrorCode = WSAETIMEDOUT then
|
|
|
|
tout := True
|
|
|
|
else
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
on E: Exception do
|
|
|
|
begin
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
if not tout then
|
|
|
|
begin
|
|
|
|
Result := StompUtils.CreateFrame(s + CHAR0);
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
s := '';
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
on E: Exception do
|
|
|
|
begin
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
{$ELSE}
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
function InternalReceiveINDY(ATimeout: Integer): IStompFrame;
|
|
|
|
var
|
2013-10-10 17:08:06 +02:00
|
|
|
c : char;
|
|
|
|
sb : TStringBuilder;
|
|
|
|
tout : boolean;
|
2012-04-18 19:19:31 +02:00
|
|
|
FirstValidChar: boolean;
|
2013-05-10 18:00:10 +02:00
|
|
|
// UTF8Encoding: TEncoding;
|
2014-09-05 12:51:51 +02:00
|
|
|
{$IF CompilerVersion < 24}
|
2013-12-04 11:42:45 +01:00
|
|
|
UTF8Encoding: TIdTextEncoding;
|
2014-09-05 12:51:51 +02:00
|
|
|
{$ELSE}
|
2013-05-10 18:00:10 +02:00
|
|
|
UTF8Encoding: IIdTextEncoding;
|
2013-12-04 11:42:45 +01:00
|
|
|
{$IFEND}
|
2012-04-18 19:19:31 +02:00
|
|
|
begin
|
2014-09-05 12:51:51 +02:00
|
|
|
{$IF CompilerVersion < 24}
|
2013-12-04 11:42:45 +01:00
|
|
|
UTF8Encoding := TEncoding.UTF8;
|
2014-09-05 12:51:51 +02:00
|
|
|
{$ELSE}
|
2013-10-10 17:08:06 +02:00
|
|
|
UTF8Encoding := IndyTextEncoding_UTF8();
|
2014-09-05 12:51:51 +02:00
|
|
|
{$ENDIF}
|
2013-10-15 10:14:36 +02:00
|
|
|
tout := False;
|
2012-04-18 19:19:31 +02:00
|
|
|
Result := nil;
|
|
|
|
try
|
|
|
|
sb := TStringBuilder.Create(1024 * 4);
|
|
|
|
try
|
|
|
|
FTCP.ReadTimeout := ATimeout;
|
|
|
|
try
|
2013-10-15 10:14:36 +02:00
|
|
|
FirstValidChar := False;
|
2012-04-18 19:19:31 +02:00
|
|
|
FTCP.Socket.CheckForDataOnSource(1);
|
|
|
|
while True do
|
|
|
|
begin
|
2012-04-26 15:32:32 +02:00
|
|
|
c := FTCP.Socket.ReadChar(UTF8Encoding);
|
|
|
|
if (c = LF) and (not FirstValidChar) then
|
2012-04-18 19:19:31 +02:00
|
|
|
Continue;
|
|
|
|
FirstValidChar := True;
|
|
|
|
if c <> CHAR0 then
|
|
|
|
sb.Append(c)
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
// FTCP.IOHandler.ReadChar(TEncoding.UTF8);
|
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
on E: EIdReadTimeout do
|
|
|
|
begin
|
|
|
|
tout := True;
|
|
|
|
end;
|
|
|
|
on E: Exception do
|
|
|
|
begin
|
|
|
|
if sb.Length > 0 then
|
|
|
|
raise EStomp.Create(E.message + sLineBreak + sb.toString)
|
|
|
|
else
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
if not tout then
|
|
|
|
begin
|
|
|
|
Result := StompUtils.CreateFrame(sb.toString + CHAR0);
|
|
|
|
if Result.GetCommand = 'ERROR' then
|
2013-10-15 10:14:36 +02:00
|
|
|
raise EStomp.Create(Result.GetHeaders.Value('message'));
|
2012-04-18 19:19:31 +02:00
|
|
|
end;
|
|
|
|
finally
|
|
|
|
sb.Free;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
on E: Exception do
|
|
|
|
begin
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
|
|
|
|
begin
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2013-10-15 10:14:36 +02:00
|
|
|
{$IFDEF USESYNAPSE}
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2013-10-15 10:14:36 +02:00
|
|
|
Result := InternalReceiveSynapse(ATimeout);
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2013-10-15 10:14:36 +02:00
|
|
|
{$ELSE}
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2013-10-15 10:14:36 +02:00
|
|
|
Result := InternalReceiveINDY(ATimeout);
|
2013-10-14 14:28:17 +02:00
|
|
|
|
2013-10-15 10:14:36 +02:00
|
|
|
{$ENDIF}
|
2013-10-10 17:08:06 +02:00
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompClient.Receive: IStompFrame;
|
|
|
|
begin
|
|
|
|
Result := Receive(FTimeout);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClient.Send(QueueOrTopicName: string; TextMessage: string; Headers: IStompHeaders);
|
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
|
|
|
Frame := TStompFrame.Create;
|
|
|
|
Frame.SetCommand('SEND');
|
|
|
|
Frame.GetHeaders.Add('destination', QueueOrTopicName);
|
|
|
|
Frame.SetBody(TextMessage);
|
|
|
|
MergeHeaders(Frame, Headers);
|
|
|
|
SendFrame(Frame);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClient.Send(QueueOrTopicName: string; TextMessage: string;
|
|
|
|
TransactionIdentifier: string; Headers: IStompHeaders);
|
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
|
|
|
Frame := TStompFrame.Create;
|
|
|
|
Frame.SetCommand('SEND');
|
|
|
|
Frame.GetHeaders.Add('destination', QueueOrTopicName);
|
|
|
|
Frame.GetHeaders.Add('transaction', TransactionIdentifier);
|
|
|
|
Frame.SetBody(TextMessage);
|
|
|
|
MergeHeaders(Frame, Headers);
|
|
|
|
SendFrame(Frame);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClient.SendFrame(AFrame: IStompFrame);
|
|
|
|
begin
|
2013-10-10 17:08:06 +02:00
|
|
|
|
|
|
|
{$IFDEF USESYNAPSE}
|
|
|
|
|
|
|
|
if Assigned(FOnBeforeSendFrame) then
|
|
|
|
FOnBeforeSendFrame(AFrame);
|
2012-04-18 19:19:31 +02:00
|
|
|
FSynapseTCP.SendString(AFrame.output);
|
2013-10-10 17:08:06 +02:00
|
|
|
if Assigned(FOnAfterSendFrame) then
|
|
|
|
FOnAfterSendFrame(AFrame);
|
|
|
|
|
|
|
|
{$ELSE}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
// FTCP.IOHandler.write(TEncoding.ASCII.GetBytes(AFrame.output));
|
2013-10-10 17:08:06 +02:00
|
|
|
if Assigned(FOnBeforeSendFrame) then
|
|
|
|
FOnBeforeSendFrame(AFrame);
|
2013-12-04 11:42:45 +01:00
|
|
|
|
|
|
|
{$IF Defined(Ver240)}
|
|
|
|
FTCP.IOHandler.write(TEncoding.UTF8.GetBytes(AFrame.output));
|
|
|
|
{$IFEND}
|
|
|
|
|
|
|
|
{$IF Defined(Ver250) or Defined(VER260)}
|
2013-05-10 18:00:10 +02:00
|
|
|
FTCP.IOHandler.write(IndyTextEncoding_UTF8.GetBytes(AFrame.output));
|
2013-12-04 11:42:45 +01:00
|
|
|
{$IFEND}
|
2013-10-10 17:08:06 +02:00
|
|
|
if Assigned(FOnAfterSendFrame) then
|
|
|
|
FOnAfterSendFrame(AFrame);
|
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompClient.SetPassword(const Value: string): IStompClient;
|
|
|
|
begin
|
|
|
|
FPassword := Value;
|
|
|
|
Result := Self;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClient.SetReceiptTimeout(const Value: Integer);
|
|
|
|
begin
|
|
|
|
FReceiptTimeout := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompClient.SetReceiveTimeout(const AMilliSeconds: Cardinal): IStompClient;
|
|
|
|
begin
|
|
|
|
FTimeout := AMilliSeconds;
|
|
|
|
Result := Self;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompClient.SetUserName(const Value: string): IStompClient;
|
|
|
|
begin
|
|
|
|
FUserName := Value;
|
|
|
|
Result := Self;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClient.Subscribe(QueueOrTopicName: string; Ack: TAckMode = amAuto;
|
|
|
|
Headers: IStompHeaders = nil);
|
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
|
|
|
Frame := TStompFrame.Create;
|
|
|
|
Frame.SetCommand('SUBSCRIBE');
|
|
|
|
Frame.GetHeaders.Add('destination', QueueOrTopicName).Add('ack', StompUtils.AckModeToStr(Ack));
|
|
|
|
if Headers <> nil then
|
|
|
|
MergeHeaders(Frame, Headers);
|
|
|
|
SendFrame(Frame);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClient.Unsubscribe(Queue: string);
|
|
|
|
var
|
|
|
|
Frame: IStompFrame;
|
|
|
|
begin
|
|
|
|
Frame := TStompFrame.Create;
|
|
|
|
Frame.SetCommand('UNSUBSCRIBE');
|
|
|
|
Frame.GetHeaders.Add('destination', Queue);
|
|
|
|
SendFrame(Frame);
|
|
|
|
end;
|
|
|
|
|
2010-04-13 12:39:09 +02:00
|
|
|
end.
|