X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fpools%2Fpeer%2Fclass_DefaultPeerPool.php;h=f872b76aa464038bba093884769e2a36cdfc1402;hb=02d2e489ecf43bd4386c2632fead7eea5dba0ad2;hp=6968fc2a902fd1a65861e63487c9a7d6aeecf9c8;hpb=62cc119003be95ce6448959e940290157ac0711e;p=hub.git diff --git a/application/hub/main/pools/peer/class_DefaultPeerPool.php b/application/hub/main/pools/peer/class_DefaultPeerPool.php index 6968fc2a9..f872b76aa 100644 --- a/application/hub/main/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/main/pools/peer/class_DefaultPeerPool.php @@ -87,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'; @@ -113,28 +124,20 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { } // END - if } else { // Server sockets won't work with socket_getpeername() - $this->debugOutput('POOL: Socket resource is server socket (' . $socketResource . '). This is not a bug.'); + self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: Socket resource is server socket (' . $socketResource . '). This is not a bug.'); } // Debug message - $this->debugOutput('POOL: Adding peer ' . $peerName . ',socketResource=' . $socketResource); - - // Create the fake array - $packageData = array( - NetworkPackage::PACKAGE_DATA_SENDER => $peerName . ':0', - NetworkPackage::PACKAGE_DATA_RECIPIENT => $this->getSessionId(), - NetworkPackage::PACKAGE_DATA_PROTOCOL => $this->getListenerInstance()->getProtocol(), - NetworkPackage::PACKAGE_DATA_STATUS => NetworkPackage::PACKAGE_STATUS_FAKED - ); + self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: Adding peer ' . $peerName . ',socketResource=' . $socketResource . ',type=' . $connectionType); - // Get a socket registry - $registryInstance = SocketRegistryFactory::createSocketRegistryInstance(); - - // Register the socket with the registry and with a half-fake array - $registryInstance->registerSocket($this->getListenerInstane(), $socketResource, $packageData); + // 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); } /** @@ -150,53 +153,123 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { 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 + array_push($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 + array_push($sockets, $socketArray[self::SOCKET_ARRAY_RESOURCE]); + } // END - if + } // END - foreach + + // Return it + return $sockets; + } + /** * "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; + $socketResource = FALSE; // Temporary resolve recipient field - $recipientIpArray = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], $packageData[NetworkPackage::PACKAGE_DATA_PROTOCOL])); + $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: */ $this->debugOutput('POOL: Checking ' . count($this->getAllSockets()) . ' socket(s), recipientIpArray[0]=' . $recipientIpArray[0] . ' ...'); + /* 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()) { + if ($socketArray[self::SOCKET_ARRAY_RESOURCE] === $this->getListenerInstance()->getSocketResource()) { // Skip 'server' sockets (local socket) - /* NOISY-DEBUG: */ $this->debugOutput('POOL: Skipping local socket ' . $socket . ' ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: Skipping server socket ' . $socketArray[self::SOCKET_ARRAY_RESOURCE] . ' ...'); continue; } // END - if // Try to get the "peer"'s name - if (!socket_getpeername($socket, $peerIp)) { + if (!socket_getpeername($socketArray[self::SOCKET_ARRAY_RESOURCE], $peerIp)) { // Handle the socket error with given package data - $this->handleSocketError(__METHOD__, __LINE__, $socket, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); + $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 = $socket; + $socketResource = $socketArray[self::SOCKET_ARRAY_RESOURCE]; // Debug message - /* NOISY-DEBUG: */ $this->debugOutput('POOL: peerIp=' . $peerIp . ' matches with recipient IP address. Taking socket=' . $socket); + /* 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: */ $this->debugOutput('POOL: socketResource[' . gettype($socketResource) . ']=' . $socketResource); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: socketResource[' . gettype($socketResource) . ']=' . $socketResource); return $socketResource; } }