Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 1 | <?php |
| 2 | |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 3 | /** |
Mark Slee | 1ecb1b0 | 2007-02-22 01:01:10 +0000 | [diff] [blame^] | 4 | * Data types that can be sent via Thrift |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 5 | */ |
Mark Slee | 1ecb1b0 | 2007-02-22 01:01:10 +0000 | [diff] [blame^] | 6 | class TType { |
| 7 | const STOP = 0; |
| 8 | const VOID = 1; |
| 9 | const BOOL = 2; |
| 10 | const BYTE = 3; |
| 11 | const I08 = 3; |
| 12 | const DOUBLE = 4; |
| 13 | const I16 = 6; |
| 14 | const I32 = 8; |
| 15 | const I64 = 10; |
| 16 | const STRING = 11; |
| 17 | const UTF7 = 11; |
| 18 | const STRUCT = 12; |
| 19 | const MAP = 13; |
| 20 | const SET = 14; |
| 21 | const LST = 15; // N.B. cannot use LIST keyword in PHP! |
| 22 | const UTF8 = 16; |
| 23 | const UTF16 = 17; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 24 | } |
Mark Slee | 1ecb1b0 | 2007-02-22 01:01:10 +0000 | [diff] [blame^] | 25 | |
| 26 | /** |
| 27 | * Message types for RPC |
| 28 | */ |
| 29 | class TMessageType { |
| 30 | const CALL = 1; |
| 31 | const REPLY = 2; |
| 32 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 33 | |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 34 | class TException extends Exception { |
| 35 | function __construct($message=null, $code=0) { |
| 36 | parent::__construct($message, $code); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | class TApplicationException extends TException { |
| 41 | |
| 42 | const UNKNOWN = 0; |
| 43 | const UNKNOWN_METHOD = 1; |
| 44 | const INVALID_MESSAGE_TYPE = 2; |
| 45 | const WRONG_METHOD_NAME = 3; |
| 46 | const BAD_SEQUENCE_ID = 4; |
| 47 | const MISSING_RESULT = 5; |
| 48 | |
| 49 | function __construct($message=null, $code=0) { |
| 50 | parent::__construct($message, $code); |
| 51 | } |
| 52 | |
| 53 | public function read($input) { |
| 54 | $xfer = 0; |
| 55 | $fname = null; |
| 56 | $ftype = 0; |
| 57 | $fid = 0; |
| 58 | $xfer += $input->readStructBegin($fname); |
| 59 | while (true) |
| 60 | { |
| 61 | $xfer += $input->readFieldBegin($fname, $ftype, $fid); |
| 62 | if ($ftype == TType::STOP) { |
| 63 | break; |
| 64 | } |
| 65 | switch ($fid) |
| 66 | { |
| 67 | case 1: |
| 68 | if ($ftype == TType::STRING) { |
| 69 | $xfer += $input->readString($this->message); |
| 70 | } else { |
| 71 | $xfer += $input->skip($ftype); |
| 72 | } |
| 73 | break; |
| 74 | case 2: |
| 75 | if ($ftype == TType::I32) { |
| 76 | $xfer += $input->readI32($this->code); |
| 77 | } else { |
| 78 | $xfer += $input->skip($ftype); |
| 79 | } |
| 80 | break; |
| 81 | default: |
| 82 | $xfer += $input->skip($ftype); |
| 83 | break; |
| 84 | } |
| 85 | $xfer += $input->readFieldEnd(); |
| 86 | } |
| 87 | $xfer += $input->readStructEnd(); |
| 88 | return $xfer; |
| 89 | } |
| 90 | |
| 91 | public function write($output) { |
| 92 | $xfer = 0; |
| 93 | $xfer += $output->writeStructBegin('TApplicationException'); |
| 94 | if ($this->getMessage()) { |
| 95 | $xfer += $output->writeFieldBegin('message', TType::STRING, 1); |
| 96 | $xfer += $output->writeString($this->getMessage()); |
| 97 | $xfer += $output->writeFieldEnd(); |
| 98 | } |
| 99 | if ($this->type !== null) { |
| 100 | $xfer += $output->writeFieldBegin('type', TType::I32, 2); |
| 101 | $xfer += $output->writeI32($this->getCode()); |
| 102 | $xfer += $output->writeFieldEnd(); |
| 103 | } |
| 104 | $xfer += $output->writeFieldStop(); |
| 105 | $xfer += $output->writeStructEnd(); |
| 106 | return $xfer; |
| 107 | } |
| 108 | } |
| 109 | |
Mark Slee | 1ecb1b0 | 2007-02-22 01:01:10 +0000 | [diff] [blame^] | 110 | /** |
| 111 | * Set global THRIFT ROOT automatically via inclusion here |
| 112 | */ |
| 113 | if (!isset($GLOBALS['THRIFT_ROOT'])) { |
| 114 | $GLOBALS['THRIFT_ROOT'] = dirname(__FILE__); |
| 115 | } |
| 116 | include_once $GLOBALS['THRIFT_ROOT'].'/protocol/TProtocol.php'; |
| 117 | include_once $GLOBALS['THRIFT_ROOT'].'/transport/TTransport.php'; |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 118 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 119 | ?> |