* @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 . */ 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__); } /** * 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 * @param $socketResource A valid socket resource * @return $stateInstance A Stateable class instance */ public static final function createPeerStateInstanceByErrorCode ($errorCode, array $packageData, $socketResource) { // So first we need our lookup table $tableInstance = self::getTableInstance(); // Purge old entries $tableInstance->purgeOldEntriesBySocketResource($socketResource); // 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'; // Register the new peer with its session id $tableInstance->registerPeerByPackageData($packageData, $socketResource); // Then get it $stateInstance = ObjectFactory::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 die(__METHOD__ . ': Lookup!' . "\n"); } // For any purposes, return the state instance return $stateInstance; } /** * Singleton getter for lookup table instances, kept public if we need this * table somewhere else. * * @return $tableInstance An instance of a lookup table */ 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 // Return it return self::$tableInstance; } } // [EOF] ?>