Net::STOMP::Client::Frame - Frame support for Net::STOMP::Client
use Net::STOMP::Client::Frame qw(); # create a connection frame $frame = Net::STOMP::Client::Frame->new( command => "CONNECT", headers => { login => "guest", passcode => "guest", }, ); # get the command $cmd = $frame->command(); # set the body $frame->body("...some data..."); # directly get a header field $msgid = $frame->header("message-id");
This module provides an object oriented interface to manipulate STOMP frames.
A frame object has the following attributes: command, headers and body_reference. The headers attribute must be a reference to a hash of header key/value pairs. The body is usually manipulated by reference to avoid string copies.
command
headers
body_reference
This module provides the following methods:
return a new Net::STOMP::Client::Frame object (class method); the options that can be given (command, headers, body_reference and body) match the accessors described below
body
get/set the command attribute
get/set the headers attribute
get/set the body_reference attribute
get/set the value associated with the given name in the header; if the given value is undefined, remove the named header (this is a convenient wrapper around the headers() method)
get/set the body as a string (this is a convenient wrapper around the body_reference() method)
encode the given frame and return a reference to a binary string suitable to be written to a TCP stream (for instance); supported options: debug (debugging flags as a string), strict (the desired strictness, overriding $StrictEncode), version (the STOMP protocol version to use)
debug
strict
version
this method is obsolete and should not be used anymore; it is left here only to provide backward compatibility with Net::STOMP::Client 1.x
This module provides the following functions (which are not exported):
decode the given string reference and return a complete frame object, if possible or false in case there is not enough data for a complete frame; supported options: the same as encode() plus parse()
parse the given string reference and return true if a complete frame is found or false otherwise; supported options: state (a hash reference that holds the parsing state); see the "FRAME PARSING" section for more information
state
This module uses the following global variables (which are not exported):
the maximum number of bytes to dump when debugging message bodies (default: 256)
whether or not to perform strict character encoding/decoding (default: false)
The parse() function can be used to parse a frame without decoding it.
It takes as input a binary string reference (to avoid string copies) and an optional state (a hash reference). It parses the string to find out where the different parts of the frames are and it updates its state (if given).
It returns false if the string does not hold a complete frame or a hash reference if a complete frame is present. This hash is in fact the same thing as the state and it contains the following keys:
the length of what is found before the frame (only frame EOL can appear here)
the start position, length and length of the EOL of the command
the start position, length and length of the EOL of the header
the start position and length of the body
the length of what is found after the frame (only frame EOL can appear here)
the value of the content-length header (if present)
content-length
the total length of the frame, including before and after parts
Here is how this could be used:
$data = "... read from socket or file ..."; $info = Net::STOMP::Client::Frame::parse(\$data); if ($info) { # extract interesting frame parts $command = substr($data, $info->{command_idx}, $info->{command_len}); # remove the frame from the buffer substr($data, 0, $info->{total_len}) = ""; }
The content-length header is special because it is sometimes used to indicate the length of the body but also the JMS type of the message in ActiveMQ as per http://activemq.apache.org/stomp.html.
If you do not supply a content-length header, following the protocol recommendations, a content-length header will be added if the frame has a body.
If you do supply a numerical content-length header, it will be used as is. Warning: this may give unexpected results if the supplied value does not match the body length. Use only with caution!
Finally, if you supply an empty string as the content-length header, it will not be sent, even if the frame has a body. This can be used to mark a message as being a TextMessage for ActiveMQ. Here is an example of this:
$stomp->send( "destination" => "/queue/test", "body" => "hello world!", "content-length" => "", );
The STOMP 1.0 specification does not define which encoding should be used to serialize frames. So, by default, this module assumes that what has been given by the user or by the server is a ready-to-use sequence of bytes and it does not perform any further encoding or decoding.
If $Net::STOMP::Client::Frame::StrictEncode is true, all encoding and decoding operations will be stricter and will report a fatal error when given malformed input. This is done by using the Encode::FB_CROAK flag instead of the default Encode::FB_DEFAULT.
N.B.: Perl's standard Encode module is used for all encoding/decoding operations.
If the Messaging::Message module is available, the following method and function are available too:
transform the frame into a Messaging::Message object (method)
transform the given Messaging::Message object into a Net::STOMP::Client::Frame object (function)
Here is how they could be used:
# frame to message $frame = $stomp->wait_for_frames(timeout => 1); if ($frame) { $message = $frame->messagify(); ... } # message to frame $frame = Net::STOMP::Client::Frame::demessagify($message); $stomp->send_frame($frame);
Note: in both cases, string copies are avoided so both objects will share the same header hash and body string. Therefore modifying one may also modify the other. Clone (copy) the objects if you do not want this behavior.
STOMP 1.0 has several ambiguities and this module does its best to work "as expected" in these gray areas.
STOMP 1.1 and STOMP 1.2 are much better specified and this module should be fully compliant with these STOMP specifications with only one exception: by default, this module is permissive and allows malformed encoded data (this is the same default as the Encode module itself); to be more strict, set $Net::STOMP::Client::Frame::StrictEncode to true (as explained above).
Encode, Messaging::Message, Net::STOMP::Client.
Lionel Cons http://cern.ch/lionel.cons
Copyright (C) CERN 2010-2021
To install Net::STOMP::Client, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::STOMP::Client
CPAN shell
perl -MCPAN -e shell install Net::STOMP::Client
For more information on module installation, please visit the detailed CPAN module installation guide.