]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/factories/states/peer/class_PeerStateFactory.php
Some 'abstract' are required
[hub.git] / application / hub / main / factories / states / peer / class_PeerStateFactory.php
index 22e340239ef4bb8813cde995f05bde166fba2588..2837ae6650ed574d83650b9ead357fe725464cb7 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -25,7 +25,7 @@ class PeerStateFactory extends ObjectFactory {
        /**
         * Static lookup table instance
         */
-       private static $tableInstance = null;
+       private static $tableInstance = NULL;
 
        /**
         * Protected constructor
@@ -37,53 +37,100 @@ class PeerStateFactory extends ObjectFactory {
                parent::__construct(__CLASS__);
        }
 
+       /**
+        * Singleton getter for lookup table instances, kept public if we need this
+        * table somewhere else.
+        *
+        * @return      $tableInstance  An instance of a lookup table
+        */
+       public static final function getTableInstance () {
+               // Is the instance null?
+               if (is_null(self::$tableInstance)) {
+                       // Get a new one
+                       self::$tableInstance = self::createObjectByConfiguredName('peer_state_lookup_db_wrapper_class');
+               } // END - if
+
+               // Return it
+               return self::$tableInstance;
+       }
+
        /**
         * Creates a peer state instance based on errorCode if no entry is found in the lookup table
         * for the peer given in $packageData 'sender' element or it changes the state if it differs
         * from current state.
         *
-        * @param       $errorCode              The last error code
-        * @param       $packageData    Raw package data
-        * @return      $stateInstance  A Stateable class instance
+        * @param       $helperInstance         An instance of a ConnectionHelper class
+        * @param       $packageData            Raw package data
+        * @param       $socketResource         A valid socket resource
+        * @param       $errorCode                      The last error code
+        * @return      $stateInstance          A Stateable class instance
         */
-       public static final function createPeerStateInstanceByErrorCode ($errorCode, array $packageData) {
+       public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, array $packageData, $socketResource, $errorCode) {
                // So first we need our lookup table
                $tableInstance = self::getTableInstance();
 
+               // Purge old entries
+               try {
+                       $tableInstance->purgeOldEntriesBySocketResource($socketResource);
+               } catch (InvalidSocketException $e) {
+                       // Just log all errors
+                       $tableInstance->debugOutput('PEER-STATE-FACTORY: Purging of old entries failed. Message from exception: ' . $e->getMessage());
+               }
+
                // Do we have an entry?
                if ($tableInstance->isSenderNewPeer($packageData)) {
-                       // Is a new peer so create the state instance based on error code, first we need a config entry
-                       $configEntry = 'peer_state_' . $errorCode . '_class';
+                       // Debug output
+                       $tableInstance->debugOutput('PEER-STATE-FACTORY: errorCode=' . $errorCode);
 
-                       // Then get it
-                       $stateInstance = ObjectFactory::createObjectByConfiguredName($configEntry);
+                       // Register the new peer with its session id
+                       $tableInstance->registerPeerByPackageData($packageData, $socketResource);
+
+                       /*
+                        * It is a new peer so create the state instance based on error
+                        * code and get an instance from it.
+                        */
+                       $stateInstance = self::createObjectByConfiguredName('peer_' . $errorCode . '_state_class');
 
                        // And register it with the lookup table
                        $tableInstance->registerPeerState($stateInstance, $packageData);
                } else {
                        // It is a known peer, so we need to check if the state has changed
-                       die(__METHOD__ . ': Lookup!' . "\n");
+                       $tableInstance->debugBackTrace(__METHOD__ . ': Lookup!' . "\n");
                }
 
+               // Debug message
+               $stateInstance->debugOutput('PEER-STATE[' . __LINE__ . ']: Has changed from ' . $helperInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . '.');
+
+               // Set the state in the helper
+               $helperInstance->setStateInstance($stateInstance);
+
                // For any purposes, return the state instance
                return $stateInstance;
        }
 
        /**
-        * Singleton getter for lookup table instances, kept public if we need this
-        * table somewhere else.
+        * Creates an instance of a configurable peer state and sets it in the
+        * given peer instance.
         *
-        * @return      $tableInstance  An instance of a lookup table
+        * @param       $stateName                      Name of the state
+        * @param       $helperInstance         A ConnectionHelper class instance
+        * @return      $stateInstance          A Stateable class instance
         */
-       public final static function getTableInstance () {
-               // Is the instance null?
-               if (is_null(self::$tableInstance)) {
-                       // Get a new one
-                       self::$tableInstance = ObjectFactory::createObjectByConfiguredName('peer_state_lookup_table_class');
-               } // END - if
+       public static final function createPeerStateInstanceByName ($stateName, ConnectionHelper $helperInstance) {
+               // Then construct the class' configuraton entry
+               $className = 'peer_' . $stateName . '_state_class';
 
-               // Return it
-               return self::$tableInstance;
+               // Get a class from that configuration entry
+               $stateInstance = self::createObjectByConfiguredName($className, array($helperInstance));
+
+               // Debug message
+               $stateInstance->debugOutput('PEER-STATE[' . __LINE__ . ']: Has changed from ' . $helperInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . ' (' . $stateInstance->__toString() . ').');
+
+               // Once we have that state, set it in the peer instance
+               $helperInstance->setStateInstance($stateInstance);
+
+               // For any purposes, return the state instance
+               return $stateInstance;
        }
 }