- handleSocketError() and all it's brothers and sisters (means call-back methods)
are now residing in SocketContainer
- still more rewrites for existing old parts
- renamed factory method to createListenFileSocket() as this creates a
listening file socket (mostly for IPC)
Signed-off-by: Roland Häder <roland@mxchange.org>
namespace Hub\Generic;
// Import application-specific stuff
-use Hub\Container\Socket\StorableSocket;
use Hub\Handler\Protocol\HandleableProtocol;
use Hub\Handler\RawData\BaseRawDataHandler;
use Hub\Information\ShareableInfo;
use CoreFramework\Listener\Listenable;
use CoreFramework\Object\BaseFrameworkSystem;
use CoreFramework\Socket\InvalidSocketException;
-use CoreFramework\Socket\NoSocketErrorDetectedException;
/**
* A general hub system class
return $isset;
}
- /**
- * Handles socket error for given socket resource and peer data. This method
- * validates socket resource stored in given container if it is a valid
- * resource (see is_resource()) but assumes valid data in array
- * $recipientData, except that count($recipientData) is always 2.
- *
- * @param $method Value of __METHOD__ from calling method
- * @param $line Value of __LINE__ from calling method
- * @param $socketInstance An instance of a StoreableSocket class
- * @param $socketData A valid socket data array (0 = IP/file name, 1 = port)
- * @return void
- * @throws InvalidSocketException If the stored socket resource is no socket resource
- * @throws NoSocketErrorDetectedException If socket_last_error() gives zero back
- * @todo Move all this socket-related stuff into own class, most of it resides in BaseListener
- */
- public final function handleSocketError ($method, $line, StorableSocket $socketInstance, array $socketData) {
- // Trace message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: Handling socket errorCode=%d - CALLED!', $socketInstance->getLastSocketError()));
-
- // This method handles only socket resources
- if (!$socketInstance->isValidSocket()) {
- // No resource, abort here
- throw new InvalidSocketException(array($this, $socketInstance->getSocketResource()), BaseListener::EXCEPTION_INVALID_SOCKET);
- } // END - if
-
- // Check socket array, 1st element is mostly IP address (or file name), 2nd is port number
- //* DEBUG-DIE: */ die(__METHOD__ . ':socketData=' . print_r($socketData, true));
- assert(isset($socketData[0]));
- assert(isset($socketData[1]));
-
- // Get error code for first validation (0 is not an error)
- $errorCode = $socketInstance->getLastSocketError();
-
- // If the error code is zero, someone called this method without an error
- if ($errorCode == 0) {
- // No error detected (or previously cleared outside this method)
- throw new NoSocketErrorDetectedException(array($this, $socketInstance->getSocketResource()), BaseListener::EXCEPTION_NO_SOCKET_ERROR);
- } // END - if
-
- // Get handler (method) name
- $handlerName = $this->getSocketErrorHandlerFromCode($errorCode);
-
- // Call-back the error handler method
- call_user_func_array(array($this, $handlerName), array($socketInstance, $socketData));
-
- // Finally clear the error because it has been handled
- $socketInstance->clearLastSocketError();
- }
-
/**
* Setter for network package handler instance
*
use CoreFramework\Listener\Listenable;
use CoreFramework\Registry\Registerable;
use CoreFramework\Socket\InvalidSocketException;
+use CoreFramework\Socket\NoSocketErrorDetectedException;
use CoreFramework\Visitor\Visitable;
use CoreFramework\Visitor\Visitor;
* Getter for last socket error
*
* @return $lastSocketError Last socket error
+ * @throws InvalidSocketException If socket is invalid
*/
public function getLastSocketError () {
// Should be valid socket
/**
* Tries to bind the socket.
*
+ * @param $bindAddress Where to bind the socket to (e.g. Uni* socket file)
+ * @param $bindPort Optional port to bind to
* @return $result Result from binding socket
+ * @throws InvalidSocketException If socket is invalid
*/
- public function bindSocketTo () {
- $this->partialStub('Unfinished method.');
+ public function bindSocketTo ($bindAddress, $bindPort = 0) {
+ // Should be valid socket
+ if (!$this->isValidSocket()) {
+ // Throw exception
+ throw new InvalidSocketException(array($this, $this->getSocketResource()), BaseListener::EXCEPTION_INVALID_SOCKET);
+ } // END - if
+
+ // Try to bind it to
+ $result = socket_bind($this->getSocketResource(), $bindAddress, $bindPort);
+
+ // Return result
+ return $result;
+ }
+
+ /**
+ * Tries to listen on the socket
+ *
+ * @return $result Result from listening on socket
+ * @throws InvalidSocketException If socket is valid
+ */
+ public function listenOnSocket () {
+ // Should be valid socket
+ if (!$this->isValidSocket()) {
+ // Throw exception
+ throw new InvalidSocketException(array($this, $this->getSocketResource()), BaseListener::EXCEPTION_INVALID_SOCKET);
+ } // END - if
+
+ // Try to listen on socket
+ $result = socket_listen($this->getSocketResource());
+
+ // Return result
+ return $result;
+ }
+
+ /**
+ * Handles socket error for given socket resource and peer data. This method
+ * validates socket resource stored in given container if it is a valid
+ * resource (see is_resource()) but assumes valid data in array
+ * $recipientData, except that count($recipientData) is always 2.
+ *
+ * @param $method Value of __METHOD__ from calling method
+ * @param $line Value of __LINE__ from calling method
+ * @param $socketData A valid socket data array (0 = IP/file name, 1 = port)
+ * @return void
+ * @throws InvalidSocketException If the stored socket resource is no socket resource
+ * @throws NoSocketErrorDetectedException If socket_last_error() gives zero back
+ * @todo Move all this socket-related stuff into own class, most of it resides in BaseListener
+ */
+ public final function handleSocketError ($method, $line, array $socketData) {
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: Handling socket errorCode=%d - CALLED!', $this->getLastSocketError()));
+
+ // This method handles only socket resources
+ if (!$this->isValidSocket()) {
+ // No resource, abort here
+ throw new InvalidSocketException(array($this, $this->getSocketResource()), BaseListener::EXCEPTION_INVALID_SOCKET);
+ } // END - if
+
+ // Check socket array, 1st element is mostly IP address (or file name), 2nd is port number
+ //* DEBUG-DIE: */ die(__METHOD__ . ':socketData=' . print_r($socketData, true));
+ assert(isset($socketData[0]));
+ assert(isset($socketData[1]));
+
+ // Get error code for first validation (0 is not an error)
+ $errorCode = $this->getLastSocketError();
+
+ // If the error code is zero, someone called this method without an error
+ if ($errorCode == 0) {
+ // No error detected (or previously cleared outside this method)
+ throw new NoSocketErrorDetectedException(array($this, $this->getSocketResource()), BaseListener::EXCEPTION_NO_SOCKET_ERROR);
+ } // END - if
+
+ // Get handler (method) name
+ $handlerName = $this->getSocketErrorHandlerFromCode($errorCode);
+
+ // Call-back the error handler method
+ call_user_func_array(array($this, $handlerName), array($this, $socketData));
+
+ // Finally clear the error because it has been handled
+ $this->clearLastSocketError();
}
}
}
/**
- * Creates a Uni*-socket container instance from given listener
+ * Creates a listening Uni*-socket container instance from given listener
*
* @param $listenerInstance An instance of a Listenable class
* @return $socketInstance An instance of a StorableSocket class
* @throws InvalidSocketException If the socket cannot be completed
*/
- public static final function createFileSocket (Listenable $listenerInstance) {
+ public static final function createListenFileSocket (Listenable $listenerInstance) {
// Create file name
$socketFile = self::createTempPathForFile($listenerInstance->getConfigInstance()->getConfigEntry('ipc_socket_file_name'));
// Check if there was an error else
if ($socketError > 0) {
// Handle this socket error with a faked recipientData array
- $listenerInstance->handleSocketError(__METHOD__, __LINE__, $socketInstance, array('null', '0'));
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array('null', '0'));
} // END - if
// Is the file there?
// Try to bind to it
if (!$socketInstance->bindSocketTo($packageData[0])) {
// Handle error here
- $listenerInstance->handleSocketError(__METHOD__, __LINE__, $socketInstance, $packageData);
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, $packageData);
} // END - if
// Start listen for connections
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Listening for connections.');
if (!$socketInstance->listenOnSocket()) {
// Handle this socket error with a faked recipientData array
- $listenerInstance->handleSocketError(__METHOD__, __LINE__, $socketInstance, $packageData);
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, $packageData);
} // END - if
// Allow non-blocking I/O
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
// Handle this socket error with a faked recipientData array
- $listenerInstance->handleSocketError(__METHOD__, __LINE__, $socketInstance, $packageData);
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, $packageData);
} // END - if
// Return socket instance
* Initializes the current connection
*
* @return void
- * @throws SocketOptionException If setting any socket option fails
*/
protected function initConnection () {
// Set the option to reuse the port
if (!$this->getSocketInstance()->enableReuseAddress()) {
// Handle this socket error with a faked recipientData array
- $this->handleSocketError(__METHOD__, __LINE__, $this->getSocketInstance(), array('0.0.0.0', '0'));
-
- // And throw again
- // @TODO Move this to the socket error handler
- throw new SocketOptionException(array($this, $this->getSocketInstance(), $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET);
+ $this->getSocketInstance()->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
} // END - if
/*
*/
if (!$this->getSocketInstance()->enableSocketNonBlocking()) {
// Handle this socket error with a faked recipientData array
- $helperInstance->handleSocketError(__METHOD__, __LINE__, $this->getSocketInstance(), array('0.0.0.0', '0'));
-
- // And throw again
- throw new SocketOptionException(array($helperInstance, $this->getSocketInstance(), $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET);
+ $this->getSocketInstance()->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
} // END - if
// Last step: mark connection as initialized
*/
public function initListener () {
// Create socket with factory
- $socketInstance = SocketFactory::createFileSocket($this);
+ $socketInstance = SocketFactory::createListenFileSocket($this);
// Set the main socket
$this->registerServerSocketInstance($socketInstance);
if ($sentBytes === FALSE) {
// Handle the error with a faked recipientData array
$this->handleSocketError(__METHOD__, __LINE__, $encodedDataArray[self::RAW_SOCKET_INDEX], array('0.0.0.0', '0'));
-
- // And throw it
- throw new InvalidSocketException(array($this, $encodedDataArray[self::RAW_SOCKET_INDEX], $socketError, $errorMessage), SocketHandler::EXCEPTION_INVALID_SOCKET);
} elseif (($sentBytes === 0) && (strlen($encodedDataArray[self::RAW_ENCODED_DATA_INDEX]) > 0)) {
// Nothing sent means we are done
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: All sent! (LINE=' . __LINE__ . ')');
// Is it without any errors?
if ($errorCode > 0) {
// Handle the socket error with a faked recipientData array
- $this->handleSocketError(__METHOD__, __LINE__, $socketInstance, array('0.0.0.0', '0'));
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
} // END - if
}
// Try to determine the peer's IP number
if (!$peerName = $socketInstance->getSocketPeerName()) {
// Handle the socket error with a faked recipientData array
- $this->handleSocketError(__METHOD__, __LINE__, $socketInstance, array('0.0.0.0', '0'));
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
} // END - if
} else {
// Server sockets won't work with socket_getpeername()
// Try to get the "peer"'s name
if (!$peerName = $socketArray[self::SOCKET_ARRAY_INSTANCE]->getSocketPeerName()) {
// Handle the socket error with given package data
- $this->handleSocketError(__METHOD__, __LINE__, $socketArray[self::SOCKET_ARRAY_INSTANCE], explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
+ $socketArray[self::SOCKET_ARRAY_INSTANCE]->handleSocketError(__METHOD__, __LINE__, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
} // END - if
// Get
*/
function ifStartEndMarkersSet ($data);
- /**
- * Handles socket error for given socket resource and peer data. This method
- * validates socket resource stored in given container if it is a valid
- * resource (see is_resource()) but assumes valid data in array
- * $recipientData, except that count($recipientData) is always 2.
- *
- * @param $method Value of __METHOD__ from calling method
- * @param $line Value of __LINE__ from calling method
- * @param $socketInstance An instance of a StoreableSocket class
- * @param $socketData A valid socket data array (0 = IP/file name, 1 = port)
- * @return void
- * @throws InvalidSocketException If the stored socket resource is no socket resource
- * @throws NoSocketErrorDetectedException If socket_last_error() gives zero back
- * @todo Move all this socket-related stuff into own class, most of it resides in BaseListener
- */
- function handleSocketError ($method, $line, StorableSocket $socketInstance, array $socketData);
-
/**
* Getter for listener pool instance
*
* Getter for last socket error
*
* @return $lastSocketError Last socket error
+ * @throws InvalidSocketException If socket is valid
*/
function getLastSocketError ();
/**
* Tries to bind the socket.
*
+ * @param $bindAddress Where to bind the socket to (e.g. Uni* socket file)
* @return $result Result from binding socket
+ * @throws InvalidSocketException If socket is valid
*/
- function bindSocketTo ();
+ function bindSocketTo ($bindAddress);
+
+ /**
+ * Tries to listen on the socket
+ *
+ * @return $result Result from listening on socket
+ * @throws InvalidSocketException If socket is valid
+ */
+ function listenOnSocket ();
+
+ /**
+ * Handles socket error for given socket resource and peer data. This method
+ * validates socket resource stored in given container if it is a valid
+ * resource (see is_resource()) but assumes valid data in array
+ * $recipientData, except that count($recipientData) is always 2.
+ *
+ * @param $method Value of __METHOD__ from calling method
+ * @param $line Value of __LINE__ from calling method
+ * @param $socketData A valid socket data array (0 = IP/file name, 1 = port)
+ * @return void
+ * @throws InvalidSocketException If the stored socket resource is no socket resource
+ * @throws NoSocketErrorDetectedException If socket_last_error() gives zero back
+ * @todo Move all this socket-related stuff into own class, most of it resides in BaseListener
+ */
+ function handleSocketError ($method, $line, array $socketData);
}