]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/helper/connection/class_BaseConnectionHelper.php
New singleton-factories introduced:
[hub.git] / application / hub / main / helper / connection / class_BaseConnectionHelper.php
index c88e89e53d49d298ac1acf3e3a053c424034cece..c1a21b6ca0df48e9f6da0cc8e7bc66dfaba2c039 100644 (file)
@@ -79,6 +79,12 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
 
                // Register this connection helper
                Registry::getRegistry()->addInstance('connection', $this);
+
+               // Initialize output stream
+               $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_output_stream_class');
+
+               // And add it to this connection helper
+               $this->setOutputStreamInstance($streamInstance);
        }
 
        /**
@@ -154,17 +160,8 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * @return      $chunkData              Raw data chunk
         */
        private function getRawDataFromPackageArray (array $packageData) {
-               // If there is no fragmenter?
-               if (!Registry::getRegistry()->instanceExists('package_fragmenter')) {
-                       // Get the fragmenter instance
-                       $fragmenterInstance = ObjectFactory::createObjectByConfiguredName('package_fragmenter_class');
-
-                       // Add it to the registry
-                       Registry::getRegistry()->addInstance('package_fragmenter', $fragmenterInstance);
-               } else {
-                       // Get fragmenter from registry
-                       $fragmenterInstance = Registry::getRegistry()->getInstance('package_fragmenter');
-               }
+               // Get the fragmenter instance
+               $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package_fragmenter_class');
 
                // Implode the package data array and fragement the resulting string, returns the final hash
                $finalHash = $fragmenterInstance->fragmentPackageArray($packageData, $this);
@@ -219,8 +216,8 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length');
 
                // Init variables
-               $rawData = '';
-               $dataStream = ' ';
+               $rawData        = '';
+               $dataStream     = ' ';
                $totalSentBytes = 0;
 
                // Fill sending buffer with data
@@ -234,8 +231,11 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                // Nothing to sent is bad news!
                assert(strlen($rawData) > 0);
 
+               // Encode the raw data with our output-stream
+               $encodedData = $this->getOutputStreamInstance()->streamData($rawData);
+
                // Calculate difference
-               $this->diff = $bufferSize - strlen($rawData);
+               $this->diff = $bufferSize - strlen($encodedData);
 
                // Get socket resource
                $socketResource = $this->getSocketResource();
@@ -246,8 +246,8 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                // Deliver all data
                while ($sentBytes !== false) {
                        // And deliver it
-                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sending out ' . strlen($rawData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
-                       $sentBytes = @socket_write($socketResource, $rawData, ($bufferSize - $this->diff));
+                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
+                       $sentBytes = @socket_write($socketResource, $encodedData, ($bufferSize - $this->diff));
 
                        // If there was an error, we don't continue here
                        if ($sentBytes === false) {
@@ -262,27 +262,27 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
 
                                // And throw it
                                throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
-                       } elseif (($sentBytes == 0) && (strlen($rawData) > 0)) {
+                       } elseif (($sentBytes == 0) && (strlen($encodedData) > 0)) {
                                // Nothing sent means we are done
                                //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
                                break;
                        }
 
                        // The difference between sent bytes and length of raw data should not be below zero
-                       assert((strlen($rawData) - $sentBytes) >= 0);
+                       assert((strlen($encodedData) - $sentBytes) >= 0);
 
                        // Add total sent bytes
                        $totalSentBytes += $sentBytes;
 
                        // Cut out the last unsent bytes
-                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sent out ' . $sentBytes . ' of ' . strlen($rawData) . ' bytes ...');
-                       $rawData = substr($rawData, $sentBytes);
+                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sent out ' . $sentBytes . ' of ' . strlen($encodedData) . ' bytes ...');
+                       $encodedData = substr($encodedData, $sentBytes);
 
                        // Calculate difference again
-                       $this->diff = $bufferSize - strlen($rawData);
+                       $this->diff = $bufferSize - strlen($encodedData);
 
                        // Can we abort?
-                       if (strlen($rawData) <= 0) {
+                       if (strlen($encodedData) <= 0) {
                                // Abort here, all sent!
                                //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
                                break;