From: Roland Häder Date: Mon, 22 May 2017 13:25:37 +0000 (+0200) Subject: Continued a bit: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=a739267e5499380183005701315eb12118f3dce7;p=hub.git Continued a bit: - implemented enableSocketReuseAddress() - imported (Base)Iterator - added missing namespaces - created new namespaces (also template) - cleared up now obsolete code as StorableSocket is now there - renamed method to listenToSocket() 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 ff3bcb065..89d5d914e 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -413,7 +413,7 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * @return $result Result from listening on socket * @throws InvalidSocketException If stored socket is invalid */ - public function listenOnSocket () { + public function listenToSocket () { // Should be valid socket if (!$this->isValidSocket()) { // Throw exception @@ -447,6 +447,26 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable return $result; } + /** + * Tries to enable option "reuse address" + * + * @return void + * @throws InvalidSocketException If stored socket is invalid + */ + public function enableSocketReuseAddress () { + // Should be valid socket + if (!$this->isValidSocket()) { + // Throw exception + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); + } // END - if + + // Tries to set option + $result = socket_set_option($this->getSocketResource(), SOL_SOCKET, SO_REUSEADDR, 1); + + // Return result + return $result; + } + /** * Do the shutdown sequence for this connection helper * diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index b553e6905..c652e29ff 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -162,7 +162,7 @@ class SocketFactory extends ObjectFactory { // Start listen for connections self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.'); - if (!$socketInstance->listenOnSocket()) { + if (!$socketInstance->listenToSocket()) { // Handle this socket error with a faked recipientData array $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData)); } // END - if @@ -268,7 +268,6 @@ class SocketFactory extends ObjectFactory { } // END - if // Set the option to reuse the port - // @TODO: , SOL_SOCKET, SO_REUSEADDR, 1 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...'); if (!$socketInstance->enableSocketReuseAddress()) { // Handle this socket error with a faked recipientData array diff --git a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php index 3ca61c854..b5b8ca22f 100644 --- a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php +++ b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php @@ -55,11 +55,6 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Visitable, Reg */ const CONNECTION_TYPE_SERVER = 'server'; - /** - * Name of used protocol for this connection - */ - private $connectionType = 'invalid'; - /** * (IP) Adress used */ @@ -138,25 +133,6 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Visitable, Reg return $class; } - /** - * Getter for connection type - * - * @return $connectionType Name of used protocol in this connection - */ - public final function getConnectionType () { - return $this->connectionType; - } - - /** - * Setter for connection type - * - * @param $connectionType Name of used protocol in this connection - * @return void - */ - protected final function setConnectionType ($connectionType) { - $this->connectionType = $connectionType; - } - /** * Getter for IP address * diff --git a/application/hub/classes/helper/messages/crypto/class_CryptoRandomUnitMessageHelper.php b/application/hub/classes/helper/messages/crypto/class_CryptoRandomUnitMessageHelper.php index f345ca7d8..8940f1cfa 100644 --- a/application/hub/classes/helper/messages/crypto/class_CryptoRandomUnitMessageHelper.php +++ b/application/hub/classes/helper/messages/crypto/class_CryptoRandomUnitMessageHelper.php @@ -83,7 +83,5 @@ class CryptoRandomUnitMessageHelper extends BaseMessageHelper implements Message // And return it return $iteratorInstance; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/iterator/hub/class_HubPingIterator.php b/application/hub/classes/iterator/hub/class_HubPingIterator.php index 8f65db578..4600d0ef9 100644 --- a/application/hub/classes/iterator/hub/class_HubPingIterator.php +++ b/application/hub/classes/iterator/hub/class_HubPingIterator.php @@ -1,4 +1,13 @@ 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.') { - } - } diff --git a/application/hub/classes/listener/class_BaseListener.php b/application/hub/classes/listener/class_BaseListener.php index 555a71230..e1aef689a 100644 --- a/application/hub/classes/listener/class_BaseListener.php +++ b/application/hub/classes/listener/class_BaseListener.php @@ -62,11 +62,6 @@ class BaseListener extends BaseHubSystem implements Visitable { */ private $listenPort = 0; // This port MUST be changed by your application - /** - * Whether we are in blocking or non-blocking mode (default: non-blocking - */ - private $blockingMode = false; - /** * A peer pool instance */ @@ -146,25 +141,6 @@ class BaseListener extends BaseHubSystem implements Visitable { $this->setListenPort($this->getConfigInstance()->getConfigEntry($configEntry)); } - /** - * Setter for blocking-mode - * - * @param $blockingMode Whether blocking-mode is disabled (default) or enabled - * @return void - */ - protected final function setBlockingMode ($blockingMode) { - $this->blockingMode = (boolean) $blockingMode; - } - - /** - * Checks whether blocking-mode is enabled or disabled - * - * @return $blockingMode Whether blocking mode is disabled or enabled - */ - public final function isBlockingModeEnabled () { - return $this->blockingMode; - } - /** * Setter for peer pool instance * @@ -184,16 +160,6 @@ class BaseListener extends BaseHubSystem implements Visitable { return $this->poolInstance; } - /** - * Getter for connection type - * - * @return $connectionType Connection type for this listener - */ - public final function getConnectionType () { - // Wrap the real getter - return $this->getProtocolName(); - } - /** * Registeres the given socket resource for "this" listener instance. This * will be done in a seperate class to allow package writers to use it diff --git a/application/hub/classes/listener/class_BaseListenerDecorator.php b/application/hub/classes/listener/class_BaseListenerDecorator.php index 2b438e565..abd07df83 100644 --- a/application/hub/classes/listener/class_BaseListenerDecorator.php +++ b/application/hub/classes/listener/class_BaseListenerDecorator.php @@ -79,15 +79,6 @@ class BaseListenerDecorator extends BaseDecorator implements Visitable { return $this->getListenerInstance()->getListenPort(); } - /** - * Getter for connection type - * - * @return $connectionType Connection type for this listener - */ - public final function getConnectionType () { - return $this->getListenerInstance()->getConnectionType(); - } - /** * Accepts the visitor to process the visit "request" * diff --git a/application/hub/classes/package/class_NetworkPackage.php b/application/hub/classes/package/class_NetworkPackage.php index 41584f769..5276e079b 100644 --- a/application/hub/classes/package/class_NetworkPackage.php +++ b/application/hub/classes/package/class_NetworkPackage.php @@ -28,6 +28,9 @@ use CoreFramework\Socket\InvalidSocketException; use CoreFramework\Visitor\Visitable; use CoreFramework\Visitor\Visitor; +// Import SPL stuff +use \Iterator; + /** * A NetworkPackage class. This class implements Deliverable and Receivable * because all network packages should be deliverable to other nodes and diff --git a/application/hub/classes/registry/socket/class_SocketRegistry.php b/application/hub/classes/registry/socket/class_SocketRegistry.php index 18699ec30..d3de513d7 100644 --- a/application/hub/classes/registry/socket/class_SocketRegistry.php +++ b/application/hub/classes/registry/socket/class_SocketRegistry.php @@ -253,7 +253,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke */ public function getRegisteredSocketResource (Listenable $listenerInstance) { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY:listener=' . $listenerInstance->getConnectionType() . ' - CALLED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY:listener=' . $listenerInstance->getProtocolName() . ' - CALLED!'); // The socket must be registered before we can return it if (!$this->isInfoRegistered($listenerInstance)) { @@ -274,7 +274,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke $socketInstance = $registryInstance->getInstance($socketKey); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY:listener=' . $listenerInstance->getConnectionType() . ',socketResource[]=' . gettype($socketInstance) . ' - EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY:listener=' . $listenerInstance->getProtocolName() . ',socketResource[]=' . gettype($socketInstance) . ' - EXIT!'); // Return the resource return $socketInstance; diff --git a/application/hub/config.php b/application/hub/config.php index 79997cf3d..314073e10 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -82,16 +82,16 @@ $cfg->setConfigEntry('socket_file_listener_class', 'Hub\Listener\Socket\SocketFi $cfg->setConfigEntry('socket_file_decorator_listener_class', 'Hub\Listener\Socket\SocketFileListenerDecorator'); // CFG: NODE-TCP-LISTENER-CLASS -$cfg->setConfigEntry('node_tcp_listener_class', 'HubTcpListenerDecorator'); +$cfg->setConfigEntry('node_tcp_listener_class', 'Hub\Listener\Hub\Tcp\HubTcpListenerDecorator'); // CFG: NODE-UDP-LISTENER-CLASS -$cfg->setConfigEntry('node_udp_listener_class', 'HubUdpListenerDecorator'); +$cfg->setConfigEntry('node_udp_listener_class', 'Hub\Listener\Hub\Udp\HubUdpListenerDecorator'); // CFG: CLIENT-TCP-LISTENER-CLASS -$cfg->setConfigEntry('client_tcp_listener_class', 'ClientTcpListenerDecorator'); +$cfg->setConfigEntry('client_tcp_listener_class', 'Hub\Listener\Client\Tcp\ClientTcpListenerDecorator'); // CFG: CLIENT-UDP-LISTENER-CLASS -$cfg->setConfigEntry('client_udp_listener_class', 'ClientUdpListenerDecorator'); +$cfg->setConfigEntry('client_udp_listener_class', 'Hub\Listener\Client\Udp\ClientUdpListenerDecorator'); // CFG: NODE-POOL-CLASS $cfg->setConfigEntry('node_pool_class', 'Hub\Pool\Peer\DefaultPeerPool'); @@ -109,16 +109,16 @@ $cfg->setConfigEntry('shutdown_pool_iterator_class', 'ShutdownPoolIterator'); $cfg->setConfigEntry('handler_pool_iterator_class', 'HandlerPoolIterator'); // CFG: NETWORK-LISTEN-ITERATOR-CLASS -$cfg->setConfigEntry('network_listen_iterator_class', 'NetworkListenIterator'); +$cfg->setConfigEntry('network_listen_iterator_class', 'Hub\Iterator\Network\Listener\NetworkListenIterator'); // CFG: SOCKET-LISTEN-ITERATOR-CLASS $cfg->setConfigEntry('socket_listen_iterator_class', 'CoreFramework\Iterator\DefaultIterator'); // CFG: TCP-RAW-DATA-HANDLER-CLASS -$cfg->setConfigEntry('tcp_raw_data_handler_class', 'TcpRawDataHandler'); +$cfg->setConfigEntry('tcp_raw_data_handler_class', 'Hub\Handler\Network\RawData\Tcp\TcpRawDataHandler'); // CFG: UDP-RAW-DATA-HANDLER-CLASS -$cfg->setConfigEntry('udp_raw_data_handler_class', 'UdpRawDataHandler'); +$cfg->setConfigEntry('udp_raw_data_handler_class', 'Hub\Handler\Network\RawData\Udp\UdpRawDataHandler'); // CFG: SOCKET-RAW-DATA-HANDLER-CLASS $cfg->setConfigEntry('socket_raw_data_handler_class', 'Hub\Handler\Network\Socket\RawData\SocketRawDataHandler'); diff --git a/application/hub/interfaces/container/socket/class_StorableSocket.php b/application/hub/interfaces/container/socket/class_StorableSocket.php index 07de27e65..b45438da6 100644 --- a/application/hub/interfaces/container/socket/class_StorableSocket.php +++ b/application/hub/interfaces/container/socket/class_StorableSocket.php @@ -73,7 +73,7 @@ interface StorableSocket extends FrameworkInterface { * @return $result Result from listening on socket * @throws InvalidSocketException If stored socket is invalid */ - function listenOnSocket (); + function listenToSocket (); /** * Tries to set non-blocking I/O on stored socket @@ -83,6 +83,14 @@ interface StorableSocket extends FrameworkInterface { */ function enableSocketNonBlocking (); + /** + * Tries to enable option "reuse address" + * + * @return void + * @throws InvalidSocketException If stored socket is invalid + */ + function enableSocketReuseAddress (); + /** * Some "getter" for peer address/port from stored socket * diff --git a/application/hub/interfaces/helper/connections/class_ConnectionHelper.php b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php index a053edcea..a5c805bad 100644 --- a/application/hub/interfaces/helper/connections/class_ConnectionHelper.php +++ b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php @@ -54,13 +54,6 @@ interface ConnectionHelper extends HubHelper { */ function getConnectionPort (); - /** - * Getter for connection type - * - * @return $type Type of connection - */ - function getConnectionType (); - /** * Getter for IP address * @@ -84,7 +77,5 @@ interface ConnectionHelper extends HubHelper { * @return $shuttedDown Whether this connection is shutted down */ function isShuttedDown (); -} -// [EOF] -?> +} diff --git a/application/hub/interfaces/helper/messages/class_MessageHelper.php b/application/hub/interfaces/helper/messages/class_MessageHelper.php index 71014a304..05bd92238 100644 --- a/application/hub/interfaces/helper/messages/class_MessageHelper.php +++ b/application/hub/interfaces/helper/messages/class_MessageHelper.php @@ -43,7 +43,5 @@ interface MessageHelper extends Helper { * @return $iteratorInstance An instance of a Iterator class */ function getKeyIterator (); -} -// [EOF] -?> +} diff --git a/application/hub/interfaces/listener/class_Listenable.php b/application/hub/interfaces/listener/class_Listenable.php index 5d9d17d94..e6470884c 100644 --- a/application/hub/interfaces/listener/class_Listenable.php +++ b/application/hub/interfaces/listener/class_Listenable.php @@ -73,13 +73,6 @@ interface Listenable extends HubInterface { */ function getListenPort (); - /** - * Getter for connection type - * - * @return $connectionType Connection type for this listener - */ - function getConnectionType (); - /** * Getter for peer pool instance *