]> git.mxchange.org Git - hub.git/commitdiff
(no commit message)
authorRoland Häder <roland@mxchange.org>
Mon, 3 Mar 2008 23:25:57 +0000 (23:25 +0000)
committerRoland Häder <roland@mxchange.org>
Mon, 3 Mar 2008 23:25:57 +0000 (23:25 +0000)
application/hub/main/class_HubCoreLoop.php
application/hub/main/class_HubPeer.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]
index a457b1e1a8ff625cd573820e83796dff9306cc80..4772a956a28cf817c9eecdbd04ce445300cc18e1 100644 (file)
@@ -124,8 +124,8 @@ class HubPeer extends BaseFrameworkSystem {
        //----------------------------------------------------------
        //                              Exceptions
        //----------------------------------------------------------
-       const EXCEPTION_PEER_SOCKET_INVALID     = 0x200;
-       const EXCEPTION_PEER_IP_CHANGED         = 0x201;
+       const EXCEPTION_PEER_SOCKET_INVALID             = 0x200;
+       const EXCEPTION_PEER_IP_CHANGED                 = 0x201;
        const EXCEPTION_PEER_SOCKET_BROKEN              = 0x202;
        //----------------------------------------------------------
 
@@ -192,7 +192,7 @@ class HubPeer extends BaseFrameworkSystem {
        /**
         * Setter for peer socket
         *
-        * @param               $peerSocket             The peer's socket
+        * @param       $peerSocket             The peer's socket
         * @return      void
         */
        public final function setPeerSocket ($peerSocket) {
@@ -203,6 +203,15 @@ class HubPeer extends BaseFrameworkSystem {
                }
        }
 
+       /**
+        * Getter for peer socket
+        *
+        * @return      $peerSocket             The peer's socket
+        */
+       public final function getPeerSocket () {
+               return $this->peerSocket;
+       }
+
        /**
         * Setter for connection timestamp only once
         *
@@ -340,7 +349,7 @@ class HubPeer extends BaseFrameworkSystem {
        /**
         * Disconnects the peer with a special reason (status)
         *
-        * @param               $reason         The special reason
+        * @param       $reason         The special reason
         * @return      void
         */
        public final function disconnectWithReason ($reason) {
@@ -361,7 +370,7 @@ class HubPeer extends BaseFrameworkSystem {
        /**
         * Sends a specified message to the peer's socket
         *
-        * @param               $message                The special message
+        * @param       $message                The special message
         * @return      void
         * @throws      BrokenPipeException             If a pipe to a socket peer is lost
         */
@@ -378,6 +387,17 @@ class HubPeer extends BaseFrameworkSystem {
                        $this->getValidatedIP()
                ));
 
+               // Is the socket still valid?
+               if (!is_resource($this->peerSocket)) {
+                       // The socket is no longer valid!
+                       throw new BrokenPipeException (
+                               array(
+                                       'this'  => $this,
+                                       'code'  => socket_last_error()
+                               ), self::EXCEPTION_PEER_SOCKET_BROKEN
+                       );
+               }
+
                // Send it to the peer
                if (socket_write($this->peerSocket, $message."\n") !== false) {
                        // Set the timestamp and message