]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/registry/socket/class_SocketRegistry.php
Continued with refactoring:
[hub.git] / application / hub / main / registry / socket / class_SocketRegistry.php
index c32b10a542f05ea97fa3c42a721abce30a13d884..737faf8c24dace19101d1936209f308684ebedae 100644 (file)
@@ -60,39 +60,28 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke
         * "Getter" to get a string respresentation for a key for the sub-registry
         * in this format: class:type:port
         *
-        * @param       $connectionInstance     An instance of a Listenable class
-        * @return      $key                            A string representation of the socket for the registry
+        * @param       $infoInstance   An instance of a ShareableInfo class
+        * @return      $key                    A string representation of the socket for the registry
         */
-       private function getSubRegistryKey (Listenable $connectionInstance) {
+       private function getSubRegistryKey (ShareableInfo $infoInstance) {
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: connectionInstance=' . $connectionInstance->__toString() . ' - ENTERED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: infoInstance=' . $infoInstance->__toString() . ' - ENTERED!');
 
                // Default address is invalid
                $address = '*invalid*';
                $port    = 0;
 
-               // Which instance is it?
-               // @TODO Tested again base class, rewrite it to a generic interface!
-               if ($connectionInstance instanceof BaseConnectionHelper) {
-                       // Get address
-                       $address = $connectionInstance->getAddress();
-                       $port    = $connectionInstance->getConnectionPort();
-               } elseif ($connectionInstance instanceof BaseListener) {
-                       // Get listen address
-                       $address = $connectionInstance->getListenAddress();
-                       $port    = $connectionInstance->getListenPort();
-               } else {
-                       // Not supported!
-                       $this->debugBackTrace('Unsupported connectionInstance=' . $connectionInstance->__toString() . ' detected. Please fix this!');
-               }
+               // Get address and port
+               $address = $infoInstance->getAddress();
+               $port    = $infoInstance->getPort();
 
                // Debug message
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: address=' . $address . ',port=' . $port);
 
                // Get connection type and port number and add both together
                $key = sprintf('%s:%s:%s:%s',
-                       $connectionInstance->__toString(),
-                       $connectionInstance->getConnectionType(),
+                       $infoInstance->__toString(),
+                       $infoInstance->getProtocolName(),
                        $address,
                        $port
                );
@@ -107,15 +96,15 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke
        /**
         * "Getter" to get a string respresentation of the listener
         *
-        * @param       $listenerInstance       An instance of a Listenable class
-        * @return      $key                            A string representation of the listener for the registry
+        * @param       $infoInstance   An instance of a ShareableInfo class
+        * @return      $key                    A string representation of the listener for the registry
         */
-       private function getRegistryKeyFromListener (Listenable $listenerInstance) {
+       private function getRegistryKeyFromInfo (ShareableInfo $infoInstance) {
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',listenerInstance=' . $listenerInstance->__toString() . ' - ENTERED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',infoInstance=' . $infoInstance->__toString() . ' - ENTERED!');
 
                // Get the key
-               $key = $listenerInstance->getConnectionType();
+               $key = $infoInstance->getProtocolName();
 
                // Debug message
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' .  $key . ' - EXIT!');
@@ -125,23 +114,23 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke
        }
 
        /**
-        * Checks whether the given listener is registered
+        * Checks whether the shared connection info is registered
         *
-        * @param       $listenerInstance       An instance of a Listenable class
-        * @return      $isRegistered           Whether the listener is registered
+        * @param       $infoInstance   An instance of a ShareableInfo class
+        * @return      $isRegistered   Whether the listener is registered
         */
-       private function isListenerRegistered (Listenable $listenerInstance) {
+       private function isInfoRegistered (ShareableInfo $infoInstance) {
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ' - ENTERED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ' - ENTERED!');
 
                // Get the key
-               $key = $this->getRegistryKeyFromListener($listenerInstance);
+               $key = $this->getRegistryKeyFromInfo($infoInstance);
 
                // Determine it
                $isRegistered = $this->instanceExists($key);
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
 
                // Return result
                return $isRegistered;
@@ -151,33 +140,33 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke
         * Checks whether given socket resource is registered. If $socketResource is
         * FALSE only the instance will be checked.
         *
-        * @param       $listenerInstance       An instance of a Listenable class
+        * @param       $infoInstance           An instance of a ShareableInfo class
         * @param       $socketResource         A valid socket resource
         * @return      $isRegistered           Whether the given socket resource is registered
         */
-       public function isSocketRegistered (Listenable $listenerInstance, $socketResource) {
+       public function isSocketRegistered (ShareableInfo $infoInstance, $socketResource) {
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
 
                // Default is not registered
                $isRegistered = FALSE;
 
                // First, check for the instance, there can be only once
-               if ($this->isListenerRegistered($listenerInstance)) {
+               if ($this->isInfoRegistered($infoInstance)) {
                        // That one is found so "get" a registry key from it
-                       $key = $this->getRegistryKeyFromListener($listenerInstance);
+                       $key = $this->getRegistryKeyFromInfo($infoInstance);
 
                        // Debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ' - Trying to get instance ...');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ' - Trying to get instance ...');
 
                        // Get the registry
                        $registryInstance = $this->getInstance($key);
 
                        // "Get" a key for the socket
-                       $socketKey = $this->getSubRegistryKey($listenerInstance);
+                       $socketKey = $this->getSubRegistryKey($infoInstance);
 
                        // Debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...');
 
                        // Is it there?
                        if ($registryInstance->instanceExists($socketKey)) {
@@ -196,7 +185,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke
                } // END - if
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
 
                // Return the result
                return $isRegistered;
@@ -205,39 +194,39 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke
        /**
         * Registeres given socket for listener or throws an exception if it is already registered
         *
-        * @param       $listenerInstance       An instance of a Listenable class
+        * @param       $infoInstance           An instance of a ShareableInfo class
         * @param       $socketResource         A valid socket resource
         * @param       $packageData            Optional raw package data
         * @throws      SocketAlreadyRegisteredException        If the given socket is already registered
         * @return      void
         */
-       public function registerSocket (Listenable $listenerInstance, $socketResource, array $packageData = array()) {
+       public function registerSocket (ShareableInfo $infoInstance, $socketResource, array $packageData = array()) {
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
 
                // Is the socket already registered?
-               if ($this->isSocketRegistered($listenerInstance, $socketResource)) {
+               if ($this->isSocketRegistered($infoInstance, $socketResource)) {
                        // Throw the exception
-                       throw new SocketAlreadyRegisteredException(array($listenerInstance, $socketResource), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
+                       throw new SocketAlreadyRegisteredException(array($infoInstance, $socketResource), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
                } // END - if
 
                // Does the instance exist?
-               if (!$this->isListenerRegistered($listenerInstance)) {
+               if (!$this->isInfoRegistered($infoInstance)) {
                        // No, not found so we create a sub registry (not needed to configure!)
                        $registryInstance = SubRegistry::createSubRegistry();
 
-                       // Now we can create the sub-registry for this listener
-                       $this->addInstance($this->getRegistryKeyFromListener($listenerInstance), $registryInstance);
+                       // Now we can create the sub-registry for this info
+                       $this->addInstance($this->getRegistryKeyFromInfo($infoInstance), $registryInstance);
                } else {
                        // Get the sub-registry back
-                       $registryInstance = $this->getInstance($this->getRegistryKeyFromListener($listenerInstance));
+                       $registryInstance = $this->getInstance($this->getRegistryKeyFromInfo($infoInstance));
                }
 
                // Get a key for sub-registries
-               $socketKey = $this->getSubRegistryKey($listenerInstance);
+               $socketKey = $this->getSubRegistryKey($infoInstance);
 
                // Get a socket container
-               $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $listenerInstance, $packageData));
+               $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $infoInstance, $packageData));
 
                // We have a sub-registry, the socket key and the socket, now we need to put all together
                /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: socketKey=' . $socketKey . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - adding socket container instance ...');
@@ -256,13 +245,13 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ' - ENTERED!');
 
                // The socket must be registered before we can return it
-               if (!$this->isListenerRegistered($listenerInstance)) {
+               if (!$this->isInfoRegistered($listenerInstance)) {
                        // Throw the exception
                        throw new NoSocketRegisteredException ($listenerInstance, self::SOCKET_NOT_REGISTERED);
                } // END - if
 
                // Now get the key from the listener
-               $key = $this->getRegistryKeyFromListener($listenerInstance);
+               $key = $this->getRegistryKeyFromInfo($listenerInstance);
 
                // And get the registry
                $registryInstance = $this->getInstance($key);
@@ -289,21 +278,25 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke
        public function getHandlerInstanceFromPackageData (array $packageData) {
                // Init protocol instance
                $protocolInstance = NULL;
+               //* DIE-DEBUG: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE));
 
                // Get all keys and check them
                foreach ($this->getInstanceRegistry() as $key => $registryInstance) {
                        // Debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
 
                        // This is always a SubRegistry instance
                        foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
                                // Debug message
-                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
+                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
 
                                // Is this a SocketContainer instance and is the address the same?
                                if (($containerInstance instanceof SocketContainer) && ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) {
                                        // Found one, so get the protocol instance and abort any further search
                                        $protocolInstance = $containerInstance->getProtocolInstance();
+
+                                       // Debug message
+                                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',protocolInstance[]=' . gettype($protocolInstance) . ' - FOUND!');
                                        break;
                                } // END - if
                        } // END - foreach