* @version 0.0.0 * @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 * * 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 . */ 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) { // 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)) { // Debug output $tableInstance->debugOutput('PEER-STATE-FACTORY: errorCode=' . $errorCode); // Is a new peer so create the state instance based on error code, first we need a config entry $configEntry = 'peer_' . $errorCode . '_state_class'; // Register the new peer with its session id $tableInstance->registerPeerByPackageData($packageData, $socketResource); // Then get it $stateInstance = self::createObjectByConfiguredName($configEntry); // 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 $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; } /** * 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) { // Then construct the class' configuraton entry $className = 'peer_' . $stateName . '_state_class'; // 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; } } // [EOF] ?>