-<?php
-/**
- * A factory class for peer states
- *
- * @author Roland Haeder <webmaster@shipsimu.org>
- * @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
- * @license GNU GPL 3.0 or any newer version
- * @link http://www.shipsimu.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 PeerStateFactory extends ObjectFactory {
- /**
- * Static lookup table instance
- */
- private static $tableInstance = NULL;
-
- /**
- * Protected constructor
- *
- * @return void
- */
- protected function __construct () {
- // Call parent constructor
- 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 $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 createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, array $packageData, $socketResource, $errorCode) {
- // Init state instance, this is better coding practice
- $stateInstance = NULL;
-
- // So first we need our lookup table
- $tableInstance = self::getTableInstance();
-
- /*
- * Now try to purge old entries before looking an entry up. This shall
- * make it sure that only accurate entries can be found.
- */
- try {
- // Purge old entries
- $tableInstance->purgeOldEntriesBySocketResource($socketResource);
- } catch (InvalidSocketException $e) {
- // Just log all errors
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Purging of old entries failed. Message from exception: ' . $e->getMessage());
- }
-
- // Do we have an entry?
- if ($tableInstance->isSenderNewPeer($packageData)) {
- // Debug output
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] errorCode=' . $errorCode);
-
- // 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);
- } elseif ($tableInstance->isSamePeerState($helperInstance, $packageData)) {
- // Debug output
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Peer state unchanged, re-generating old state ...');
-
- /*
- * The peer's state has noot changed, still we have to return a
- * state instance, so generate it here.
- */
- $stateInstance = self::createPeerStateInstanceByName($helperInstance->getPrintableState(), $helperInstance);
- } else {
- // Debug output
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Updating peer state ...');
-
- /*
- * It is an already known peer but with a changed state. So first
- * get an instance of the state.
- */
- $stateInstance = self::createObjectByConfiguredName('peer_' . $errorCode . '_state_class');
-
- // The peer's state has changed, update database now
- $tableInstance->registerPeerState($stateInstance, $packageData);
- }
-
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Peer state has changed from ' . $helperInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . ' (' . $stateInstance->__toString() . ').');
-
- // Set the state in the helper
- $helperInstance->setStateInstance($stateInstance);
-
- // For any purposes, return the state instance
- return $stateInstance;
- }
-
- /**
- * Creates an instance of a configurable peer state and sets it in the
- * given peer instance.
- *
- * @param $stateName Name of the state
- * @param $helperInstance A ConnectionHelper class instance
- * @return $stateInstance A Stateable class instance
- */
- public static final function createPeerStateInstanceByName ($stateName, ConnectionHelper $helperInstance) {
- // Get a class from a configuration entry
- $stateInstance = self::createObjectByConfiguredName('peer_' . $stateName . '_state_class', array($helperInstance));
-
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Peer state 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;
- }
-}
-
-// [EOF]
-?>