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; |
Mark Slee | d395d57 | 2007-02-27 01:16:55 +0000 | [diff] [blame] | 32 | const EXCEPTION = 3; |
Mark Slee | 1ecb1b0 | 2007-02-22 01:01:10 +0000 | [diff] [blame] | 33 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 34 | |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 35 | class TException extends Exception { |
| 36 | function __construct($message=null, $code=0) { |
| 37 | parent::__construct($message, $code); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | class TApplicationException extends TException { |
| 42 | |
| 43 | const UNKNOWN = 0; |
| 44 | const UNKNOWN_METHOD = 1; |
| 45 | const INVALID_MESSAGE_TYPE = 2; |
| 46 | const WRONG_METHOD_NAME = 3; |
| 47 | const BAD_SEQUENCE_ID = 4; |
| 48 | const MISSING_RESULT = 5; |
| 49 | |
| 50 | function __construct($message=null, $code=0) { |
| 51 | parent::__construct($message, $code); |
| 52 | } |
| 53 | |
| 54 | public function read($input) { |
| 55 | $xfer = 0; |
| 56 | $fname = null; |
| 57 | $ftype = 0; |
| 58 | $fid = 0; |
| 59 | $xfer += $input->readStructBegin($fname); |
| 60 | while (true) |
| 61 | { |
| 62 | $xfer += $input->readFieldBegin($fname, $ftype, $fid); |
| 63 | if ($ftype == TType::STOP) { |
| 64 | break; |
| 65 | } |
| 66 | switch ($fid) |
| 67 | { |
| 68 | case 1: |
| 69 | if ($ftype == TType::STRING) { |
| 70 | $xfer += $input->readString($this->message); |
| 71 | } else { |
| 72 | $xfer += $input->skip($ftype); |
| 73 | } |
| 74 | break; |
| 75 | case 2: |
| 76 | if ($ftype == TType::I32) { |
| 77 | $xfer += $input->readI32($this->code); |
| 78 | } else { |
| 79 | $xfer += $input->skip($ftype); |
| 80 | } |
| 81 | break; |
| 82 | default: |
| 83 | $xfer += $input->skip($ftype); |
| 84 | break; |
| 85 | } |
| 86 | $xfer += $input->readFieldEnd(); |
| 87 | } |
| 88 | $xfer += $input->readStructEnd(); |
| 89 | return $xfer; |
| 90 | } |
| 91 | |
| 92 | public function write($output) { |
| 93 | $xfer = 0; |
| 94 | $xfer += $output->writeStructBegin('TApplicationException'); |
| 95 | if ($this->getMessage()) { |
| 96 | $xfer += $output->writeFieldBegin('message', TType::STRING, 1); |
| 97 | $xfer += $output->writeString($this->getMessage()); |
| 98 | $xfer += $output->writeFieldEnd(); |
| 99 | } |
Mark Slee | 0af62d1 | 2007-02-27 22:11:09 +0000 | [diff] [blame^] | 100 | if ($this->getCode()) { |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 101 | $xfer += $output->writeFieldBegin('type', TType::I32, 2); |
| 102 | $xfer += $output->writeI32($this->getCode()); |
| 103 | $xfer += $output->writeFieldEnd(); |
| 104 | } |
| 105 | $xfer += $output->writeFieldStop(); |
| 106 | $xfer += $output->writeStructEnd(); |
| 107 | return $xfer; |
| 108 | } |
| 109 | } |
| 110 | |
Mark Slee | 1ecb1b0 | 2007-02-22 01:01:10 +0000 | [diff] [blame] | 111 | /** |
| 112 | * Set global THRIFT ROOT automatically via inclusion here |
| 113 | */ |
| 114 | if (!isset($GLOBALS['THRIFT_ROOT'])) { |
| 115 | $GLOBALS['THRIFT_ROOT'] = dirname(__FILE__); |
| 116 | } |
| 117 | include_once $GLOBALS['THRIFT_ROOT'].'/protocol/TProtocol.php'; |
| 118 | include_once $GLOBALS['THRIFT_ROOT'].'/transport/TTransport.php'; |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 119 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 120 | ?> |