X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fmain%2Fclasses%2Flistener%2Fsocket%2Fclass_SocketFileListener.php;h=50a242f0244607d873ec3710282e41251b0b2602;hb=1a97bd3eff556623099970d5a548fb24ecb4408b;hp=53fc32b0cf095bb8c4278c4713870fca6b63f18c;hpb=3bfe3653b3792fb76816d1db3a8589073c82b1cd;p=core.git diff --git a/inc/main/classes/listener/socket/class_SocketFileListener.php b/inc/main/classes/listener/socket/class_SocketFileListener.php index 53fc32b0..50a242f0 100644 --- a/inc/main/classes/listener/socket/class_SocketFileListener.php +++ b/inc/main/classes/listener/socket/class_SocketFileListener.php @@ -30,6 +30,9 @@ class SocketFileListener extends BaseListener implements Listenable { protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); + + // Set the protocol to file + $this->setProtocolName('file'); } /** @@ -49,20 +52,146 @@ class SocketFileListener extends BaseListener implements Listenable { * Initializes the listener by setting up the required socket server * * @return void - * @todo 0% done */ public function initListener() { - $this->partialStub('Need to implement this method.'); + // Init socket + $mainSocket = socket_create(AF_UNIX, SOCK_STREAM, 0); + + // Is the socket resource valid? + if (!is_resource($mainSocket)) { + // Something bad happened + throw new InvalidSocketException(array($this, $mainSocket), BaseListener::EXCEPTION_INVALID_SOCKET); + } // END - if + + // Get socket error code for verification + $socketError = socket_last_error($mainSocket); + + // Check if there was an error else + if ($socketError > 0) { + // Handle this socket error with a faked recipientData array + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('null', '0')); + } // END - if + + // Create file name + $socketFile = self::createTempPathForFile($this->getConfigInstance()->getConfigEntry('ipc_socket_file_name')); + + // Debug message + self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: socketFile=' . $socketFile . ' ...'); + + // File name must not be empty + assert(!empty($socketFile)); + + // Is the file there? + if ((self::isReachableFilePath($socketFile)) && (file_exists($socketFile))) { + // Old socket found + self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: WARNING: Old socket at ' . $socketFile . ' found. Will not start.'); + + // Shutdown this socket + $this->shutdownSocket($mainSocket); + + // Quit here + exit; + } // END - if + + // Debug message + self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Binding to ' . $socketFile . ' ...'); + + // Try to bind to it + if (!socket_bind($mainSocket, $socketFile)) { + // Handle error here + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0')); + /* + // Get socket error code for verification + $socketError = socket_last_error($mainSocket); + + // Get error message + $errorMessage = socket_strerror($socketError); + + // Shutdown this socket + $this->shutdownSocket($mainSocket); + + // And throw again + throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); + */ + } // END - if + + // Start listen for connections + self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Listening for connections.'); + if (!socket_listen($mainSocket)) { + // Handle this socket error with a faked recipientData array + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0')); + /* + // Get socket error code for verification + $socketError = socket_last_error($mainSocket); + + // Get error message + $errorMessage = socket_strerror($socketError); + + // Shutdown this socket + $this->shutdownSocket($mainSocket); + + // And throw again + throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); + */ + } // END - if + + // Now, we want non-blocking mode + self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Setting non-blocking mode.'); + if (!socket_set_nonblock($mainSocket)) { + // Handle this socket error with a faked recipientData array + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0')); + /* + // Get socket error code for verification + $socketError = socket_last_error($mainSocket); + + // Get error message + $errorMessage = socket_strerror($socketError); + + // Shutdown this socket + $this->shutdownSocket($mainSocket); + + // And throw again + throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); + */ + } // END - if + + // Set the main socket + $this->registerServerSocketResource($mainSocket); + + // Initialize the peer pool instance + $poolInstance = ObjectFactory::createObjectByConfiguredName('application_pool_class', array($this)); + + // Add main socket + $poolInstance->addPeer($mainSocket, BaseConnectionHelper::CONNECTION_TYPE_SERVER); + + // And add it to this listener + $this->setPoolInstance($poolInstance); + + // Initialize iterator for listening on packages + $iteratorInstance = ObjectFactory::createObjectByConfiguredName('socket_listen_iterator_class', array($poolInstance->getPoolEntriesInstance())); + + // Rewind it and remember it in this class + $iteratorInstance->rewind(); + $this->setIteratorInstance($iteratorInstance); + + // Initialize the raw data handler + $handlerInstance = ObjectFactory::createObjectByConfiguredName('socket_raw_data_handler_class'); + + // Set it in this class + $this->setHandlerInstance($handlerInstance); + + // Output message + self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Socket listener now ready on socket ' . $socketFile . ' for service.'); } /** * "Listens" for incoming network packages * * @return void - * @todo 0% done */ public function doListen() { - $this->partialStub('Need to implement this method.'); + // Call super method + $this->doListenSocketSelect(''); } /** @@ -84,15 +213,6 @@ class SocketFileListener extends BaseListener implements Listenable { public function monitorIncomingRawData () { $this->partialStub('Need to implement this method.'); } - - /** - * Getter for connection type - * - * @return $connectionType Connection type for this listener - */ - public function getConnectionType () { - $this->partialStub('Need to implement this method.'); - } } // [EOF]