*
* @author Roland Haeder <webmaster@ship-simu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.ship-simu.org
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-class BaseListener extends BaseHubSystem {
+class BaseListener extends BaseHubSystem implements Visitable {
// Exception code constants
- const EXCEPTION_INVALID_SOCKET = 0xa00;
+ const EXCEPTION_INVALID_SOCKET = 0xa00;
+ const EXCEPTION_SOCKET_ALREADY_REGISTERED = 0xa01;
+ const EXCEPTION_SOCKET_CREATION_FAILED = 0xa02;
+ const EXCEPTION_NO_SOCKET_ERROR = 0xa03;
+ const EXCEPTION_CONNECTION_ALREADY_REGISTERED = 0xa04;
+ const EXCEPTION_UNEXPECTED_PACKAGE_STATUS = 0xa05;
+ const EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER = 0xa06;
+ const EXCEPTION_FINAL_CHUNK_VERIFICATION = 0xa07;
+ const EXCEPTION_INVALID_DATA_CHECKSUM = 0xa08;
/**
* Used protocol (Default: invalid, which is indeed invalid...)
*/
- private $protcol = 'invalid';
+ private $protocol = 'invalid';
/**
* Address (IP mostly) we shall listen on
private $listenPort = 0; // This port MUST be changed by your application
/**
- * Wether we are in blocking or non-blocking mode (default: non-blocking
+ * Whether we are in blocking or non-blocking mode (default: non-blocking
*/
private $blockingMode = false;
/**
- * Socket resource
+ * A peer pool instance
*/
- private $socketResource = false;
-
- /**
- * A client pool instance
- */
- private $poolInstance = null;
+ private $poolInstance = NULL;
/**
* Protected constructor
parent::__construct($className);
}
+ /**
+ * Checks whether the given socket resource is a server socket
+ *
+ * @param $socketResource A valid socket resource
+ * @return $isServerSocket Whether the socket resource is a server socket
+ */
+ protected function isServerSocketResource ($socketResource) {
+ // Check it
+ $isServerSocket = ((is_resource($socketResource)) && (!@socket_getpeername($socketResource, $peerName)));
+
+ // We need to clear the error here if it is a resource
+ if ($isServerSocket === true) {
+ // Clear the error
+ //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('socketResource[]=' . gettype($socketResource));
+ socket_clear_error($socketResource);
+ } // END - if
+
+ // Check peer name, it must be empty
+ $isServerSocket = (($isServerSocket) && (empty($peerName)));
+
+ // Return result
+ return $isServerSocket;
+ }
+
/**
* Setter for listen address
*
return $this->listenPort;
}
+ /**
+ * Getter for port number to satify ProtocolHandler
+ *
+ * @return $port The port number
+ */
+ public final function getPort () {
+ return $this->getListenPort();
+ }
+
/**
* "Setter" to set listen address from configuration entry
*
* @return void
*/
public final function setListenAddressByConfiguration ($configEntry) {
- $this->setListenAddress($this->getConfigInstance()->readConfig($configEntry));
+ $this->setListenAddress($this->getConfigInstance()->getConfigEntry($configEntry));
}
/**
* @return void
*/
public final function setListenPortByConfiguration ($configEntry) {
- $this->setListenPort($this->getConfigInstance()->readConfig($configEntry));
+ $this->setListenPort($this->getConfigInstance()->getConfigEntry($configEntry));
}
/**
/**
* Setter for blocking-mode
*
- * @param $blockingMode Wether blocking-mode is disabled (default) or enabled
+ * @param $blockingMode Whether blocking-mode is disabled (default) or enabled
* @return void
*/
protected final function setBlockingMode ($blockingMode) {
}
/**
- * Checks wether blocking-mode is enabled or disabled
+ * Checks whether blocking-mode is enabled or disabled
*
- * @return $blockingMode Wether blocking mode is disabled or enabled
+ * @return $blockingMode Whether blocking mode is disabled or enabled
*/
public final function isBlockingModeEnabled () {
return $this->blockingMode;
}
/**
- * Setter for socket resource
+ * Setter for peer pool instance
+ *
+ * @param $poolInstance The peer pool instance we shall set
+ * @return void
+ */
+ protected final function setPoolInstance (PoolablePeer $poolInstance) {
+ $this->poolInstance = $poolInstance;
+ }
+
+ /**
+ * Getter for peer pool instance
+ *
+ * @return $poolInstance The peer pool instance we shall set
+ */
+ public final function getPoolInstance () {
+ return $this->poolInstance;
+ }
+
+ /**
+ * Registeres the given socket resource for "this" listener instance. This
+ * will be done in a seperate class to allow package writers to use it
+ * again.
*
- * @param $socketResource The socket resource we shall set
+ * @param $socketResource A valid server socket resource
* @return void
+ * @throws InvalidServerSocketException If the given resource is no server socket
+ * @throws SocketAlreadyRegisteredException If the given resource is already registered
*/
- protected final function setSocketResource ($socketResource) {
- $this->socketResource = $setSocketResource;
+ protected function registerServerSocketResource ($socketResource) {
+ // First check if it is valid
+ if (!$this->isServerSocketResource($socketResource)) {
+ // No server socket
+ throw new InvalidServerSocketException(array($this, $socketResource), self::EXCEPTION_INVALID_SOCKET);
+ } elseif ($this->isServerSocketRegistered($socketResource)) {
+ // Already registered
+ throw new SocketAlreadyRegisteredException($this, self::EXCEPTION_SOCKET_ALREADY_REGISTERED);
+ }
+
+ // Get a socket registry instance (singleton)
+ $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
+
+ // Register the socket
+ $registryInstance->registerSocket($this, $socketResource);
+
+ // And set it here
+ $this->setSocketResource($socketResource);
}
/**
- * Getter for socket resource
+ * Checks whether given socket resource is registered in socket registry
*
- * @return $socketResource The socket resource we shall set
+ * @param $socketResource A valid server socket resource
+ * @return $isRegistered Whether given server socket is registered
*/
- protected final function getSocketResource () {
- return $this->socketResource;
+ protected function isServerSocketRegistered ($socketResource) {
+ // Get a socket registry instance (singleton)
+ $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
+
+ // Check it
+ $isRegistered = $registryInstance->isSocketRegistered($this, $socketResource);
+
+ // Return result
+ return $isRegistered;
}
/**
- * Setter for client pool instance
+ * Accepts the visitor to process the visit "request"
*
- * @param $poolInstance The client pool instance we shall set
+ * @param $visitorInstance An instance of a Visitor class
* @return void
*/
- protected final function setPoolInstance (PoolableClient $poolInstance) {
- $this->poolInstance = $setPoolInstance;
+ public function accept (Visitor $visitorInstance) {
+ // Debug message
+ //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($this->getProtocol()) . '-LISTENER[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - START');
+
+ // Visit this listener
+ $visitorInstance->visitListener($this);
+
+ // Visit the pool if set
+ if ($this->getPoolInstance() instanceof Poolable) {
+ $this->getPoolInstance()->accept($visitorInstance);
+ } // END - if
+
+ // Debug message
+ //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($this->getProtocol()) . '-LISTENER[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - FINISHED');
}
/**
- * Getter for client pool instance
+ * Monitors incoming raw data from the handler and transfers it to the
+ * given receiver instance. This method should not be called, please call
+ * the decorator's version instead to separator node/client traffic.
*
- * @return $poolInstance The client pool instance we shall set
+ * @param $receiverInstance An instance of a Receivable class
+ * @return void
+ * @throws UnsupportedOperatorException If this method is called by a mistake
*/
- protected final function getPoolInstance () {
- return $this->poolInstance;
+ public function monitorIncomingRawData (Receivable $receiverInstance) {
+ throw new UnsupportedOperationException(array($this, __FUNCTION__, $receiverInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
}
}