Mark Slee | 254ce20 | 2007-05-16 02:21:06 +0000 | [diff] [blame^] | 1 | # |
| 2 | # Copyright (c) 2006- Facebook |
| 3 | # Distributed under the Thrift Software License |
| 4 | # |
| 5 | # See accompanying file LICENSE or visit the Thrift site at: |
| 6 | # http://developers.facebook.com/thrift/ |
| 7 | # |
| 8 | # package - thrift |
| 9 | # author - T Jake Luciani <jakers@gmail.com> |
| 10 | # author - Mark Slee <mcslee@facebook.com> |
| 11 | # |
| 12 | |
| 13 | our $VERSION = '0.1'; |
| 14 | |
| 15 | require 5.6.0; |
| 16 | use strict; |
| 17 | use warnings; |
| 18 | |
| 19 | # |
| 20 | # Data types that can be sent via Thrift |
| 21 | # |
| 22 | package TType; |
| 23 | use constant STOP => 0; |
| 24 | use constant VOID => 1; |
| 25 | use constant BOOL => 2; |
| 26 | use constant BYTE => 3; |
| 27 | use constant I08 => 3; |
| 28 | use constant DOUBLE => 4; |
| 29 | use constant I16 => 6; |
| 30 | use constant I32 => 8; |
| 31 | use constant I64 => 10; |
| 32 | use constant STRING => 11; |
| 33 | use constant UTF7 => 11; |
| 34 | use constant STRUCT => 12; |
| 35 | use constant MAP => 13; |
| 36 | use constant SET => 14; |
| 37 | use constant LIST => 15; |
| 38 | use constant UTF8 => 16; |
| 39 | use constant UTF16 => 17; |
| 40 | 1; |
| 41 | |
| 42 | # |
| 43 | # Message types for RPC |
| 44 | # |
| 45 | package TMessageType; |
| 46 | use constant CALL => 1; |
| 47 | use constant REPLY => 2; |
| 48 | use constant EXCEPTION => 3; |
| 49 | 1; |
| 50 | |
| 51 | package Thrift::TException; |
| 52 | |
| 53 | sub new { |
| 54 | my $classname = shift; |
| 55 | my $self = {message => shift, code => shift || 0}; |
| 56 | |
| 57 | return bless($self,$classname); |
| 58 | } |
| 59 | 1; |
| 60 | |
| 61 | package TApplicationException; |
| 62 | use base('Thrift::TException'); |
| 63 | |
| 64 | use constant UNKNOWN => 0; |
| 65 | use constant UNKNOWN_METHOD => 1; |
| 66 | use constant INVALID_MESSAGE_TYPE => 2; |
| 67 | use constant WRONG_METHOD_NAME => 3; |
| 68 | use constant BAD_SEQUENCE_ID => 4; |
| 69 | use constant MISSING_RESULT => 5; |
| 70 | |
| 71 | sub new { |
| 72 | my $classname = shift; |
| 73 | |
| 74 | my $self = $classname->SUPER::new(); |
| 75 | |
| 76 | return bless($self,$classname); |
| 77 | } |
| 78 | |
| 79 | sub read { |
| 80 | my $self = shift; |
| 81 | my $input = shift; |
| 82 | |
| 83 | my $xfer = 0; |
| 84 | my $fname = undef; |
| 85 | my $ftype = 0; |
| 86 | my $fid = 0; |
| 87 | |
| 88 | $xfer += $input->readStructBegin($fname); |
| 89 | |
| 90 | while (1) |
| 91 | { |
| 92 | $xfer += $input->readFieldBegin($fname, $ftype, $fid); |
| 93 | if ($ftype == TType::STOP) { |
| 94 | last; next; |
| 95 | } |
| 96 | |
| 97 | SWITCH: for($fid) |
| 98 | { |
| 99 | /1/ && do{ |
| 100 | |
| 101 | if ($ftype == TType::STRING) { |
| 102 | $xfer += $input->readString($self->{message}); |
| 103 | } else { |
| 104 | $xfer += $input->skip($ftype); |
| 105 | } |
| 106 | |
| 107 | last; |
| 108 | }; |
| 109 | |
| 110 | /2/ && do{ |
| 111 | if ($ftype == TType::I32) { |
| 112 | $xfer += $input->readI32($self->{code}); |
| 113 | } else { |
| 114 | $xfer += $input->skip($ftype); |
| 115 | } |
| 116 | last; |
| 117 | }; |
| 118 | |
| 119 | $xfer += $input->skip($ftype); |
| 120 | } |
| 121 | |
| 122 | $xfer += $input->readFieldEnd(); |
| 123 | } |
| 124 | $xfer += $input->readStructEnd(); |
| 125 | |
| 126 | return $xfer; |
| 127 | } |
| 128 | |
| 129 | sub write { |
| 130 | my $self = shift; |
| 131 | my $output = shift; |
| 132 | |
| 133 | my $xfer = 0; |
| 134 | |
| 135 | $xfer += $output->writeStructBegin('TApplicationException'); |
| 136 | |
| 137 | if ($self->getMessage()) { |
| 138 | $xfer += $output->writeFieldBegin('message', TType::STRING, 1); |
| 139 | $xfer += $output->writeString($self->getMessage()); |
| 140 | $xfer += $output->writeFieldEnd(); |
| 141 | } |
| 142 | |
| 143 | if ($self->getCode()) { |
| 144 | $xfer += $output->writeFieldBegin('type', TType::I32, 2); |
| 145 | $xfer += $output->writeI32($self->getCode()); |
| 146 | $xfer += $output->writeFieldEnd(); |
| 147 | } |
| 148 | |
| 149 | $xfer += $output->writeFieldStop(); |
| 150 | $xfer += $output->writeStructEnd(); |
| 151 | |
| 152 | return $xfer; |
| 153 | } |
| 154 | |
| 155 | sub getMessage |
| 156 | { |
| 157 | my $self = shift; |
| 158 | |
| 159 | return $self->{message}; |
| 160 | } |
| 161 | |
| 162 | sub getCode |
| 163 | { |
| 164 | my $self = shift; |
| 165 | |
| 166 | return $self->{code}; |
| 167 | } |
| 168 | |
| 169 | 1; |