X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fpools%2Fpeer%2Fclass_DefaultPeerPool.php;h=e2fce1b11fbb3f3b4a73543ea85126e0872a10be;hb=9a1243bea20f7602ef7cdfe912a7db5ee958fba8;hp=fc62b4134b4f321dfa56a4be8a430444e50d732d;hpb=8103a7179cc4bfacfb8078e2ffd96644a424678c;p=hub.git diff --git a/application/hub/main/pools/peer/class_DefaultPeerPool.php b/application/hub/main/pools/peer/class_DefaultPeerPool.php index fc62b4134..e2fce1b11 100644 --- a/application/hub/main/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/main/pools/peer/class_DefaultPeerPool.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -54,12 +54,13 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { * * @param $socketResource A valid socket resource * @return void + * @throws InvalidSocketException If the given socket has an error */ private function validateSocket ($socketResource) { // Is it a valid resource? if (!is_resource($socketResource)) { // Throw an exception - throw new InvalidSocketException(array($this, gettype($socketResource), 0, 'invalid'), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $socketResource), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if // Get error code @@ -67,6 +68,9 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // Is it without any errors? if ($errorCode > 0) { + // Handle the socket error with a faked recipientData array + $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0')); + /* // Get error message $errorMessage = socket_strerror($errorCode); @@ -74,7 +78,8 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { $this->getListenerInstance()->shutdownSocket($socketResource); // And throw again - throw new InvalidSocketException(array($this, gettype($socketResource), $errorCode, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $socketResource, $errorCode, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); + */ } // END - if } @@ -82,13 +87,24 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { * Adds a socket resource to the peer pool * * @param $socketResource A valid (must be!) socket resource + * @param $connectionType Type of connection, can only be 'incoming', 'outgoing' or 'server' * @return void * @throws InvalidSocketException If the given resource is invalid or errorous + * @throws InvalidConnectionTypeException If the provided connection type is not valid */ - public function addPeer ($socketResource) { + public function addPeer ($socketResource, $connectionType) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ': socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',connectionType=' . $connectionType . ' - ENTERED!'); + // Validate the socket $this->validateSocket($socketResource); + // Is the connection type valid? + if (!$this->isValidConnectionType($connectionType)) { + // Is not a valid connection type! + throw new InvalidConnectionTypeException(array($this, $connectionType), self::EXCEPTION_INVALID_CONNECTION_TYPE); + } // END - if + // Default is this peer's IP $peerName = '0.0.0.0'; @@ -96,22 +112,32 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { if ($socketResource != $this->getListenerInstance()->getSocketResource()) { // Try to determine the peer's IP number if (!socket_getpeername($socketResource, $peerName)) { + // Handle the socket error with a faked recipientData array + $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0')); + /* // Get last error $lastError = socket_last_error($socketResource); // Doesn't work! - throw new InvalidSocketException(array($this, gettype($socketResource), $lastError, socket_strerror($lastError)), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $socketResource, $lastError, socket_strerror($lastError)), BaseListener::EXCEPTION_INVALID_SOCKET); + */ } // END - if } else { // Server sockets won't work with socket_getpeername() - $this->debugOutput('POOL: Socket resource is server socket. This is not a bug.'); + self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: Socket resource is server socket (' . $socketResource . '). This is not a bug.'); } - // Output error message - $this->debugOutput('POOL: Adding peer ' . $peerName); + // Debug message + self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: Adding peer ' . $peerName . ',socketResource=' . $socketResource . ',type=' . $connectionType); + + // Construct the array + $socketArray = array( + self::SOCKET_ARRAY_RESOURCE => $socketResource, + self::SOCKET_ARRAY_CONN_TYPE => $connectionType + ); // Add it finally to the pool - $this->addPoolEntry($socketResource); + $this->addPoolEntry($socketArray); } /** @@ -121,7 +147,62 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { */ public final function getAllSockets () { // Get the list - $sockets = $this->getArrayFromList('generic'); + $sockets = $this->getArrayFromList('pool'); + + // Return it + return $sockets; + } + + /** + * Getter for array of all socket arrays + * + * @return $sockets An array with all socket arrays + */ + public final function getAllSingleSockets () { + // Get the array list + $socketArrays = $this->getArrayFromList('pool'); + + // Init socket array + $sockets = array(); + + // "Walk" through all socket arrays + foreach ($socketArrays as $socketArray) { + // Add the socket + $sockets[] = $socketArray[self::SOCKET_ARRAY_RESOURCE]; + } // END - foreach + + // Return it + return $sockets; + } + + /** + * "Getter" for all sockets of specified type + * + * @param $connectionType Type of connection, can only be 'incoming', 'outgoing' or 'server' + * @return $sockets An array with sockets of given type + * @throws InvalidConnectionTypeException If the provided connection type is not valid + */ + public function getSocketsByConnectionType ($connectionType) { + // Is the connection type valid? + if (!$this->isValidConnectionType($connectionType)) { + // Is not a valid connection type! + throw new InvalidConnectionTypeException(array($this, $connectionType), self::EXCEPTION_INVALID_CONNECTION_TYPE); + } // END - if + + // Get the array list + $socketArrays = $this->getArrayFromList('pool'); + + // Init socket array + $sockets = array(); + + // "Walk" through all socket arrays + foreach ($socketArrays as $socketArray) { + // Does it match? + if ($socketArray[self::SOCKET_ARRAY_CONN_TYPE] === $connectionType) { + // Add the socket + $sockets[] = $socketArray[self::SOCKET_ARRAY_RESOURCE]; + } // END - if + } // END - foreach // Return it return $sockets; @@ -131,25 +212,64 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { * "Getter" for a valid socket resource from given packae data. * * @param $packageData Raw package data + * @param $connectionType Type of connection, can be 'incoming', 'outgoing', 'server' or default * @return $socketResource Socket resource + * @throws InvalidConnectionTypeException If the provided connection type is not valid */ - public function getSocketFromPackageData (array $packageData) { + public function getSocketFromPackageData (array $packageData, $connectionType = NULL) { // Default is no socket $socketResource = false; + // Temporary resolve recipient field + $recipientIpArray = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); + + // Make sure it is a valid ip:port array (2 elements) + assert(count($recipientIpArray) == 2); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: Checking ' . count($this->getAllSockets()) . ' socket(s),recipientIpArray[0]=' . $recipientIpArray[0] . ',recipientIpArray[1]=' . $recipientIpArray[1] . ' ...'); + + // Default is all sockets + $sockets = $this->getAllSockets(); + + // Is connection type set? + if ((is_string($connectionType)) && ($this->isValidConnectionType($connectionType))) { + // Then get a list of this type + $sockets = $this->getSocketsByConnectionType($connectionType); + } elseif (is_string($connectionType)) { + // Is not a valid connection type! + throw new InvalidConnectionTypeException(array($this, $connectionType), self::EXCEPTION_INVALID_CONNECTION_TYPE); + } + // Get all sockets and check them, skip the server socket - foreach ($this->getAllSockets() as $socket) { + foreach ($sockets as $socketArray) { // Is this a server socket? - if ($socket === $this->getListenerInstance()->getSocketResource()) { - // Skip this + if ($socketArray[self::SOCKET_ARRAY_RESOURCE] === $this->getListenerInstance()->getSocketResource()) { + // Skip 'server' sockets (local socket) + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: Skipping server socket ' . $socketArray[self::SOCKET_ARRAY_RESOURCE] . ' ...'); continue; } // END - if - // @TODO Check for IP - die(__METHOD__.':'.print_r($packageData, true)); + // Try to get the "peer"'s name + if (!socket_getpeername($socketArray[self::SOCKET_ARRAY_RESOURCE], $peerIp)) { + // Handle the socket error with given package data + $this->handleSocketError(__METHOD__, __LINE__, $socketArray[self::SOCKET_ARRAY_RESOURCE], explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); + } // END - if + + // Get + // If the "peer" IP and recipient is same, use it + if ($peerIp == $recipientIpArray[0]) { + // IPs match, so take the socket and quit this loop + $socketResource = $socketArray[self::SOCKET_ARRAY_RESOURCE]; + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: peerIp=' . $peerIp . ' matches with recipient IP address. Taking socket=' . $socketArray[self::SOCKET_ARRAY_RESOURCE] . ',type=' . $socketArray[self::SOCKET_ARRAY_CONN_TYPE]); + break; + } // END - if } // END - foreach // Return the determined socket resource + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: socketResource[' . gettype($socketResource) . ']=' . $socketResource); return $socketResource; } }