* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class BaseConnectionHelper extends BaseHubHelper implements Registerable, ProtocolHandler {
+ // Exception codes
+ const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900;
+
/**
* Protocol used
*/
*/
private $diff = 0;
- /**
- * Connect retries for this connection
- */
- private $retryCount = 0;
-
/**
* Wether this connection is shutted down
*/
// Call parent constructor
parent::__construct($className);
- // 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);
- }
- /**
- * Getter for port number to satify ProtocolHandler
- *
- * @return $port The port number
- */
- public final function getPort () {
- return $this->port;
- }
+ // Init state which sets the state to 'init'
+ $this->initState();
- /**
- * Setter for port number to satify ProtocolHandler
- *
- * @param $port The port number
- * @return void
- */
- protected final function setPort ($port) {
- $this->port = $port;
+ // Register this connection helper
+ Registry::getRegistry()->addInstance('connection', $this);
}
/**
- * Getter for protocol
+ * Getter for real class name, overwrites generic method and is final
*
- * @return $protocol Used protocol
+ * @return $class Name of this class
*/
- public final function getProtocol () {
- return $this->protocol;
- }
+ public final function __toString () {
+ // Class name representation
+ $class = self::getConnectionClassName($this->getAddress(), $this->getPort(), parent::__toString());
- /**
- * Setter for protocol
- *
- * @param $protocol Used protocol
- * @return void
- */
- protected final function setProtocol ($protocol) {
- $this->protocol = $protocol;
+ // Return it
+ return $class;
}
/**
- * Getter for IP address
+ * Static "getter" for this connection class' name
*
- * @return $address The IP address
+ * @param $address IP address
+ * @param $port Port number
+ * @param $className Original class name
+ * @return $class Expanded class name
*/
- public final function getAddress () {
- return $this->address;
+ public static function getConnectionClassName ($address, $port, $className) {
+ // Construct it
+ $class = $address . ':' . $port . ':' . $className;
+
+ // ... and return it
+ return $class;
}
/**
- * Setter for IP address
+ * Initializes the peer's state which sets it to 'init'
*
- * @param $address The IP address
* @return void
*/
- protected final function setAddress ($address) {
- $this->address = $address;
+ private function initState() {
+ /*
+ * Get the state factory and create the initial state, we don't need
+ * the state instance here
+ */
+ PeerStateFactory::createPeerStateInstanceByName('init', $this);
}
/**
*/
private function getRawDataFromPackageArray (array $packageData) {
// Get the fragmenter instance
- $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package_fragmenter_class');
+ $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
// Implode the package data array and fragement the resulting string, returns the final hash
$finalHash = $fragmenterInstance->fragmentPackageArray($packageData, $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
*
* @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();
+
// Cache buffer length
$bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length');
$rawData .= $dataStream;
} // END - while
- // Nothing to sent is bad news!
+ // Nothing to sent is bad news, so assert on it
assert(strlen($rawData) > 0);
// Encode the raw data with our output-stream
// 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);
+ // Handle the error with a faked recipientData array
+ $this->handleSocketError($socketResource, array('0.0.0.0', '0'));
// And throw it
- throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
} 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
+ // The difference between sent bytes and length of raw data should not go below zero
assert((strlen($encodedData) - $sentBytes) >= 0);
// Add total sent bytes
return $totalSentBytes;
}
- /**
- * 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 () {
+ protected final function markConnectionShuttedDown () {
+ /* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ' has been marked as shutted down');
$this->shuttedDown = true;
}
* @return $shuttedDown Wether this connection is shutted down
*/
public final function isShuttedDown () {
+ /* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
return $this->shuttedDown;
}
+
+ // ************************************************************************
+ // Socket error handler call-back methods
+ // ************************************************************************
+
+ /**
+ * Handles socket error 'connection timed out', but does not clear it for
+ * later debugging purposes.
+ *
+ * @param $socketResource A valid socket resource
+ * @throws SocketConnectionException The connection attempts fails with a time-out
+ */
+ private function socketErrorConnectionTimedOutHandler ($socketResource) {
+ // Get socket error code for verification
+ $socketError = socket_last_error($socketResource);
+
+ // Get error message
+ $errorMessage = socket_strerror($socketError);
+
+ // Shutdown this socket
+ $this->shutdownSocket($socketResource);
+
+ // Throw it again
+ throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+ }
}
// [EOF]