3 * A general listener class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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;
28 const EXCEPTION_SOCKET_CREATION_FAILED = 0xa02;
29 const EXCEPTION_NO_SOCKET_ERROR = 0xa03;
30 const EXCEPTION_CONNECTION_ALREADY_REGISTERED = 0xa04;
31 const EXCEPTION_UNEXPECTED_PACKAGE_STATUS = 0xa05;
32 const EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER = 0xa06;
33 const EXCEPTION_FINAL_CHUNK_VERIFICATION = 0xa07;
34 const EXCEPTION_INVALID_DATA_CHECKSUM = 0xa08;
37 * Address (IP mostly) we shall listen on
39 private $listenAddress = '0.0.0.0'; // This is the default and listens on all interfaces
42 * Port we shall listen on (or wait for incoming data)
44 private $listenPort = 0; // This port MUST be changed by your application
47 * Whether we are in blocking or non-blocking mode (default: non-blocking
49 private $blockingMode = FALSE;
52 * A peer pool instance
54 private $poolInstance = NULL;
57 * Protected constructor
59 * @param $className Name of the class
62 protected function __construct ($className) {
63 // Call parent constructor
64 parent::__construct($className);
68 * Checks whether the given socket resource is a server socket
70 * @param $socketResource A valid socket resource
71 * @return $isServerSocket Whether the socket resource is a server socket
73 protected function isServerSocketResource ($socketResource) {
75 $isServerSocket = ((is_resource($socketResource)) && (!@socket_getpeername($socketResource, $peerName)));
77 // We need to clear the error here if it is a resource
78 if ($isServerSocket === TRUE) {
80 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('socketResource[]=' . gettype($socketResource));
81 socket_clear_error($socketResource);
84 // Check peer name, it must be empty
85 $isServerSocket = (($isServerSocket) && (empty($peerName)));
88 return $isServerSocket;
92 * Setter for listen address
94 * @param $listenAddress The address this listener should listen on
97 protected final function setListenAddress ($listenAddress) {
98 $this->listenAddress = (string) $listenAddress;
102 * Getter for listen address
104 * @return $listenAddress The address this listener should listen on
106 public final function getListenAddress () {
107 return $this->listenAddress;
111 * Setter for listen port
113 * @param $listenPort The port this listener should listen on
116 protected final function setListenPort ($listenPort) {
117 $this->listenPort = (int) $listenPort;
121 * Getter for listen port
123 * @return $listenPort The port this listener should listen on
125 public final function getListenPort () {
126 return $this->listenPort;
130 * Getter for port number to satify ProtocolHandler
132 * @return $port The port number
134 public final function getPort () {
135 return $this->getListenPort();
139 * "Setter" to set listen address from configuration entry
141 * @param $configEntry The configuration entry holding our listen address
144 public final function setListenAddressByConfiguration ($configEntry) {
145 $this->setListenAddress($this->getConfigInstance()->getConfigEntry($configEntry));
149 * "Setter" to set listen port from configuration entry
151 * @param $configEntry The configuration entry holding our listen port
154 public final function setListenPortByConfiguration ($configEntry) {
155 $this->setListenPort($this->getConfigInstance()->getConfigEntry($configEntry));
159 * Setter for blocking-mode
161 * @param $blockingMode Whether blocking-mode is disabled (default) or enabled
164 protected final function setBlockingMode ($blockingMode) {
165 $this->blockingMode = (boolean) $blockingMode;
169 * Checks whether blocking-mode is enabled or disabled
171 * @return $blockingMode Whether blocking mode is disabled or enabled
173 public final function isBlockingModeEnabled () {
174 return $this->blockingMode;
178 * Setter for peer pool instance
180 * @param $poolInstance The peer pool instance we shall set
183 protected final function setPoolInstance (PoolablePeer $poolInstance) {
184 $this->poolInstance = $poolInstance;
188 * Getter for peer pool instance
190 * @return $poolInstance The peer pool instance we shall set
192 public final function getPoolInstance () {
193 return $this->poolInstance;
197 * Registeres the given socket resource for "this" listener instance. This
198 * will be done in a seperate class to allow package writers to use it
201 * @param $socketResource A valid server socket resource
203 * @throws InvalidServerSocketException If the given resource is no server socket
204 * @throws SocketAlreadyRegisteredException If the given resource is already registered
206 protected function registerServerSocketResource ($socketResource) {
207 // First check if it is valid
208 if (!$this->isServerSocketResource($socketResource)) {
210 throw new InvalidServerSocketException(array($this, $socketResource), self::EXCEPTION_INVALID_SOCKET);
211 } elseif ($this->isServerSocketRegistered($socketResource)) {
212 // Already registered
213 throw new SocketAlreadyRegisteredException($this, self::EXCEPTION_SOCKET_ALREADY_REGISTERED);
216 // Get a socket registry instance (singleton)
217 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
219 // Register the socket
220 $registryInstance->registerSocket($this, $socketResource);
223 $this->setSocketResource($socketResource);
227 * Checks whether given socket resource is registered in socket registry
229 * @param $socketResource A valid server socket resource
230 * @return $isRegistered Whether given server socket is registered
232 protected function isServerSocketRegistered ($socketResource) {
233 // Get a socket registry instance (singleton)
234 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
237 $isRegistered = $registryInstance->isSocketRegistered($this, $socketResource);
240 return $isRegistered;
244 * Accepts the visitor to process the visit "request"
246 * @param $visitorInstance An instance of a Visitor class
249 public function accept (Visitor $visitorInstance) {
251 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - START');
253 // Visit this listener
254 $visitorInstance->visitListener($this);
256 // Visit the pool if set
257 if ($this->getPoolInstance() instanceof Poolable) {
258 $this->getPoolInstance()->accept($visitorInstance);
262 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - FINISHED');
266 * Monitors incoming raw data from the handler and transfers it to the
267 * given receiver instance. This method should not be called, please call
268 * the decorator's version instead to separator node/client traffic.
270 * @param $receiverInstance An instance of a Receivable class
272 * @throws UnsupportedOperatorException If this method is called by a mistake
274 public function monitorIncomingRawData (Receivable $receiverInstance) {
275 throw new UnsupportedOperationException(array($this, __FUNCTION__, $receiverInstance), self::EXCEPTION_UNSPPORTED_OPERATION);