From dc66adc3e73cc116d8f74633c753c2c5d1f5f95b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 21 May 2017 22:25:26 +0200 Subject: [PATCH] Continued: - setter/getter for socket instance is now in BaseListener - imported StorableSocket - moved constants to Poolable interface - updated core framework MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../factories/socket/class_SocketFactory.php | 1 + .../network/tcp/class_TcpRawDataHandler.php | 16 ++++++------- .../network/udp/class_UdpRawDataHandler.php | 6 ++--- .../network/class_BaseRawDataHandler.php | 3 ++- .../ipv4/class_BaseIpV4ConnectionHelper.php | 1 - .../classes/listener/class_BaseListener.php | 5 ++-- .../classes/package/class_NetworkPackage.php | 1 + .../hub/classes/pools/class_BasePool.php | 6 ----- .../pools/peer/class_DefaultPeerPool.php | 23 ++++++++++--------- .../registry/socket/class_SocketRegistry.php | 4 ++-- .../hub/interfaces/class_HubInterface.php | 15 ------------ .../interfaces/listener/class_Listenable.php | 16 +++++++++++++ .../hub/interfaces/pool/class_Poolable.php | 5 ++++ core | 2 +- 14 files changed, 54 insertions(+), 50 deletions(-) diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index 8262f2ce9..44cd11ff7 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -3,6 +3,7 @@ namespace Hub\Factory\Socket; // Import application-specific stuff +use Hub\Container\Socket\StorableSocket; use Hub\Handler\Protocol\HandleableProtocol; use Hub\Listener\BaseListener; use Hub\Listener\Listenable; diff --git a/application/hub/classes/handler/network/tcp/class_TcpRawDataHandler.php b/application/hub/classes/handler/network/tcp/class_TcpRawDataHandler.php index 460835bb6..ee1f41f52 100644 --- a/application/hub/classes/handler/network/tcp/class_TcpRawDataHandler.php +++ b/application/hub/classes/handler/network/tcp/class_TcpRawDataHandler.php @@ -5,7 +5,7 @@ namespace Hub\Handler\Network\RawData\Tcp; // Import application-specific stuff use Hub\Handler\RawData\BaseRawDataHandler; use Hub\Network\Networkable; -use Hub\Pool\BasePool; +use Hub\Pool\Poolable; /** * A TCP raw data handler @@ -65,7 +65,7 @@ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable { */ public function processRawDataFromResource (array $socketArray) { // Check the resource - if ((!isset($socketArray[BasePool::SOCKET_ARRAY_INSTANCE])) || (!is_resource($socketArray[BasePool::SOCKET_ARRAY_INSTANCE]))) { + if ((!isset($socketArray[Poolable::SOCKET_ARRAY_INSTANCE])) || (!is_resource($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]))) { // Throw an exception throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); } // END - if @@ -74,33 +74,33 @@ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable { $this->setErrorCode(self::SOCKET_ERROR_UNHANDLED); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: Handling TCP package from resource=' . $socketArray[BasePool::SOCKET_ARRAY_INSTANCE] . ',type=' . $socketArray[BasePool::SOCKET_ARRAY_CONN_TYPE] . ',last error=' . socket_strerror($this->lastSocketError)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: Handling TCP package from resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE] . ',type=' . $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE] . ',last error=' . socket_strerror($this->lastSocketError)); /* * Read the raw data from socket. If you change PHP_BINARY_READ to * PHP_NORMAL_READ, this line will endless block. This script does only * provide simultanous threads, not real. */ - $rawData = socket_read($socketArray[BasePool::SOCKET_ARRAY_INSTANCE], $this->getConfigInstance()->getConfigEntry('tcp_buffer_length'), PHP_BINARY_READ); + $rawData = socket_read($socketArray[Poolable::SOCKET_ARRAY_INSTANCE], $this->getConfigInstance()->getConfigEntry('tcp_buffer_length'), PHP_BINARY_READ); // Get socket error code back - $this->lastSocketError = socket_last_error($socketArray[BasePool::SOCKET_ARRAY_INSTANCE]); + $this->lastSocketError = socket_last_error($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]); // Debug output of read data length - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: rawData[' . gettype($rawData) . ']=' . strlen($rawData) . ',MD5=' . md5($rawData) . ',resource=' . $socketArray[BasePool::SOCKET_ARRAY_INSTANCE] . ',error=' . socket_strerror($this->lastSocketError)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: rawData[' . gettype($rawData) . ']=' . strlen($rawData) . ',MD5=' . md5($rawData) . ',resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE] . ',error=' . socket_strerror($this->lastSocketError)); //* NOISY-DEBUG: */ if ($rawData !== FALSE) self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: rawData=' . $rawData); // Is it valid? if ($this->lastSocketError == 11) { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: Ignoring error 11 (Resource temporary unavailable) from socket resource=' . $socketArray[BasePool::SOCKET_ARRAY_INSTANCE]); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: Ignoring error 11 (Resource temporary unavailable) from socket resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]); /* * Error code 11 (Resource temporary unavailable) can be safely * ignored on non-blocking sockets. The socket is currently not * sending any data. */ - socket_clear_error($socketArray[BasePool::SOCKET_ARRAY_INSTANCE]); + socket_clear_error($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]); // Skip any further processing return; diff --git a/application/hub/classes/handler/network/udp/class_UdpRawDataHandler.php b/application/hub/classes/handler/network/udp/class_UdpRawDataHandler.php index 39e8d123f..2c3e0a911 100644 --- a/application/hub/classes/handler/network/udp/class_UdpRawDataHandler.php +++ b/application/hub/classes/handler/network/udp/class_UdpRawDataHandler.php @@ -5,7 +5,7 @@ namespace Hub\Handler\RawData\Udp; // Import application-specific stuff use Hub\Handler\RawData\BaseRawDataHandler; use Hub\Network\Networkable; -use Hub\Pool\BasePool; +use Hub\Pool\Poolable; /** * A UDP raw data handler @@ -67,13 +67,13 @@ class UdpRawDataHandler extends BaseRawDataHandler implements Networkable { */ public function processRawDataFromResource (array $socketArray) { // Check the resource - if ((!isset($socketArray[BasePool::SOCKET_ARRAY_INSTANCE])) || (!is_resource($socketArray[BasePool::SOCKET_ARRAY_INSTANCE]))) { + if ((!isset($socketArray[Poolable::SOCKET_ARRAY_INSTANCE])) || (!is_resource($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]))) { // Throw an exception throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); } // END - if // Implement processing here - $this->partialStub('Please implement this method. resource=' . $socketArray[BasePool::SOCKET_ARRAY_INSTANCE] . ',type=' . $socketArray[BasePool::SOCKET_ARRRAY_CONN_TYPE]); + $this->partialStub('Please implement this method. resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE] . ',type=' . $socketArray[Poolable::SOCKET_ARRRAY_CONN_TYPE]); } } diff --git a/application/hub/classes/handler/raw_data/network/class_BaseRawDataHandler.php b/application/hub/classes/handler/raw_data/network/class_BaseRawDataHandler.php index 2544d5289..3374872e2 100644 --- a/application/hub/classes/handler/raw_data/network/class_BaseRawDataHandler.php +++ b/application/hub/classes/handler/raw_data/network/class_BaseRawDataHandler.php @@ -3,6 +3,7 @@ namespace Hub\Handler\RawData; // Import application-specific stuff +use Hub\Container\Socket\StorableSocket; use Hub\Network\Package\NetworkPackage; // Import framework stuff @@ -68,7 +69,7 @@ class BaseRawDataHandler extends BaseHandler { parent::__construct($className); // Set error code to 'unknown' - $this->setErrorCode(self::SOCKET_ERROR_UNKNOWN); + $this->setErrorCode(StorableSocket::SOCKET_ERROR_UNKNOWN); // Init stacker instance for processed raw data $stackInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_stacker_class'); diff --git a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php index 0af6dbc8a..09dfafda2 100644 --- a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php @@ -3,7 +3,6 @@ namespace Hub\Helper\Connection\IpV4; // Import application-specific stuff -use Hub\Container\Socket\StorableSocket; use Hub\Helper\Connection\BaseConnectionHelper; use Hub\Locator\Node\LocateableNode; diff --git a/application/hub/classes/listener/class_BaseListener.php b/application/hub/classes/listener/class_BaseListener.php index b2e5e7ffe..555a71230 100644 --- a/application/hub/classes/listener/class_BaseListener.php +++ b/application/hub/classes/listener/class_BaseListener.php @@ -9,6 +9,7 @@ use Hub\Generic\BaseHubSystem; use Hub\Helper\Connection\BaseConnectionHelper; use Hub\Network\Package\NetworkPackage; use Hub\Pool\Peer\PoolablePeer; +use Hub\Pool\Poolable; // Import framework stuff use CoreFramework\Factory\ObjectFactory; @@ -416,8 +417,8 @@ class BaseListener extends BaseHubSystem implements Visitable { $currentSocketData = $this->getIteratorInstance()->current(); // Handle it here, if not main server socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: currentSocketData=' . $currentSocketData[BasePool::SOCKET_ARRAY_INSTANCE] . ',type=' . $currentSocketData[BasePool::SOCKET_ARRAY_CONN_TYPE] . ',serverSocket=' . $this->getSocketInstance()->getSocketResource()); - if (($currentSocketData[BasePool::SOCKET_ARRAY_CONN_TYPE] != BaseConnectionHelper::CONNECTION_TYPE_SERVER) && (!$currentSocketData[BasePool::SOCKET_ARRAY_INSTANCE]->equals($this->getSocketInstance()))) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: currentSocketData=' . $currentSocketData[Poolable::SOCKET_ARRAY_INSTANCE] . ',type=' . $currentSocketData[Poolable::SOCKET_ARRAY_CONN_TYPE] . ',serverSocket=' . $this->getSocketInstance()->getSocketResource()); + if (($currentSocketData[Poolable::SOCKET_ARRAY_CONN_TYPE] != BaseConnectionHelper::CONNECTION_TYPE_SERVER) && (!$currentSocketData[Poolable::SOCKET_ARRAY_INSTANCE]->equals($this->getSocketInstance()))) { // ... or else it will raise warnings like 'Transport endpoint is not connected' $this->getHandlerInstance()->processRawDataFromResource($currentSocketData); } // END - if diff --git a/application/hub/classes/package/class_NetworkPackage.php b/application/hub/classes/package/class_NetworkPackage.php index 3c26b71d1..41584f769 100644 --- a/application/hub/classes/package/class_NetworkPackage.php +++ b/application/hub/classes/package/class_NetworkPackage.php @@ -3,6 +3,7 @@ namespace Hub\Network\Package; // Import application-specific stuff +use Hub\Container\Socket\StorableSocket; use Hub\Factory\Assembler\Package\PackageAssemblerFactory; use Hub\Factory\Dht\DhtObjectFactory; use Hub\Factory\Information\Connection\ConnectionInfoFactory; diff --git a/application/hub/classes/pools/class_BasePool.php b/application/hub/classes/pools/class_BasePool.php index fc62e8149..a95573cd7 100644 --- a/application/hub/classes/pools/class_BasePool.php +++ b/application/hub/classes/pools/class_BasePool.php @@ -36,12 +36,6 @@ use CoreFramework\Visitor\Visitor; * along with this program. If not, see . */ abstract class BasePool extends BaseHubSystem implements Poolable, Visitable { - /** - * Socket array elements - */ - const SOCKET_ARRAY_INSTANCE = 'instance'; - const SOCKET_ARRAY_CONN_TYPE = 'connection_type'; - /** * A list of pool entries */ diff --git a/application/hub/classes/pools/peer/class_DefaultPeerPool.php b/application/hub/classes/pools/peer/class_DefaultPeerPool.php index 74ec3123e..42f681b72 100644 --- a/application/hub/classes/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/classes/pools/peer/class_DefaultPeerPool.php @@ -8,6 +8,7 @@ use Hub\Listener\Listenable; use Hub\Locator\Node\LocateableNode; use Hub\Network\Package\NetworkPackage; use Hub\Pool\BasePool; +use Hub\Pool\Poolable; use Hub\Pool\Peer\PoolablePeer; // Import framework stuff @@ -132,8 +133,8 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // Construct the array $socketArray = array( - self::SOCKET_ARRAY_INSTANCE => $socketInstance, - self::SOCKET_ARRAY_CONN_TYPE => $connectionType + Poolable::SOCKET_ARRAY_INSTANCE => $socketInstance, + Poolable::SOCKET_ARRAY_CONN_TYPE => $connectionType ); // Add it finally to the pool @@ -168,7 +169,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // "Walk" through all socket arrays foreach ($socketArrays as $socketArray) { // Add the socket - array_push($sockets, $socketArray[self::SOCKET_ARRAY_INSTANCE]); + array_push($sockets, $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]); } // END - foreach // Return it @@ -198,9 +199,9 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // "Walk" through all socket arrays foreach ($socketArrays as $socketArray) { // Does it match? - if ($socketArray[self::SOCKET_ARRAY_CONN_TYPE] === $connectionType) { + if ($socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE] === $connectionType) { // Add the socket - array_push($sockets, $socketArray[self::SOCKET_ARRAY_INSTANCE]); + array_push($sockets, $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]); } // END - if } // END - foreach @@ -249,9 +250,9 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // Get all sockets and check them, skip the server socket foreach ($sockets as $socketArray) { // Is this a server socket? - if ($socketArray[self::SOCKET_ARRAY_INSTANCE]->equals($this->getListenerInstance()->getSocketInstance())) { + if ($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->equals($this->getListenerInstance()->getSocketInstance())) { // Skip 'server' sockets (local socket) - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('POOL: Skipping server socket ' . $socketArray[self::SOCKET_ARRAY_INSTANCE]->getSocketResource() . ' ...'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('POOL: Skipping server socket ' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource() . ' ...'); continue; } // END - if @@ -260,19 +261,19 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { $peerPort = '0'; // Try to get the "peer"'s name - if (!$socketArray[self::SOCKET_ARRAY_INSTANCE]->getSocketPeerName($peerAddress, $peerPort)) { + if (!$socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketPeerName($peerAddress, $peerPort)) { // Handle the socket error with given package data - $socketArray[self::SOCKET_ARRAY_INSTANCE]->handleSocketError(__METHOD__, __LINE__, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); + $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->handleSocketError(__METHOD__, __LINE__, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); } // END - if // Get // If the "peer" IP and recipient is same, use it if ($peerAddress == $unlData[LocateableNode::UNL_PART_ADDRESS]) { // IPs match, so take the socket and quit this loop - $socketInstance = $socketArray[self::SOCKET_ARRAY_INSTANCE]; + $socketInstance = $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]; // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('POOL: peerAddress=' . $peerAddress . ' matches with recipient IP address. Taking socket=' . $socketArray[self::SOCKET_ARRAY_INSTANCE] . ',type=' . $socketArray[self::SOCKET_ARRAY_CONN_TYPE]); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('POOL: peerAddress=' . $peerAddress . ' matches with recipient IP address. Taking socket=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE] . ',type=' . $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE]); break; } // END - if } // END - foreach diff --git a/application/hub/classes/registry/socket/class_SocketRegistry.php b/application/hub/classes/registry/socket/class_SocketRegistry.php index 01ca7cede..18699ec30 100644 --- a/application/hub/classes/registry/socket/class_SocketRegistry.php +++ b/application/hub/classes/registry/socket/class_SocketRegistry.php @@ -41,7 +41,7 @@ use CoreFramework\Registry\Sub\SubRegistry; */ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocket { // Exception constants - const SOCKET_NOT_REGISTERED = 0xd200; + const EXCEPTION_SOCKET_NOT_REGISTERED = 0xd200; /** * Instance of this class @@ -258,7 +258,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke // The socket must be registered before we can return it if (!$this->isInfoRegistered($listenerInstance)) { // Throw the exception - throw new NoSocketRegisteredException ($listenerInstance, self::SOCKET_NOT_REGISTERED); + throw new NoSocketRegisteredException ($listenerInstance, self::EXCEPTION_SOCKET_NOT_REGISTERED); } // END - if // Now get the key from the listener diff --git a/application/hub/interfaces/class_HubInterface.php b/application/hub/interfaces/class_HubInterface.php index c3d579884..9ba4ce718 100644 --- a/application/hub/interfaces/class_HubInterface.php +++ b/application/hub/interfaces/class_HubInterface.php @@ -82,19 +82,4 @@ interface HubInterface extends FrameworkInterface { */ function getSessionId (); - /** - * Setter for socket instance - * - * @param $socketInstance A StorableSocket instance - * @return void - */ - function setSocketInstance (StorableSocket $socketInstance); - - /** - * Getter for socket instance - * - * @return $socketInstance An instance of a StorableSocket class - */ - function getSocketInstance (); - } diff --git a/application/hub/interfaces/listener/class_Listenable.php b/application/hub/interfaces/listener/class_Listenable.php index 5e6c97461..5d9d17d94 100644 --- a/application/hub/interfaces/listener/class_Listenable.php +++ b/application/hub/interfaces/listener/class_Listenable.php @@ -3,6 +3,7 @@ namespace Hub\Listener; // Import application-specific stuff +use Hub\Container\Socket\StorableSocket; use Hub\Generic\HubInterface; /** @@ -86,4 +87,19 @@ interface Listenable extends HubInterface { */ function getPoolInstance (); + /** + * Setter for socket instance + * + * @param $socketInstance A StorableSocket instance + * @return void + */ + function setSocketInstance (StorableSocket $socketInstance); + + /** + * Getter for socket instance + * + * @return $socketInstance An instance of a StorableSocket class + */ + function getSocketInstance (); + } diff --git a/application/hub/interfaces/pool/class_Poolable.php b/application/hub/interfaces/pool/class_Poolable.php index d48f7cd26..653421a32 100644 --- a/application/hub/interfaces/pool/class_Poolable.php +++ b/application/hub/interfaces/pool/class_Poolable.php @@ -28,6 +28,11 @@ use Hub\Generic\HubInterface; * along with this program. If not, see . */ interface Poolable extends HubInterface { + /** + * Socket array elements + */ + const SOCKET_ARRAY_INSTANCE = 'instance'; + const SOCKET_ARRAY_CONN_TYPE = 'connection_type'; /** * Pre-shuts down the pool diff --git a/core b/core index ca40302a3..b89f58d4d 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit ca40302a326b814bbd5273a0540e18d672cf2d70 +Subproject commit b89f58d4d90b6d608b8e16b0e5e3235eaff4f54e -- 2.39.5