2011-04-14 11:13:25 +02:00
|
|
|
// Stomp Client for Embarcadero Delphi & FreePascal
|
2016-01-01 23:14:46 +01:00
|
|
|
// Tested With ApacheMQ 5.2/5.3, Apache Apollo 1.2, RabbitMQ
|
|
|
|
// Copyright (c) 2009-2015 Daniele Teti
|
2011-04-14 11:13:25 +02:00
|
|
|
//
|
|
|
|
// Contributors:
|
|
|
|
// Daniel Gaspary: dgaspary@gmail.com
|
|
|
|
// Oliver Marr: oliver.sn@wmarr.de
|
|
|
|
// WebSite: www.danieleteti.it
|
|
|
|
// email:d.teti@bittime.it
|
|
|
|
// *******************************************************
|
2012-04-18 19:19:31 +02:00
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
unit StompTypes;
|
2013-10-14 14:28:17 +02:00
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE DELPHI}
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
SysUtils,
|
|
|
|
Classes;
|
|
|
|
|
|
|
|
const
|
|
|
|
LINE_END: char = #10;
|
|
|
|
COMMAND_END: char = #0;
|
|
|
|
DEFAULT_STOMP_PORT = 61613;
|
|
|
|
|
|
|
|
type
|
2012-04-18 19:19:31 +02:00
|
|
|
TAckMode = (amAuto, amClient, amClientIndividual { STOMP 1.1 } );
|
|
|
|
|
|
|
|
TStompAcceptProtocol = (STOMP_Version_1_0, STOMP_Version_1_1);
|
2010-04-14 00:25:14 +02:00
|
|
|
|
|
|
|
EStomp = class(Exception)
|
|
|
|
end;
|
|
|
|
|
|
|
|
TKeyValue = record
|
|
|
|
Key: string;
|
|
|
|
Value: string;
|
|
|
|
end;
|
|
|
|
|
|
|
|
PKeyValue = ^TKeyValue;
|
|
|
|
|
|
|
|
IStompHeaders = interface
|
|
|
|
['{BD087D9D-0576-4C35-88F9-F5D6348E3894}']
|
|
|
|
function Add(Key, Value: string): IStompHeaders; overload;
|
|
|
|
function Add(HeaderItem: TKeyValue): IStompHeaders; overload;
|
|
|
|
function Value(Key: string): string;
|
|
|
|
function Remove(Key: string): IStompHeaders;
|
|
|
|
function IndexOf(Key: string): Integer;
|
|
|
|
function Count: Cardinal;
|
|
|
|
function GetAt(const index: Integer): TKeyValue;
|
|
|
|
function Output: string;
|
|
|
|
end;
|
|
|
|
|
|
|
|
IStompFrame = interface
|
|
|
|
['{68274885-D3C3-4890-A058-03B769B2191E}']
|
|
|
|
function Output: string;
|
|
|
|
procedure SetHeaders(const Value: IStompHeaders);
|
|
|
|
function GetCommand: string;
|
|
|
|
procedure SetCommand(const Value: string);
|
|
|
|
function GetBody: string;
|
|
|
|
procedure SetBody(const Value: string);
|
|
|
|
function GetHeaders: IStompHeaders;
|
2013-10-17 16:56:18 +02:00
|
|
|
function MessageID: string;
|
2015-05-26 03:23:19 +02:00
|
|
|
function ContentLength: Integer;
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
IStompClient = interface
|
|
|
|
['{EDE6EF1D-59EE-4FCC-9CD7-B183E606D949}']
|
2016-01-01 23:14:46 +01:00
|
|
|
function Receive(out StompFrame: IStompFrame; ATimeout: Integer)
|
|
|
|
: Boolean; overload;
|
2010-04-14 00:25:14 +02:00
|
|
|
function Receive: IStompFrame; overload;
|
|
|
|
function Receive(ATimeout: Integer): IStompFrame; overload;
|
|
|
|
procedure Receipt(const ReceiptID: string);
|
2016-01-01 23:14:46 +01:00
|
|
|
procedure Connect(Host: string = '127.0.0.1'; Port: Integer = 61613;
|
|
|
|
ClientID: string = '';
|
2012-04-18 19:19:31 +02:00
|
|
|
AcceptVersion: TStompAcceptProtocol = STOMP_Version_1_0);
|
2013-10-15 10:14:36 +02:00
|
|
|
procedure Disconnect;
|
2013-10-14 14:28:17 +02:00
|
|
|
procedure Subscribe(QueueOrTopicName: string; Ack: TAckMode = amAuto;
|
|
|
|
Headers: IStompHeaders = nil);
|
2010-04-14 00:25:14 +02:00
|
|
|
procedure Unsubscribe(Queue: string);
|
2013-10-14 14:28:17 +02:00
|
|
|
procedure Send(QueueOrTopicName: string; TextMessage: string;
|
|
|
|
Headers: IStompHeaders = nil); overload;
|
2016-01-01 23:14:46 +01:00
|
|
|
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 }
|
2016-01-01 23:14:46 +01:00
|
|
|
procedure Nack(const MessageID: string;
|
|
|
|
const TransactionIdentifier: string = '');
|
2010-04-14 00:25:14 +02:00
|
|
|
procedure BeginTransaction(const TransactionIdentifier: string);
|
|
|
|
procedure CommitTransaction(const TransactionIdentifier: string);
|
|
|
|
procedure AbortTransaction(const TransactionIdentifier: string);
|
|
|
|
/// ////////////
|
|
|
|
function SetPassword(const Value: string): IStompClient;
|
|
|
|
function SetUserName(const Value: string): IStompClient;
|
|
|
|
function SetReceiveTimeout(const AMilliSeconds: Cardinal): IStompClient;
|
|
|
|
function Connected: Boolean;
|
2013-10-14 14:28:17 +02:00
|
|
|
function GetProtocolVersion: string;
|
|
|
|
function GetServer: string;
|
|
|
|
function GetSession: string;
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
TStompHeaders = class(TInterfacedObject, IStompHeaders)
|
|
|
|
private
|
|
|
|
FList: TList;
|
|
|
|
function GetItems(index: Cardinal): TKeyValue;
|
|
|
|
procedure SetItems(index: Cardinal; const Value: TKeyValue);
|
|
|
|
|
|
|
|
public
|
2016-06-28 15:21:47 +02:00
|
|
|
class function NewDurableSubscriptionHeader(const SubscriptionName: string): TKeyValue;
|
|
|
|
deprecated 'Use Subscription instead';
|
2010-04-14 00:25:14 +02:00
|
|
|
class function NewPersistentHeader(const Value: Boolean): TKeyValue;
|
2016-06-28 15:21:47 +02:00
|
|
|
deprecated 'Use Persistent instead';
|
2010-04-14 00:25:14 +02:00
|
|
|
class function NewReplyToHeader(const DestinationName: string): TKeyValue;
|
2016-06-28 15:21:47 +02:00
|
|
|
deprecated 'Use ReplyTo instead';
|
|
|
|
|
|
|
|
class function Subscription(const SubscriptionName: string): TKeyValue;
|
|
|
|
class function Persistent(const Value: Boolean): TKeyValue;
|
|
|
|
class function ReplyTo(const DestinationName: string): TKeyValue;
|
2013-10-15 10:14:36 +02:00
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
/// /////////////////////////////////////////////7
|
2013-10-15 10:14:36 +02:00
|
|
|
const
|
|
|
|
MESSAGE_ID: string = 'message-id';
|
|
|
|
TRANSACTION: string = 'transaction';
|
|
|
|
/// /
|
2010-04-14 00:25:14 +02:00
|
|
|
function Add(Key, Value: string): IStompHeaders; overload;
|
|
|
|
function Add(HeaderItem: TKeyValue): IStompHeaders; overload;
|
|
|
|
function Value(Key: string): string;
|
|
|
|
function Remove(Key: string): IStompHeaders;
|
|
|
|
function IndexOf(Key: string): Integer;
|
|
|
|
function Count: Cardinal;
|
|
|
|
function GetAt(const index: Integer): TKeyValue;
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
function Output: string;
|
2016-01-01 23:14:46 +01:00
|
|
|
property Items[index: Cardinal]: TKeyValue read GetItems
|
|
|
|
write SetItems; default;
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
TStompFrame = class(TInterfacedObject, IStompFrame)
|
|
|
|
private
|
|
|
|
FCommand: string;
|
2015-03-12 13:08:38 +01:00
|
|
|
FBody: string;
|
2015-05-26 03:23:19 +02:00
|
|
|
FContentLength: Integer;
|
2010-04-14 00:25:14 +02:00
|
|
|
FHeaders: IStompHeaders;
|
|
|
|
procedure SetHeaders(const Value: IStompHeaders);
|
|
|
|
function GetCommand: string;
|
|
|
|
procedure SetCommand(const Value: string);
|
|
|
|
function GetBody: string;
|
|
|
|
procedure SetBody(const Value: string);
|
|
|
|
function GetHeaders: IStompHeaders;
|
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
property Command: string read GetCommand write SetCommand;
|
|
|
|
property Body: string read GetBody write SetBody;
|
|
|
|
// return '', when Key doesn't exist or Value of Key is ''
|
|
|
|
// otherwise, return Value;
|
|
|
|
function Output: string;
|
2013-10-17 16:56:18 +02:00
|
|
|
function MessageID: string;
|
2015-05-26 03:23:19 +02:00
|
|
|
function ContentLength: Integer;
|
2010-04-14 00:25:14 +02:00
|
|
|
property Headers: IStompHeaders read GetHeaders write SetHeaders;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TAddress = record
|
|
|
|
Host: string;
|
|
|
|
Port: Integer;
|
|
|
|
UserName: string;
|
|
|
|
Password: string;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TAddresses = array of TAddress;
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
IStompListener = interface
|
|
|
|
['{CB3EB297-8616-408E-A0B2-7CCC11224DBC}']
|
|
|
|
procedure StopListening;
|
|
|
|
end;
|
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
IStompClientListener = interface
|
|
|
|
['{C4C0D932-8994-43FB-9D32-A03FE86AEFE4}']
|
2013-10-14 14:28:17 +02:00
|
|
|
procedure OnMessage(StompClient: IStompClient; StompFrame: IStompFrame;
|
|
|
|
var StompListening: Boolean);
|
2012-04-18 19:19:31 +02:00
|
|
|
procedure OnStopListen(StompClient: IStompClient);
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
2012-04-18 19:19:31 +02:00
|
|
|
TStompClientListener = class(TThread, IStompListener)
|
2010-04-14 00:25:14 +02:00
|
|
|
strict protected
|
|
|
|
FStompClientListener: IStompClientListener;
|
2015-03-12 13:08:38 +01:00
|
|
|
FStompClient: IStompClient;
|
2010-04-14 00:25:14 +02:00
|
|
|
procedure Execute; override;
|
|
|
|
|
|
|
|
public
|
2016-01-01 23:14:46 +01:00
|
|
|
constructor Create(StompClient: IStompClient;
|
|
|
|
StompClientListener: IStompClientListener);
|
2010-04-14 00:25:14 +02:00
|
|
|
procedure StopListening;
|
2012-04-18 19:19:31 +02:00
|
|
|
function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
|
|
|
|
function _AddRef: Integer; stdcall;
|
|
|
|
function _Release: Integer; stdcall;
|
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
type
|
|
|
|
StompUtils = class
|
2013-10-14 14:28:17 +02:00
|
|
|
class function StripLastChar(Buf: string; LastChar: char): string;
|
2010-04-14 00:25:14 +02:00
|
|
|
class function CreateFrame(Buf: string): TStompFrame;
|
|
|
|
class function AckModeToStr(AckMode: TAckMode): string;
|
2016-06-28 15:21:47 +02:00
|
|
|
class function NewHeaders: IStompHeaders; deprecated 'Use Headers instead';
|
|
|
|
class function Headers: IStompHeaders;
|
2010-04-14 00:25:14 +02:00
|
|
|
class function NewFrame: IStompFrame;
|
|
|
|
class function TimestampAsDateTime(const HeaderValue: string): TDateTime;
|
2016-01-01 23:14:46 +01:00
|
|
|
class function NewStomp(Host: string = '127.0.0.1';
|
|
|
|
Port: Integer = DEFAULT_STOMP_PORT; ClientID: string = '';
|
|
|
|
const UserName: string = 'guest'; const Password: string = 'guest';
|
2015-03-12 13:08:38 +01:00
|
|
|
AcceptVersion: TStompAcceptProtocol = STOMP_Version_1_0): IStompClient;
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
2016-06-28 15:21:47 +02:00
|
|
|
function NewStompClient(Host: string = '127.0.0.1';
|
|
|
|
Port: Integer = DEFAULT_STOMP_PORT; ClientID: string = '';
|
|
|
|
const UserName: string = 'guest'; const Password: string = 'guest';
|
|
|
|
AcceptVersion: TStompAcceptProtocol = STOMP_Version_1_0): IStompClient;
|
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2011-04-14 11:13:25 +02:00
|
|
|
Dateutils,
|
|
|
|
StompClient;
|
2010-04-14 00:25:14 +02:00
|
|
|
|
2016-06-28 15:21:47 +02:00
|
|
|
function NewStompClient(Host: string; Port: Integer; ClientID: string;
|
|
|
|
const UserName, Password: string;
|
|
|
|
AcceptVersion: TStompAcceptProtocol): IStompClient;
|
|
|
|
begin
|
|
|
|
Result := StompUtils.NewStomp(Host, Port, ClientID, UserName, Password, AcceptVersion);
|
|
|
|
end;
|
|
|
|
|
2016-01-01 23:14:46 +01:00
|
|
|
class function StompUtils.NewStomp(Host: string = '127.0.0.1';
|
|
|
|
Port: Integer = DEFAULT_STOMP_PORT; ClientID: string = '';
|
|
|
|
const UserName: string = 'guest'; const Password: string = 'guest';
|
2013-06-03 18:34:01 +02:00
|
|
|
AcceptVersion: TStompAcceptProtocol = STOMP_Version_1_0): IStompClient;
|
2010-04-14 00:25:14 +02:00
|
|
|
begin
|
|
|
|
Result := TStompClient.Create;
|
|
|
|
Result.SetUserName(UserName);
|
|
|
|
Result.SetPassword(Password);
|
2012-04-18 19:19:31 +02:00
|
|
|
Result.Connect(Host, Port, ClientID, AcceptVersion);
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
2013-10-14 14:28:17 +02:00
|
|
|
class function StompUtils.StripLastChar(Buf: string; LastChar: char): string;
|
2013-06-03 18:34:01 +02:00
|
|
|
var
|
|
|
|
p: Integer;
|
|
|
|
begin
|
|
|
|
p := Pos(COMMAND_END, Buf);
|
|
|
|
if (p = 0) then
|
|
|
|
raise EStomp.Create('frame no ending');
|
|
|
|
Result := Copy(Buf, 1, p - 1);
|
|
|
|
end;
|
|
|
|
|
2016-01-01 23:14:46 +01:00
|
|
|
class function TStompHeaders.NewDurableSubscriptionHeader(const SubscriptionName
|
|
|
|
: string): TKeyValue;
|
2010-04-14 00:25:14 +02:00
|
|
|
begin
|
2016-06-28 15:21:47 +02:00
|
|
|
Result := Subscription(SubscriptionName);
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
2016-01-01 23:14:46 +01:00
|
|
|
class function TStompHeaders.NewPersistentHeader(const Value: Boolean)
|
|
|
|
: TKeyValue;
|
2010-04-14 00:25:14 +02:00
|
|
|
begin
|
2016-06-28 15:21:47 +02:00
|
|
|
Result := Persistent(Value);
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
2016-01-01 23:14:46 +01:00
|
|
|
class function TStompHeaders.NewReplyToHeader(const DestinationName: string)
|
|
|
|
: TKeyValue;
|
2010-04-14 00:25:14 +02:00
|
|
|
begin
|
2016-06-28 15:21:47 +02:00
|
|
|
Result := ReplyTo(DestinationName);
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
class function StompUtils.NewHeaders: IStompHeaders;
|
|
|
|
begin
|
2016-06-28 15:21:47 +02:00
|
|
|
Result := Headers;
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
2016-01-01 23:14:46 +01:00
|
|
|
class function StompUtils.TimestampAsDateTime(const HeaderValue: string)
|
|
|
|
: TDateTime;
|
2010-04-14 00:25:14 +02:00
|
|
|
begin
|
2016-01-01 23:14:46 +01:00
|
|
|
Result := EncodeDateTime(1970, 1, 1, 0, 0, 0, 0) + StrToInt64(HeaderValue)
|
|
|
|
/ 86400000;
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
class function StompUtils.AckModeToStr(AckMode: TAckMode): string;
|
|
|
|
begin
|
|
|
|
case AckMode of
|
|
|
|
amAuto:
|
|
|
|
Result := 'auto';
|
|
|
|
amClient:
|
|
|
|
Result := 'client';
|
2012-04-18 19:19:31 +02:00
|
|
|
amClientIndividual:
|
|
|
|
Result := 'client-individual'; // stomp 1.1
|
2015-03-12 13:08:38 +01:00
|
|
|
else
|
|
|
|
raise EStomp.Create('Unknown AckMode');
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TStompFrame.Create;
|
|
|
|
begin
|
|
|
|
FHeaders := TStompHeaders.Create;
|
|
|
|
self.FCommand := '';
|
|
|
|
self.FBody := '';
|
2015-05-26 03:23:19 +02:00
|
|
|
self.FContentLength := 0;
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TStompFrame.Destroy;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompFrame.GetBody: string;
|
|
|
|
begin
|
|
|
|
Result := FBody;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompFrame.GetCommand: string;
|
|
|
|
begin
|
|
|
|
Result := FCommand;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompFrame.GetHeaders: IStompHeaders;
|
|
|
|
begin
|
|
|
|
Result := FHeaders;
|
|
|
|
end;
|
|
|
|
|
2013-10-17 16:56:18 +02:00
|
|
|
function TStompFrame.MessageID: string;
|
|
|
|
begin
|
|
|
|
Result := self.GetHeaders.Value(TStompHeaders.MESSAGE_ID);
|
|
|
|
end;
|
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
function TStompFrame.Output: string;
|
|
|
|
begin
|
2016-01-01 23:14:46 +01:00
|
|
|
Result := FCommand + LINE_END + FHeaders.Output + LINE_END + FBody +
|
|
|
|
COMMAND_END;
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
2015-05-26 03:23:19 +02:00
|
|
|
function TStompFrame.ContentLength: Integer;
|
|
|
|
begin
|
|
|
|
Result := FContentLength;
|
|
|
|
end;
|
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
procedure TStompFrame.SetBody(const Value: string);
|
|
|
|
begin
|
|
|
|
FBody := Value;
|
2015-05-26 03:23:19 +02:00
|
|
|
FContentLength := Length(TEncoding.UTF8.GetBytes(FBody));
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompFrame.SetCommand(const Value: string);
|
|
|
|
begin
|
|
|
|
FCommand := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompFrame.SetHeaders(const Value: IStompHeaders);
|
|
|
|
begin
|
|
|
|
FHeaders := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function GetLine(Buf: string; var From: Integer): string;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
if (From > Length(Buf)) then
|
|
|
|
raise EStomp.Create('From out of bound.');
|
|
|
|
|
|
|
|
i := From;
|
|
|
|
|
|
|
|
while (i <= Length(Buf)) do
|
|
|
|
begin
|
|
|
|
if (Buf[i] <> LINE_END) then
|
|
|
|
inc(i)
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if (Buf[i] = LINE_END) then
|
|
|
|
begin
|
|
|
|
Result := Copy(Buf, From, i - From);
|
|
|
|
From := i + 1;
|
|
|
|
exit;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise EStomp.Create('End of Line not found.');
|
|
|
|
end;
|
|
|
|
|
|
|
|
class function StompUtils.CreateFrame(Buf: string): TStompFrame;
|
|
|
|
var
|
2015-03-12 13:08:38 +01:00
|
|
|
line: string;
|
|
|
|
i: Integer;
|
|
|
|
p: Integer;
|
2010-04-14 00:25:14 +02:00
|
|
|
Key, Value: string;
|
2015-03-12 13:08:38 +01:00
|
|
|
other: string;
|
|
|
|
contLen: Integer;
|
|
|
|
sContLen: string;
|
2010-04-14 00:25:14 +02:00
|
|
|
begin
|
|
|
|
Result := TStompFrame.Create;
|
|
|
|
i := 1;
|
|
|
|
try
|
|
|
|
Result.Command := GetLine(Buf, i);
|
2012-04-18 19:19:31 +02:00
|
|
|
while true do
|
2010-04-14 00:25:14 +02:00
|
|
|
begin
|
|
|
|
line := GetLine(Buf, i);
|
|
|
|
if (line = '') then
|
|
|
|
break;
|
|
|
|
p := Pos(':', line);
|
|
|
|
if (p = 0) then
|
|
|
|
raise Exception.Create('header line error');
|
|
|
|
Key := Copy(line, 1, p - 1);
|
|
|
|
Value := Copy(line, p + 1, Length(line) - p);
|
|
|
|
Result.Headers.Add(Key, Value);
|
|
|
|
end;
|
|
|
|
other := Copy(Buf, i, high(Integer));
|
|
|
|
sContLen := Result.Headers.Value('content-length');
|
|
|
|
if (sContLen <> '') then
|
|
|
|
begin
|
2013-06-03 18:34:01 +02:00
|
|
|
if other[Length(other)] <> #0 then
|
|
|
|
raise EStomp.Create('frame no ending');
|
2010-04-14 00:25:14 +02:00
|
|
|
contLen := StrToInt(sContLen);
|
2013-06-03 18:34:01 +02:00
|
|
|
other := StripLastChar(other, COMMAND_END);
|
2015-03-12 13:08:38 +01:00
|
|
|
|
2016-01-01 23:14:46 +01:00
|
|
|
if TEncoding.UTF8.GetByteCount(other) <> contLen then
|
2016-06-28 15:21:47 +02:00
|
|
|
// there is still the command_end
|
2010-04-14 00:25:14 +02:00
|
|
|
raise EStomp.Create('frame too short');
|
2013-06-03 18:34:01 +02:00
|
|
|
Result.Body := other;
|
2010-04-14 00:25:14 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2013-06-03 18:34:01 +02:00
|
|
|
Result.Body := StripLastChar(other, COMMAND_END)
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
except
|
|
|
|
on EStomp do
|
|
|
|
begin
|
|
|
|
// ignore
|
|
|
|
Result.Free;
|
|
|
|
Result := nil;
|
|
|
|
end;
|
|
|
|
on e: Exception do
|
|
|
|
begin
|
|
|
|
Result.Free;
|
|
|
|
raise EStomp.Create(e.message);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2016-06-28 15:21:47 +02:00
|
|
|
class function StompUtils.Headers: IStompHeaders;
|
|
|
|
begin
|
|
|
|
Result := TStompHeaders.Create;
|
|
|
|
end;
|
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
class function StompUtils.NewFrame: IStompFrame;
|
|
|
|
begin
|
|
|
|
Result := TStompFrame.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TStompHeaders }
|
|
|
|
|
|
|
|
function TStompHeaders.Add(Key, Value: string): IStompHeaders;
|
|
|
|
var
|
|
|
|
p: PKeyValue;
|
|
|
|
begin
|
|
|
|
New(p);
|
|
|
|
p^.Key := Key;
|
|
|
|
p^.Value := Value;
|
|
|
|
FList.Add(p);
|
|
|
|
Result := self;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompHeaders.Add(HeaderItem: TKeyValue): IStompHeaders;
|
|
|
|
begin
|
|
|
|
Result := Add(HeaderItem.Key, HeaderItem.Value);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompHeaders.Count: Cardinal;
|
|
|
|
begin
|
|
|
|
Result := FList.Count;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TStompHeaders.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
FList := TList.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TStompHeaders.Destroy;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
if FList.Count > 0 then
|
|
|
|
for i := 0 to FList.Count - 1 do
|
2011-04-14 11:13:25 +02:00
|
|
|
Dispose(PKeyValue(FList[i]));
|
2010-04-14 00:25:14 +02:00
|
|
|
FList.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompHeaders.GetAt(const index: Integer): TKeyValue;
|
|
|
|
begin
|
|
|
|
Result := GetItems(index)
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompHeaders.GetItems(index: Cardinal): TKeyValue;
|
|
|
|
begin
|
|
|
|
Result := PKeyValue(FList[index])^;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompHeaders.IndexOf(Key: string): Integer;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
2016-01-01 23:14:46 +01:00
|
|
|
Result := -1;
|
2010-04-14 00:25:14 +02:00
|
|
|
for i := 0 to FList.Count - 1 do
|
|
|
|
begin
|
|
|
|
if GetItems(i).Key = Key then
|
|
|
|
begin
|
|
|
|
Result := i;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompHeaders.Output: string;
|
|
|
|
var
|
2015-03-12 13:08:38 +01:00
|
|
|
i: Integer;
|
2010-04-14 00:25:14 +02:00
|
|
|
kv: TKeyValue;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
if FList.Count > 0 then
|
|
|
|
for i := 0 to FList.Count - 1 do
|
|
|
|
begin
|
|
|
|
kv := Items[i];
|
|
|
|
Result := Result + kv.Key + ':' + kv.Value + LINE_END;
|
|
|
|
end
|
2011-04-14 11:13:25 +02:00
|
|
|
else
|
|
|
|
Result := LINE_END;
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
2016-06-28 15:21:47 +02:00
|
|
|
class function TStompHeaders.Persistent(const Value: Boolean): TKeyValue;
|
|
|
|
begin
|
|
|
|
Result.Key := 'persistent';
|
|
|
|
Result.Value := LowerCase(BoolToStr(Value, true));
|
|
|
|
end;
|
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
function TStompHeaders.Remove(Key: string): IStompHeaders;
|
|
|
|
var
|
|
|
|
p: Integer;
|
|
|
|
begin
|
|
|
|
p := IndexOf(Key);
|
2011-04-14 11:13:25 +02:00
|
|
|
Dispose(PKeyValue(FList[p]));
|
2010-04-14 00:25:14 +02:00
|
|
|
FList.Delete(p);
|
|
|
|
Result := self;
|
|
|
|
end;
|
|
|
|
|
2016-06-28 15:21:47 +02:00
|
|
|
class function TStompHeaders.ReplyTo(const DestinationName: string): TKeyValue;
|
|
|
|
begin
|
|
|
|
Result.Key := 'reply-to';
|
|
|
|
Result.Value := DestinationName;
|
|
|
|
end;
|
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
procedure TStompHeaders.SetItems(index: Cardinal; const Value: TKeyValue);
|
|
|
|
var
|
|
|
|
p: Integer;
|
|
|
|
begin
|
|
|
|
p := IndexOf(Value.Key);
|
2016-01-01 23:14:46 +01:00
|
|
|
if p > -1 then
|
2010-04-14 00:25:14 +02:00
|
|
|
begin
|
|
|
|
PKeyValue(FList[p])^.Key := Value.Key;
|
|
|
|
PKeyValue(FList[p])^.Value := Value.Value;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise EStomp.Create('Error SetItems');
|
|
|
|
end;
|
|
|
|
|
2016-06-28 15:21:47 +02:00
|
|
|
class function TStompHeaders.Subscription(
|
|
|
|
const SubscriptionName: string): TKeyValue;
|
|
|
|
begin
|
|
|
|
Result.Key := 'id';
|
|
|
|
Result.Value := SubscriptionName;
|
|
|
|
end;
|
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
function TStompHeaders.Value(Key: string): string;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
i := IndexOf(Key);
|
2016-01-01 23:14:46 +01:00
|
|
|
if i > -1 then
|
2010-04-14 00:25:14 +02:00
|
|
|
Result := GetItems(i).Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TStompListener }
|
|
|
|
|
2013-10-14 14:28:17 +02:00
|
|
|
constructor TStompClientListener.Create(StompClient: IStompClient;
|
|
|
|
StompClientListener: IStompClientListener);
|
2010-04-14 00:25:14 +02:00
|
|
|
begin
|
|
|
|
FStompClientListener := StompClientListener;
|
|
|
|
FStompClient := StompClient;
|
2016-01-01 23:14:46 +01:00
|
|
|
inherited Create(false);
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TStompClientListener.Execute;
|
|
|
|
var
|
2015-03-12 13:08:38 +01:00
|
|
|
frame: IStompFrame;
|
2012-04-18 19:19:31 +02:00
|
|
|
StopListen: Boolean;
|
2010-04-14 00:25:14 +02:00
|
|
|
begin
|
2016-06-28 15:21:47 +02:00
|
|
|
StopListen := false;
|
2013-10-15 10:14:36 +02:00
|
|
|
while not terminated do
|
2010-04-14 00:25:14 +02:00
|
|
|
begin
|
2015-03-12 13:08:38 +01:00
|
|
|
if FStompClient.Receive(frame, 1000) then
|
2013-10-15 10:14:36 +02:00
|
|
|
begin
|
|
|
|
FStompClientListener.OnMessage(FStompClient, frame, StopListen);
|
|
|
|
if StopListen then
|
2012-04-18 19:19:31 +02:00
|
|
|
begin
|
2013-10-15 10:14:36 +02:00
|
|
|
FStompClientListener.OnStopListen(FStompClient);
|
2015-03-12 13:08:38 +01:00
|
|
|
if not terminated then
|
|
|
|
StopListening;
|
2012-04-18 19:19:31 +02:00
|
|
|
end;
|
|
|
|
end;
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2016-01-01 23:14:46 +01:00
|
|
|
function TStompClientListener.QueryInterface(const IID: TGUID; out Obj)
|
|
|
|
: HRESULT;
|
2012-04-18 19:19:31 +02:00
|
|
|
begin
|
2016-01-01 23:14:46 +01:00
|
|
|
Result := E_NOINTERFACE;
|
2012-04-18 19:19:31 +02:00
|
|
|
end;
|
|
|
|
|
2010-04-14 00:25:14 +02:00
|
|
|
procedure TStompClientListener.StopListening;
|
|
|
|
begin
|
|
|
|
Terminate;
|
2015-03-12 13:08:38 +01:00
|
|
|
// Free;
|
2013-10-15 10:14:36 +02:00
|
|
|
// WaitFor;
|
2012-04-18 19:19:31 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompClientListener._AddRef: Integer;
|
|
|
|
begin
|
2016-01-01 23:14:46 +01:00
|
|
|
Result := -1;
|
2012-04-18 19:19:31 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TStompClientListener._Release: Integer;
|
|
|
|
begin
|
2016-01-01 23:14:46 +01:00
|
|
|
Result := -1;
|
2010-04-14 00:25:14 +02:00
|
|
|
end;
|
|
|
|
|
2010-04-13 12:39:09 +02:00
|
|
|
end.
|