Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame] | 1 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame] | 9 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame] | 11 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame] | 18 | # |
| 19 | |
Jake Farrell | 6fcecd4 | 2012-10-11 20:34:25 +0000 | [diff] [blame] | 20 | our $VERSION = '1.0.0-dev'; |
Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame] | 21 | |
| 22 | require 5.6.0; |
| 23 | use strict; |
| 24 | use warnings; |
| 25 | |
| 26 | # |
| 27 | # Data types that can be sent via Thrift |
| 28 | # |
| 29 | package TType; |
| 30 | use constant STOP => 0; |
| 31 | use constant VOID => 1; |
| 32 | use constant BOOL => 2; |
| 33 | use constant BYTE => 3; |
| 34 | use constant I08 => 3; |
| 35 | use constant DOUBLE => 4; |
| 36 | use constant I16 => 6; |
| 37 | use constant I32 => 8; |
| 38 | use constant I64 => 10; |
| 39 | use constant STRING => 11; |
| 40 | use constant UTF7 => 11; |
| 41 | use constant STRUCT => 12; |
| 42 | use constant MAP => 13; |
| 43 | use constant SET => 14; |
| 44 | use constant LIST => 15; |
| 45 | use constant UTF8 => 16; |
| 46 | use constant UTF16 => 17; |
| 47 | 1; |
| 48 | |
| 49 | # |
| 50 | # Message types for RPC |
| 51 | # |
| 52 | package TMessageType; |
| 53 | use constant CALL => 1; |
| 54 | use constant REPLY => 2; |
| 55 | use constant EXCEPTION => 3; |
David Reiss | deda141 | 2009-04-02 19:22:31 +0000 | [diff] [blame] | 56 | use constant ONEWAY => 4; |
Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame] | 57 | 1; |
| 58 | |
| 59 | package Thrift::TException; |
| 60 | |
| 61 | sub new { |
| 62 | my $classname = shift; |
| 63 | my $self = {message => shift, code => shift || 0}; |
| 64 | |
| 65 | return bless($self,$classname); |
| 66 | } |
| 67 | 1; |
| 68 | |
| 69 | package TApplicationException; |
| 70 | use base('Thrift::TException'); |
| 71 | |
Roger Meier | 0193149 | 2012-12-22 21:31:03 +0100 | [diff] [blame] | 72 | use constant UNKNOWN => 0; |
| 73 | use constant UNKNOWN_METHOD => 1; |
| 74 | use constant INVALID_MESSAGE_TYPE => 2; |
| 75 | use constant WRONG_METHOD_NAME => 3; |
| 76 | use constant BAD_SEQUENCE_ID => 4; |
| 77 | use constant MISSING_RESULT => 5; |
| 78 | use constant INTERNAL_ERROR => 6; |
| 79 | use constant PROTOCOL_ERROR => 7; |
| 80 | use constant INVALID_TRANSFORM => 8; |
| 81 | use constant INVALID_PROTOCOL => 9; |
| 82 | use constant UNSUPPORTED_CLIENT_TYPE => 10; |
Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame] | 83 | |
| 84 | sub new { |
| 85 | my $classname = shift; |
| 86 | |
| 87 | my $self = $classname->SUPER::new(); |
| 88 | |
| 89 | return bless($self,$classname); |
| 90 | } |
| 91 | |
| 92 | sub read { |
| 93 | my $self = shift; |
| 94 | my $input = shift; |
| 95 | |
| 96 | my $xfer = 0; |
| 97 | my $fname = undef; |
| 98 | my $ftype = 0; |
| 99 | my $fid = 0; |
| 100 | |
Bryan Duxbury | ee8255d | 2010-09-02 00:52:46 +0000 | [diff] [blame] | 101 | $xfer += $input->readStructBegin(\$fname); |
Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame] | 102 | |
| 103 | while (1) |
| 104 | { |
Bryan Duxbury | ee8255d | 2010-09-02 00:52:46 +0000 | [diff] [blame] | 105 | $xfer += $input->readFieldBegin(\$fname, \$ftype, \$fid); |
Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame] | 106 | if ($ftype == TType::STOP) { |
| 107 | last; next; |
| 108 | } |
| 109 | |
| 110 | SWITCH: for($fid) |
| 111 | { |
| 112 | /1/ && do{ |
| 113 | |
| 114 | if ($ftype == TType::STRING) { |
Bryan Duxbury | ee8255d | 2010-09-02 00:52:46 +0000 | [diff] [blame] | 115 | $xfer += $input->readString(\$self->{message}); |
Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame] | 116 | } else { |
| 117 | $xfer += $input->skip($ftype); |
| 118 | } |
| 119 | |
| 120 | last; |
| 121 | }; |
| 122 | |
| 123 | /2/ && do{ |
| 124 | if ($ftype == TType::I32) { |
Bryan Duxbury | ee8255d | 2010-09-02 00:52:46 +0000 | [diff] [blame] | 125 | $xfer += $input->readI32(\$self->{code}); |
Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame] | 126 | } else { |
| 127 | $xfer += $input->skip($ftype); |
| 128 | } |
| 129 | last; |
| 130 | }; |
| 131 | |
| 132 | $xfer += $input->skip($ftype); |
| 133 | } |
| 134 | |
| 135 | $xfer += $input->readFieldEnd(); |
| 136 | } |
| 137 | $xfer += $input->readStructEnd(); |
| 138 | |
| 139 | return $xfer; |
| 140 | } |
| 141 | |
| 142 | sub write { |
| 143 | my $self = shift; |
| 144 | my $output = shift; |
| 145 | |
| 146 | my $xfer = 0; |
| 147 | |
| 148 | $xfer += $output->writeStructBegin('TApplicationException'); |
| 149 | |
| 150 | if ($self->getMessage()) { |
| 151 | $xfer += $output->writeFieldBegin('message', TType::STRING, 1); |
| 152 | $xfer += $output->writeString($self->getMessage()); |
| 153 | $xfer += $output->writeFieldEnd(); |
| 154 | } |
| 155 | |
| 156 | if ($self->getCode()) { |
| 157 | $xfer += $output->writeFieldBegin('type', TType::I32, 2); |
| 158 | $xfer += $output->writeI32($self->getCode()); |
| 159 | $xfer += $output->writeFieldEnd(); |
| 160 | } |
| 161 | |
| 162 | $xfer += $output->writeFieldStop(); |
| 163 | $xfer += $output->writeStructEnd(); |
| 164 | |
| 165 | return $xfer; |
| 166 | } |
| 167 | |
| 168 | sub getMessage |
| 169 | { |
| 170 | my $self = shift; |
| 171 | |
| 172 | return $self->{message}; |
| 173 | } |
| 174 | |
| 175 | sub getCode |
| 176 | { |
| 177 | my $self = shift; |
| 178 | |
| 179 | return $self->{code}; |
| 180 | } |
| 181 | |
| 182 | 1; |