From: Roland Häder Date: Thu, 3 Dec 2020 23:33:10 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ef4b0af8a3e01e92adf4da9f2afe58bc470c86b7;p=hub.git Continued: - fixed NPE when socket is being shutdown on creation - improved some logger messages - updated core framework Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 8f74603a0..e794f2db7 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -18,6 +18,7 @@ use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Generic\NullPointerException; +use Org\Mxchange\CoreFramework\Helper\Helper; use Org\Mxchange\CoreFramework\Registry\Registerable; use Org\Mxchange\CoreFramework\Socket\InvalidSocketException; use Org\Mxchange\CoreFramework\Socket\NoSocketErrorDetectedException; @@ -229,7 +230,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function shutdownSocket () { // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Shutting down socket ' . $this->getSocketResource() . ' with state ' . $this->getHelperInstance()->getPrintableState() . ' ...'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Shutting down socket ' . $this->getSocketResource() . ' with state ' . ($this->getHelperInstance() instanceof Helper ? $this->getHelperInstance()->getPrintableState() : 'NULL') . ' ...'); // Get a visitor instance $visitorInstance = ObjectFactory::createObjectByConfiguredName('shutdown_socket_visitor_class'); @@ -770,7 +771,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $socketResource = socket_accept($this->getSocketResource()); // Create socket instance from it - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socketResource=%s,serverSocket=%s', strtoupper($this->getSocketProtocol()), $socketResource, $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s,serverSocket=%s', strtoupper($this->getSocketProtocol()), $socketResource, $this->getSocketResource())); $socketInstance = SocketFactory::createIncomingSocketInstance($socketResource, $this->getSocketProtocol()); // Return accepted socket instance @@ -876,7 +877,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function clearLastSocketError () { // Should be valid socket - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Clearing socket error, socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Clearing socket error, this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -889,7 +890,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita socket_clear_error($this->getSocketResource()); // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Error cleared - EXIT!', strtoupper($this->getSocketProtocol()))); } /** @@ -901,12 +902,12 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function registerInfoInstance (ShareableInfo $infoInstance) { // Get listener/helper from info class - //* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling infoInstance->getListenerInstance() - CALLED!', strtoupper($this->getSocketProtocol()))); + /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: infoInstance=%s,this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $infoInstance->__toString(), $this->getSocketResource())); $listenerInstance = $infoInstance->getListenerInstance(); $helperInstance = $infoInstance->getHelperInstance(); // Is there a listener instance set? - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: listenerInstance[]=%s,helperInstance[]=%s', strtoupper($this->getSocketProtocol()), gettype($listenerInstance), gettype($helperInstance))); if ($listenerInstance instanceof Listenable) { // Set it here for later usage /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...'); @@ -957,7 +958,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } // Is some data still pending or sent all out? - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Sending out %d bytes,rawBufferSize=%d,diff=%d to socketResource=%s', strtoupper($this->getSocketProtocol()), strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]), $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE], $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF], $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Sending out %d bytes,rawBufferSize=%d,diff=%d to this->socketResource=%s', strtoupper($this->getSocketProtocol()), strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]), $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE], $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF], $this->getSocketResource())); if ($socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF] >= 0) { // Send all out (encodedData is smaller than or equal buffer size) /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: MD5=%s', strtoupper($this->getSocketProtocol()), md5(substr($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], 0, ($socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF]))))); diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index 0180d652c..2d2ae56e5 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -21,6 +21,7 @@ use Org\Mxchange\CoreFramework\Socket\InvalidSocketException; use \InvalidArgumentException; use \LogicException; use \SplFileInfo; +use \UnexpectedValueException; /** * A socket factory class @@ -105,20 +106,22 @@ class SocketFactory extends ObjectFactory { * @param $listenerInstance An instance of a Listenable class * @return $socketInstance An instance of a StorableSocket class * @throws InvalidSocketException If the socket cannot be completed + * @throws UnexpectedValueException If $socketFile is empty */ public static final function createListenFileSocket (Listenable $listenerInstance) { // Create SplFileInfo - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - EXIT!', $listenerInstance->__toString())); + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString())); $fileInfo = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('ipc_socket_file_name')); // Create file name $socketFile = self::createTempPathForFile($fileInfo); - // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: socketFile=' . $socketFile . ' ...'); - // File name must not be empty - assert(!empty($socketFile)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: socketFile=' . $socketFile . ' ...'); + if (empty($socketFile)) { + // Something unexpected + throw new UnexpectedValueException('socketFile is empty'); + } // Init package instance $packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class'); @@ -127,12 +130,12 @@ class SocketFactory extends ObjectFactory { $socketResource = socket_create(AF_UNIX, SOCK_STREAM, 0); // Get container from it - $socketInstance = self::createObjectByConfiguredName('socket_container_class', array( + $socketInstance = self::createObjectByConfiguredName('socket_container_class', [ $socketResource, StorableSocket::SOCKET_PROTOCOL_FILE, $packageInstance, StorableSocket::CONNECTION_TYPE_SERVER, - )); + ]); // Set socket file $socketInstance->setSocketFile($socketFile); diff --git a/application/hub/classes/listener/socket/class_SocketFileListener.php b/application/hub/classes/listener/socket/class_SocketFileListener.php index e73e8b54c..4a97d89ed 100644 --- a/application/hub/classes/listener/socket/class_SocketFileListener.php +++ b/application/hub/classes/listener/socket/class_SocketFileListener.php @@ -70,6 +70,7 @@ class SocketFileListener extends BaseListener implements Listenable { */ public function initListener () { // Create socket with factory + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: CALLED!'); $socketInstance = SocketFactory::createListenFileSocket($this); // Set the socket instance diff --git a/core b/core index ddbb50472..ac256a664 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit ddbb50472e5383629958295be9f063cf8124b41f +Subproject commit ac256a664aa490665d37705439807ab6d62a7e3a