]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/pools/peer/class_DefaultPeerPool.php
Opps, wrong name :(
[hub.git] / application / hub / main / pools / peer / class_DefaultPeerPool.php
index b0d7621612f7e869b6e09a18740c58b35fffc0ec..f872b76aa464038bba093884769e2a36cdfc1402 100644 (file)
@@ -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(__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';
 
@@ -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[' . __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[' . __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]));
+               $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] . ',recipientIpArray[1]=' . $recipientIpArray[1] . ' ...');
+               /* 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();
@@ -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[' . __LINE__ . ']: Skipping server socket ' . $socketArray[self::SOCKET_ARRAY_RESOURCE] . ' ...');
                                continue;
                        } // END - if
 
@@ -246,13 +263,13 @@ class DefaultPeerPool extends BasePool implements PoolablePeer {
                                $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[' . __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;
        }
 }