]> git.mxchange.org Git - hub.git/commitdiff
New visitor added, handling of refused connections basicly implemented:
authorRoland Häder <roland@mxchange.org>
Fri, 28 May 2010 01:40:31 +0000 (01:40 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 28 May 2010 01:40:31 +0000 (01:40 +0000)
- New ShutdownSocketVisitor added. This visitor should shutdown a socket and
  may notify other objects later on.
- Handling of refused connections (e.g. while announcement phase) basicly
  implemented. Yet there is some work left to do.

.gitattributes
application/hub/config.php
application/hub/main/class_BaseHubSystem.php
application/hub/main/helper/connection/class_BaseConnectionHelper.php
application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php
application/hub/main/package/class_NetworkPackage.php
application/hub/main/visitor/class_
application/hub/main/visitor/socket/.htaccess [new file with mode: 0644]
application/hub/main/visitor/socket/class_ShutdownSocketVisitor.php [new file with mode: 0644]

index e1c935d89ced816793fc47caa9a025c1774de9cc..269bdaf923619a0f5b2a7f7407857013c0b09870 100644 (file)
@@ -317,6 +317,7 @@ application/hub/main/visitor/pool/handler/class_Handler -text
 application/hub/main/visitor/pool/shutdown/.htaccess -text
 application/hub/main/visitor/pool/shutdown/class_Shutdown -text
 application/hub/main/visitor/pool/shutdown/class_ShutdownListenerPoolVisitor.php -text
+application/hub/main/visitor/socket/.htaccess -text
 application/hub/main/visitor/tasks/.htaccess -text
 application/hub/main/visitor/tasks/class_ActiveTaskVisitor.php -text
 application/hub/starter.php -text
index 509c9798bc9a393ac14770e9e228dd3da7857d07..70ff10389513224832e74138467d8b6d8d658e50 100644 (file)
@@ -105,6 +105,9 @@ $cfg->setConfigEntry('shutdown_listener_pool_visitor_class', 'ShutdownListenerPo
 // CFG: SHUTDOWN-TASK-VISITOR-CLASS
 $cfg->setConfigEntry('shutdown_task_visitor_class', 'ShutdownTaskVisitor');
 
+// CFG: SHUTDOWN-SOCKET-VISITOR-CLASS
+$cfg->setConfigEntry('shutdown_socket_visitor_class', 'ShutdownSocketVisitor');
+
 // CFG: ACTIVE-TASK-VISITOR-CLASS
 $cfg->setConfigEntry('active_task_visitor_class', 'ActiveTaskVisitor');
 
@@ -402,5 +405,11 @@ $cfg->setConfigEntry('tcp_buffer_length', 1024);
 // CFG: UDP-BUFFER-LENGTH
 $cfg->setConfigEntry('udp_buffer_length', 1024);
 
+// CFG: TCP-CONNECT-RETRY-MAX
+$cfg->setConfigEntry('tcp_connect_retry_max', 10);
+
+// CFG: UDP-CONNECT-RETRY-MAX
+$cfg->setConfigEntry('udp_connect_retry_max', 10);
+
 // [EOF]
 ?>
index 293da4cc09509d374987219a510f28d3ad7b754f..f1595d1612222bfb5000d2c04cb05a075273fa76 100644 (file)
@@ -104,6 +104,27 @@ class BaseHubSystem extends BaseFrameworkSystem {
        protected final function getPackageInstance () {
                return $this->packageInstance;
        }
+
+       /**
+        * Shuts down a given socket resource. This method does only ease calling
+        * the right visitor.
+        *
+        * @param       $socketResource         A valid socket resource
+        * @return      void
+        */
+       public function shutdownSocket ($socketResource) {
+               // Debug message
+               $this->debugOutput('Shutting down socket ' . $socketResource . ' ...');
+
+               // Set socket resource
+               $this->setSocketResource($socketResource);
+
+               // Get a visitor instance
+               $visitorInstance = ObjectFactory::createObjectByConfiguredName('shutdown_socket_visitor_class');
+
+               // Call the visitor
+               $this->accept($visitorInstance);
+       }
 }
 
 // [EOF]
index 99f1221f12abb3e5db37e79777cbf6534d452072..8c0a4f82d3e55f507a47608f2b44c0e81f8cad22 100644 (file)
@@ -47,6 +47,16 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         */
        private $offset = 0;
 
+       /**
+        * Connect retries for this connection
+        */
+       private $retryCount = 0;
+
+       /**
+        * Wether this connection is shutted down
+        */
+       private $shuttedDown = false;
+
        /**
         * Protected constructor
         *
@@ -118,6 +128,17 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                $this->address = $address;
        }
 
+       /**
+        * "Accept" a visitor by simply calling it back
+        *
+        * @param       $visitorInstance        A Visitable instance
+        * @return      void
+        */
+       protected final function accept (Visitor $visitorInstance) {
+               // Just call the visitor
+               $visitorInstance->visitConnectionHelper($this);
+       }
+
        /**
         * Sends raw package data to the recipient
         *
@@ -166,6 +187,49 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                // Return it
                return $class;
        }
+
+       /**
+        * Checks wether the connect retry is exhausted
+        *
+        * @return      $isExhaused             Wether connect retry is exchausted
+        */
+       public final function isConnectRetryExhausted () {
+               // Construct config entry
+               $configEntry = $this->getProtocol() . '_connect_retry_max';
+
+               // Check it out
+               $isExhausted = ($this->retryCount >=  $this->getConfigInstance()->getConfigEntry($configEntry));
+
+               // Return it
+               return $isExhausted;
+       }
+
+       /**
+        * Increases the connect retry count
+        *
+        * @return      void
+        */
+       public final function increaseConnectRetry () {
+               $this->retryCount++;
+       }
+
+       /**
+        * Marks this connection as shutted down
+        *
+        * @return      void
+        */
+       protected final function markConnectionShutdown () {
+               $this->shuttedDown = true;
+       }
+
+       /**
+        * Getter for shuttedDown
+        *
+        * @return      $shuttedDown    Wether this connection is shutted down
+        */
+       public final function isShuttedDown () {
+               return $this->shuttedDown;
+       }
 }
 
 // [EOF]
index b8c1295dc932e554d85edb041da0e8814076ed45..bf92729dd6eaede720c27528bc5de9b0048e3b59 100644 (file)
@@ -132,6 +132,30 @@ class TcpConnectionHelper extends BaseConnectionHelper {
                // Okay, that should be it. So return it...
                return $socketResource;
        }
+
+       /**
+        * Do the shutdown sequence for TCP connections
+        *
+        * @todo        We may want to implement a filter for ease notification of other objects like our pool
+        * @return      void
+        * @throws      SocketShutdownException         If the current socket could not be shut down
+        */
+       public function doShutdown () {
+               // Clear any previous errors
+               socket_clear_error($this->getSocketResource());
+
+               // Call the shutdown function on the currently set socket
+               if (!@socket_shutdown($this->getSocketResource())) {
+                       // Could not shutdown socket, this is fine if e.g. the other side is not connected, so analyse it
+                       if (socket_last_error($this->getSocketResource()) != 107) {
+                               // Something bad happened while we shutdown a socket
+                               throw new SocketShutdownException($this, BaseListener::EXCEPTION_INVALID_SOCKET);
+                       } // END - if
+               } // END - if
+
+               // Mark this connection as shutted down
+               $this->markConnectionShutdown();
+       }
 }
 
 // [EOF]
