File Coverage

blib/lib/Thrift/Parser/Message.pm
Criterion Covered Total %
statement 20 39 51.2
branch 0 8 0.0
condition 1 4 25.0
subroutine 6 8 75.0
pod 3 5 60.0
total 30 64 46.8


line stmt bran cond sub pod time code
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 6     6   36 use strict;
  6         12  
  6         223  
12 6     6   36 use warnings;
  6         13  
  6         198  
13 6     6   31 use base qw(Class::Accessor::Grouped);
  6         12  
  6         5010  
14             __PACKAGE__->mk_group_accessors(simple => qw(method type seqid arguments));
15              
16             =head1 USAGE
17              
18             =head2 method
19              
20             Returns the L class that is represented in this message.
21              
22             =head2 type
23              
24             Returns the L type id of this message.
25              
26             =head2 seqid
27              
28             A L sequence ID.
29              
30             =head2 arguments
31              
32             A L object representing the arguments of this message.
33              
34             =cut
35              
36             sub new {
37 4     4 0 228 my ($class, $self) = @_;
38 4   50     17 $self ||= {};
39 4         24 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. C is inherited.
47              
48             =cut
49              
50             sub compose_reply {
51 1     1 1 722 my ($self, $reply) = @_;
52 1         3 my $class = ref $self;
53              
54 1         9 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. C 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 0     0 1 0 my ($self, $throw) = @_;
84 0         0 my $class = ref $self;
85              
86 0 0       0 if (ref($throw) eq 'HASH') {
87 0         0 my @keys = keys %$throw;
88 0 0       0 Thrift::Parser::InvalidArgument->throw("compose_reply_exception() must be passed a hash with exactly one key")
89             if int @keys != 1;
90              
91 0         0 my $throw_class = $self->method->throw_classes->{$keys[0]};
92 0 0       0 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 0         0 $throw = $throw_class->compose(%{ $throw->{$keys[0]} });
  0         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 0         0 my ($field) = grep { $_->type->name eq $throw->name } @{ $self->method->idl->throws };
  0         0  
  0         0  
104 0 0       0 if (! $field) {
105 0         0 die "Couldn't find a throw in method ".$self->method->idl->name." with the type ".$throw->name;
106             }
107              
108 0         0 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 with the error message and code as passed. seqid is inherited.
127              
128             =cut
129              
130             sub compose_reply_application_exception {
131 0     0 1 0 my ($self, $error, $code) = @_;
132 0         0 my $class = ref $self;
133              
134 0   0     0 $code ||= TApplicationException::UNKNOWN;
135              
136 0         0 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 1     1 0 8 my ($self, $output) = @_;
157              
158 1         8 $output->writeMessageBegin($self->method->name, $self->type, $self->seqid);
159 1         6 $self->arguments->write($output);
160 1         5 $output->writeMessageEnd();
161 1         10 $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
173              
174             =cut
175              
176             1;