From 0cdc6c8cd94cad4407e38db5975715fb67c71fb7 Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Tue, 13 Nov 2007 10:19:08 +0000 Subject: [PATCH] Merging PHP transport changes from www trunk to thrift trunk Summary: Some empty() fixes, plus a few other socket helpers Reviewed By: lucas Test Plan: Use the transports to make IPC calls! git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665331 13f79535-47bb-0310-9956-ffa450edef68 --- lib/php/src/transport/TBufferedTransport.php | 11 ++++----- lib/php/src/transport/TFramedTransport.php | 9 ++++--- lib/php/src/transport/THttpClient.php | 2 +- lib/php/src/transport/TMemoryBuffer.php | 2 +- lib/php/src/transport/TPhpStream.php | 2 +- lib/php/src/transport/TSocket.php | 25 +++++++++++++++++--- lib/php/src/transport/TSocketPool.php | 14 ++++++++++- 7 files changed, 47 insertions(+), 18 deletions(-) diff --git a/lib/php/src/transport/TBufferedTransport.php b/lib/php/src/transport/TBufferedTransport.php index 4ed07373..8f410dfc 100644 --- a/lib/php/src/transport/TBufferedTransport.php +++ b/lib/php/src/transport/TBufferedTransport.php @@ -78,16 +78,15 @@ class TBufferedTransport extends TTransport { } public function putBack($data) { - if (empty($this->rBuf_)) { + if (strlen($this->rBuf_) === 0) { $this->rBuf_ = $data; - } - else { + } else { $this->rBuf_ = ($data . $this->rBuf_); } } - + public function read($len) { - if (empty($this->rBuf_)){ + if (strlen($this->rBuf_) === 0) { $this->rBuf_ = $this->transport_->read($this->rBufSize_); } @@ -111,7 +110,7 @@ class TBufferedTransport extends TTransport { } public function flush() { - if (!empty($this->wBuf_)) { + if (strlen($this->wBuf_) > 0) { $this->transport_->write($this->wBuf_); $this->wBuf_ = ''; } diff --git a/lib/php/src/transport/TFramedTransport.php b/lib/php/src/transport/TFramedTransport.php index 5750d784..e2f8d6bc 100644 --- a/lib/php/src/transport/TFramedTransport.php +++ b/lib/php/src/transport/TFramedTransport.php @@ -89,7 +89,7 @@ class TFramedTransport extends TTransport { return $this->transport_->read($len); } - if (empty($this->rBuf_)) { + if (strlen($this->rBuf_) === 0) { $this->readFrame(); } @@ -99,7 +99,7 @@ class TFramedTransport extends TTransport { $this->rBuf_ = null; return $out; } - + // Return substr $out = substr($this->rBuf_, 0, $len); $this->rBuf_ = substr($this->rBuf_, $len); @@ -112,10 +112,9 @@ class TFramedTransport extends TTransport { * @param string $data data to return */ public function putBack($data) { - if (empty($this->rBuf_)) { + if (strlen($this->rBuf_) === 0) { $this->rBuf_ = $data; - } - else { + } else { $this->rBuf_ = ($data . $this->rBuf_); } } diff --git a/lib/php/src/transport/THttpClient.php b/lib/php/src/transport/THttpClient.php index 06cf9d82..d5d03c41 100644 --- a/lib/php/src/transport/THttpClient.php +++ b/lib/php/src/transport/THttpClient.php @@ -159,7 +159,7 @@ class THttpClient extends TTransport { 'User-Agent: PHP/THttpClient', 'Content-Type: application/x-thrift', 'Content-Length: '.strlen($this->buf_)); - + $options = array('method' => 'POST', 'header' => implode("\r\n", $headers), 'max_redirects' => 1, diff --git a/lib/php/src/transport/TMemoryBuffer.php b/lib/php/src/transport/TMemoryBuffer.php index f62bb572..c34fb67c 100644 --- a/lib/php/src/transport/TMemoryBuffer.php +++ b/lib/php/src/transport/TMemoryBuffer.php @@ -45,7 +45,7 @@ class TMemoryBuffer extends TTransport { } public function read($len) { - if (empty($this->buf_)) { + if (strlen($this->buf_) === 0) { throw new TTransportException('TMemoryBuffer: Could not read ' . $len . ' bytes from buffer.', TTransportException::UNKNOWN); diff --git a/lib/php/src/transport/TPhpStream.php b/lib/php/src/transport/TPhpStream.php index 03837a8a..d4646f1a 100644 --- a/lib/php/src/transport/TPhpStream.php +++ b/lib/php/src/transport/TPhpStream.php @@ -50,7 +50,7 @@ class TPhpStream extends TTransport { } } } - + public function close() { if ($this->read_) { @fclose($this->inStream_); diff --git a/lib/php/src/transport/TSocket.php b/lib/php/src/transport/TSocket.php index cce3a44b..e1bd3bd6 100644 --- a/lib/php/src/transport/TSocket.php +++ b/lib/php/src/transport/TSocket.php @@ -28,7 +28,7 @@ class TSocket extends TTransport { /** * Remote hostname - * + * * @var string */ protected $host_ = 'localhost'; @@ -127,6 +127,24 @@ class TSocket extends TTransport { $this->debug_ = $debug; } + /** + * Get the host that this socket is connected to + * + * @return string host + */ + public function getHost() { + return $this->host_; + } + + /** + * Get the remote port that this socket is connected to + * + * @return int port + */ + public function getPort() { + return $this->port_; + } + /** * Tests whether this is open * @@ -140,6 +158,7 @@ class TSocket extends TTransport { * Connects the socket. */ public function open() { + if ($this->persist_) { $this->handle_ = @pfsockopen($this->host_, $this->port_, @@ -162,7 +181,7 @@ class TSocket extends TTransport { } throw new TException($error); } - + stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000); $this->sendTimeoutSet_ = TRUE; } @@ -176,7 +195,7 @@ class TSocket extends TTransport { $this->handle_ = null; } } - + /** * Uses stream get contents to do the reading * diff --git a/lib/php/src/transport/TSocketPool.php b/lib/php/src/transport/TSocketPool.php index b3efb2d2..bd28db65 100644 --- a/lib/php/src/transport/TSocketPool.php +++ b/lib/php/src/transport/TSocketPool.php @@ -103,6 +103,18 @@ class TSocketPool extends TSocket { } } + /** + * Add a server to the pool + * + * This function does not prevent you from adding a duplicate server entry. + * + * @param string $host hostname or IP + * @param int $port port + */ + public function addServer($host, $port) { + $this->servers_[] = array('host' => $host, 'port' => $port); + } + /** * Sets how many time to keep retrying a host in the connect function. * @@ -206,7 +218,7 @@ class TSocketPool extends TSocket { // Set underlying TSocket params to this one $this->host_ = $host; $this->port_ = $port; - + // Try up to numRetries_ connections per server for ($attempt = 0; $attempt < $this->numRetries_; $attempt++) { try { -- 2.17.1