]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/class_HubCoreLoop.php
(no commit message)
[hub.git] / application / hub / main / class_HubCoreLoop.php
index 86f496f5daf5cb26670f82d676555ecf6f1a707d..5304fbea935267807bad2b51b631142a55c03f48 100644 (file)
 class HubCoreLoop extends BaseFrameworkSystem {
        /**
         * Wether the hub is active and running
-      */
+        */
        private $hubActivated = false;
 
        /**
         * Wether the hub is shutting down
-      */
+        */
        private $hubShutsDown = false;
 
        /**
@@ -93,10 +93,10 @@ class HubCoreLoop extends BaseFrameworkSystem {
        private $masterConnector = null;
 
        // Exception codes
-       const EXCEPTION_SOCKET_PROBLEM          = 0xb00;
+       const EXCEPTION_SOCKET_PROBLEM                  = 0xb00;
        const EXCEPTION_HUB_PEER_TIMEOUT                = 0xb01;
        const EXCEPTION_HUB_PEER_FAILED_AUTH    = 0xb02;
-       const EXCEPTION_HELLO_TIMED_OUT         = 0xb03;
+       const EXCEPTION_HELLO_TIMED_OUT                 = 0xb03;
 
        /**
         * The private constructor
@@ -183,8 +183,8 @@ class HubCoreLoop extends BaseFrameworkSystem {
                );
 
                // Transfer readers and writers
-               $this->readPeers = $read;
-               $this->writePeers = $write;
+               $this->readPeers        = $read;
+               $this->writePeers       = $write;
 
                // Return the number
                return $num;
@@ -313,6 +313,9 @@ class HubCoreLoop extends BaseFrameworkSystem {
 
                                // Do the ping and update our authPeer list (LATER!)
                                $this->lastPeerInstance->handlePingPeer();
+
+                               // Avoids a notice...
+                               if (is_null($this->getCurrPeerInstance())) break;
                        } // END - for
                } // END - for
        }
@@ -418,7 +421,14 @@ class HubCoreLoop extends BaseFrameworkSystem {
                                if ($this->getCurrPeerInstance()->equals($peer)) {
                                        // Remove him!
                                        $idx->offsetUnset($idx->key());
+
+                                       // Remove the last instance as well
                                        $this->lastPeerInstance = null;
+
+                                       // Remove the socket as well
+                                       $this->removeSocket($peer);
+
+                                       // Stop searching here
                                        break;
                                }
                        }
@@ -500,11 +510,11 @@ class HubCoreLoop extends BaseFrameworkSystem {
        }
 
        /**
-         * Do the main loop
-         *
-         * @return     void
-         * @throws     SocketCreationException         If the main socket is not a resource
-         */
+               * Do the main loop
+               *
+               * @return       void
+               * @throws       SocketCreationException         If the main socket is not a resource
+               */
         public function coreLoop () {
                // Is the main socket vailid?
                if (!is_resource($this->main_socket)) {
@@ -557,6 +567,9 @@ class HubCoreLoop extends BaseFrameworkSystem {
                                        $this->__toString(),
                                        $e->getMessage()
                                ));
+
+                               // Get new total connected peers
+                               $num = $this->getTotalConnectedPeers();
                        } catch (BrokenPipeException $e) {
                                // Broken pipes are bad for us... :(
                                $this->removePeerFromConnectList();
@@ -569,7 +582,6 @@ class HubCoreLoop extends BaseFrameworkSystem {
                                $num = $this->getTotalConnectedPeers();
                        } catch (FrameworkException $e) {
                                // Catch all other exceptions and output them
-                               echo "CATCH".__LINE__.":".$e->__toString()."\n";
                                hub_exception_handler($e);
 
                                // Get new total connected peers
@@ -609,7 +621,6 @@ class HubCoreLoop extends BaseFrameworkSystem {
                                $num = $this->getTotalConnectedPeers();
                        } catch (FrameworkException $e) {
                                // Catch all exceptions and output them to avoid aborting the program unexpectly
-                               echo "CATCH".__LINE__.":".$e->__toString()."\n";
                                hub_exception_handler($e);
 
                                // Get new total connected peers
@@ -647,7 +658,6 @@ class HubCoreLoop extends BaseFrameworkSystem {
                                $this->announceToMasterHub();
                        } catch (FrameworkException $e) {
                                // Catch all exceptions and output them
-                               echo "CATCH".__LINE__.":".$e->__toString()."\n";
                                hub_exception_handler($e);
                        }
                } // END - else
@@ -660,6 +670,7 @@ class HubCoreLoop extends BaseFrameworkSystem {
         * @return      void
         */
         public function disconnectPeerWithReason ($reasonEntry) {
+               // Default is that we cannot read the peer's IP number
                $ip = "0.0.0.0";
 
                // Try to disconnect here
@@ -671,7 +682,6 @@ class HubCoreLoop extends BaseFrameworkSystem {
                        $this->getCurrPeerInstance()->disconnectWithReason($this->getConfigInstance()->readConfig($reasonEntry));
                } catch (FrameworkException $e) {
                        // Catch all exceptions and output them
-                       echo "CATCH".__LINE__.":".$e->__toString()."\n";
                        hub_exception_handler($e);
                }
 
@@ -707,6 +717,44 @@ class HubCoreLoop extends BaseFrameworkSystem {
                }
        }
 
+       /**
+        * Removes a given peer instance (socket) from all lists
+        *
+        * @param       $peerInstance           An instance of a HubPeer class
+        * @return      void
+        */
+       private function removeSocket (HubPeer $peerInstance) {
+               // Get socket from peer
+               $socket = $peerInstance->getPeerSocket();
+
+               // Search for readers
+               $key = array_search($socket, $this->readPeers, true);
+               if ($key !== false) {
+                       // Remove from reader list
+                       unset($this->readPeers[$key]);
+               }
+
+               // Search for writers
+               $key = array_search($socket, $this->writePeers, true);
+               if ($key !== false) {
+                       // Remove from writer list
+                       unset($this->writePeers[$key]);
+               }
+
+               // Remove from auth peers as well
+               for ($idx = $this->authPeers->getIterator(); $idx->valid(); $idx->next()) {
+                       // Get current entry
+                       $current = $idx->current();
+
+                       // Is it the same?
+                       if ($current->equals($peerInstance)) {
+                               // Remove from auth (-awaiting) list
+                               $idx->offsetUnset($idx->key());
+                               break;
+                       }
+               }
+       }
+
 } // END - class
 
 // [EOF]