]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/class_BaseHubSystem.php
Used exit() instead of die()
[hub.git] / application / hub / main / class_BaseHubSystem.php
index 18a448d7aebfcd33a6c1c0f1fee221566e021af2..03579227987b32f3114470a5900b96f0ebb9471d 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
  */
 class BaseHubSystem extends BaseFrameworkSystem {
        // Exception codes
-       const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900;
-       const EXCEPTION_CHUNK_ALREADY_ASSEMBLED   = 0x901;
+       const EXCEPTION_UNSUPPORTED_ERROR_HANDLER  = 0x900;
+       const EXCEPTION_CHUNK_ALREADY_ASSEMBLED    = 0x901;
+       const EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED  = 0x902;
+       const EXCEPTION_INVALID_CONNECTION_TYPE    = 0x903;
+       const EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED = 0x904;
+
+       // Message status codes
+       const MESSAGE_STATUS_CODE_OKAY = 'OKAY';
 
        /**
         * Separator for all bootstrap node entries
@@ -76,6 +82,11 @@ class BaseHubSystem extends BaseFrameworkSystem {
         */
        private $decoderInstance = NULL;
 
+       /**
+        * Assembler instance
+        */
+       private $assemblerInstance = NULL;
+
        /**
         * Protected constructor
         *
@@ -204,7 +215,7 @@ class BaseHubSystem extends BaseFrameworkSystem {
        /**
         * Setter for listener pool instance
         *
-        * @param       $listenerPoolInstance   Our new listener pool instance
+        * @param       $listenerPoolInstance   The new listener pool instance
         * @return      void
         */
        protected final function setListenerPoolInstance (PoolableListener $listenerPoolInstance) {
@@ -258,6 +269,107 @@ class BaseHubSystem extends BaseFrameworkSystem {
                return $this->decoderInstance;
        }
 
+       /**
+        * Setter for assembler instance
+        *
+        * @param       $assemblerInstance      A Decodeable instance
+        * @return      void
+        */
+       protected final function setAssemblerInstance (Assembler $assemblerInstance) {
+               $this->assemblerInstance = $assemblerInstance;
+       }
+
+       /**
+        * Getter for assembler instance
+        *
+        * @return      $assemblerInstance      A Decodeable instance
+        */
+       protected final function getAssemblerInstance () {
+               return $this->assemblerInstance;
+       }
+
+       /**
+        * Setter for node id
+        *
+        * @param       $nodeId         The new node id
+        * @return      void
+        */
+       protected final function setNodeId ($nodeId) {
+               // Set it config now
+               $this->getConfigInstance()->setConfigEntry('node_id', (string) $nodeId);
+       }
+
+       /**
+        * Getter for node id
+        *
+        * @return      $nodeId         Current node id
+        */
+       public final function getNodeId () {
+               // Get it from config
+               return $this->getConfigInstance()->getConfigEntry('node_id');
+       }
+
+       /**
+        * Setter for private key
+        *
+        * @param       $privateKey             The new private key
+        * @return      void
+        */
+       protected final function setPrivateKey ($privateKey) {
+               // Set it config now
+               $this->getConfigInstance()->setConfigEntry('private_key', (string) $privateKey);
+       }
+
+       /**
+        * Getter for private key
+        *
+        * @return      $privateKey             Current private key
+        */
+       public final function getPrivateKey () {
+               // Get it from config
+               return $this->getConfigInstance()->getConfigEntry('private_key');
+       }
+
+       /**
+        * Setter for private key hash
+        *
+        * @param       $privateKeyHash         The new private key hash
+        * @return      void
+        */
+       protected final function setPrivateKeyHash ($privateKeyHash) {
+               // Set it config now
+               $this->getConfigInstance()->setConfigEntry('private_key_hash', (string) $privateKeyHash);
+       }
+
+       /**
+        * Getter for private key hash
+        *
+        * @return      $privateKeyHash         Current private key hash
+        */
+       public final function getPrivateKeyHash () {
+               // Get it from config
+               return $this->getConfigInstance()->getConfigEntry('private_key_hash');
+       }
+
+       /**
+        * Setter for session id
+        *
+        * @param       $sessionId              The new session id
+        * @return      void
+        */
+       protected final function setSessionId ($sessionId) {
+               $this->getConfigInstance()->setConfigEntry('session_id', (string) $sessionId);
+       }
+
+       /**
+        * Getter for session id
+        *
+        * @return      $sessionId              Current session id
+        */
+       public final function getSessionId () {
+               return $this->getConfigInstance()->getConfigEntry('session_id');
+       }
+
        /**
         * Constructs a callable method name from given socket error code. If the
         * method is not found, a generic one is used.
@@ -286,13 +398,15 @@ class BaseHubSystem extends BaseFrameworkSystem {
         * but assumes valid data in array $recipientData, except that
         * count($recipientData) is always 2.
         *
+        * @param       $method                         Value of __METHOD__ from calling method
+        * @param       $line                           Value of __LINE__ from calling method
         * @param       $socketResource         A valid socket resource
         * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
         * @return      void
         * @throws      InvalidSocketException  If $socketResource is no socket resource
         * @throws      NoSocketErrorDetectedException  If socket_last_error() gives zero back
         */
-       protected final function handleSocketError ($socketResource, array $recipientData) {
+       protected final function handleSocketError ($method, $line, $socketResource, array $recipientData) {
                // This method handles only socket resources
                if (!is_resource($socketResource)) {
                        // No resource, abort here
@@ -315,7 +429,7 @@ class BaseHubSystem extends BaseFrameworkSystem {
                $handlerName = $this->getSocketErrorHandlerFromCode($errorCode);
 
                // Call-back the error handler method
-               call_user_func(array($this, $handlerName), $socketResource);
+               call_user_func_array(array($this, $handlerName), array($socketResource, $recipientData));
 
                // Finally clear the error because it has been handled
                socket_clear_error($socketResource);
@@ -335,7 +449,7 @@ class BaseHubSystem extends BaseFrameworkSystem {
                $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunks[count($chunks) - 1]);
 
                // Make sure chunks with only 3 elements are parsed (for details see ChunkHandler)
-               //* NOISY-DEBUG: */ $this->debugOutput('eopChunk=' . $chunks[count($chunks) - 1] . ',chunkSplits=' . print_r($chunkSplits,true));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('eopChunk=' . $chunks[count($chunks) - 1] . ',chunkSplits=' . print_r($chunkSplits,true));
                assert(count($chunkSplits) == 3);
 
                // Validate final chunk
@@ -399,7 +513,7 @@ class BaseHubSystem extends BaseFrameworkSystem {
 
                        default: // Everything else <> 0
                                // Unhandled error code detected, so first debug it because we may want to handle it like the others
-                               $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] UNKNOWN ERROR CODE = ' . $errorCode . ', MESSAGE = ' . socket_strerror($errorCode));
+                               self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] UNKNOWN ERROR CODE = ' . $errorCode . ', MESSAGE = ' . socket_strerror($errorCode));
 
                                // Change it only in this class
                                $errorName = BaseRawDataHandler::SOCKET_ERROR_UNKNOWN;
@@ -419,7 +533,7 @@ class BaseHubSystem extends BaseFrameworkSystem {
         */
        public function shutdownSocket ($socketResource) {
                // Debug message
-               $this->debugOutput('HUB-SYSTEM: Shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...');
+               self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM: Shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...');
 
                // Set socket resource
                $this->setSocketResource($socketResource);
@@ -428,7 +542,7 @@ class BaseHubSystem extends BaseFrameworkSystem {
                $visitorInstance = ObjectFactory::createObjectByConfiguredName('shutdown_socket_visitor_class');
 
                // Debug output
-               $this->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString());
+               self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString());
 
                // Call the visitor
                $this->accept($visitorInstance);
@@ -443,7 +557,7 @@ class BaseHubSystem extends BaseFrameworkSystem {
         */
        public function halfShutdownSocket ($socketResource) {
                // Debug message
-               $this->debugOutput('HUB-SYSTEM: Half-shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...');
+               self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM: Half-shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...');
 
                // Set socket resource
                $this->setSocketResource($socketResource);
@@ -452,7 +566,7 @@ class BaseHubSystem extends BaseFrameworkSystem {
                $visitorInstance = ObjectFactory::createObjectByConfiguredName('half_shutdown_socket_visitor_class');
 
                // Debug output
-               $this->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString());
+               self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString());
 
                // Call the visitor
                $this->accept($visitorInstance);
@@ -479,6 +593,20 @@ class BaseHubSystem extends BaseFrameworkSystem {
                // Return result
                return $stateName;
        }
+
+       /**
+        * Checks whether start/end marker are set
+        *
+        * @param       $data   Data to be checked
+        * @return      $isset  Whether start/end marker are set
+        */
+       public final function ifStartEndMarkerSet ($data) {
+               // Determine it
+               $isset = ((substr($data, 0, strlen(BaseRawDataHandler::STREAM_START_MARKER)) == BaseRawDataHandler::STREAM_START_MARKER) && (substr($data, -1 * strlen(BaseRawDataHandler::STREAM_END_MARKER), strlen(BaseRawDataHandler::STREAM_END_MARKER)) == BaseRawDataHandler::STREAM_END_MARKER));
+
+               // ... and return it
+               return $isset;
+       }
 }
 
 // [EOF]