From cc1ac838d8f2b204de83013058855d2ed929cd85 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 26 Oct 2020 04:54:19 +0100 Subject: [PATCH] Continued: - SocketContainer->bindSocketTo() is now split into bindSocketToFile() and bindSocketToListener() - also fixed wrong getter/setter invocation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../socket/class_SocketContainer.php | 86 +++++++++++++++++-- .../factories/socket/class_SocketFactory.php | 24 +++--- .../package/deliverable/class_PackageData.php | 53 ------------ .../container/socket/class_StorableSocket.php | 47 +++++++++- .../package/class_DeliverablePackage.php | 33 ------- 5 files changed, 132 insertions(+), 111 deletions(-) diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 170bcb020..3de25c65e 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -16,6 +16,7 @@ use Org\Shipsimu\Hub\Network\Package\NetworkPackageHandler; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\ObjectFactory; +use Org\Mxchange\CoreFramework\Generic\NullPointerException; use Org\Mxchange\CoreFramework\Registry\Registerable; use Org\Mxchange\CoreFramework\Socket\InvalidSocketException; use Org\Mxchange\CoreFramework\Socket\NoSocketErrorDetectedException; @@ -30,6 +31,7 @@ use Org\Mxchange\CoreFramework\Visitor\Visitor; use \BadMethodCallException; use \InvalidArgumentException; use \LogicException; +use \SplFileInfo; /** * A Socket Container class @@ -76,6 +78,16 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ private $packageDataInstance = NULL; + /** + * An instance of a SplFileInfo class + */ + private $socketFile; + + /** + * Socket type + */ + private $socketType; + /** * Protected constructor * @@ -507,30 +519,48 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } /** - * Tries to bind the socket. + * Tries to bind to socket file * * @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 ($bindAddress, $bindPort = 0) { + public function bindSocketToFile () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + } + /** + * Tries to bind to listener's address and port + * + * @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 + * @throws NullPointerException If listener instance is not given + */ + public function bindSocketToListener () { // Should be valid socket + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); - } // END - if + } elseif (is_null($this->getListenerInstance())) { + // Required listener not set + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } - // Try to bind it to - $result = socket_bind($this->getSocketResource(), $bindAddress, $bindPort); + // Get bind address and port + $address = $this->getListenerInstance()->getListenAddress(); + $port = $this->getListenerInstance()->getListenAddress(); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); + // Try to bind it to + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Binding socket to %s:%d ...', strtoupper($this->getSocketProtocol()), $address, $port)); + $result = socket_bind($this->getSocketResource(), $address, $port); // Return result + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); return $result; } @@ -1485,4 +1515,44 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita return $result; } + /** + * Getter for socket file instance + * + * @return $socketFile An instance of a SplFileInfo class + */ + public function getSocketFile () { + return $this->socketFile; + } + + /** + * Setter for socket file instance + * + * @param $socketFile An instance of a SplFileInfo class + * @return void + */ + public function setSocketFile (SplFileInfo $socketFile) { + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Setting socketFile=%s ...', strtoupper($this->getSocketType()), $socketFile)); + $this->socketFile = $socketFile; + } + + /** + * Getter for socket type + * + * @return $socketType Stocket type + */ + public function getSocketType () { + return $this->socketType; + } + + /** + * Setter for socket type + * + * @param $socketType Socket type + * @return void + */ + public function setSocketType ($socketType) { + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Setting socketType=%s ...', strtoupper($this->getSocketType()), $socketType)); + $this->socketType = $socketType; + } + } diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index c41de7545..65e00eaa9 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -127,10 +127,6 @@ class SocketFactory extends ObjectFactory { // Init package instance $packageInstance = PackageDataFactory::createPackageDataInstance(); - // Set file socket data - $packageInstance->setSocketFile($socketFile); - $packageInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER); - // Init main socket $socketResource = socket_create(AF_UNIX, SOCK_STREAM, 0); @@ -141,6 +137,10 @@ class SocketFactory extends ObjectFactory { $packageInstance, )); + // Set file socket data + $socketInstance->setSocketFile($socketFile); + $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER); + // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance[]=%s', gettype($socketInstance))); @@ -157,9 +157,9 @@ class SocketFactory extends ObjectFactory { } // END - if // Is the file there? - if ((FrameworkBootstrap::isReachableFilePath($packageInstance->getSocketFile())) && (file_exists($packageInstance->getSocketFile()))) { + if ((FrameworkBootstrap::isReachableFilePath($socketInstance->getSocketFile())) && (file_exists($socketInstance->getSocketFile()))) { // Old socket found - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: WARNING: Old socket at ' . $packageInstance->getSocketFile() . ' found. Will not start.'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: WARNING: Old socket at ' . $socketInstance->getSocketFile() . ' found. Will not start.'); // Shutdown this socket $socketInstance->shutdownSocket(); @@ -169,10 +169,10 @@ class SocketFactory extends ObjectFactory { } // END - if // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $packageInstance->getSocketFile() . ' ...'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $socketInstance->getSocketFile() . ' ...'); // Try to bind to it - if (!$socketInstance->bindSocketTo($packageInstance->getSocketFile())) { + if (!$socketInstance->bindSocketToFile()) { // Handle error here $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData)); } // END - if @@ -295,8 +295,6 @@ class SocketFactory extends ObjectFactory { // Set listener instance and type $socketInstance->setListenerInstance($listenerInstance); $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER); - $socketInstance->setSocketListenAddress($listenerInstance->getListenAddress()); - $socketInstance->setSocketListenPort($listenerInstance->getListenPort()); // Is the socket resource valid? if (!$socketInstance->isValidSocket()) { @@ -323,7 +321,7 @@ class SocketFactory extends ObjectFactory { * send/recv data, disconnect, etc.. */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort()); - if (!$socketInstance->bindSocketTo($listenerInstance->getListenAddress(), $listenerInstance->getListenPort())) { + if (!$socketInstance->bindSocketToListener()) { // Handle this socket error with a faked recipientData array $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0')); } // END - if @@ -376,8 +374,6 @@ class SocketFactory extends ObjectFactory { // Set listener instance and socket type $socketInstance->setListenerInstance($listenerInstance); $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER); - $socketInstance->setSocketListenAddress($listenerInstance->getListenAddress()); - $socketInstance->setSocketListenPort($listenerInstance->getListenPort()); // Is the socket resource valid? if (!$socketInstance->isValidSocket()) { @@ -404,7 +400,7 @@ class SocketFactory extends ObjectFactory { * send/recv data, disconnect, etc.. */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort()); - if (!$socketInstance->bindSocketTo($listenerInstance->getListenAddress(), $listenerInstance->getListenPort())) { + if (!$socketInstance->bindSocketToListener()) { // Handle this socket error with a faked recipientData array $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0')); } // END - if diff --git a/application/hub/classes/package/deliverable/class_PackageData.php b/application/hub/classes/package/deliverable/class_PackageData.php index 911536ff0..e512e1835 100644 --- a/application/hub/classes/package/deliverable/class_PackageData.php +++ b/application/hub/classes/package/deliverable/class_PackageData.php @@ -8,9 +8,6 @@ use Org\Shipsimu\Hub\Generic\BaseHubSystem; // Import framework stuff use Org\Mxchange\CoreFramework\Registry\Registerable; -// Import SPL stuff -use \SplFileInfo; - /** * A DeliverablePackage class for raw package data * @@ -45,16 +42,6 @@ class PackageData extends BaseHubSystem implements DeliverablePackage, Registera */ private $packageContent; - /** - * An instance of a SplFileInfo class - */ - private $socketFile; - - /** - * Socket type - */ - private $socketType; - /** * Protected constructor * @@ -78,46 +65,6 @@ class PackageData extends BaseHubSystem implements DeliverablePackage, Registera return $packageInstance; } - /** - * Getter for socket file instance - * - * @return $socketFile An instance of a SplFileInfo class - */ - public function getSocketFile () { - return $this->socketFile; - } - - /** - * Setter for socket file instance - * - * @param $socketFile An instance of a SplFileInfo class - * @return void - */ - public function setSocketFile (SplFileInfo $socketFile) { - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-DATA: Setting socketFile=%s ...', $socketFile)); - $this->socketFile = $socketFile; - } - - /** - * Getter for socket type - * - * @return $socketType Stocket type - */ - public function getSocketType () { - return $this->socketType; - } - - /** - * Setter for socket type - * - * @param $socketType Socket type - * @return void - */ - public function setSocketType ($socketType) { - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-DATA: Setting socketType=%s ...', $socketType)); - $this->socketType = $socketType; - } - /** * Getter for package content * diff --git a/application/hub/interfaces/container/socket/class_StorableSocket.php b/application/hub/interfaces/container/socket/class_StorableSocket.php index c67c53a5c..943648258 100644 --- a/application/hub/interfaces/container/socket/class_StorableSocket.php +++ b/application/hub/interfaces/container/socket/class_StorableSocket.php @@ -8,6 +8,9 @@ use Org\Shipsimu\Hub\Information\ShareableInfo; // Inport frameworks stuff use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; +// Import PHP stuff +use \SplFileInfo; + /** * An interface for socket containers * @@ -72,13 +75,21 @@ interface StorableSocket extends FrameworkInterface { const CONNECTION_TYPE_SERVER = 'server'; /** - * Tries to bind the socket. + * Tries to bind to attached socket file + * + * @return $result Result from binding socket + * @throws InvalidSocketException If stored socket is invalid + */ + function bindSocketToFile (); + + /** + * Tries to bind to attached listener's address and port * - * @param $bindAddress Where to bind the socket to (e.g. Uni* socket file) * @return $result Result from binding socket * @throws InvalidSocketException If stored socket is invalid + * @throws NullPointerException If listener instance is not given */ - function bindSocketTo ($bindAddress); + function bindSocketToListener (); /** * Tries to listen on the socket @@ -318,4 +329,34 @@ interface StorableSocket extends FrameworkInterface { */ function readDataFromSocket (); + /** + * Getter for socket file instance + * + * @return $socketFile An instance of a SplFileInfo class + */ + public function getSocketFile (); + + /** + * Setter for socket file instance + * + * @param $socketFile An instance of a SplFileInfo class + * @return void + */ + public function setSocketFile (SplFileInfo $socketFile); + + /** + * Getter for socket type + * + * @return $socketFile Stocket type + */ + public function getSocketType (); + + /** + * Setter for socket type + * + * @param $socketType Socket type + * @return void + */ + public function setSocketType ($socketType); + } diff --git a/application/hub/interfaces/delivery/package/class_DeliverablePackage.php b/application/hub/interfaces/delivery/package/class_DeliverablePackage.php index 41a7402c6..16df801be 100644 --- a/application/hub/interfaces/delivery/package/class_DeliverablePackage.php +++ b/application/hub/interfaces/delivery/package/class_DeliverablePackage.php @@ -5,9 +5,6 @@ namespace Org\Shipsimu\Hub\Network\Package; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; -// Import PHP stuff -use \SplFileInfo; - /** * An interface for package delivery boys... ;-) * @@ -32,34 +29,4 @@ use \SplFileInfo; */ interface DeliverablePackage extends HubInterface { - /** - * Getter for socket file instance - * - * @return $socketFile An instance of a SplFileInfo class - */ - public function getSocketFile (); - - /** - * Setter for socket file instance - * - * @param $socketFile An instance of a SplFileInfo class - * @return void - */ - public function setSocketFile (SplFileInfo $socketFile); - - /** - * Getter for socket type - * - * @return $socketFile Stocket type - */ - public function getSocketType (); - - /** - * Setter for socket type - * - * @param $socketType Socket type - * @return void - */ - public function setSocketType ($socketType); - } -- 2.39.5