TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
Abstract.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: Abstract.php 8/31/2012 11:06:09 scp@orilla $
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @package TeamSpeak3
23  * @version 1.1.16
24  * @author Sven 'ScP' Paulsen
25  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
26  */
27 
28 /**
29  * @class TeamSpeak3_Transport_Abstract
30  * @brief Abstract class for connecting to a TeamSpeak 3 Server through different ways of transport.
31  */
33 {
34  /**
35  * Stores user-provided configuration settings.
36  *
37  * @var array
38  */
39  protected $config = null;
40 
41  /**
42  * Stores the stream resource of the connection.
43  *
44  * @var resource
45  */
46  protected $stream = null;
47 
48  /**
49  * Stores the TeamSpeak3_Adapter_Abstract object using this transport.
50  *
51  * @var TeamSpeak3_Adapter_Abstract
52  */
53  protected $adapter = null;
54 
55  /**
56  * The TeamSpeak3_Transport_Abstract constructor.
57  *
58  * @param array $config
59  * @throws TeamSpeak3_Transport_Exception
60  * @return TeamSpeak3_Transport_Abstract
61  */
62  public function __construct(array $config)
63  {
64  if(!array_key_exists("host", $config))
65  {
66  throw new TeamSpeak3_Transport_Exception("config must have a key for 'host' which specifies the server host name");
67  }
68 
69  if(!array_key_exists("port", $config))
70  {
71  throw new TeamSpeak3_Transport_Exception("config must have a key for 'port' which specifies the server port number");
72  }
73 
74  if(!array_key_exists("timeout", $config))
75  {
76  $config["timeout"] = 10;
77  }
78 
79  if(!array_key_exists("blocking", $config))
80  {
81  $config["blocking"] = 1;
82  }
83 
84  $this->config = $config;
85  }
86 
87  /**
88  * Commit pending data.
89  *
90  * @return array
91  */
92  public function __sleep()
93  {
94  return array("config");
95  }
96 
97  /**
98  * Reconnects to the remote server.
99  *
100  * @return void
101  */
102  public function __wakeup()
103  {
104  $this->connect();
105  }
106 
107  /**
108  * The TeamSpeak3_Transport_Abstract destructor.
109  *
110  * @return void
111  */
112  public function __destruct()
113  {
114  if($this->adapter instanceof TeamSpeak3_Adapter_Abstract)
115  {
116  $this->adapter->__destruct();
117  }
118 
119  $this->disconnect();
120  }
121 
122  /**
123  * Connects to a remote server.
124  *
125  * @throws TeamSpeak3_Transport_Exception
126  * @return void
127  */
128  abstract public function connect();
129 
130  /**
131  * Disconnects from a remote server.
132  *
133  * @return void
134  */
135  abstract public function disconnect();
136 
137  /**
138  * Reads data from the stream.
139  *
140  * @param integer $length
141  * @throws TeamSpeak3_Transport_Exception
142  * @return TeamSpeak3_Helper_String
143  */
144  abstract public function read($length = 4096);
145 
146  /**
147  * Writes data to the stream.
148  *
149  * @param string $data
150  * @return void
151  */
152  abstract public function send($data);
153 
154  /**
155  * Returns the underlying stream resource.
156  *
157  * @return resource
158  */
159  public function getStream()
160  {
161  return $this->stream;
162  }
163 
164  /**
165  * Returns the configuration variables in this adapter.
166  *
167  * @param string $key
168  * @param mixed $default
169  * @return array
170  */
171  public function getConfig($key = null, $default = null)
172  {
173  if($key !== null)
174  {
175  return array_key_exists($key, $this->config) ? $this->config[$key] : $default;
176  }
177 
178  return $this->config;
179  }
180 
181  /**
182  * Sets the TeamSpeak3_Adapter_Abstract object using this transport.
183  *
184  * @param TeamSpeak3_Adapter_Abstract $adapter
185  * @return void
186  */
187  public function setAdapter(TeamSpeak3_Adapter_Abstract $adapter)
188  {
189  $this->adapter = $adapter;
190  }
191 
192  /**
193  * Returns the TeamSpeak3_Adapter_Abstract object using this transport.
194  *
195  * @return TeamSpeak3_Adapter_Abstract
196  */
197  public function getAdapter()
198  {
199  return $this->adapter;
200  }
201 
202  /**
203  * Returns the adapter type.
204  *
205  * @return string
206  */
207  public function getAdapterType()
208  {
209  if($this->adapter instanceof TeamSpeak3_Adapter_Abstract)
210  {
211  $string = TeamSpeak3_Helper_String::factory(get_class($this->adapter));
212 
213  return $string->substr($string->findLast("_"))->replace(array("_", " "), "")->toString();
214  }
215 
216  return "Unknown";
217  }
218 
219  /**
220  * Returns header/meta data from stream pointer.
221  *
222  * @throws TeamSpeak3_Transport_Exception
223  * @return array
224  */
225  public function getMetaData()
226  {
227  if($this->stream === null)
228  {
229  throw new TeamSpeak3_Transport_Exception("unable to retrieve header/meta data from stream pointer");
230  }
231 
232  return stream_get_meta_data($this->stream);
233  }
234 
235  /**
236  * Returns TRUE if the transport is connected.
237  *
238  * @return boolean
239  */
240  public function isConnected()
241  {
242  return (is_resource($this->stream)) ? TRUE : FALSE;
243  }
244 
245  /**
246  * Blocks a stream until data is available for reading if the stream is connected
247  * in non-blocking mode.
248  *
249  * @param integer $time
250  * @return void
251  */
252  protected function waitForReadyRead($time = 0)
253  {
254  if(!$this->isConnected() || $this->config["blocking"]) return;
255 
256  do {
257  $read = array($this->stream);
258  $null = null;
259 
260  if($time)
261  {
262  TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "WaitTimeout", $time, $this->getAdapter());
263  }
264 
265  $time = $time+$this->config["timeout"];
266  } while(@stream_select($read, $null, $null, $this->config["timeout"]) == 0);
267  }
268 }