* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.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 . */ class BaseConnectionHelper extends BaseHubHelper implements Registerable, ProtocolHandler { /** * Protocol used */ private $protocol = 'invalid'; /** * Port number used */ private $port = 0; /** * (IP) Adress used */ private $address = 0; /** * Sent data in bytes */ private $sentData = 0; /** * Offset */ private $offset = 0; /** * Connect retries for this connection */ private $retryCount = 0; /** * Wether this connection is shutted down */ private $shuttedDown = false; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct ($className) { // Call parent constructor parent::__construct($className); // Register this connection helper Registry::getRegistry()->addInstance('connection', $this); } /** * Getter for port number to satify ProtocolHandler * * @return $port The port number */ public final function getPort () { return $this->port; } /** * Setter for port number to satify ProtocolHandler * * @param $port The port number * @return void */ protected final function setPort ($port) { $this->port = $port; } /** * Getter for protocol * * @return $protocol Used protocol */ public final function getProtocol () { return $this->protocol; } /** * Setter for protocol * * @param $protocol Used protocol * @return void */ protected final function setProtocol ($protocol) { $this->protocol = $protocol; } /** * 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; } /** * "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); } /** * "Getter" for raw package data from a package array. This method does * honor any unsend bytes in the back-buffer and the sending buffer size. * * @param $packageData Raw package data array * @return $rawData Raw package data bytes */ private function getRawDataFromPackageArray (array $packageData) { // Get the fragmenter instance $fragmenterInstance = ObjectFactory::createObjectByConfiguredName('package_fragmenter_class'); $fragmenterInstance->debugInstance(); // Return it return $rawData; } /** * Sends raw package data to the recipient * * @param $packageData Raw package data * @return $sentBytes Actual sent bytes to the peer * @throws InvalidSocketException If we got a problem with this socket */ public function sendRawPackageData (array $packageData) { // We need to "package" all data. This is done by a implode() $rawData = $this->getRawDataFromPackageArray($packageData); // Get socket resource $socketResource = $this->getSocketResource(); // And deliver it $sentBytes = @socket_write($socketResource, $rawData, $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length') - $this->offset); // If there was an error, we don't continue here if ($sentBytes === false) { // Get socket error code for verification $socketError = socket_last_error($socketResource); // Get error message $errorMessage = socket_strerror($socketError); // Shutdown this socket $this->shutdownSocket($socketResource); // And throw it throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if // The difference between sent bytes and length of raw data should not be below zero assert((strlen($rawData) - $sentBytes) >= 0); // Return sent bytes return $sentBytes; } /** * Getter for real class name * * @return $class Name of this class */ public function __toString () { // Class name representation $class = $this->getAddress() . ':' . $this->getPort() . ':' . parent::__toString(); // Return it return $class; } /** * Checks wether the connect retry is exhausted * * @return $isExhaused Wether connect retry is exchausted */ public final function isConnectRetryExhausted () { // Construct config entry $configEntry = $this->getProtocol() . '_connect_retry_max'; // Check it out $isExhausted = ($this->retryCount >= $this->getConfigInstance()->getConfigEntry($configEntry)); // Return it return $isExhausted; } /** * Increases the connect retry count * * @return void */ public final function increaseConnectRetry () { $this->retryCount++; } /** * Marks this connection as shutted down * * @return void */ protected final function markConnectionShutdown () { $this->shuttedDown = true; } /** * Getter for shuttedDown * * @return $shuttedDown Wether this connection is shutted down */ public final function isShuttedDown () { return $this->shuttedDown; } } // [EOF] ?>