* @version 0.0.0 * @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 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class DefaultPeerPool extends BasePool implements PoolablePeer { /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Creates an instance of this class * * @param $listenerInstance A Listenable instance * @return $poolInstance An instance a Poolable class */ public static final function createDefaultPeerPool (Listenable $listenerInstance) { // Get new instance $poolInstance = new DefaultPeerPool(); // Set the application instance $poolInstance->setListenerInstance($listenerInstance); // Return the prepared instance return $poolInstance; } /** * Validates given socket * * @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, $socketResource), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if // Get error code $errorCode = socket_last_error($socketResource); // 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); // Shutdown this socket $this->getListenerInstance()->shutdownSocket($socketResource); // And throw again throw new InvalidSocketException(array($this, $socketResource, $errorCode, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); */ } // END - if } /** * Adds a socket resource to the peer pool * * @param $socketResource A valid (must be!) socket resource * @return void * @throws InvalidSocketException If the given resource is invalid or errorous */ public function addPeer ($socketResource) { // Validate the socket $this->validateSocket($socketResource); // Default is this peer's IP $peerName = '0.0.0.0'; // The socket resource should not match server socket 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, $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 (' . $socketResource . '). This is not a bug.'); } // Debug message $this->debugOutput('POOL: Adding peer ' . $peerName . ',socketResource=' . $socketResource); // Add it finally to the pool $this->addPoolEntry($socketResource); } /** * Getter for array of all socket resources * * @return $sockets An array with all sockets */ public final function getAllSockets () { // Get the list $sockets = $this->getArrayFromList('pool'); // Return it return $sockets; } /** * "Getter" for a valid socket resource from given packae data. * * @param $packageData Raw package data * @return $socketResource Socket resource */ public function getSocketFromPackageData (array $packageData) { // Default is no socket $socketResource = false; // Temporary resolve recipient field $recipientIpArray = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], $packageData[NetworkPackage::PACKAGE_DATA_PROTOCOL])); // 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] . ' ...'); // Get all sockets and check them, skip the server socket foreach ($this->getAllSockets() as $socket) { // Is this a server socket? if ($socket === $this->getListenerInstance()->getSocketResource()) { // Skip 'server' sockets (local socket) /* NOISY-DEBUG: */ $this->debugOutput('POOL: Skipping local socket ' . $socket . ' ...'); continue; } // END - if // Try to get the "peer"'s name if (!socket_getpeername($socket, $peerIp)) { // Handle the socket error with given package data $this->handleSocketError(__METHOD__, __LINE__, $socket, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); } // END - if // 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; // Debug message /* NOISY-DEBUG: */ $this->debugOutput('POOL: peerIp=' . $peerIp . ' matches with recipient IP address. Taking socket=' . $socket); break; } // END - if } // END - foreach // Return the determined socket resource /* NOISY-DEBUG: */ $this->debugOutput('POOL: socketResource[' . gettype($socketResource) . ']=' . $socketResource); return $socketResource; } } // [EOF] ?>