]> git.mxchange.org Git - hub.git/commitdiff
Rewrites:
authorRoland Häder <roland@mxchange.org>
Tue, 12 Feb 2013 06:16:43 +0000 (06:16 +0000)
committerRoland Häder <roland@mxchange.org>
Tue, 12 Feb 2013 06:16:43 +0000 (06:16 +0000)
- 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

application/hub/interfaces/distributable/class_Distributable.php
application/hub/main/dht/node/class_NodeDhtFacade.php
application/hub/main/handler/answer-status/announcement/class_AnnouncementAnswerOkayHandler.php
application/hub/main/handler/class_BaseDataHandler.php
application/hub/main/handler/class_BaseHandler.php
application/hub/main/handler/message-types/class_BaseMessageHandler.php

index 4af98d668253ba7039b6e21557031b7cfb935400..ad031b4565cb10c9ee72abaed4692fb8f3c98a7b 100644 (file)
@@ -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]
index b71b7eaa36b8543e79d1c10c1eff2e080712a188..c21dfc18f426d7c6cb022c002e1c6bd511e49644 100644 (file)
@@ -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
index 1b1b12b830c87b50b0f4617acd6c9011b654c795..df6ca8f6e4b4dc53d8a7d910bd2d42041a8b1827 100644 (file)
@@ -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');
 
index 77cee0af4a964aa2628205b8085c8badb4690ccb..9429cd825b1958d853572000d9bc765f64f64238 100644 (file)
@@ -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);
        }
 
        /**
index aebb056257f5eb4d92339f2864f02b5ad21d9f2d..d73a461b3c785c727d4573c06b6176f046cfea6f 100644 (file)
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class BaseHandler extends BaseHubSystem {
+class BaseHandler extends BaseHubSystem implements Handleable {
        /**
         * Handler name
         */
index 23d2153a1142ffb9f76c47bf5261f043c4b43e37..75a7526fa8b46eb0d2f0a7d0a6cee4170098ad69 100644 (file)
@@ -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);
        }
 
        /**