| 1 | package Thrift::Parser::Message; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Thrift::Parser::Message - Message object | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | =cut | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | use base qw(Class::Accessor::Grouped); | ||
| 14 | __PACKAGE__->mk_group_accessors(simple => qw(method type seqid arguments)); | ||
| 15 | |||
| 16 | =head1 USAGE | ||
| 17 | |||
| 18 | =head2 method | ||
| 19 | |||
| 20 | Returns the L<Thrift::Parser::Method> class that is represented in this message. | ||
| 21 | |||
| 22 | =head2 type | ||
| 23 | |||
| 24 | Returns the L<Thrift> type id of this message. | ||
| 25 | |||
| 26 | =head2 seqid | ||
| 27 | |||
| 28 | A L<Thrift> sequence ID. | ||
| 29 | |||
| 30 | =head2 arguments | ||
| 31 | |||
| 32 | A L<Thrift::Parser::FieldSet> object representing the arguments of this message. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my ($class, $self) = @_; | ||
| 38 | $self ||= {}; | ||
| 39 | return bless $self, $class; | ||
| 40 | } | ||
| 41 | |||
| 42 | =head2 compose_reply | ||
| 43 | |||
| 44 | my $reply_message = $message->compose_reply($reply); | ||
| 45 | |||
| 46 | A helper method, use this to compose a new message which is a reply to another message. Expects the type of the reply to correspond with the L<Thrift::Parser::Method/return_class>. C<seqid> is inherited. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub compose_reply { | ||
| 51 | my ($self, $reply) = @_; | ||
| 52 | my $class = ref $self; | ||
| 53 | |||
| 54 | return $class->new({ | ||
| 55 | type => TMessageType::REPLY, | ||
| 56 | arguments => Thrift::Parser::FieldSet->new({ fields => [ | ||
| 57 | Thrift::Parser::Field->new({ | ||
| 58 | id => 0, | ||
| 59 | name => 'return_value', | ||
| 60 | value => $self->method->return_class->compose_with_idl($self->method->idl->returns, $reply), | ||
| 61 | }), | ||
| 62 | ] }), | ||
| 63 | seqid => $self->seqid, | ||
| 64 | method => $self->method, | ||
| 65 | }); | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 compose_reply_exception | ||
| 69 | |||
| 70 | my $reply_message = $message->compose_reply_exception($exception); | ||
| 71 | |||
| 72 | my $reply_message = $message->compose_reply_exception({ | ||
| 73 | ouch => { | ||
| 74 | message => 'you made a mistake, | ||
| 75 | } | ||
| 76 | }); | ||
| 77 | |||
| 78 | A helper method, use this to compose a new message which is a reply to another message. Expects the type of the reply to correspond with one of the L<Thrift::Parser::Method/throw_classes>. C<seqid> is inherited. You may pass it a blessed object or a hashref with one key (the name of the throw you're using). | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub compose_reply_exception { | ||
| 83 | my ($self, $throw) = @_; | ||
| 84 | my $class = ref $self; | ||
| 85 | |||
| 86 | if (ref($throw) eq 'HASH') { | ||
| 87 | my @keys = keys %$throw; | ||
| 88 | Thrift::Parser::InvalidArgument->throw("compose_reply_exception() must be passed a hash with exactly one key") | ||
| 89 | if int @keys != 1; | ||
| 90 | |||
| 91 | my $throw_class = $self->method->throw_classes->{$keys[0]}; | ||
| 92 | Thrift::Parser::InvalidArgument->throw( | ||
| 93 | error => "Method ".$self->method." doesn't have a throw named '$keys[0]'", | ||
| 94 | key => $keys[0], | ||
| 95 | ) if ! $throw_class; | ||
| 96 | |||
| 97 | $throw = $throw_class->compose(%{ $throw->{$keys[0]} }); | ||
| 98 | } | ||
| 99 | |||
| 100 | # Get the Thrift::IDL::Field that represents this named throw in the IDL::Method throws() array | ||
| 101 | # TODO: This is not perfect; if the Method has more than one throw of the same type, | ||
| 102 | # we won't know which to use (i.e., "throws (1: InvalidArguments ouch, 2: InvalidArguments pain)") | ||
| 103 | my ($field) = grep { $_->type->name eq $throw->name } @{ $self->method->idl->throws }; | ||
| 104 | if (! $field) { | ||
| 105 | die "Couldn't find a throw in method ".$self->method->idl->name." with the type ".$throw->name; | ||
| 106 | } | ||
| 107 | |||
| 108 | return $class->new({ | ||
| 109 | type => TMessageType::REPLY, | ||
| 110 | arguments => Thrift::Parser::FieldSet->new({ fields => [ | ||
| 111 | Thrift::Parser::Field->new({ | ||
| 112 | id => $field->id, | ||
| 113 | name => $field->name, | ||
| 114 | value => $throw, | ||
| 115 | }), | ||
| 116 | ] }), | ||
| 117 | seqid => $self->seqid, | ||
| 118 | method => $self->method, | ||
| 119 | }); | ||
| 120 | } | ||
| 121 | |||
| 122 | =head2 compose_reply_application_exception | ||
| 123 | |||
| 124 | my $reply_message = $message->compose_reply_application_exception($error, $code); | ||
| 125 | |||
| 126 | A helper method, use this to compose a new message which is a reply to another message. Throws as L<TApplicationException> with the error message and code as passed. seqid is inherited. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub compose_reply_application_exception { | ||
| 131 | my ($self, $error, $code) = @_; | ||
| 132 | my $class = ref $self; | ||
| 133 | |||
| 134 | $code ||= TApplicationException::UNKNOWN; | ||
| 135 | |||
| 136 | return $class->new({ | ||
| 137 | type => TMessageType::EXCEPTION, | ||
| 138 | arguments => Thrift::Parser::FieldSet->new({ fields => [ | ||
| 139 | Thrift::Parser::Field->new({ | ||
| 140 | id => 1, | ||
| 141 | name => 'message', | ||
| 142 | value => Thrift::Parser::Type::string->new({ value => $error }), | ||
| 143 | }), | ||
| 144 | Thrift::Parser::Field->new({ | ||
| 145 | id => 2, | ||
| 146 | name => 'type', | ||
| 147 | value => Thrift::Parser::Type::i32->new({ value => $code }), | ||
| 148 | }), | ||
| 149 | ] }), | ||
| 150 | seqid => $self->seqid, | ||
| 151 | method => $self->method, | ||
| 152 | }); | ||
| 153 | } | ||
| 154 | |||
| 155 | sub write { | ||
| 156 | my ($self, $output) = @_; | ||
| 157 | |||
| 158 | $output->writeMessageBegin($self->method->name, $self->type, $self->seqid); | ||
| 159 | $self->arguments->write($output); | ||
| 160 | $output->writeMessageEnd(); | ||
| 161 | $output->getTransport->flush(); | ||
| 162 | } | ||
| 163 | |||
| 164 | =head1 COPYRIGHT | ||
| 165 | |||
| 166 | Copyright (c) 2009 Eric Waters and XMission LLC (http://www.xmission.com/). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 167 | |||
| 168 | The full text of the license can be found in the LICENSE file included with this module. | ||
| 169 | |||
| 170 | =head1 AUTHOR | ||
| 171 | |||
| 172 | Eric Waters <ewaters@gmail.com> | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | 1; |