From: Roland Häder Date: Tue, 12 Feb 2013 06:16:43 +0000 (+0000) Subject: Rewrites: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=63f6cc4835c56c57008955d039aaaaa235a87d3c;p=hub.git Rewrites: - Rewrote AnnouncementAnswerOkayHandler to use DHT instance instead of deprecated wrapper class - BaseHandler now implements Handleable because both are generics - NodeDhtFacade now allows forced node updates, if no record is found an exception is thrown. This should help to find "stealing" of session ids --- diff --git a/application/hub/interfaces/distributable/class_Distributable.php b/application/hub/interfaces/distributable/class_Distributable.php index 4af98d668..ad031b456 100644 --- a/application/hub/interfaces/distributable/class_Distributable.php +++ b/application/hub/interfaces/distributable/class_Distributable.php @@ -46,10 +46,11 @@ interface Distributable extends FrameworkInterface { * - listen-port (TCP/UDP listen port for inbound connections) * * @param $messageArray An array with all minimum message data - * @param $handlerInstance An instance of a HandleableMessage class + * @param $handlerInstance An instance of a Handleable class + * @param $forceUpdate Optionally force update, don't register (default: register if not found) * @return void */ - function registerNodeByMessageData (array $messageData, HandleableMessage $handlerInstance); + function registerNodeByMessageData (array $messageData, Handleable $handlerInstance, $forceUpdate = FALSE); } // [EOF] diff --git a/application/hub/main/dht/node/class_NodeDhtFacade.php b/application/hub/main/dht/node/class_NodeDhtFacade.php index b71b7eaa3..c21dfc18f 100644 --- a/application/hub/main/dht/node/class_NodeDhtFacade.php +++ b/application/hub/main/dht/node/class_NodeDhtFacade.php @@ -100,10 +100,12 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable { * - listen-port (TCP/UDP listen port for inbound connections) * * @param $messageArray An array with all minimum message data - * @param $handlerInstance An instance of a HandleableMessage class + * @param $handlerInstance An instance of a Handleable class + * @param $forceUpdate Optionally force update, don't register (default: register if not found) * @return void + * @throws NodeSessionIdVerficationException If the node was not found and update is forced */ - public function registerNodeByMessageData (array $messageData, HandleableMessage $handlerInstance) { + public function registerNodeByMessageData (array $messageData, Handleable $handlerInstance, $forceUpdate = FALSE) { // Get a search criteria class $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); @@ -132,9 +134,16 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable { if ($resultInstance->next()) { // Entry found, so update it $this->getWrapperInstance()->updateNodeByMessageData($messageData, $handlerInstance, $searchInstance); - } else { + } elseif ($forceUpdate === FALSE) { // Nothing found, so register it $this->getWrapperInstance()->registerNodeByMessageData($messageData, $handlerInstance); + } else { + /* + * Do not register non-existent nodes here. This is maybe fatal, + * caused by "stolen" session id and/or not matching IP + * number/port combination. + */ + throw new NodeSessionIdVerficationException(array($this, $messageData), BaseHubSystem::EXCEPTION_NODE_SESSION_ID_NOT_VERIFYING); } // Save last exception diff --git a/application/hub/main/handler/answer-status/announcement/class_AnnouncementAnswerOkayHandler.php b/application/hub/main/handler/answer-status/announcement/class_AnnouncementAnswerOkayHandler.php index 1b1b12b83..df6ca8f6e 100644 --- a/application/hub/main/handler/answer-status/announcement/class_AnnouncementAnswerOkayHandler.php +++ b/application/hub/main/handler/answer-status/announcement/class_AnnouncementAnswerOkayHandler.php @@ -31,6 +31,13 @@ class AnnouncementAnswerOkayHandler extends BaseAnserStatusHandler implements Ha // Call parent constructor parent::__construct(__CLASS__); + // Init array + $this->searchData = array( + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP, + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_LISTEN_PORT + ); + // Set handler name $this->setHandlerName('announcement_answer_okay'); } @@ -54,33 +61,14 @@ class AnnouncementAnswerOkayHandler extends BaseAnserStatusHandler implements Ha * @param $messageData An array of message data * @param $packageInstance An instance of a Receivable class * @return void - * @throws NodeSessionIdVerficationException If the provided session id is not matching * @todo Do some more here: Handle karma, et cetera? - * @todo Rewrite this to use DHT */ public function handleAnswerMessageData (array $messageData, Receivable $packageInstance) { - // Get a database wrapper instance - $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class'); - - // Get also a search criteria instance - $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); - - // Lookup external session id/external IP/port - $searchInstance->addCriteria('node_session_id' , $messageData[XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID]); - $searchInstance->addCriteria('node_external_ip', $messageData[XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP]); - $searchInstance->addCriteria('node_listen_port', $messageData[XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_LISTEN_PORT]); - - // Only one entry is fine - $searchInstance->setLimit(1); - - // Run the query - $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance); - - // Is there an try? - if (!$resultInstance->next()) { - // This is fatal, caused by "stolen" session id and/or not matching IP number/port combination - throw new NodeSessionIdVerficationException(array($this, $messageData), BaseHubSystem::EXCEPTION_NODE_SESSION_ID_NOT_VERIFYING); - } // END - if + /* + * Query DHT and force update (which will throw an exception if the + * node is not found). + */ + $this->getDhtInstance()->registerNodeByMessageData($messageData, $this, TRUE); // Get handler instance $handlerInstance = Registry::getRegistry()->getInstance('task_handler'); @@ -91,9 +79,6 @@ class AnnouncementAnswerOkayHandler extends BaseAnserStatusHandler implements Ha // Register it as well $handlerInstance->registerTask('dht_bootstrap', $taskInstance); - // Update node data (include status code) - $wrapperInstance->updateNodeByMessageData($messageData, $this, $searchInstance); - // Get the node instance $nodeInstance = Registry::getRegistry()->getInstance('node'); diff --git a/application/hub/main/handler/class_BaseDataHandler.php b/application/hub/main/handler/class_BaseDataHandler.php index 77cee0af4..9429cd825 100644 --- a/application/hub/main/handler/class_BaseDataHandler.php +++ b/application/hub/main/handler/class_BaseDataHandler.php @@ -57,6 +57,12 @@ abstract class BaseDataHandler extends BaseHandler { protected function __construct ($className) { // Call parent constructor parent::__construct($className); + + // Get a DHT instance + $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node'); + + // Set it here + $this->setDhtInstance($dhtInstance); } /** diff --git a/application/hub/main/handler/class_BaseHandler.php b/application/hub/main/handler/class_BaseHandler.php index aebb05625..d73a461b3 100644 --- a/application/hub/main/handler/class_BaseHandler.php +++ b/application/hub/main/handler/class_BaseHandler.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class BaseHandler extends BaseHubSystem { +class BaseHandler extends BaseHubSystem implements Handleable { /** * Handler name */ diff --git a/application/hub/main/handler/message-types/class_BaseMessageHandler.php b/application/hub/main/handler/message-types/class_BaseMessageHandler.php index 23d2153a1..75a7526fa 100644 --- a/application/hub/main/handler/message-types/class_BaseMessageHandler.php +++ b/application/hub/main/handler/message-types/class_BaseMessageHandler.php @@ -32,12 +32,6 @@ abstract class BaseMessageHandler extends BaseDataHandler { protected function __construct ($className) { // Call parent constructor parent::__construct($className); - - // Get a DHT instance - $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node'); - - // Set it here - $this->setDhtInstance($dhtInstance); } /**