From 06298a0f2592ac965b303b0f65dfa4f9b68855a3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 4 Nov 2011 19:36:47 +0000 Subject: [PATCH] Hub project continued: - ifPeerStateHasChanged() introduced (also to interface) - Some if() block comments added (minor) - Other minor improvements --- application/hub/class_ApplicationHelper.php | 4 +- .../peer_states/class_LookupablePeerState.php | 10 +++++ .../class_PeerStateLookupDatabaseWrapper.php | 37 ++++++++++++---- .../socket/class_PackageSocketDiscovery.php | 3 ++ .../states/node/class_NodeStateFactory.php | 2 +- .../states/peer/class_PeerStateFactory.php | 43 +++++++++++++------ .../main/listener/udp/class_UdpListener.php | 2 +- .../hub/main/package/class_NetworkPackage.php | 16 ++++++- .../socket/class_ShutdownSocketVisitor.php | 2 +- 9 files changed, 92 insertions(+), 27 deletions(-) diff --git a/application/hub/class_ApplicationHelper.php b/application/hub/class_ApplicationHelper.php index dafd73e55..49d16fde6 100644 --- a/application/hub/class_ApplicationHelper.php +++ b/application/hub/class_ApplicationHelper.php @@ -78,7 +78,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica // Is the instance there? if (is_null(self::$thisInstance)) { self::$thisInstance = new ApplicationHelper(); - } + } // END - if // Return the instance return self::$thisInstance; @@ -217,7 +217,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica // Walk through all messages foreach ($messageList as $message) { die('MSG:' . $message); - } + } // END - foreach } /** diff --git a/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php b/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php index e530c1c76..fc8e1df6f 100644 --- a/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php +++ b/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php @@ -55,6 +55,16 @@ interface LookupablePeerState extends Lookupable { * @return void */ function purgeOldEntriesBySocketResource ($socketResource); + + /** + * Checks wether a given peer state (in helper instance) has changed from + * stored one in database. + * + * @param $helperInstance An instance of a ConnectionHelper class + * @param $packageData Valid package data array + * @return $isPeerStateChanged Wether the peer's state has changed + */ + function ifPeerStateHasChanged (ConnectionHelper $helperInstance, array $packageData); } // [EOF] diff --git a/application/hub/main/database/wrapper/states/class_PeerStateLookupDatabaseWrapper.php b/application/hub/main/database/wrapper/states/class_PeerStateLookupDatabaseWrapper.php index e5ebe3e65..97becaf7a 100644 --- a/application/hub/main/database/wrapper/states/class_PeerStateLookupDatabaseWrapper.php +++ b/application/hub/main/database/wrapper/states/class_PeerStateLookupDatabaseWrapper.php @@ -79,7 +79,9 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements Look * @return $isNewPeer Wether 'sender' is a new peer to this peer */ public function isSenderNewPeer (array $packageData, StoreableCriteria $dataSetInstance = NULL) { - //* DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: ' . __FUNCTION__ . ' called with packageData()=' . count($packageData) . ' - ENTERED!'); + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: ' . __FUNCTION__ . ' called with packageData()=' . count($packageData) . ' - ENTERED!'); + // Is always new peer by default $isNewPeer = true; @@ -90,7 +92,7 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements Look } // END - if // Get a search criteria instance - //* DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: Searching for sender ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER]); + //* NOISY-DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: Searching for sender ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER]); $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Add 'sender' as the peer's IP address @@ -107,11 +109,11 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements Look $entries = $this->doSelectCountByCriteria($searchInstance, array(self::DB_COLUMN_PEER_SESSION_ID => true)); // Is it there? - //* DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: entries=' . $entries); + //* NOISY-DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: entries=' . $entries); $isNewPeer = ($entries != 1); // Return the result - //* DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: isNewPeer=' . intval($isNewPeer) . ' - EXIT!'); + //* NOISY-DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: isNewPeer=' . intval($isNewPeer) . ' - EXIT!'); return $isNewPeer; } @@ -157,7 +159,7 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements Look $this->queryInsertDataSet($dataSetInstance, array(self::DB_COLUMN_PEER_SESSION_ID => true)); // Debug message - /* DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER] . ' has been registered.'); + /* NOISY-DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER] . ' has been registered.'); } /** @@ -178,7 +180,7 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements Look // Add session id $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]); - $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_STATE, $stateInstance->getStateName()); + $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_STATE , $stateInstance->getStateName()); // Try to resolve sender's session id $senderData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_SENDER])); @@ -200,7 +202,7 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements Look } // Debug message - /* DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER] . ' has been registered/updated with state ' . $stateInstance->getStateName()); + /* NOISY-DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER] . ' has been registered/updated with state ' . $stateInstance->getStateName()); } /** @@ -222,7 +224,26 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements Look } // END - if // Debug message - /* DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: peerName=' . $peerName . ',peerPort=' . $peerPort . ' - UNFINISHED!'); + /* NOISY-DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: peerName=' . $peerName . ',peerPort=' . $peerPort . ' - UNFINISHED!'); + } + + /** + * Checks wether a given peer state (in helper instance) has changed from + * stored one in database. + * + * @param $helperInstance An instance of a ConnectionHelper class + * @param $packageData Valid package data array + * @return $isPeerStateChanged Wether the peer's state has changed + */ + public function ifPeerStateHasChanged (ConnectionHelper $helperInstance, array $packageData) { + // Debug message + /* NOISY-DEBUG: */ $this->debugOutput('DATABASE-WRAPPER: State ' . $helperInstance->getPrintableState() . ' needs to be checked it it has changed ...'); + + // By default the peer's state has not changed + $isPeerStateChanged = false; + + // Return it + return $isPeerStateChanged; } } diff --git a/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php b/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php index 024e45a5a..f061c1791 100644 --- a/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php +++ b/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php @@ -132,6 +132,9 @@ class PackageSocketDiscovery extends BaseHubDiscovery implements DiscoverableSoc // Get the helper instance from registry $helperInstance = Registry::getRegistry()->getInstance('connection'); + // Possibly noisy debug message + /* NOISY-DEBUG: */ $this->debugOutput('SOCKET-DISCOVERY: Going to resolve state by given package data ...'); + // Resolve the peer's state (but ignore return value) PeerStateResolver::resolveStateByPackage($helperInstance, $packageData, $socketResource); diff --git a/application/hub/main/factories/states/node/class_NodeStateFactory.php b/application/hub/main/factories/states/node/class_NodeStateFactory.php index db351b678..a404c4605 100644 --- a/application/hub/main/factories/states/node/class_NodeStateFactory.php +++ b/application/hub/main/factories/states/node/class_NodeStateFactory.php @@ -48,7 +48,7 @@ class NodeStateFactory extends ObjectFactory { $stateInstance = self::createObjectByConfiguredName($className, array($nodeInstance)); // Debug message - $stateInstance->debugOutput('NODE-STATE: Has changed from ' . $nodeInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . '.'); + $stateInstance->debugOutput('NODE-STATE-FACTORY[' . __LINE__ . ']: Node state has changed from ' . $nodeInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . '.'); // Once we have that state, set it in the node instance $nodeInstance->setStateInstance($stateInstance); diff --git a/application/hub/main/factories/states/peer/class_PeerStateFactory.php b/application/hub/main/factories/states/peer/class_PeerStateFactory.php index 5fa84f337..20234fc04 100644 --- a/application/hub/main/factories/states/peer/class_PeerStateFactory.php +++ b/application/hub/main/factories/states/peer/class_PeerStateFactory.php @@ -72,18 +72,22 @@ class PeerStateFactory extends ObjectFactory { // So first we need our lookup table $tableInstance = self::getTableInstance(); - // Purge old entries + /* + * 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 - $tableInstance->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ']: Purging of old entries failed. Message from exception: ' . $e->getMessage()); + /* NOISY-DEBUG: */ $tableInstance->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 - $tableInstance->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ']: errorCode=' . $errorCode); + /* NOISY-DEBUG: */ $tableInstance->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] errorCode=' . $errorCode); // Register the new peer with its session id $tableInstance->registerPeerByPackageData($packageData, $socketResource); @@ -96,13 +100,31 @@ class PeerStateFactory extends ObjectFactory { // And register it with the lookup table $tableInstance->registerPeerState($stateInstance, $packageData); + } elseif ($tableInstance->ifPeerStateHasChanged($helperInstance, $packageData)) { + // Debug output + /* NOISY-DEBUG: */ $tableInstance->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); } else { - // It is a known peer, so we need to check if the state has changed - $tableInstance->debugBackTrace(__METHOD__ . ': Lookup!'); + // Debug output + /* NOISY-DEBUG: */ $tableInstance->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); } // Debug message - $stateInstance->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ']: Peer state has changed from ' . $helperInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . '.'); + /* NOISY-DEBUG: */ $stateInstance->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); @@ -120,14 +142,11 @@ class PeerStateFactory extends ObjectFactory { * @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)); + // Get a class from a configuration entry + $stateInstance = self::createObjectByConfiguredName('peer_' . $stateName . '_state_class', array($helperInstance)); // Debug message - $stateInstance->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ']: Peer state has changed from ' . $helperInstance->getPrintableState() . ' to ' . $stateInstance->getStateName() . ' (' . $stateInstance->__toString() . ').'); + /* NOISY-DEBUG: */ $stateInstance->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); diff --git a/application/hub/main/listener/udp/class_UdpListener.php b/application/hub/main/listener/udp/class_UdpListener.php index 4e8c52b6a..bae286155 100644 --- a/application/hub/main/listener/udp/class_UdpListener.php +++ b/application/hub/main/listener/udp/class_UdpListener.php @@ -66,7 +66,7 @@ class UdpListener extends BaseListener implements Listenable { // Is the socket a valid resource or do we have any error? if (!is_resource($mainSocket)) { // Then throw an InvalidSocketException - throw new InvalidSocketException(array($this, gettype($mainSocket), $errno, $errstr), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $mainSocket), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if // Set the option to reuse the port diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index 4cfe47c87..452427941 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -298,21 +298,33 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva // Now discover the right protocol $socketResource = $discoveryInstance->discoverSocket($packageData); - // Get the listener from registry - $helperInstance = Registry::getRegistry()->getInstance('connection'); + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after discoverSocket() has been called.'); // We have to put this socket in our registry, so get an instance $registryInstance = SocketRegistry::createSocketRegistry(); + // Get the listener from registry + $helperInstance = Registry::getRegistry()->getInstance('connection'); + + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.'); + // Is it not there? if ((is_resource($socketResource)) && (!$registryInstance->isSocketRegistered($helperInstance, $socketResource))) { // Then register it $registryInstance->registerSocket($helperInstance, $socketResource, $packageData); } // END - if + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.'); + // Make sure the connection is up $helperInstance->getStateInstance()->validatePeerStateConnected(); + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.'); + // We enqueue it again, but now in the out-going queue $this->getStackerInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageData); } diff --git a/application/hub/main/visitor/socket/class_ShutdownSocketVisitor.php b/application/hub/main/visitor/socket/class_ShutdownSocketVisitor.php index 69354167f..b584b7ed8 100644 --- a/application/hub/main/visitor/socket/class_ShutdownSocketVisitor.php +++ b/application/hub/main/visitor/socket/class_ShutdownSocketVisitor.php @@ -52,7 +52,7 @@ class ShutdownSocketVisitor extends BaseVisitor implements Visitor { * "Visit" method to do the actual request. Here we want to shutdown the * attached socket. * - * @param $helperInstance A ConnectionHelper instance + * @param $helperInstance An instance of a ConnectionHelper class * @return void */ public function visitConnectionHelper (ConnectionHelper $helperInstance) { -- 2.39.5