TeamSpeak 3 PHP Framework  1.1.12
libraries/TeamSpeak3/Transport/Abstract.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * @file
00005  * TeamSpeak 3 PHP Framework
00006  *
00007  * $Id: Abstract.php 2/18/2012 12:42:46 scp@orilla $
00008  *
00009  * This program is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation, either version 3 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00021  *
00022  * @package   TeamSpeak3
00023  * @version   1.1.12
00024  * @author    Sven 'ScP' Paulsen
00025  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
00026  */
00027 
00028 /**
00029  * @class TeamSpeak3_Transport_Abstract
00030  * @brief Abstract class for connecting to a TeamSpeak 3 Server through different ways of transport.
00031  */
00032 abstract class TeamSpeak3_Transport_Abstract
00033 {
00034   /**
00035    * Stores user-provided configuration settings.
00036    *
00037    * @var array
00038    */
00039   protected $config = null;
00040 
00041   /**
00042    * Stores the stream resource of the connection.
00043    *
00044    * @var resource
00045    */
00046   protected $stream = null;
00047 
00048   /**
00049    * Stores the TeamSpeak3_Adapter_Abstract object using this transport.
00050    *
00051    * @var TeamSpeak3_Adapter_Abstract
00052    */
00053   protected $adapter = null;
00054 
00055   /**
00056    * The TeamSpeak3_Transport_Abstract constructor.
00057    *
00058    * @param  array $config
00059    * @throws TeamSpeak3_Transport_Exception
00060    * @return TeamSpeak3_Transport_Abstract
00061    */
00062   public function __construct(array $config)
00063   {
00064     if(!array_key_exists("host", $config))
00065     {
00066       throw new TeamSpeak3_Transport_Exception("config must have a key for 'host' which specifies the server host name");
00067     }
00068 
00069     if(!array_key_exists("port", $config))
00070     {
00071       throw new TeamSpeak3_Transport_Exception("config must have a key for 'port' which specifies the server port number");
00072     }
00073 
00074     if(!array_key_exists("timeout", $config))
00075     {
00076       $config["timeout"] = 10;
00077     }
00078 
00079     if(!array_key_exists("blocking", $config))
00080     {
00081       $config["blocking"] = 1;
00082     }
00083 
00084     $this->config = $config;
00085   }
00086 
00087   /**
00088    * Commit pending data.
00089    *
00090    * @return array
00091    */
00092   public function __sleep()
00093   {
00094     return array("config");
00095   }
00096 
00097   /**
00098    * Reconnects to the remote server.
00099    *
00100    * @return void
00101    */
00102   public function __wakeup()
00103   {
00104     $this->connect();
00105   }
00106 
00107   /**
00108    * The TeamSpeak3_Transport_Abstract destructor.
00109    *
00110    * @return void
00111    */
00112   public function __destruct()
00113   {
00114     if($this->adapter instanceof TeamSpeak3_Adapter_Abstract)
00115     {
00116       $this->adapter->__destruct();
00117     }
00118 
00119     $this->disconnect();
00120   }
00121 
00122   /**
00123    * Connects to a remote server.
00124    *
00125    * @throws TeamSpeak3_Transport_Exception
00126    * @return void
00127    */
00128   abstract public function connect();
00129 
00130   /**
00131    * Disconnects from a remote server.
00132    *
00133    * @return void
00134    */
00135   abstract public function disconnect();
00136 
00137   /**
00138    * Reads data from the stream.
00139    *
00140    * @param  integer $length
00141    * @throws TeamSpeak3_Transport_Exception
00142    * @return TeamSpeak3_Helper_String
00143    */
00144   abstract public function read($length = 4096);
00145 
00146   /**
00147    * Writes data to the stream.
00148    *
00149    * @param  string $data
00150    * @return void
00151    */
00152   abstract public function send($data);
00153 
00154   /**
00155    * Returns the underlying stream resource.
00156    *
00157    * @return resource
00158    */
00159   public function getStream()
00160   {
00161     return $this->stream;
00162   }
00163 
00164   /**
00165    * Returns the configuration variables in this adapter.
00166    *
00167    * @param  string $key
00168    * @param  mixed  $default
00169    * @return array
00170    */
00171   public function getConfig($key = null, $default = null)
00172   {
00173     if($key !== null)
00174     {
00175       return array_key_exists($key, $this->config) ? $this->config[$key] : $default;
00176     }
00177 
00178     return $this->config;
00179   }
00180 
00181   /**
00182    * Sets the TeamSpeak3_Adapter_Abstract object using this transport.
00183    *
00184    * @param  TeamSpeak3_Adapter_Abstract $adapter
00185    * @return void
00186    */
00187   public function setAdapter(TeamSpeak3_Adapter_Abstract $adapter)
00188   {
00189     $this->adapter = $adapter;
00190   }
00191 
00192   /**
00193    * Returns the TeamSpeak3_Adapter_Abstract object using this transport.
00194    *
00195    * @return TeamSpeak3_Adapter_Abstract
00196    */
00197   public function getAdapter()
00198   {
00199     return $this->adapter;
00200   }
00201 
00202   /**
00203    * Returns the adapter type.
00204    *
00205    * @return string
00206    */
00207   public function getAdapterType()
00208   {
00209     if($this->adapter instanceof TeamSpeak3_Adapter_Abstract)
00210     {
00211       $string = TeamSpeak3_Helper_String::factory(get_class($this->adapter));
00212 
00213       return $string->substr($string->findLast("_"))->replace(array("_", " "), "")->toString();
00214     }
00215 
00216     return "Unknown";
00217   }
00218 
00219   /**
00220    * Returns header/meta data from stream pointer.
00221    *
00222    * @throws TeamSpeak3_Transport_Exception
00223    * @return array
00224    */
00225   public function getMetaData()
00226   {
00227     if($this->stream === null)
00228     {
00229       throw new TeamSpeak3_Transport_Exception("unable to retrieve header/meta data from stream pointer");
00230     }
00231 
00232     return stream_get_meta_data($this->stream);
00233   }
00234 
00235   /**
00236    * Returns TRUE if the transport is connected.
00237    *
00238    * @return boolean
00239    */
00240   public function isConnected()
00241   {
00242     return (is_resource($this->stream)) ? TRUE : FALSE;
00243   }
00244 
00245   /**
00246    * Blocks a stream until data is available for reading if the stream is connected
00247    * in non-blocking mode.
00248    *
00249    * @param  integer $time
00250    * @return void
00251    */
00252   protected function waitForReadyRead($time = 0)
00253   {
00254     if(!$this->isConnected() || $this->config["blocking"]) return;
00255 
00256     do {
00257       $read = array($this->stream);
00258       $null = null;
00259 
00260       if($time)
00261       {
00262         TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "WaitTimeout", $time, $this->getAdapter());
00263       }
00264 
00265       $time = $time+$this->config["timeout"];
00266     } while(@stream_select($read, $null, $null, $this->config["timeout"]) == 0);
00267   }
00268 }
 All Classes Files Functions Variables