// Is the instance there?
if (is_null(self::$thisInstance)) {
self::$thisInstance = new ApplicationHelper();
- }
+ } // END - if
// Return the instance
return self::$thisInstance;
// Walk through all messages
foreach ($messageList as $message) {
die('MSG:' . $message);
- }
+ } // END - foreach
}
/**
* @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]
* @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;
} // 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
$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;
}
$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.');
}
/**
// 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]));
}
// 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());
}
/**
} // 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;
}
}
// 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);
$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);
// 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);
// 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);
* @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);
// 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
// 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);
}
* "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) {