Thrift now works in PHP, hot stuff
Summary: End to end communication working in Thrift with PHP
Problem: It's a bit slower than pillar still. Need to find out why.
Reviewed By: aditya
Test Plan: Unit tests are in the test directory. Get lucas on the PHP case...
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664720 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/php/src/protocol/TBinaryProtocol.php b/lib/php/src/protocol/TBinaryProtocol.php
new file mode 100644
index 0000000..867a8aa
--- /dev/null
+++ b/lib/php/src/protocol/TBinaryProtocol.php
@@ -0,0 +1,195 @@
+<?php
+
+/** For transport operations */
+require_once THRIFT_ROOT.'/transport/TTransport.php';
+
+/**
+ * Binary implementation of the Thrift protocol.
+ *
+ * @package thrift.protocol
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+class TBinaryProtocol extends TProtocol {
+
+ public function writeStructBegin($out, $name) {
+ return 0;
+ }
+
+ public function writeStructEnd($out) {
+ return 0;
+ }
+
+ public function writeFieldBegin($out, $fieldName, $fieldType, $fieldId) {
+ return
+ $this->writeByte($out, $fieldType) +
+ $this->writeI32($out, $fieldId);
+ }
+
+ public function writeFieldEnd($out) {
+ return 0;
+ }
+
+ public function writeFieldStop($out) {
+ return
+ $this->writeByte($out, TType::STOP);
+ }
+
+ public function writeMapBegin($out, $keyType, $valType, $size) {
+ return
+ $this->writeByte($out, $keyType) +
+ $this->writeByte($out, $valType) +
+ $this->writeI32($out, $size);
+ }
+
+ public function writeMapEnd($out) {
+ return 0;
+ }
+
+ public function writeListBegin($out, $elemType, $size) {
+ return
+ $this->writeByte($out, $elemType) +
+ $this->writeI32($out, $size);
+ }
+
+ public function writeListEnd($out) {
+ return 0;
+ }
+
+ public function writeSetBegin($out, $elemType, $size) {
+ return
+ $this->writeByte($out, $elemType) +
+ $this->writeI32($out, $size);
+ }
+
+ public function writeSetEnd($out) {
+ return 0;
+ }
+
+ public function writeByte($out, $byte) {
+ $data = pack('c', $byte);
+ $out->write($data, 1);
+ return 1;
+ }
+
+ public function writeI32($out, $i32) {
+ $data = pack('l', $i32);
+ //if (!defined('BIG_ENDIAN')) {
+ $data = strrev($data);
+ //}
+ $out->write($data, 4);
+ return 4;
+ }
+
+ public function writeI64($out, $i64) {
+ $hi = $i64 >> 32;
+ $lo = $i64 & 0xFFFFFFFF;
+ if (!defined('BIG_ENDIAN')) {
+ $data = pack('N2', $hi, $lo);
+ } else {
+ $data = pack('N2', $lo, $hi);
+ }
+ $out->write($data, 8);
+ return 8;
+ }
+
+ public function writeString($out, $str) {
+ $len = strlen($str);
+ $result = $this->writeI32($out, $len);
+ $out->write($str, $len);
+ return $result + $len;
+ }
+
+ public function readStructBegin($in, &$name) {
+ $name = '';
+ return 0;
+ }
+
+ public function readStructEnd($in) {
+ return 0;
+ }
+
+ public function readFieldBegin($in, &$name, &$fieldType, &$fieldId) {
+ $result = $this->readByte($in, $fieldType);
+ if ($fieldType == TType::STOP) {
+ $fieldId = 0;
+ return $result;
+ }
+ $result += $this->readI32($in, $fieldId);
+ return $result;
+ }
+
+ public function readFieldEnd($in) {
+ return 0;
+ }
+
+ public function readMapBegin($in, &$keyType, &$valType, &$size) {
+ $result = $this->readByte($in, $keyType);
+ $result += $this->readByte($in, $valType);
+ $result += $this->readI32($in, $size);
+ return $result;
+ }
+
+ public function readMapEnd($in) {
+ return 0;
+ }
+
+ public function readListBegin($in, &$elemType, &$size) {
+ $result = $this->readByte($in, $elemType);
+ $result += $this->readI32($in, $size);
+ return $result;
+ }
+
+ public function readListEnd($in) {
+ return 0;
+ }
+
+ public function readSetBegin($in, &$elemType, &$size) {
+ $result = $this->readByte($in, $elemType);
+ $result += $this->readI32($in, $size);
+ return $result;
+ }
+
+ public function readSetEnd($in) {
+ return 0;
+ }
+
+ public function readByte($in, &$byte) {
+ $data = $in->readAll(1);
+ $arr = unpack('c', $data);
+ $byte = $arr[1];
+ return 1;
+ }
+
+ public function readI32($in, &$i32) {
+ $data = $in->readAll(4);
+ if (!defined('BIG_ENDIAN')) {
+ $data = strrev($data);
+ }
+ $arr = unpack('l', $data);
+ $i32 = $arr[1];
+ return 4;
+ }
+
+ public function readI64($in, &$i64) {
+ $data = $in->readAll(8);
+ $arr = unpack('N2', $data);
+
+ // Check for a negative
+ if ($arr[1] & 0x80000000) {
+ $arr[1] = $arr[1] ^ 0xFFFFFFFF;
+ $arr[2] = $arr[2] ^ 0xFFFFFFFF;
+ $i64 = 0 - $arr[1]*4294967296 - $arr[2] - 1;
+ } else {
+ $i64 = $arr[1]*4294967296 + $arr[2];
+ }
+ return 8;
+ }
+
+ public function readString($in, &$str) {
+ $result = $this->readI32($in, $len);
+ $str = $in->readAll($len);
+ return $result + $len;
+ }
+}
+
+?>
\ No newline at end of file
diff --git a/lib/php/src/protocol/TProtocol.php b/lib/php/src/protocol/TProtocol.php
new file mode 100644
index 0000000..5a69bbd
--- /dev/null
+++ b/lib/php/src/protocol/TProtocol.php
@@ -0,0 +1,157 @@
+<?php
+
+/** Types */
+require_once THRIFT_ROOT.'/protocol/TType.php';
+
+/**
+ * Protocol module.
+ *
+ * @package thrift.protocol
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+abstract class TProtocol {
+
+ /**
+ * Writes a struct header.
+ *
+ * @param TTransport $out Output transport
+ * @param string $name Struct name
+ * @throws TException on write error
+ * @return int How many bytes written
+ */
+ public abstract function writeStructBegin($out, $name);
+
+
+ /**
+ * Close a struct.
+ *
+ * @param TTransport $out Output transport
+ * @throws TException on write error
+ * @return int How many bytes written
+ */
+ public abstract function writeStructEnd($out);
+
+ /*
+ * Starts a field.
+ *
+ * @param TTransport $out Output transport
+ * @param string $name Field name
+ * @param int $type Field type
+ * @param int $fid Field id
+ * @throws TException on write error
+ * @return int How many bytes written
+ */
+ public abstract function writeFieldBegin($out, $fieldName, $fieldType, $fieldId);
+
+ public abstract function writeFieldEnd($out);
+
+ public abstract function writeFieldStop($out);
+
+ public abstract function writeMapBegin($out, $keyType, $valType, $size);
+
+ public abstract function writeMapEnd($out);
+
+ public abstract function writeListBegin($out, $elemType, $size);
+
+ public abstract function writeListEnd($out);
+
+ public abstract function writeSetBegin($out, $elemType, $size);
+
+ public abstract function writeSetEnd($out);
+
+ public abstract function writeByte($out, $byte);
+
+ public abstract function writeI32($out, $i32);
+
+ public abstract function writeI64($out, $i64);
+
+ public abstract function writeString($out, $str);
+
+
+ public abstract function readStructBegin($in, &$name);
+
+ public abstract function readStructEnd($in);
+
+ public abstract function readFieldBegin($in, &$name, &$fieldType, &$fieldId);
+
+ public abstract function readFieldEnd($in);
+
+ public abstract function readMapBegin($in, &$keyType, &$valType, &$size);
+
+ public abstract function readMapEnd($in);
+
+ public abstract function readListBegin($in, &$elemType, &$size);
+
+ public abstract function readListEnd($in);
+
+ public abstract function readSetBegin($in, &$elemType, &$size);
+
+ public abstract function readSetEnd($in);
+
+ public abstract function readByte($in, &$byte);
+
+ public abstract function readI32($in, &$i32);
+
+ public abstract function readI64($in, &$i64);
+
+ public abstract function readString($in, &$str);
+
+ public function skip($in, $type) {
+ switch ($type) {
+ case TType::BYTE:
+ return $this->readByte($in, $byte);
+ case TType::I32:
+ return $this->readI32($in, $i32);
+ case TType::I64:
+ return $this->readI64($in, $i64);
+ case TType::STRING:
+ return $this->readString($in, $str);
+ case TType::STRUCT:
+ {
+ $result = $this->readStructBegin($in, $name);
+ while (true) {
+ $result += $this->readFieldBegin($in, $name, $ftype, $fid);
+ if ($ftype == TType::STOP) {
+ break;
+ }
+ $result += $this->skip($in, $ftype);
+ $result += $this->readFieldEnd($in);
+ }
+ $result += $this->readStructEnd($in);
+ return $result;
+ }
+ case TType::MAP:
+ {
+ $result = $this->readMapBegin($in, $keyType, $valType, $size);
+ for ($i = 0; $i < $size; $i++) {
+ $result += $this->skip($in, $keyType);
+ $result += $this->skip($in, $valType);
+ }
+ $result += $this->readMapEnd($in);
+ return $result;
+ }
+ case TType::SET:
+ {
+ $result = $this->readSetBegin($in, $elemType, $size);
+ for ($i = 0; $i < $size; $i++) {
+ $result += $this->skip($in, $elemType);
+ }
+ $result += $this->readSetEnd($in);
+ return $result;
+ }
+ case TType::LST:
+ {
+ $result = $this->readListBegin($in, $elemType, $size);
+ for ($i = 0; $i < $size; $i++) {
+ $result += $this->skip($in, $elemType);
+ }
+ $result += $this->readListEnd($in);
+ return $result;
+ }
+ default:
+ return 0;
+ }
+ }
+}
+
+?>
diff --git a/lib/php/src/protocol/TType.php b/lib/php/src/protocol/TType.php
new file mode 100644
index 0000000..957efe6
--- /dev/null
+++ b/lib/php/src/protocol/TType.php
@@ -0,0 +1,19 @@
+<?php
+
+/**
+ * Constants for Thrift data types.
+ *
+ * @package thrift.protocol
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+class TType {
+ const STOP = 1;
+ const BYTE = 2;
+ const I32 = 6;
+ const I64 = 8;
+ const STRING = 9;
+ const STRUCT = 10;
+ const MAP = 11;
+ const SET = 12;
+ const LST = 13; // cannot use LIST keyword in PHP!
+}