X-Git-Url: https://git.mxchange.org/?p=hub.git;a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fdiscovery%2Frecipient%2Fsocket%2Fclass_PackageSocketDiscovery.php;h=698c44cb186f296907f0061fd88d76f2d76d0f54;hp=b36aee53aedfb3a88942d11a19d094caea0592d8;hb=73aff29b9bc78031853b0b8c0fe0a8e04f66ac29;hpb=0553b20da2815d73580a07212106ee3be7aa3407 diff --git a/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php b/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php index b36aee53a..698c44cb1 100644 --- a/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php +++ b/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * @@ -51,13 +51,13 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera /** * Tries to dicover the right listener instance * - * @param $protocolName Protocol name + * @param $protocolInstance An instance of a HandleableProtocol class * @param $packageData Raw package data * @return $listenerInstance An instance of a Listenable instance or null */ - public function discoverListenerInstance ($protocolName, array $packageData) { + public function discoverListenerInstance (HandleableProtocol $protocolInstance, array $packageData) { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: protocolName=' . $protocolName . ' - ENTERED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: protocolInstance=' . $protocolInstance->__toString() . ' - CALLED!'); /* * Get the listener pool instance, we need to lookup the matching @@ -68,14 +68,23 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera // Init listener instance $listenerInstance = NULL; + // Get handler name + $protocolName = $protocolInstance->getProtocolName(); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: protocolName=' . $protocolName . ',poolEntriesInstance=' . $poolInstance->getPoolEntriesInstance()->__toString()); + /* * Now we need to choose again. It is whether 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) { + foreach ($poolInstance->getPoolEntriesInstance()->getArrayFromList($protocolName) as $listenerInstance) { // Debug output - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: protocolName=' . $protocolName . ',listenerInstance=' . $listenerInstance->__toString()); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: protocolName=' . $protocolName . ',listenerInstance[' . gettype($listenerInstance) . ']=' . $listenerInstance); + + // Make sure the instance is valid + assert($listenerInstance instanceof Listenable); // Does the listener want that package? if ($listenerInstance->ifListenerAcceptsPackageData($packageData)) { @@ -84,10 +93,13 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera break; } // END - if - // Debug output + // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Listener is NOT accepting package data.'); } // END - foreach + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: listenerInstance=' . $listenerInstance->__toString()); + // Return it return $listenerInstance; } @@ -101,21 +113,30 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera * @return $socketResource A valid socket resource or FALSE if an error occured * @throws NoListGroupException If the procol group is not found in peer list * @throws NullPointerException If listenerInstance is NULL + * @throws InvalidUnlException If the provided UNL cannot be validated by the protocol handler */ public function discoverSocket (array $packageData, $connectionType) { - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: connectionType=' . $connectionType . ' - CALLED!'); + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: connectionType=' . $connectionType . ' - CALLED!'); - // Assert on type + // Assert on type and recipient assert($connectionType != BaseConnectionHelper::CONNECTION_TYPE_SERVER); + assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); - // Determine protocol name - $protocolName = $this->determineProtocolByPackageData($packageData); + // Determine protocol instance + $protocolInstance = ProtocolDiscovery::determineProtocolByPackageData($packageData); - // Is it the same? - assert(strtoupper($protocolName) == $packageData[NetworkPackage::PACKAGE_DATA_PROTOCOL]); + // Is it valid? + assert($protocolInstance instanceof HandleableProtocol); + + // Does the UNL validate? + if (!$protocolInstance->isValidUniversalNodeLocatorByPackageData($packageData)) { + // Not valid, throw exception + throw new InvalidUnlException(array($this, $protocolInstance, $packageData), BaseHubSystem::EXCEPTION_INVALID_UNL); + } // END - if // Get the listener instance - $listenerInstance = $this->discoverListenerInstance($protocolName, $packageData); + $listenerInstance = $this->discoverListenerInstance($protocolInstance, $packageData); // If there is no listener who wants to have that package, we simply drop it here if (is_null($listenerInstance)) { @@ -124,7 +145,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera } // END - if // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('protocolName=' . $protocolName . ',packageData=' . print_r($packageData, TRUE)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('protocolInstance=' . $protocolInstance . ',packageData=' . print_r($packageData, TRUE)); /* * Now we have the listener instance, we can determine the right @@ -135,6 +156,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera $socketResource = $listenerInstance->getPoolInstance()->getSocketFromPackageData($packageData, $connectionType); // Debug message + // @TODO FIXME: I don't like these abuse of variables, better strict types if (is_resource($socketResource)) { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',error=' . socket_strerror(socket_last_error($socketResource)) . ',packageData=' . print_r($packageData, TRUE)); } else { @@ -146,10 +168,10 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera // Try to create a new socket resource try { // Possibly noisy debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Trying to establish a ' . strtoupper($protocolName) . ' connection to ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ' ...'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Trying to establish a ' . strtoupper($protocolInstance) . ' connection to ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ' ...'); // Get a socket resource from our factory (if succeeded) - $socketResource = SocketFactory::createSocketFromPackageData($packageData, $protocolName); + $socketResource = SocketFactory::createSocketFromPackageData($packageData, $protocolInstance); } catch (SocketConnectionException $e) { // The connection fails of being established, so log it away self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Caught ' . $e->__toString() . ',message=' . $e->getMessage());