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 peer 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 if it is a resource
81 if ($isServerSocket === true) {
83 //* DEBUG: */ $this->debugOutput('socketResource[]=' . gettype($socketResource));
84 socket_clear_error($socketResource);
87 // Check peer name, it must be empty
88 $isServerSocket = (($isServerSocket) && (empty($peerName)));
91 return $isServerSocket;
95 * Setter for listen address
97 * @param $listenAddress The address this listener should listen on
100 protected final function setListenAddress ($listenAddress) {
101 $this->listenAddress = (string) $listenAddress;
105 * Getter for listen address
107 * @return $listenAddress The address this listener should listen on
109 public final function getListenAddress () {
110 return $this->listenAddress;
114 * Setter for listen port
116 * @param $listenPort The port this listener should listen on
119 protected final function setListenPort ($listenPort) {
120 $this->listenPort = (int) $listenPort;
124 * Getter for listen port
126 * @return $listenPort The port this listener should listen on
128 public final function getListenPort () {
129 return $this->listenPort;
133 * Getter for port number to satify ProtocolHandler
135 * @return $port The port number
137 public final function getPort () {
138 return $this->getListenPort();
142 * "Setter" to set listen address from configuration entry
144 * @param $configEntry The configuration entry holding our listen address
147 public final function setListenAddressByConfiguration ($configEntry) {
148 $this->setListenAddress($this->getConfigInstance()->getConfigEntry($configEntry));
152 * "Setter" to set listen port from configuration entry
154 * @param $configEntry The configuration entry holding our listen port
157 public final function setListenPortByConfiguration ($configEntry) {
158 $this->setListenPort($this->getConfigInstance()->getConfigEntry($configEntry));
162 * Setter for protocol
164 * @param $protocol Used protocol
167 protected final function setProtocol ($protocol) {
168 $this->protocol = (string) $protocol;
172 * Getter for protocol
174 * @return $protocol Used protocol
176 public final function getProtocol () {
177 return $this->protocol;
181 * Setter for blocking-mode
183 * @param $blockingMode Wether blocking-mode is disabled (default) or enabled
186 protected final function setBlockingMode ($blockingMode) {
187 $this->blockingMode = (boolean) $blockingMode;
191 * Checks wether blocking-mode is enabled or disabled
193 * @return $blockingMode Wether blocking mode is disabled or enabled
195 public final function isBlockingModeEnabled () {
196 return $this->blockingMode;
200 * Setter for peer pool instance
202 * @param $poolInstance The peer pool instance we shall set
205 protected final function setPoolInstance (Poolablepeer $poolInstance) {
206 $this->poolInstance = $poolInstance;
210 * Getter for peer pool instance
212 * @return $poolInstance The peer pool instance we shall set
214 public final function getPoolInstance () {
215 return $this->poolInstance;
219 * Setter for network package handler instance
221 * @param $packageInstance The network package handler instance we shall set
224 protected final function setPackageInstance (Networkable $packageInstance) {
225 $this->packageInstance = $packageInstance;
229 * Getter for network package handler instance
231 * @return $packageInstance The network package handler instance we shall set
233 protected final function getPackageInstance () {
234 return $this->packageInstance;
238 * Registeres the given socket resource for "this" listener instance. This
239 * will be done in a seperate class to allow package writers to use it
242 * @param $socketResource A valid server socket resource
244 * @throws InvalidServerSocketException If the given resource is no server socket
245 * @throws SocketAlreadyRegisteredException If the given resource is already registered
247 protected function registerServerSocketResource ($socketResource) {
248 // First check if it is valid
249 if (!$this->isServerSocketResource($socketResource)) {
251 throw new InvalidServerSocketException(array($this, $socketResource), self::EXCEPTION_INVALID_SOCKET);
252 } elseif ($this->isServerSocketRegistered($socketResource)) {
253 // Already registered
254 throw new SocketAlreadyRegisteredException($this, self::EXCEPTION_SOCKET_ALREADY_REGISTERED);
257 // Get a socket registry instance (singleton)
258 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
260 // Register the socket
261 $registryInstance->registerSocket($this, $socketResource);
265 * Checks wether given socket resource is registered in socket registry
267 * @param $socketResource A valid server socket resource
268 * @return $isRegistered Wether given server socket is registered
270 protected function isServerSocketRegistered ($socketResource) {
271 // Get a socket registry instance (singleton)
272 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
275 $isRegistered = $registryInstance->isSocketRegistered($this, $socketResource);
278 return $isRegistered;
282 * Getter for "this" socket resource
284 * @return $socketResource A valid socket resource
286 public final function getSocketResource () {
287 // Get a socket registry instance (singleton)
288 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
290 // Get the socket resource
291 $socketResource = $registryInstance->getRegisteredSocketResource($this);
294 return $socketResource;
298 * Accepts the visitor to process the visit "request"
300 * @param $visitorInstance An instance of a Visitor class
303 public function accept (Visitor $visitorInstance) {
305 //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - START');
307 // Visit this listener
308 $visitorInstance->visitListener($this);
310 // Visit the pool if set
311 if ($this->getPoolInstance() instanceof Poolable) {
312 $this->getPoolInstance()->accept($visitorInstance);
316 //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - FINISHED');