]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/helper/connection/class_BaseConnectionHelper.php
Updated 'core' + renamed 'main' -> 'classes'.
[hub.git] / application / hub / main / helper / connection / class_BaseConnectionHelper.php
diff --git a/application/hub/main/helper/connection/class_BaseConnectionHelper.php b/application/hub/main/helper/connection/class_BaseConnectionHelper.php
deleted file mode 100644 (file)
index 78e2db8..0000000
+++ /dev/null
@@ -1,349 +0,0 @@
-<?php
-/**
- * A general ConnectionHelper class
- *
- * @author             Roland Haeder <webmaster@shipsimu.org>
- * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
- * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.shipsimu.org
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * 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 BaseConnectionHelper extends BaseHubSystemHelper implements Registerable {
-       // Exception codes
-       const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x9100;
-
-       /**
-        * Connection type 'incoming'
-        */
-       const CONNECTION_TYPE_INCOMING = 'incoming';
-
-       /**
-        * Connection type 'outgoing'
-        */
-       const CONNECTION_TYPE_OUTGOING = 'outgoing';
-
-       /**
-        * Connection type 'server'
-        */
-       const CONNECTION_TYPE_SERVER   = 'server';
-
-       /**
-        * Name of used protocol for this connection
-        */
-       private $connectionType = 'invalid';
-
-       /**
-        * (IP) Adress used
-        */
-       private $address = 0;
-
-       /**
-        * Sent data in bytes
-        */
-       private $sentData = 0;
-
-       /**
-        * Whether this connection is initialized
-        */
-       private $isInitialized = FALSE;
-
-       /**
-        * Whether this connection is shutted down
-        */
-       private $shuttedDown = FALSE;
-
-       /**
-        * Currently queued chunks
-        */
-       private $queuedChunks = array();
-
-       /**
-        * Current final hash
-        */
-       private $currentFinalHash = '';
-
-       /**
-        * Protected constructor
-        *
-        * @param       $className      Name of the class
-        * @return      void
-        */
-       protected function __construct ($className) {
-               // Call parent constructor
-               parent::__construct($className);
-
-               // Init state which sets the state to 'init'
-               $this->initState();
-
-               // Initialize output stream
-               $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_output_stream_class');
-
-               // And add it to this connection helper
-               $this->setOutputStreamInstance($streamInstance);
-
-               // Get package instance from factory
-               $packageInstance = NetworkPackageFactory::createNetworkPackageInstance();
-
-               // ... and set it here
-               $this->setPackageInstance($packageInstance);
-
-               // Register this connection helper
-               Registry::getRegistry()->addInstance('connection', $this);
-
-               // Get the fragmenter instance
-               $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
-
-               // Set it here
-               $this->setFragmenterInstance($fragmenterInstance);
-       }
-
-       /**
-        * Getter for real class name, overwrites generic method and is final
-        *
-        * @return      $class  Name of this class
-        */
-       public final function __toString () {
-               // Class name representation
-               $class = self::getConnectionClassName($this->getAddress(), $this->getConnectionPort(), parent::__toString());
-
-               // Return it
-               return $class;
-       }
-
-       /**
-        * Getter for connection type
-        *
-        * @return      $connectionType         Name of used protocol in this connection
-        */
-       public final function getConnectionType () {
-               return $this->connectionType;
-       }
-
-       /**
-        * Setter for connection type
-        *
-        * @param       $connectionType         Name of used protocol in this connection
-        * @return      void
-        */
-       protected final function setConnectionType ($connectionType) {
-               $this->connectionType = $connectionType;
-       }
-
-       /**
-        * Getter for IP address
-        *
-        * @return      $address        The IP address
-        */
-       public final function getAddress () {
-               return $this->address;
-       }
-
-       /**
-        * Setter for IP address
-        *
-        * @param       $address        The IP address
-        * @return      void
-        */
-       protected final function setAddress ($address) {
-               $this->address = $address;
-       }
-
-       /**
-        * Setter for isInitialized
-        *
-        * @param       $isInitialized          Name of used protocol in this connection
-        * @return      void
-        */
-       protected final function setIsInitialized ($isInitialized) {
-               $this->isInitialized = $isInitialized;
-       }
-
-       /**
-        * Getter for isInitialized (NOTE: no 'get' prefix for boolean attributes!)
-        *
-        * @return      $isInitialized          Name of used protocol in this connection
-        */
-       protected final function isInitialized () {
-               return $this->isInitialized;
-       }
-
-       /**
-        * Static "getter" for this connection class' name
-        *
-        * @param       $address        IP address
-        * @param       $port           Port number
-        * @param       $className      Original class name
-        * @return      $class          Expanded class name
-        */
-       public static function getConnectionClassName ($address, $port, $className) {
-               // Construct it
-               $class = $address . ':' . $port . ':' . $className;
-
-               // ... and return it
-               return $class;
-       }
-
-       /**
-        * Initializes the peer's state which sets it to 'init'
-        *
-        * @return      void
-        */
-       private function initState() {
-               // Get the state factory and create the initial state.
-               PeerStateFactory::createPeerStateInstanceByName('init', $this);
-       }
-
-       /**
-        * "Getter" for raw data from a package array. A fragmenter is used which
-        * will returns us only so many raw data which fits into the back buffer.
-        * The rest is being held in a back-buffer and waits there for the next
-        * cycle and while be then sent.
-        *
-        * This method does 2 simple steps:
-        * 1) Request a chunk from set fragmenter instance
-        * 2) Finally return the chunk (array) to the caller
-        *
-        * @param       $packageData    Raw package data array
-        * @return      $chunkData              Raw data chunk
-        */
-       private function getRawDataFromPackageArray (array $packageData) {
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: currentFinalHash=' . $this->currentFinalHash);
-
-               // Make sure the final hash is set
-               assert((is_string($this->currentFinalHash)) && (!empty($this->currentFinalHash)));
-
-               // Get the next raw data chunk from the fragmenter
-               $rawDataChunk = $this->getFragmenterInstance()->getNextRawDataChunk($this->currentFinalHash);
-
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: rawDataChunk=' . print_r($rawDataChunk, TRUE));
-
-               // Get chunk hashes and chunk data
-               $chunkHashes = array_keys($rawDataChunk);
-               $chunkData   = array_values($rawDataChunk);
-
-               // Is the required data there?
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: chunkHashes[]=' . count($chunkHashes) . ',chunkData[]=' . count($chunkData));
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('chunkData='.print_r($chunkData, TRUE));
-               if ((isset($chunkHashes[0])) && (isset($chunkData[0]))) {
-                       // Remember this chunk as queued
-                       $this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
-
-                       // Return the raw data
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Returning ' . strlen($chunkData[0]) . ' bytes from ' . __METHOD__ . ' ...');
-                       return $chunkData[0];
-               } else {
-                       // Return zero string
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Returning zero bytes from ' . __METHOD__ . '!');
-                       return '';
-               }
-       }
-
-       /**
-        * "Accept" a visitor by simply calling it back
-        *
-        * @param       $visitorInstance        A Visitor instance
-        * @return      void
-        */
-       protected final function accept (Visitor $visitorInstance) {
-               // Just call the visitor
-               $visitorInstance->visitConnectionHelper($this);
-       }
-
-       /**
-        * Sends raw package data to the recipient
-        *
-        * @param       $packageData            Raw package data
-        * @return      void
-        * @throws      InvalidSocketException  If we got a problem with this socket
-        */
-       public function sendRawPackageData (array $packageData) {
-               // The helper's state must be 'connected'
-               $this->getStateInstance()->validatePeerStateConnected();
-
-               // Implode the package data array and fragement the resulting string, returns the final hash
-               $finalHash = $this->getFragmenterInstance()->fragmentPackageArray($packageData, $this);
-
-               // Is the final hash set?
-               if ($finalHash !== TRUE) {
-                       // Debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Setting finalHash=' . $finalHash . ',currentFinalHash[' . gettype($this->currentFinalHash) . ']=' . $this->currentFinalHash);
-
-                       // Set final hash
-                       $this->currentFinalHash = $finalHash;
-               } // END - if
-
-               // Reset serial number
-               $this->getFragmenterInstance()->resetSerialNumber($this->currentFinalHash);
-
-               // Init variables
-               $rawData        = '';
-               $dataStream     = ' ';
-
-               // Fill sending buffer with data
-               while (strlen($dataStream) > 0) {
-                       // Debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: packageData=' . print_r($packageData, TRUE));
-
-                       // Convert the package data array to a raw data stream
-                       $dataStream = $this->getRawDataFromPackageArray($packageData);
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
-                       $rawData .= $dataStream;
-               } // END - while
-
-               // Nothing to sent is bad news, so assert on it
-               assert(strlen($rawData) > 0);
-
-               // Calculate buffer size
-               $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocolName() . '_buffer_length');
-
-               // Encode the raw data with our output-stream
-               $encodedData = $this->getOutputStreamInstance()->streamData($rawData);
-
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('HELPER[' . __METHOD__ . ':' . __LINE__ . ']: socketResource[]=' . gettype($this->getSocketResource()) . PHP_EOL);
-
-               // Init array
-               $encodedDataArray = array(
-                       NetworkPackage::RAW_FINAL_HASH_INDEX   => $this->currentFinalHash,
-                       NetworkPackage::RAW_ENCODED_DATA_INDEX => $encodedData,
-                       NetworkPackage::RAW_SENT_BYTES_INDEX   => 0,
-                       NetworkPackage::RAW_SOCKET_INDEX       => $this->getSocketResource(),
-                       NetworkPackage::RAW_BUFFER_SIZE_INDEX  => $bufferSize,
-                       NetworkPackage::RAW_DIFF_INDEX         => 0
-               );
-
-               // Calculate difference
-               $diff = $encodedDataArray[NetworkPackage::RAW_BUFFER_SIZE_INDEX] - strlen($encodedDataArray[NetworkPackage::RAW_ENCODED_DATA_INDEX]);
-
-               // Push raw data to the package's outgoing stack
-               $this->getPackageInstance()->getStackInstance()->pushNamed(NetworkPackage::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray);
-       }
-
-       /**
-        * Getter for shuttedDown
-        *
-        * @return      $shuttedDown    Whether this connection is shutted down
-        */
-       public final function isShuttedDown () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
-               return $this->shuttedDown;
-       }
-}
-
-// [EOF]
-?>