]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/pools/peer/class_DefaultPeerPool.php
Added more UNL (Universal Node Locator) stuff:
[hub.git] / application / hub / main / pools / peer / class_DefaultPeerPool.php
index b0d7621612f7e869b6e09a18740c58b35fffc0ec..5c5fdbcbcd839e32176e6466036942f9647d3b38 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 - 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
@@ -90,11 +90,21 @@ class DefaultPeerPool extends BasePool implements PoolablePeer {
         * @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';
 
@@ -114,11 +124,11 @@ 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.');
                }
 
                // 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(
@@ -158,7 +168,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer {
                // "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
@@ -170,8 +180,15 @@ class DefaultPeerPool extends BasePool implements PoolablePeer {
         *
         * @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');
 
@@ -183,7 +200,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer {
                        // 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
 
@@ -201,16 +218,16 @@ class DefaultPeerPool extends BasePool implements PoolablePeer {
         */
        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();
@@ -229,7 +246,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer {
                        // 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
 
@@ -241,18 +258,18 @@ class DefaultPeerPool extends BasePool implements PoolablePeer {
 
                        // 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;
        }
 }