/**
* Static lookup table instance
*/
- private static $tableInstance = null;
+ private static $tableInstance = NULL;
/**
* Protected 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 $errorCode The last error code
+ * @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, $socketResource) {
+ public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, array $packageData, $socketResource, $errorCode) {
// So first we need our lookup table
$tableInstance = self::getTableInstance();
// Purge old entries
- $tableInstance->purgeOldEntriesBySocketResource($socketResource);
+ 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);
// Register the new peer with its session id
$tableInstance->registerPeerByPackageData($packageData, $socketResource);
- // Then get it
- $stateInstance = ObjectFactory::createObjectByConfiguredName($configEntry);
+ /*
+ * 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 static final function getTableInstance () {
- // Is the instance null?
- if (is_null(self::$tableInstance)) {
- // Get a new one
- self::$tableInstance = ObjectFactory::createObjectByConfiguredName('node_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;
}
}