* @return $isRegistered Whether *this* node is registered in the DHT
*/
public function isLocalNodeRegistered () {
- // Get a search criteria instance
- $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+ // Is there cache?
+ if (!isset($GLOBALS[__METHOD__])) {
+ // Get a search criteria instance
+ $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
- // Get node instance
- $nodeInstance = Registry::getRegistry()->getInstance('node');
+ // Get node instance
+ $nodeInstance = Registry::getRegistry()->getInstance('node');
- // Add ip:port as criteria
- $searchInstance->addCriteria(self::DB_COLUMN_IP_PORT, $nodeInstance->getAddressPort());
- $searchInstance->setLimit(1);
+ // Add ip:port as criteria
+ $searchInstance->addCriteria(self::DB_COLUMN_IP_PORT, $nodeInstance->getAddressPort());
+ $searchInstance->setLimit(1);
- // Query database and get a result instance back
- $resultInstance = $this->doSelectByCriteria($searchInstance);
+ // Query database and get a result instance back
+ $resultInstance = $this->doSelectByCriteria($searchInstance);
- // Is there an entry?
- $isRegistered = $resultInstance->next();
+ // Cache result of if there is an entry, next() will tell us if the next entry is valid
+ $GLOBALS[__METHOD__] = $resultInstance->next();
+ } // END - if
// Return result
- return $isRegistered;
+ return $GLOBALS[__METHOD__];
}
/**
return $wrapperInstance;
}
+ /**
+ * Checks whether there is an entry for given node instance
+ *
+ * @param $nodeInstance An instance of a NodeHelper class
+ * @return $isFound Whether a node id has been found for this node
+ */
+ public function ifNodeDataIsFound (NodeHelper $nodeInstance) {
+ // Is there cache?
+ if (!isset($GLOBALS[__METHOD__])) {
+ // Now get a search criteria instance
+ $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+ // Search for the node number one which is hard-coded the default
+ $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
+ $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $nodeInstance->getRequestInstance()->getRequestElement('mode'));
+ $searchInstance->setLimit(1);
+
+ // Get a result back
+ $resultInstance = $this->doSelectByCriteria($searchInstance);
+
+ // Set result instance in node instance
+ $nodeInstance->setResultInstance($resultInstance);
+
+ // Is it valid?
+ $GLOBALS[__METHOD__] = $resultInstance->next();
+ } // END - if
+
+ // Return it
+ return $GLOBALS[__METHOD__];
+ }
+
/**
* 'Registers' a new node id along with data provided in the node instance.
* This may sound confusing but avoids double code very nicely...
* @return void
*/
public function bootstrapAcquireNodeId (Requestable $requestInstance, Responseable $responseInstance) {
- // Now get a search criteria instance
- $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
-
- // Search for the node number one which is hard-coded the default
- $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
- $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $this->getRequestInstance()->getRequestElement('mode'));
- $searchInstance->setLimit(1);
-
- // Get a result back
- $resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance);
-
- // Is it valid?
- if ($resultInstance->next()) {
- // Save the result instance in this class
- $this->setResultInstance($resultInstance);
-
+ // Is there a node id?
+ if ($this->getWrapperInstance()->ifNodeDataIsFound($this)) {
// Get the node id from result and set it
$this->setNodeId($this->getField(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID));
* @return void
*/
public function bootstrapGeneratePrivateKey () {
- // Now get a search criteria instance
- $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
-
- // Search for the node number one which is hard-coded the default
- $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
- $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $this->getRequestInstance()->getRequestElement('mode'));
- $searchInstance->setLimit(1);
-
- // Get a result back
- $resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance);
-
// Is it valid?
- if ($resultInstance->next()) {
- // Save the result instance in this class
- $this->setResultInstance($resultInstance);
-
+ if ($this->getWrapperInstance()->ifNodeDataIsFound($this)) {
// Is the element set?
if (is_null($this->getField(NodeInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY))) {
/*