Net::AMQP::Frame - AMQP wire-level Frame object


Net-AMQP documentation Contained in the Net-AMQP distribution.

Index


Code Index:

NAME

Top

Net::AMQP::Frame - AMQP wire-level Frame object

CLASS METHODS

Top

new (...)

Takes an arbitrary list of key/value pairs and casts it into this class. Nothing special here.

factory (...)

Pass in 'type_id', 'channel' and 'payload'. Will attempt to identify a Net::AMQP::Frame subclass for further parsing, and will croak on failure. Returns a Net::AMQP::Frame subclass object.

OBJECT METHODS

Top

Field accessors

Each subclass extends these accessors, but they share in common the following:

type_id
channel
size
payload

parse_payload

Performs the parsing of the 'payload' binary data.

to_raw_payload

Returns the binary data the represents this frame's payload.

to_raw_frame

Returns a raw binary string representing this frame on the wire.

type_string

Returns a string that uniquely represents this frame type, such as 'Method Basic.Consume', 'Header Basic' or 'Body'

SEE ALSO

Top

Net::AMQP

COPYRIGHT

Top

AUTHOR

Top

Eric Waters <ewaters@gmail.com>


Net-AMQP documentation Contained in the Net-AMQP distribution.
package Net::AMQP::Frame;

use strict;
use warnings;
use base qw(Class::Data::Inheritable Class::Accessor);
use Net::AMQP::Common qw(:all);
use Params::Validate qw(validate validate_with);
use Carp;

BEGIN {
    __PACKAGE__->mk_classdata('type_id');
    __PACKAGE__->mk_accessors(qw(
        channel
        size
        payload
    ));
}

# Use all the subclasses
use Net::AMQP::Frame::Method;
use Net::AMQP::Frame::Header;
use Net::AMQP::Frame::Body;

our $VERSION = 0.01;

sub new {
    my ($class, %self) = @_;
    return bless \%self, $class;
}

sub factory {
    my $class = shift;
    my %args = validate(@_, {
        type_id => 1,
        channel => 1,
        payload => 1,
    });

    my $subclass;
    if ($args{type_id} == 1) {
        $subclass = 'Method';
    }
    elsif ($args{type_id} == 2) {
        $subclass = 'Header';
    }
    elsif ($args{type_id} == 3) {
        $subclass = 'Body';
    }
    else {
        croak "Unknown type_id $args{type_id}";
    }

    $subclass = 'Net::AMQP::Frame::' . $subclass;
    my $object = bless \%args, $subclass;
    $object->parse_payload();
    return $object;
}

sub to_raw_frame {
    my $self = shift;
    my $class = ref $self;

    if (! defined $self->channel) {
        $self->channel(0);
    }

    return pack('Cn', $self->type_id, $self->channel)
        . pack_long_string($self->to_raw_payload())
        . pack('C', 206);
}

sub type_string {
    my $self = shift;

    my ($type) = ref($self) =~ m{::([^:]+)$};

    my $subtype;
    if ($self->can('method_frame')) {
        ($subtype) = ref($self->method_frame) =~ m{^Net::AMQP::Protocol::(.+)$};
        my ($class, $method) = split /::/, $subtype;
        $subtype = join '.', $class, $method;
    }
    elsif ($self->can('header_frame')) {
        ($subtype) = ref($self->header_frame) =~ m{^Net::AMQP::Protocol::(.+)::ContentHeader$};
    }

    return $type . ($subtype ? " $subtype" : '');
}

1;