index 838f4bf4c9ef9b5d9863f0c5e18275dcb8b8f449..5c7f051947176cb8c9b53d6c3f02a43abc6c62f2 100644 (file)
@@ -243,6 +243,13 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
                // Get the right connection instance
                $connectionInstance = SocketRegistry::createSocketRegistry()->getHandlerInstanceFromPackageData($packageData);
 
+               // Is this connection still alive?
+               if ($connectionInstance->isShuttedDown()) {
+                       // This connection is shutting down
+                       // @TODO We may want to do somthing more here?
+                       return;
+               } // END - if
+
                // Sent it away (we catch exceptions one method above
                $connectionInstance->sendRawPackageData($packageData);
        }
index 6442569ad29aaba9fd26c2bd79226557856ea876..32dd618581b03e19cb686c8521000777e442a55d 100644 (file)
@@ -38,7 +38,7 @@ class ???Visitor extends BaseVisitor implements Visitor {
        /**
         * Creates an instance of this class
         *
-        * @return      $visitorInstance                An instance a Visitorable class
+        * @return      $visitorInstance        An instance a Visitorable class
         */
        public final static function create???Visitor () {
                // Get new instance
diff --git a/application/hub/main/visitor/socket/.htaccess b/application/hub/main/visitor/socket/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/application/hub/main/visitor/socket/class_ShutdownSocketVisitor.php b/application/hub/main/visitor/socket/class_ShutdownSocketVisitor.php
new file mode 100644 (file)
index 0000000..d2abe68
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+/**
+ * A ShutdownSocket visitor
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+class ShutdownSocketVisitor extends BaseVisitor implements Visitor {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+
+               // Set visitor mode
+               $this->setVisitorMode('ShutdownSocket');
+       }
+
+       /**
+        * Creates an instance of this class
+        *
+        * @return      $visitorInstance        An instance a Visitorable class
+        */
+       public final static function createShutdownSocketVisitor () {
+               // Get new instance
+               $visitorInstance = new ShutdownSocketVisitor();
+
+               // Return the prepared instance
+               return $visitorInstance;
+       }
+
+       /**
+        * "Visit" method to do the actual request. Here we want to shutdown the
+        * attached socket.
+        *
+        * @param       $helperInstance         A BaseConnectionHelper instance
+        * @return      void
+        */
+       public function visitConnectionHelper (BaseConnectionHelper $helperInstance) {
+               // Do we have reached the retry count?
+               if ($helperInstance->isConnectRetryExhausted()) {
+                       // Shutdown the connection
+                       $helperInstance->doShutdown();
+               } else {
+                       // We can still move on and retry the connection attempt
+                       $helperInstance->increaseConnectRetry();
+               }
+       }
+}
+
+// [EOF]
+?>