]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/handler/message-types/class_BaseMessageHandler.php
Rewrites:
[hub.git] / application / hub / main / handler / message-types / class_BaseMessageHandler.php
index b20a8ab3bab15e40845262c06d6a358a274b36b0..75a7526fa8b46eb0d2f0a7d0a6cee4170098ad69 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-abstract class BaseMessageHandler extends BaseHandler {
-       /**
-        * Array with search criteria elements
-        */
-       protected $searchData = array();
-
-       /**
-        * Array with all data XML nodes (which hold the actual data) and their values
-        */
-       protected $messageDataElements = array();
-
-       /**
-        * Array for translating message data elements (other node's data mostly)
-        * into configuration elements.
-        */
-       protected $messageToConfig = array();
-
-       /**
-        * Array for copying configuration entries
-        */
-       protected $configCopy = array();
-
-       /**
-        * Last exception instance from database layer or NULL (default)
-        */
-       private $lastException = NULL;
-
+abstract class BaseMessageHandler extends BaseDataHandler {
        /**
         * Protected constructor
         *
@@ -60,15 +34,6 @@ abstract class BaseMessageHandler extends BaseHandler {
                parent::__construct($className);
        }
 
-       /**
-        * Getter for last exception
-        *
-        * @return      $lastException  Last thrown exception
-        */
-       protected final function getLastException () {
-               return $this->lastException;
-       }
-
        /**
         * "Getter" for a translated last exception as a status code
         *
@@ -79,9 +44,9 @@ abstract class BaseMessageHandler extends BaseHandler {
                $statusCode = self::MESSAGE_STATUS_CODE_OKAY;
 
                // Is the last exception not NULL?
-               if ($this->lastException instanceof FrameworkException) {
+               if ($this->getLastException() instanceof FrameworkException) {
                        // "Determine" the right status code (may differ from exception to exception)
-                       $this->debugInstance('lastException=' . $this->lastException->__toString() . ',message=' . $this->lastException->getMessage() . ' is not finished!');
+                       $this->debugInstance('lastException=' . $this->getLastException()->__toString() . ',message=' . $this->getLastException()->getMessage() . ' is not finished!');
                } // END - if
 
                // Return the status code
@@ -94,104 +59,18 @@ abstract class BaseMessageHandler extends BaseHandler {
         *
         * - session-id  (for finding the node's record together with below data)
         * - external-ip (hostname or IP number)
-        * - tcp-port    (TCP port for inbound connections)
+        * - listen-port (TCP/UDP listen port for inbound connections)
         *
         * @param       $messageArray   An array with all minimum message data
         * @return      void
-        * @todo        Add something more, e.g. spreading information over DHT
         */
        protected function registerNodeByMessageData (array $messageData) {
                // Check if searchData has entries
-               assert(count($this->searchData) > 0);
-
-               // Get a wrapper instance
-               $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class');
-
-               // Get a search criteria class
-               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
-
-               // Search for the node's session id and external IP/hostname + TCP port
-               foreach ($this->searchData as $key) {
-                       // Add criteria
-                       $searchInstance->addCriteria('node_' . str_replace('my-', '', $key), $messageData[$key]);
-               } // END - foreach
-
-               // Only one entry is fine
-               $searchInstance->setLimit(1);
-
-               // Run the query
-               $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
+               assert(count($this->getSearchData()) > 0);
 
-               // Is there already an entry?
-               if ($resultInstance->next()) {
-                       // Entry found, so update it
-                       $wrapperInstance->updateNodeByMessageData($messageData, $this, $searchInstance);
-               } else {
-                       // Nothing found, so register it
-                       $wrapperInstance->registerNodeByMessageData($messageData, $this);
-               }
-
-               // Save last exception
-               $this->lastException = $wrapperInstance->getLastException();
+               // Let the DHT facade do the work
+               $this->getDhtInstance()->registerNodeByMessageData($messageData, $this);
        }
-
-       /**
-        * Prepares a message as answer for given message data for delivery.
-        *
-        * @param       $messageData            An array with all message data
-        * @param       $packageInstance        An instance of a Deliverable instance
-        * @return      void
-        */
-       protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) {
-               // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . $this->__toString() . ']: Going to send an answer message ...');
-
-               // Get a helper instance based on this handler's name
-               $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData));
-
-               // Load descriptor XML
-               $helperInstance->loadDescriptorXml();
-
-               /*
-                * Set missing (temporary) configuration data, mostly it needs to be
-                * copied from message data array.
-                */
-               $this->initMessageConfigurationData($messageData);
-
-               // Compile any configuration variables
-               $helperInstance->getTemplateInstance()->compileConfigInVariables();
-
-               // Get node instance
-               $nodeInstance = Registry::getRegistry()->getInstance('node');
-
-               // Deliver the package
-               $helperInstance->sendPackage($nodeInstance);
-
-               /*
-                * Remove temporary configuration
-                */
-               $this->removeMessageConfigurationData($messageData);
-
-               // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . $this->__toString() . ']: Answer message has been prepared.');
-       }
-
-       /**
-        * Initializes configuration data from given message data array
-        *
-        * @param       $messageData    An array with all message data
-        * @return      void
-        */
-       abstract protected function initMessageConfigurationData (array $messageData);
-
-       /**
-        * Removes configuration data with given message data array from global
-        * configuration
-        *
-        * @param       $messageData    An array with all message data
-        * @return      void
-        */
-       abstract protected function removeMessageConfigurationData (array $messageData);
 }
 
 // [EOF]