/**
* A default peer pool class
*
- * @author Roland Haeder <webmaster@ship-simu.org>
+ * @author Roland Haeder <webmaster@shipsimu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team
* @license GNU GPL 3.0 or any newer version
- * @link http://www.ship-simu.org
+ * @link http://www.shipsimu.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
* @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, $connectionType) {
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DEFAULT-PEER-POOL[' . __METHOD__ . ':' . __LINE__ . ']: 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';
} // 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[' . __METHOD__ . ':' . __LINE__ . ']: Socket resource is server socket (' . $socketResource . '). This is not a bug.');
}
// Debug message
- $this->debugOutput('POOL: Adding peer ' . $peerName . ',socketResource=' . $socketResource);
+ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __LINE__ . ']: Adding peer ' . $peerName . ',socketResource=' . $socketResource . ',type=' . $connectionType);
// Construct the array
$socketArray = array(
// "Walk" through all socket arrays
foreach ($socketArrays as $socketArray) {
// Add the socket
- $sockets[] = $socketArray[self::SOCKET_ARRAY_RESOURCE];
+ array_push($sockets, $socketArray[self::SOCKET_ARRAY_RESOURCE]);
} // END - foreach
// Return it
*
* @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');
// Does it match?
if ($socketArray[self::SOCKET_ARRAY_CONN_TYPE] === $connectionType) {
// Add the socket
- $sockets[] = $socketArray[self::SOCKET_ARRAY_RESOURCE];
+ array_push($sockets, $socketArray[self::SOCKET_ARRAY_RESOURCE]);
} // END - if
} // END - foreach
*/
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]));
+ $unlArray = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
- // Make sure it is a valid ip:port array (2 elements)
- assert(count($recipientIpArray) == 2);
+ // Make sure it is a valid Universal Node Locator array (3 elements)
+ assert(count($unlArray) == 3);
// Debug message
- /* NOISY-DEBUG: */ $this->debugOutput('POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),recipientIpArray[0]=' . $recipientIpArray[0] . ',recipientIpArray[1]=' . $recipientIpArray[1] . ' ...');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __LINE__ . ']: Checking ' . count($this->getAllSockets()) . ' socket(s),unlArray[0]=' . $unlArray[0] . ',unlArray[1]=' . $unlArray[1] . ' ...');
// Default is all sockets
$sockets = $this->getAllSockets();
// Is this a server socket?
if ($socketArray[self::SOCKET_ARRAY_RESOURCE] === $this->getListenerInstance()->getSocketResource()) {
// Skip 'server' sockets (local socket)
- /* NOISY-DEBUG: */ $this->debugOutput('POOL: Skipping server socket ' . $socketArray[self::SOCKET_ARRAY_RESOURCE] . ' ...');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __LINE__ . ']: Skipping server socket ' . $socketArray[self::SOCKET_ARRAY_RESOURCE] . ' ...');
continue;
} // END - if
// Get
// If the "peer" IP and recipient is same, use it
- if ($peerIp == $recipientIpArray[0]) {
+ if ($peerIp == $unlArray[0]) {
// IPs match, so take the socket and quit this loop
$socketResource = $socketArray[self::SOCKET_ARRAY_RESOURCE];
// Debug message
- /* NOISY-DEBUG: */ $this->debugOutput('POOL: peerIp=' . $peerIp . ' matches with recipient IP address. Taking socket=' . $socketArray[self::SOCKET_ARRAY_RESOURCE] . ',type=' . $socketArray[self::SOCKET_ARRAY_CONN_TYPE]);
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __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[' . __METHOD__ . ':' . __LINE__ . ']: socketResource[' . gettype($socketResource) . ']=' . $socketResource);
return $socketResource;
}
}