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
diff --git a/lib/php/src/transport/TBufferedTransport.php b/lib/php/src/transport/TBufferedTransport.php
index 4ed0737..8f410df 100644
--- a/lib/php/src/transport/TBufferedTransport.php
+++ b/lib/php/src/transport/TBufferedTransport.php
@@ -78,16 +78,15 @@
   }
 
   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 @@
   }
 
   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 5750d78..e2f8d6b 100644
--- a/lib/php/src/transport/TFramedTransport.php
+++ b/lib/php/src/transport/TFramedTransport.php
@@ -89,7 +89,7 @@
       return $this->transport_->read($len);
     }
 
-    if (empty($this->rBuf_)) {
+    if (strlen($this->rBuf_) === 0) {
       $this->readFrame();
     }
 
@@ -99,7 +99,7 @@
       $this->rBuf_ = null;
       return $out;
     }
-    
+
     // Return substr
     $out = substr($this->rBuf_, 0, $len);
     $this->rBuf_ = substr($this->rBuf_, $len);
@@ -112,10 +112,9 @@
    * @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 06cf9d8..d5d03c4 100644
--- a/lib/php/src/transport/THttpClient.php
+++ b/lib/php/src/transport/THttpClient.php
@@ -159,7 +159,7 @@
                      '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 f62bb57..c34fb67 100644
--- a/lib/php/src/transport/TMemoryBuffer.php
+++ b/lib/php/src/transport/TMemoryBuffer.php
@@ -45,7 +45,7 @@
   }
 
   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 03837a8..d4646f1 100644
--- a/lib/php/src/transport/TPhpStream.php
+++ b/lib/php/src/transport/TPhpStream.php
@@ -50,7 +50,7 @@
       }
     }
   }
-  
+
   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 cce3a44..e1bd3bd 100644
--- a/lib/php/src/transport/TSocket.php
+++ b/lib/php/src/transport/TSocket.php
@@ -28,7 +28,7 @@
 
   /**
    * Remote hostname
-   * 
+   *
    * @var string
    */
   protected $host_ = 'localhost';
@@ -128,6 +128,24 @@
   }
 
   /**
+   * 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
    *
    * @return bool true if the socket is open
@@ -140,6 +158,7 @@
    * Connects the socket.
    */
   public function open() {
+
     if ($this->persist_) {
       $this->handle_ = @pfsockopen($this->host_,
                                    $this->port_,
@@ -162,7 +181,7 @@
       }
       throw new TException($error);
     }
-    
+
     stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000);
     $this->sendTimeoutSet_ = TRUE;
   }
@@ -176,7 +195,7 @@
       $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 b3efb2d..bd28db6 100644
--- a/lib/php/src/transport/TSocketPool.php
+++ b/lib/php/src/transport/TSocketPool.php
@@ -104,6 +104,18 @@
   }
 
   /**
+   * 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.
    *
    * @param int $numRetries
@@ -206,7 +218,7 @@
         // 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 {