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