3 * A general listener class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseListener extends BaseHubSystem implements Visitable {
25 // Exception code constants
26 const EXCEPTION_INVALID_SOCKET = 0xa00;
27 const EXCEPTION_SOCKET_ALREADY_REGISTERED = 0xa01;
30 * Used protocol (Default: invalid, which is indeed invalid...)
32 private $protcol = 'invalid';
35 * Address (IP mostly) we shall listen on
37 private $listenAddress = '0.0.0.0'; // This is the default and listens on all interfaces
40 * Port we shall listen on (or wait for incoming data)
42 private $listenPort = 0; // This port MUST be changed by your application
45 * Wether we are in blocking or non-blocking mode (default: non-blocking
47 private $blockingMode = false;
50 * A client pool instance
52 private $poolInstance = null;
55 * A network package handler instance
57 private $packageInstance = null;
60 * Protected constructor
62 * @param $className Name of the class
65 protected function __construct ($className) {
66 // Call parent constructor
67 parent::__construct($className);
71 * Checks wether the given socket resource is a server socket
73 * @param $socketResource A valid socket resource
74 * @return $isServerSocket Wether the socket resource is a server socket
76 protected function isServerSocketResource ($socketResource) {
78 $isServerSocket = ((is_resource($socketResource)) && (!@socket_getpeername($socketResource, $peerName)));
80 // We need to clear the error here
81 socket_clear_error($socketResource);
83 // Check peer name, it must be empty
84 $isServerSocket = (($isServerSocket) && (empty($peerName)));
87 return $isServerSocket;
91 * Setter for listen address
93 * @param $listenAddress The address this listener should listen on
96 protected final function setListenAddress ($listenAddress) {
97 $this->listenAddress = (string) $listenAddress;
101 * Getter for listen address
103 * @return $listenAddress The address this listener should listen on
105 public final function getListenAddress () {
106 return $this->listenAddress;
110 * Setter for listen port
112 * @param $listenPort The port this listener should listen on
115 protected final function setListenPort ($listenPort) {
116 $this->listenPort = (int) $listenPort;
120 * Getter for listen port
122 * @return $listenPort The port this listener should listen on
124 public final function getListenPort () {
125 return $this->listenPort;
129 * "Setter" to set listen address from configuration entry
131 * @param $configEntry The configuration entry holding our listen address
134 public final function setListenAddressByConfiguration ($configEntry) {
135 $this->setListenAddress($this->getConfigInstance()->getConfigEntry($configEntry));
139 * "Setter" to set listen port from configuration entry
141 * @param $configEntry The configuration entry holding our listen port
144 public final function setListenPortByConfiguration ($configEntry) {
145 $this->setListenPort($this->getConfigInstance()->getConfigEntry($configEntry));
149 * Setter for protocol
151 * @param $protocol Used protocol
154 protected final function setProtocol ($protocol) {
155 $this->protocol = (string) $protocol;
159 * Getter for protocol
161 * @return $protocol Used protocol
163 public final function getProtocol () {
164 return $this->protocol;
168 * Setter for blocking-mode
170 * @param $blockingMode Wether blocking-mode is disabled (default) or enabled
173 protected final function setBlockingMode ($blockingMode) {
174 $this->blockingMode = (boolean) $blockingMode;
178 * Checks wether blocking-mode is enabled or disabled
180 * @return $blockingMode Wether blocking mode is disabled or enabled
182 public final function isBlockingModeEnabled () {
183 return $this->blockingMode;
187 * Setter for client pool instance
189 * @param $poolInstance The client pool instance we shall set
192 protected final function setPoolInstance (PoolableClient $poolInstance) {
193 $this->poolInstance = $poolInstance;
197 * Getter for client pool instance
199 * @return $poolInstance The client pool instance we shall set
201 protected final function getPoolInstance () {
202 return $this->poolInstance;
206 * Setter for network package handler instance
208 * @param $packageInstance The network package handler instance we shall set
211 protected final function setPackageInstance (Networkable $packageInstance) {
212 $this->packageInstance = $packageInstance;
216 * Getter for network package handler instance
218 * @return $packageInstance The network package handler instance we shall set
220 protected final function getPackageInstance () {
221 return $this->packageInstance;
225 * Registeres the given socket resource for "this" listener instance. This
226 * will be done in a seperate class to allow package writers to use it
229 * @param $socketResource A valid server socket resource
231 * @throws InvalidServerSocketException If the given resource is no server socket
232 * @throws SocketAlreadyRegisteredException If the given resource is already registered
234 protected function registerServerSocketResource ($socketResource) {
235 // First check if it is valid
236 if (!$this->isServerSocketResource($socketResource)) {
238 throw new InvalidServerSocketException(array($this, $socketResource), self::EXCEPTION_INVALID_SOCKET);
239 } elseif ($this->isServerSocketRegistered($socketResource)) {
240 // Already registered
241 throw new SocketAlreadyRegisteredException($this, self::EXCEPTION_SOCKET_ALREADY_REGISTERED);
244 // Get a socket registry instance (singleton)
245 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
247 // Register the socket
248 $registryInstance->registerSocket($this, $socketResource);
252 * Checks wether given socket resource is registered in socket registry
254 * @param $socketResource A valid server socket resource
255 * @return $isRegistered Wether given server socket is registered
257 protected function isServerSocketRegistered ($socketResource) {
258 // Get a socket registry instance (singleton)
259 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
262 $isRegistered = $registryInstance->isSocketRegistered($this, $socketResource);
265 return $isRegistered;
269 * Getter for "this" socket resource
271 * @return $socketResource A valid socket resource
273 public final function getSocketResource () {
274 // Get a socket registry instance (singleton)
275 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
277 // Get the socket resource
278 $socketResource = $registryInstance->getSocketResource($this);
281 return $socketResource;
285 * Accepts the visitor to process the visit "request"
287 * @param $visitorInstance An instance of a Visitor class
290 public function accept (Visitor $visitorInstance) {
292 //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - START');
294 // Visit this listener
295 $visitorInstance->visitListener($this);
297 // Visit the pool if set
298 if ($this->getPoolInstance() instanceof Poolable) {
299 $this->getPoolInstance()->accept($visitorInstance);
303 //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - FINISHED');