* 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 $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) {
+ 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);