]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/discovery/socket/class_PackageSocketDiscovery.php
HubConnectionHelper is more a factory than a helper class, HelpableHub interface...
[hub.git] / application / hub / main / discovery / socket / class_PackageSocketDiscovery.php
index 2435a4d67aa5dfb9d5b9392d8078b51ac03d511f..4492ca9f676d03800062229b17e0641bd099f7ee 100644 (file)
@@ -48,6 +48,40 @@ class PackageSocketDiscovery extends BaseDiscovery implements DiscoverableSocket
                return $discoveryInstance;
        }
 
+       /**
+        * Tries to dicover the right listener instance
+        *
+        * @param       $protocolName           Protocol name
+        * @param       $packageData            Raw package data
+        * @return      $listenerInstance       An instance of a Listenable instance or null
+        */
+       public function discoverListenerInstance ($protocolName, array $packageData) {
+               /*
+                * Get the listener pool instance, we need to lookup the matching
+                * listener->poolInstance chain there.
+                */
+               $poolInstance = Registry::getRegistry()->getInstance('node')->getListenerPoolInstance();
+
+               // Init listener instance
+               $listenerInstance = null;
+
+               /*
+                * Now we need to choose again. It is wether we are speaking with a hub
+                * or with a client. So just handle it over to all listeners in this
+                * pool.
+                */
+               foreach ($poolInstance->getPoolEntriesInstance()->getArrayFromGroup($protocolName) as $listenerInstance) {
+                       // Does the listener want that package?
+                       if ($listenerInstance->ifListenerAcceptsPackageData($packageData)) {
+                               // This listener likes our package data, so abort here
+                               break;
+                       } // END - if
+               } // END - foreach
+
+               // Return it
+               return $listenerInstance;
+       }
+
        /**
         * Tries to discover the right socket for given package data and returns a
         * matching socket resource for that protocol.
@@ -69,29 +103,29 @@ class PackageSocketDiscovery extends BaseDiscovery implements DiscoverableSocket
                 */
                $protocolName = $tagsInstance->chooseProtocolFromPackageData($packageData);
 
-               /*
-                * Get the listener pool instance, we need to lookup the matching
-                * listener->poolInstance chain there.
-                */
-               $poolInstance = Registry::getRegistry()->getInstance('node')->getListenerPoolInstance();
+               // Get the listener instance
+               $listenerInstance = $this->discoverListenerInstance($protocolName, $packageData);
 
-               // So get the pool entries list first
-               $poolEntriesInstance = $poolInstance->getPoolEntriesInstance();
+               // If there is no listener who wants to have that package, we simply drop it here
+               if (is_null($listenerInstance)) {
+                       // @TODO We may need some locking here
+                       // Abort with no resource
+                       return false;
+               } // END - if
 
                /*
-                * Now we need to choose again. It is wether we are speaking with a hub
-                * or with a client. So just handle it over to all listeners in that
-                * pool.
+                * Now we have the listener instance, we can determine the right
+                * resource to continue. The first step is to get the attached pool
+                * instance and pass over the whole package data to get the right
+                * socket.
                 */
-               foreach ($poolEntriesInstance->getArrayFromGroup($protocolName) as $listenerInstance) {
-                       // Does the listener want that package?
-                       if ($listenerInstance->ifListenerAcceptsPackageData($packageData)) {
-                               // This listener likes our package data!
-                               die(print_r($listenerInstance, true));
-                               break;
-                       } // END - if
-               } // END - foreach
-               die();
+               $socketResource = $listenerInstance->getPoolInstance()->getSocketFromPackageData($packageData);
+
+               // Is it false, the recipient isn't known to us and we have no connection to it
+               if (!is_resource($socketResource)) {
+                       // Create a new socket resource
+                       $socketResource = SocketFactory::createSocketFromPackageData($packageData, $protocolName);
+               } // END - if
 
                // And return it
                return $socketResource;