*
* @author Roland Haeder <webmaster@shipsimu.org>
* @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
*
/**
* 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
// 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)) {
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;
}
* @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)) {
} // 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
$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 {
// 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());