]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/pools/peer/class_DefaultPeerPool.php
Updated 'core'.
[hub.git] / application / hub / main / pools / peer / class_DefaultPeerPool.php
index b052c2a5d34070265cc460a76d2cf7ff1aea2856..90e76fde8207dbfc8aa8677ea609af4b916feca7 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * 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 - 2015 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
@@ -69,7 +69,7 @@ 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($socketResource, array('0.0.0.0', '0'));
+                       $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
                        /*
                        // Get error message
                        $errorMessage = socket_strerror($errorCode);
@@ -87,22 +87,33 @@ 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('DEFAULT-PEER-POOL[' . __METHOD__ . ':' . __LINE__ . ']: socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',connectionType=' . $connectionType . ' - CALLED!');
+
                // 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';
 
                // 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)) {
+                       if (!socket_getpeername($socketResource, $peerName)) {
                                // Handle the socket error with a faked recipientData array
-                               $this->handleSocketError($socketResource, array('0.0.0.0', '0'));
+                               $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
                                /*
                                // Get last error
                                $lastError = socket_last_error($socketResource);
@@ -113,14 +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[' . __METHOD__ . ':' . __LINE__ . ']: Socket resource is server socket (' . $socketResource . '). This is not a bug.');
                }
 
-               // Output error message
-               $this->debugOutput('POOL: Adding peer ' . $peerName . ', socketResource=' . $socketResource);
+               // Debug message
+               self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __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);
        }
 
        /**
@@ -136,53 +153,128 @@ 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]));
+               // Resolve recipient (UNL) into a handler instance
+               $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($packageData);
 
-               // Make sure it is a valid ip:port array (2 elements)
-               assert(count($recipientIpArray) == 2);
+               // Get UNL data
+               $unlData = $handlerInstance->getUniversalNodeLocatorDataArray();
+
+               // Make sure it is a valid Universal Node Locator array (3 elements)
+               assert(isset($unlData[UniversalNodeLocator::UNL_PART_PROTOCOL]));
+               assert(isset($unlData[UniversalNodeLocator::UNL_PART_ADDRESS]));
+               assert(isset($unlData[UniversalNodeLocator::UNL_PART_PORT]));
 
                // Debug message
-               /* NOISY-DEBUG: */ $this->debugOutput('POOL: Checking ' . count($this->getAllSockets()) . ' socket(s), recipientIpArray[0]=' . $recipientIpArray[0] . ' ...');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __METHOD__ . ':' . __LINE__ . ']: Checking ' . count($this->getAllSockets()) . ' socket(s),unlData[' . UniversalNodeLocator::UNL_PART_ADDRESS . ']=' . $unlData[UniversalNodeLocator::UNL_PART_ADDRESS] . ',unlData[' . UniversalNodeLocator::UNL_PART_PORT . ']=' . $unlData[UniversalNodeLocator::UNL_PART_PORT] . ' ...');
+
+               // 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[' . __METHOD__ . ':' . __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($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]) {
+                       if ($peerIp == $unlData[UniversalNodeLocator::UNL_PART_ADDRESS]) {
                                // 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[' . __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;
        }
 }