* @param $socketResource A valid socket resource
* @param $socketProtocol Socket protocol (TCP, UDP, file)
* @param $packageInstance An instance of a DeliverablePackage class
+ * @param $socketType Socket type (incoming, outgoing, server, file, ...)
* @return $socketInstance An instance of this Container class
* @throws InvalidArgumentException If socket type is not set in $packageData array
*/
- public static final function createSocketContainer ($socketResource, $socketProtocol, DeliverablePackage $packageInstance) {
+ public static final function createSocketContainer ($socketResource, $socketProtocol, DeliverablePackage $packageInstance, $socketType) {
// Trace message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketResource[%s]=%s,socketProtocol=%s,packageInstance[]=%s - CALLED!', gettype($socketResource), $socketResource, $socketProtocol, gettype($packageInstance)));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketResource[%s]=%s,socketProtocol=%s,packageInstance[]=%s,socketType=%s - CALLED!', gettype($socketResource), $socketResource, $socketProtocol, gettype($packageInstance), $socketType));
//* DEBUG-PRINT: */ printf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE));
// Get a new instance
$socketInstance = new SocketContainer();
- // Trace message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketResource=%s,socketProtocol=%s', $socketResource, $socketProtocol));
-
// Set all values
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketResource=%s,socketProtocol=%s', $socketResource, $socketProtocol));
$socketInstance->setSocketProtocol($socketProtocol);
$socketInstance->setSocketResource($socketResource);
$socketInstance->setPackageDataInstance($packageInstance);
+ $socketInstance->setSocketType($socketType);
// Return the prepared instance
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socketInstance=%s - EXIT!', strtoupper($socketProtocol), $socketInstance->__toString()));
* @return $matches Whether $address matches with the one from package data
*/
public function ifAddressMatches ($unl) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unl=%s - CALLED!', strtoupper($this->getSocketProtocol()), $unl));
-
// Get current package data
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unl=%s - CALLED!', strtoupper($this->getSocketProtocol()), $unl));
$packageInstance = $this->getPackageDataInstance();
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: unl=' . $unl . ',packageInstance=' . print_r($packageInstance, true));
-
// So, does both match?
+ //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: unl=' . $unl . ',packageInstance=' . print_r($packageInstance, true));
$matches = (($packageInstance->getRecipientUnl() !== '') && ($packageInstance->getRecipientUnl() === $unl));
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches)));
-
// Return result
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches)));
return $matches;
}
* @return $matches Whether given socket matches
*/
public function ifSocketResourceMatches ($socketResource) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource());
-
// So, does both match?
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource());
$matches = ((is_resource($socketResource)) && ($socketResource === $this->getSocketResource()));
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches)));
-
// Return result
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches)));
return $matches;
}
* @throws InvalidSocketException If socket is invalid
*/
public function getSocketRecipient () {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
-
// Should be valid socket
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
// Get package data
$packageInstance = $this->getPackageDataInstance();
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: packageInstance.recipient=%s - EXIT!', strtoupper($this->getSocketProtocol()), $packageInstance->getRecipientUnl()));
-
// Return it
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: packageInstance.recipient=%s - EXIT!', strtoupper($this->getSocketProtocol()), $packageInstance->getRecipientUnl()));
return $packageInstance->getRecipientUnl();
}
*/
public function getSocketRecipientAddress () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Should be valid socket
if (!$this->isValidSocket()) {
*/
public function getSocketRecipientPort () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Should be valid socket
if (!$this->isValidSocket()) {
*/
public function isValidSocket () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Get socket resource
$socketResource = $this->getSocketResource();
*/
public function getLastSocketErrorCode () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Should be valid socket
if (!$this->isValidSocket()) {
*/
public function getLastSocketErrorMessage () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Should be valid socket
if (!$this->isValidSocket()) {
*/
public function translateLastSocketErrorCodeToName () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Get last error code
$errorCode = $this->getLastSocketErrorCode();
*/
public function bindSocketToFile () {
// Should be valid socket
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
*/
public function bindSocketToListener () {
// Should be valid socket
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
*/
public function listenToSocket () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Should be valid socket
if (!$this->isValidSocket()) {
*/
public function enableSocketNonBlocking () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Should be valid socket
if (!$this->isValidSocket()) {
*/
public function enableSocketReuseAddress () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Should be valid socket
if (!$this->isValidSocket()) {
* @throws InvalidSocketException If stored socket is invalid
*/
public function connectToSocketRecipient () {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
-
// Should be valid socket
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
// Get recipient UNL
$unlRecipient = $this->getSocketRecipient();
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlRecipient=%s', strtoupper($this->getSocketProtocol()), $unlRecipient));
-
// Create UNL instance from it. This will validate the connection
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlRecipient=%s', strtoupper($this->getSocketProtocol()), $unlRecipient));
$locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($unlRecipient);
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlAddress=%s,unlPort=%d', strtoupper($this->getSocketProtocol()), $locatorInstance->getUnlAddress(), $locatorInstance->getUnlPort()));
-
// Try to connect to it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlAddress=%s,unlPort=%d', strtoupper($this->getSocketProtocol()), $locatorInstance->getUnlAddress(), $locatorInstance->getUnlPort()));
$result = socket_connect($this->getSocketResource(), $locatorInstance->getUnlAddress(), $locatorInstance->getUnlPort());
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result)));
-
// Return result
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result)));
return $result;
}
* @todo rewrite this!
*/
public function doShutdown () {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
-
// Should be valid socket
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
throw new BadMethodCallException(sprintf('[%s:%d]: Shutdown on invalid socket. Maybe called already?', __METHOD__, __LINE__), self::EXCEPTION_INVALID_SOCKET);
$this->markConnectionShuttedDown();
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: EXIT!');
}
/**
* @throws InvalidSocketException If stored socket is invalid
*/
public function acceptNewIncomingSocket () {
- // Trace message
- //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
-
// Should be valid socket
+ /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
} // END - if
// Debug message
- //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Init all arrays, at least readers
+ /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
$readers = array($this->getSocketResource());
$writers = array();
$excepts = array();
- // Trace message
- //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling socket_select(%d,%d,%d,0%d) ...', strtoupper($this->getSocketProtocol()), count($readers), count($writers), count($excepts), $this->socketSelectTimeout));
-
// Check if we have some peers left
+ /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling socket_select(%d,%d,%d,0%d) ...', strtoupper($this->getSocketProtocol()), count($readers), count($writers), count($excepts), $this->socketSelectTimeout));
$left = socket_select(
$readers,
$writers,
);
// Some new peers found?
- //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: left=%d,readers()=%d,writers()=%d,except()=%d) ...', strtoupper($this->getSocketProtocol()), $left, count($readers), count($writers), count($excepts)));
+ /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: left=%d,readers()=%d,writers()=%d,except()=%d) ...', strtoupper($this->getSocketProtocol()), $left, count($readers), count($writers), count($excepts)));
if ($left < 1) {
// Nothing new found
- //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: left=%d,serverSocket=%s,readers()=%d', strtoupper($this->getSocketProtocol()), $left, $this->getSocketResource(), count($readers)));
+ /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: left=%d,serverSocket=%s,readers()=%d', strtoupper($this->getSocketProtocol()), $left, $this->getSocketResource(), count($readers)));
return NULL;
} elseif (!in_array($this->getSocketResource(), $readers)) {
// Abort here
/*
* Then accept it, if this socket is set to non-blocking IO and the
* connection is NOT sending any data, socket_read() may throw
- * error 11 (Resource temporary unavailable). This really nasty
+ * error 11 (Resource temporary unavailable). This is really nasty
* because if you have blocking IO socket_read() will wait and wait
* and wait ...
*/
* @throws InvalidSocketException If the stored socket is not valid
*/
public function readDataFromSocket () {
- // Trace message
- /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
-
// Should be valid socket
+ /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
} // END - if
- // Debug message
- /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
-
/*
* Read raw data from socket. If you change PHP_BINARY_READ to
* PHP_NORMAL_READ, this line will endless block. This script does only
* provide simultanous or faked threads, not real.
*/
+ /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
$rawData = socket_read($this->getSocketResource(), $this->getConfigInstance()->getConfigEntry('tcp_buffer_length'), PHP_BINARY_READ);
- // Trace message
- /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: rawData[%s]()=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($rawData), strlen($rawData)));
-
// Return it
+ /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: rawData[%s]()=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($rawData), strlen($rawData)));
return $rawData;
}
*/
public function identifySocketPeer () {
// Init peer address (IP)/port
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
$peerAddress = '0.0.0.0';
$peerPort = '0';
$result = $this->getSocketPeerName($peerAddress, $peerPort);
// Valid?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d,peerAddress[%s]=%s,peerPort[%s]=%d', strtoupper($this->getSocketProtocol()), intval($result), gettype($peerAddress), $peerAddress, gettype($peerPort), $peerPort));
if ($result === TRUE) {
// Set both
$this->setPeerAddress($peerAddress);
$this->setPeerPort($peerPort);
} // END - if
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result)));
-
// Return result
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result)));
return $result;
}
*/
public function setSocketTimeoutOptions () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Array for timeout settings
$options = array(
* @return $result Whether OOB has been enabled
*/
public function enableSocketOutOfBandData () {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
-
// Call inner method
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
$result = $this->setSocketOption(SOL_SOCKET, SO_OOBINLINE, 1);
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result)));
// Return result
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result)));
return $result;
}
* @throws BadMethodCallException If no socket error was reported but method called
*/
public function clearLastSocketError () {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Clearing socket error - CALLED!');
-
// Should be valid socket
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Clearing socket error - CALLED!');
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
socket_clear_error($this->getSocketResource());
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!');
}
/**
*/
protected function handleSocketErrorPermissionDenied () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Get socket error code for verification
$socketError = $this->getLastSocketErrorCode();
*/
protected function handleSocketErrorConnectionTimedOut () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Get socket error code for verification
$socketError = $this->getLastSocketErrorCode();
*/
protected function handleSocketErrorResourceUnavailable () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Get socket error code for verification
$socketError = $this->getLastSocketErrorCode();
*/
protected function handleSocketErrorConnectionRefused () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Get socket error code for verification
$socketError = $this->getLastSocketErrorCode();
*/
protected function handleSocketErrorNoRouteToHost () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Get socket error code for verification
$socketError = $this->getLastSocketErrorCode();
*/
protected function handleSocketErrorConnectionResetByPeer () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// Get socket error code for verification
$socketError = $this->getLastSocketErrorCode();
* @throws SocketOperationException The connection attempts fails with a time-out
*/
protected function handleSocketErrorOperationNotSupported () {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
-
// Get socket error code for verification
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
$socketError = $this->getLastSocketErrorCode();
// Get error message
* @return void
*/
protected function handleSocketErrorOperationInProgress () {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
-
+ // Very common with non-blocking I/O
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-HELPER: Operation is now in progress, this is usual for non-blocking connections and is no bug.');
}
* @return $errorName The translated name (all lower-case, with underlines)
*/
private function translateSocketErrorCodeToName ($errorCode) {
- // Trace message
+ // Unknown error code by default
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $errorCode));
-
- // Nothing bad happened by default
- $errorName = StorableSocket::SOCKET_CONNECTED;
+ $errorName = StorableSocket::SOCKET_ERROR_UNKNOWN;
// Is the code a number, then we have to change it
switch ($errorCode) {
- case 0: // Silently ignored, the socket is connected
+ case 0: // No error, the socket is connected
+ $errorName = StorableSocket::SOCKET_CONNECTED;
break;
case 11: // "Resource temporary unavailable"
default: // Everything else <> 0
// Unhandled error code detected, so first debug it because we may want to handle it like the others
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf(strtoupper($this->getSocketProtocol()) . '-SOCKET: Unsupported errorCode=%d,message=%s', $errorCode, socket_strerror($errorCode)));
-
- // Change it only in this class
- $errorName = StorableSocket::SOCKET_ERROR_UNKNOWN;
break;
} // END - switch
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorName=%d - EXIT!', strtoupper($this->getSocketProtocol()), $errorName));
-
// Return translated name
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorName=%d - EXIT!', strtoupper($this->getSocketProtocol()), $errorName));
return $errorName;
}
* @param $socketType Socket type
* @return void
*/
- public function setSocketType ($socketType) {
+ protected function setSocketType ($socketType) {
$this->socketType = $socketType;
}
// Debugging
//* DEBUG-DIE: */ die(__METHOD__.':poolInstance='.print_r($poolInstance, TRUE));
- //* DEBUG-DIE: */ die(__METHOD__.':poolInstance.arrayFromList('.$protocolName.')='.print_r($poolInstance->getArrayFromList($protocolName), TRUE));
+ //* DEBUG-DIE: */ die(__METHOD__.':poolInstance->arrayFromList('.$protocolName.')='.print_r($poolInstance->getArrayFromList($protocolName), TRUE));
/*
* Now we need to choose again. It is whether we are speaking with a hub
* @throws LogicException If the discovered listener instance has no pool set
*/
public function discoverSocket (DeliverablePackage $packageInstance, $connectionType) {
- // Debug message
+ // Make sure all parameters are valid
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISOVERY: packageInstance=%s,connectionType=%s - CALLED!', $packageInstance->__toString(), $connectionType));
//* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISOVERY: packageInstance=' . print_r($packageInstance, TRUE));
-
- // Make sure all parameters are valid
if (($connectionType != StorableSocket::CONNECTION_TYPE_INCOMING) && ($connectionType != StorableSocket::CONNECTION_TYPE_OUTGOING)) {
// Abort here
throw new InvalidArgumentException(sprintf('connectionType=%s is whether "%s" nor "%s".',
StorableSocket::CONNECTION_TYPE_INCOMING,
StorableSocket::CONNECTION_TYPE_OUTGOING
));
- } // END - if
+ }
// Determine protocol instance
$protocolInstance = ProtocolDiscovery::determineProtocolByPackageInstance($packageInstance);
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('protocolInstance=' . $protocolInstance . ',packageInstance=' . print_r($packageInstance, TRUE));
-
// Is it valid?
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('protocolInstance=' . $protocolInstance . ',packageInstance=' . print_r($packageInstance, TRUE));
assert($protocolInstance instanceof HandleableProtocol);
// Does the UNL validate?
if (!$protocolInstance->isValidUniversalNodeLocatorByPackageInstance($packageInstance)) {
// Not valid, throw exception
throw new InvalidUnlException(array($this, $protocolInstance, $packageInstance), BaseHubSystem::EXCEPTION_INVALID_UNL);
- } // END - if
+ }
// Get the listener instance
$listenerInstance = $this->discoverListenerInstance($protocolInstance, $packageInstance);
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: listenerInstance=' . $listenerInstance . ',packageInstance=' . print_r($packageInstance, TRUE));
-
// If there is no listener who wants to have that package, we simply drop it here
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: listenerInstance=' . $listenerInstance . ',packageInstance=' . print_r($packageInstance, TRUE));
if (is_null($listenerInstance)) {
// Abort with no resource
throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
*/
$socketInstance = $listenerInstance->getPoolInstance()->getSocketFromPackageInstance($packageInstance, $connectionType);
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: socketInstance[]=%s', gettype($socketInstance)));
-
// Is it FALSE, the recipient isn't known to us and we have no connection to it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: socketInstance[]=%s', gettype($socketInstance)));
if (!($socketInstance instanceof StorableSocket) || ($socketInstance->isValidSocket()) || ($socketInstance->getLastSocketErrorCode() > 0)) {
// Try to create a new socket resource
try {
- // Possibly noisy debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Trying to establish a ' . strtoupper($listenerInstance->getProtocolName()) . ' connection to ' . $packageInstance->getRecipientUnl() . ' ...');
-
// Get a socket resource from our factory (if succeeded)
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: Trying to establish a %s connection to %s ...', strtoupper($listenerInstance->getProtocolName()), $packageInstance->getRecipientUnl()));
$socketInstance = SocketFactory::createSocketFromPackageInstance($packageInstance, $protocolInstance);
} catch (SocketConnectionException $e) {
// The connection fails of being established, so log it away
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Caught ' . $e->__toString() . ',message=' . $e->getMessage());
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: Caught %s,message=%s', $e->__toString(), $e->getMessage()));
}
- } // END - if
+ }
// Try to resolve the socket resource
try {
$helperInstance = GenericRegistry::getRegistry()->getInstance('connection');
// Is a state set and it's name is other than socket's error code?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: helperInstance->stateInstance[]=%s', gettype($helperInstance->getStateInstance())));
if ($helperInstance->getStateInstance() instanceof PeerStateable && $helperInstance->getPrintableState() !== $socketInstance->translateLastSocketErrorCodeToName()) {
// Resolve the peer's state (but ignore return value)
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Going to resolve socket from peer state and given package data ...');
}
// And return it
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: socketInstance=%s,socketInstance->socketResource=%s - EXIT!', $socketInstance->__toString(), $socketInstance->getSocketResource()));
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: socketResource=' . $socketInstance->getSocketResource() . ',packageInstance=' . print_r($packageInstance, TRUE));
return $socketInstance;
}
* @todo Also validate protocol to be sure if there is really a protocol handler for it available
*/
public static final function createConnectionInfoInstance ($protocolName, $type) {
- // Init instance
+ // Init factory instance
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: protocolName=%s,type=%s - CALLED!', $protocolName, $type));
$factoryInstance = new ConnectionInfoFactory();
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: key=%s', $key));
if (GenericRegistry::getRegistry()->instanceExists($key)) {
// Get info from registry
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: Getting instance for key=%s ...', $key));
$infoInstance = GenericRegistry::getRegistry()->getInstance($key);
} else {
// Get the info instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: Creating info instance for type=%s ...', $type));
$infoInstance = self::createObjectByConfiguredName('connection_info_class', array($type));
// Add it to the registry
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: Adding key=%s,infoInstance=%s ...', $key, $infoInstance->__toString()));
GenericRegistry::getRegistry()->addInstance($key, $infoInstance);
}
*/
public static function createSocketFromPackageInstance (DeliverablePackage $packageInstance, HandleableProtocol $protocolInstance) {
// Init instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s,protocolInstance=%s - CALLED!', $packageInstance->__toString(), $protocolInstance->__toString()));
$socketInstance = NULL;
// Construct registry key
$packageInstance->getRecipientType()
);
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Trying to find a socket with registryKey=' . $registryKey);
-
// Is the key there?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Trying to find a socket with registryKey=%s', $registryKey));
if (GenericRegistry::getRegistry()->instanceExists($registryKey)) {
// Get container instance
$socketInstance = GenericRegistry::getRegistry()->getInstance($registryKey);
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Using socket ' . $socketInstance->getSocketResource() . '(' . gettype($socketInstance->getSocketResource()) . ') from registry.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Using socketResource[%s]=%s from registry.', gettype($socketInstance->getSocketResource()), $socketInstance->getSocketResource()));
} else {
// Construct configuration entry for object factory and get it
$className = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($protocolInstance->getProtocolName() . '_connection_helper_class');
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Going to use class ' . $className . ' for creating a socket resource ...');
-
// And call the static method
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Going to use className=%s for creating a socket resource ...', $className));
$socketInstance = call_user_func($className . '::createConnectionFromPackageInstance', $packageInstance);
// Register it with the registry
GenericRegistry::getRegistry()->addInstance($registryKey, $socketInstance);
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Socket is now registered in registry.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Socket is now registered in registry.');
}
- // Trace message
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
-
// Return the socket (container) instance
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}
$socketResource,
StorableSocket::SOCKET_PROTOCOL_FILE,
$packageInstance,
+ StorableSocket::CONNECTION_TYPE_SERVER,
));
- // Set file socket data
+ // Set socket file
$socketInstance->setSocketFile($socketFile);
- $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER);
-
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance[]=%s', gettype($socketInstance)));
// Is the socket resource valid?
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance[]=%s', gettype($socketInstance)));
if (!$socketInstance->isValidSocket()) {
// Something bad happened
throw new InvalidSocketException(array($listenerInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
// Check if there was an error else
if ($socketInstance->getLastSocketErrorCode() > 0) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Start listen for connections
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.');
if (!$socketInstance->listenToSocket()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Allow non-blocking I/O
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
$socketResource,
StorableSocket::SOCKET_PROTOCOL_TCP,
$packageInstance,
+ StorableSocket::CONNECTION_TYPE_OUTGOING,
));
- // Set socket type to outgoing
- $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_OUTGOING);
-
// Is the socket resource valid?
if (!$socketInstance->isValidSocket()) {
/*
// Check if there was an error else
if ($socketInstance->getLastSocketErrorCode() > 0) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$helperInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Set the option to reuse the port
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...');
if (!$socketInstance->enableSocketReuseAddress()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
*/
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
$socketResource,
StorableSocket::SOCKET_PROTOCOL_TCP,
$packageInstance,
+ StorableSocket::CONNECTION_TYPE_SERVER,
));
- // Set listener instance and type
+ // Set listener instance
$socketInstance->setListenerInstance($listenerInstance);
- $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER);
// Is the socket resource valid?
if (!$socketInstance->isValidSocket()) {
// Check if there was an error else
if ($socketInstance->getLastSocketErrorCode() > 0) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Set the option to reuse the port
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...');
if (!$socketInstance->enableSocketReuseAddress()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
*/
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
if (!$socketInstance->bindSocketToListener()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Start listen for connections
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.');
if (!$socketInstance->listenToSocket()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Now, we want non-blocking mode
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
$socketResource,
StorableSocket::SOCKET_PROTOCOL_UDP,
$packageInstance,
+ StorableSocket::CONNECTION_TYPE_SERVER,
));
- // Set listener instance and socket type
+ // Set listener instance
$socketInstance->setListenerInstance($listenerInstance);
- $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER);
// Is the socket resource valid?
if (!$socketInstance->isValidSocket()) {
// Check if there was an error else
if ($socketInstance->getLastSocketErrorCode() > 0) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Set the option to reuse the port
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...');
if (!$socketInstance->enableSocketReuseAddress()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
*/
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
if (!$socketInstance->bindSocketToListener()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Now, we want non-blocking mode
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
*/
public static final function createNextAcceptedSocketFromPool (Poolable $poolInstance, StorableSocket $socketInstance) {
// Trace message
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: poolInstance=%s,socketInstance.socketResource=%s - CALLED!', $poolInstance->__toString(), $socketInstance->getSocketResource()));
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: poolInstance=%s,socketInstance->socketResource=%s - CALLED!', $poolInstance->__toString(), $socketInstance->getSocketResource()));
// Get socket protocol
$socketProtocol = $socketInstance->getSocketProtocol();
$socketResource,
$socketProtocol,
$packageInstance,
+ StorableSocket::CONNECTION_TYPE_INCOMING,
));
// Set all socket data
- $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_INCOMING);
$socketInstance->setPeerAddress('invalid');
$socketInstance->setPeerPort('0');
// Try to identify socket peer
if (!$socketInstance->identifySocketPeer()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Set timeout to configured seconds
if (!$socketInstance->setSocketTimeoutOptions()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Enable SO_OOBINLINE
if (!$socketInstance->enableSocketOutOfBandData()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Set non-blocking
if (!$socketInstance->enableSocketNonBlocking()) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
- // Trace message
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
-
// Return found socket instance
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}
}
* @return void
*/
protected function prepareAnswerMessage (DeliverableMessage $messageInstance, Deliverable $packageInstance) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATA-HANDLER:Going to send an answer message for ' . $this->getHandlerName() . ' ...');
-
// Get a helper instance based on this handler's name
- $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageInstance));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATA-HANDLER: Going to send an answer message for ' . $this->getHandlerName() . ' ...');
+ $helperInstance = ObjectFactory::createObjectByConfiguredName(sprintf('node_answer_%s_helper_class', $this->getHandlerName()), array($messageInstance));
// Get node instance
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATA-HANDLER: Creating node instance ...');
$this->removeMessageConfigurationData($messageInstance);
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATA-HANDLER:Answer message has been prepared.');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATA-HANDLER: Answer message has been prepared.');
}
/**
* @param $packageInstance An instance of a DeliverablePackage class
* @return $hash Private key's hash
* @throws InvalidPrivateKeyHashException If the private key's hash is not valid
- * @throws InvalidArgumentException If $packageInstance does not contain senderId/senderAddress
+ * @throws InvalidArgumentException If $packageInstance does not contain senderId
*/
private function determineSenderPrivateKeyHash (DeliverablePackage $packageInstance) {
+ // Is the parameter valid?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
+ if ($packageInstance->getSenderId() == '') {
+ // Invalid $packageInstance
+ throw new InvalidArgumentException('packageInstance does not contain senderId');
+ }
+
// Get DHT instance
$dhtInstance = DhtObjectFactory::createDhtInstance('node');
- // Init sender data array
- $senderData = array();
-
// Ask DHT for sender's id or address (both session id)
- if ($packageInstance->getSenderId() != '') {
- // Field senderId is given
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Finding local node data for packageInstance->senderId=%s ...', $packageInstance->getSenderId()));
- $senderData = $dhtInstance->findNodeLocalBySessionId($packageInstance->getSenderId());
- } elseif ($packageInstance->getSenderAddress() != '') {
- // Field senderAddress is given
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Finding local node data for packageInstance->senderAddress=%s ...', $packageInstance->getSenderAddress()));
- $senderData = $dhtInstance->findNodeLocalBySessionId($packageInstance->getSenderAddress());
- } else {
- // Invalid $packageInstance
- throw new InvalidArgumentException('packageInstance does not contain senderId/senderAddress');
- }
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Finding local node data for packageInstance->senderId=%s ...', $packageInstance->getSenderId()));
+ $senderData = $dhtInstance->findNodeLocalBySessionId($packageInstance->getSenderId());
// Is an entry found?
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: senderData()=%d', count($senderData)));
//* DEBUG-DIE: */ die(sprintf('[%s:%d]: senderData=%s', __METHOD__, __LINE__, print_r($senderData, TRUE)));
- if (count($senderData) > 0) {
- // Make sure the element 'private_key_hash' is there
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: senderData=' . print_r($senderData, TRUE));
- assert(isset($senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH]));
-
- // Don't accept empty hashes
- if (empty($senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH])) {
- // Don't accept
- throw new InvalidPrivateKeyHashException(array($this, $senderData, 'empty hash in senderData'), BaseHubSystem::EXCEPTION_INVALID_PRIVATE_KEY_HASH);
- } // END - if
-
+ if (count($senderData) > 0 || !empty($senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH])) {
// Return it
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Returning senderData[%s]=%s', NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH, $senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH]));
return $senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH];
- } // END - if
+ }
// Don't accept empty keys
if (empty($packageInstance->getSenderPrivateKeyHash())) {
// This needs fixing
throw new InvalidPrivateKeyHashException(array($this, $senderData, 'empty hash in decodedData'), BaseHubSystem::EXCEPTION_INVALID_PRIVATE_KEY_HASH);
- } // END - if
+ }
// There is no DHT entry so, accept the hash from decoded data
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Returning packagheInstance->senderPrivateKeyHash=%s - EXIT!', $packageInstance->getSenderPrivateKeyHash()));
return $packageInstance->getSenderPrivateKeyHash();
}
$packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class');
// Set all data
- $packageInstance->setSenderAddress($this->getNodeInstance()->getSessionId());
- $packageInstance->setSenderPort('0');
+ $packageInstance->setSenderId($this->getNodeInstance()->getSessionId());
$packageInstance->setRecipientId($helperInstance->getRecipientId());
$packageInstance->setRecipientType($helperInstance->getRecipientType());
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Setting packageContent=%s ...', $packageContent));
if (!$this->isChecksumValid($packageInstance)) {
// Is not the same, so throw an exception here
throw new InvalidDataChecksumException(array($this, $packageInstance), self::EXCEPTION_INVALID_DATA_CHECKSUM);
- } // END - if
+ }
/*
* The checksum is the same, then it can be decompressed safely. The
use Org\Shipsimu\Hub\Locator\Node\Tools\NodeLocatorUtils;
use Org\Shipsimu\Hub\Network\Package\DeliverablePackage;
+// Import SPL stuff
+use \BadMethodCallException;
+
/**
* A general handler for IPv4 protocols such as TCP, UDP and others.
*
*
* @param $packageInstance Valid raw package data
* @return $isValid Whether the UNL can be validated
+ * @throws BadMethodCallException If protocol of package's recipient UNL is not matching handler's name
*/
public function isValidUniversalNodeLocatorByPackageInstance (DeliverablePackage $packageInstance) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('IPV4-PROTOCOL-HANDLER: CALLED!');
-
// Is the correct handler choosen?
- assert(substr($packageInstance->getRecipientUnl(), 0, strlen($this->getHandlerName())) != $this->getHandlerName());
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('IPV4-PROTOCOL-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
+ if (substr($packageInstance->getRecipientUnl(), 0, strlen($this->getProtocolName())) != $this->getProtocolName()) {
+ // Not matching, throw exception
+ throw new BadMethodCallException(sprintf('packageInstance->recipientUnl=%s cannot be handled by this protocol handler (%s)', $packageInstance->getRecipientUnl(), $this->getProtocolName()));
+ }
// Default is from generic validation
$isValid = NodeLocatorUtils::isValidUniversalNodeLocator($packageInstance->getRecipientUnl());
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('IPV4-PROTOCOL-HANDLER: packageInstance.recipient=' . $packageInstance->getRecipientUnl() . ',isValid[' . gettype($isValid) . ']=' . intval($isValid));
-
// If this doesn't fail, continue validating the IP:port combination
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('IPV4-PROTOCOL-HANDLER: packageInstance->recipientUnl=%s,isValid[%s]=%d', $packageInstance->getRecipientUnl(), gettype($isValid), intval($isValid)));
if ($isValid === TRUE) {
// ... and validate IP:port, first "parse" the UNL
$locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipientUnl());
} // END - if
// Return result
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('IPV4-PROTOCOL-HANDLER: isValid=' . intval($isValid) . ' - EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('IPV4-PROTOCOL-HANDLER: isValid=%d - EXIT!', intval($isValid)));
return $isValid;
}
* @return void
*/
public function processRawDataFromSocketInstance (StorableSocket $socketInstance) {
- // Trace message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-RAW-DATA-HANDLER: socketInstance=%s - CALLED!', $socketInstance->__toString()));
-
// Reset error code to unhandled
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-RAW-DATA-HANDLER: socketInstance=%s - CALLED!', $socketInstance->__toString()));
$this->setErrorCode(StorableSocket::SOCKET_ERROR_UNHANDLED);
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: Handling TCP package from resource=' . $socketInstance->getSocketResource() . ',last error=' . $socketInstance->getLastSocketErrorMessage());
-
// Read the raw data from socket in a loop
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-RAW-DATA-HANDLER: Handling TCP package from resource=%s,last error=%s,socketType=%s', $socketInstance->getSocketResource(), $socketInstance->getLastSocketErrorMessage(), $socketInstance->getSocketType()));
while ($rawData = $socketInstance->readDataFromSocket()) {
// Get socket error code back
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-RAW-DATA-HANDLER: rawData(%d)[]=%s', strlen($rawData), gettype($rawData)));
$this->lastSocketError = $socketInstance->getLastSocketErrorCode();
- // Debug output of read data length
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: rawData[' . gettype($rawData) . ']=' . strlen($rawData) . ',MD5=' . md5($rawData) . ',resource=' . $socketInstance . ',this->lastSocketError=' . socket_strerror($this->lastSocketError));
-
// Is not FALSE?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: rawData[' . gettype($rawData) . ']=' . strlen($rawData) . ',MD5=' . md5($rawData) . ',resource=' . $socketInstance . ',this->lastSocketError=' . socket_strerror($this->lastSocketError));
if ($rawData !== FALSE) {
// Output raw data
/* NOISY-DEBUG: */self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: rawData=' . $rawData);
// Is it valid?
if ($this->lastSocketError == 11) {
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: Ignoring error 11 (Resource temporary unavailable) from socket resource=' . $socketInstance);
-
/*
* Error code 11 (Resource temporary unavailable) can be safely
* ignored on non-blocking sockets. The socket is currently not
* sending any data.
*/
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: Ignoring error 11 (Resource temporary unavailable) from socket resource=' . $socketInstance);
$socketInstance->clearLastSocketError();
// Skip any further processing
$this->setErrorCode($this->lastSocketError);
break;
} elseif (empty($rawData)) {
- // Trace message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: No raw data pending. - EXIT!');
-
// The peer did send nothing to us which is now being ignored
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: No raw data pending. - EXIT!');
continue;
} else {
/*
* well-formed BASE64-encoded message with start and markers. This
* will be checked later on.
*/
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: Adding ' . strlen($rawData) . ' bytes to stacker ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-RAW-DATA-HANDLER: Adding %d bytes to stacker ...', strlen($rawData)));
$this->addRawDataToStacker($rawData);
}
} // END - while
// Trace message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: EXIT!');
-
}
}
// Now connect to it
if (!$helperInstance->connectToPeerBySocketRecipient()) {
// Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-CONNECTION-HELPER: helperInstance=%s,locatorInstance.unlData=%s', $helperInstance->__toString(), print_r($locatorInstance->getUnlData(), TRUE)));
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-CONNECTION-HELPER: helperInstance=%s,locatorInstance->unlData=%s', $helperInstance->__toString(), print_r($locatorInstance->getUnlData(), TRUE)));
// Handle socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
*/
public final static function createConnectionInfo ($connectionType) {
// Get new instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: connectionType=%s - CALLED!', $connectionType));
$infoInstance = new ConnectionInfo();
// Set connection type here
$infoInstance->setConnectionType($connectionType);
// Return the prepared instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: infoInstance=%s - EXIT!', $infoInstance->__toString()));
return $infoInstance;
}
* @return void
*/
public function fillWithListenerInformation (Listenable $listenerInstance) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: protocolName=' . $listenerInstance->getProtocolName() . ',listenerInstance=' . $listenerInstance->__toString() . ' - CALLED!');
-
// Fill the generic array with several data from the listener:
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: protocolName=' . $listenerInstance->getProtocolName() . ',listenerInstance=' . $listenerInstance->__toString() . ' - CALLED!');
$this->setProtocolName($listenerInstance->getProtocolName());
$this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $listenerInstance->getListenAddress());
$this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $listenerInstance->getListenPort());
// Set listener here
$this->setListenerInstance($listenerInstance);
- // Debug message
+ // Trace message
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: EXIT!');
}
* @return void
*/
public function fillWithConnectionHelperInformation (ConnectionHelper $helperInstance) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: protocolName=' . $helperInstance->getProtocolName() . ',helperInstance=' . $helperInstance->__toString() . ',socketResource=' . $helperInstance->getSocketResource() . ' - CALLED!');
-
// Fill the generic array with several data from the listener:
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: protocolName=' . $helperInstance->getProtocolName() . ',helperInstance=' . $helperInstance->__toString() . ',socketResource=' . $helperInstance->getSocketResource() . ' - CALLED!');
$this->setProtocolName($helperInstance->getSocketInstance()->getSocketProtocol());
$this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $helperInstance->getSocketInstance()->getSocketRecipientAddress());
$this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $helperInstance->getSocketInstance()->getSocketRecipientPort());
* @throws InvalidSocketException If no peer data can be retrived from socket
*/
public function fillWithSocketPeerInformation (StorableSocket $socketInstance) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: socketInstance.socketResource=%s - CALLED!', $socketInstance->getSocketResource()));
-
// Init variables
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: socketInstance->socketResource=%s - CALLED!', $socketInstance->getSocketResource()));
$socketAddress = '0.0.0.0';
$socketPort = '0';
throw new InvalidSocketException(array($this, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
} // END - if
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: socketAddress=%s,socketPort=%d', $socketAddress, $socketPort));
-
// Fill the generic array with several data from the listener:
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: socketAddress=%s,socketPort=%d', $socketAddress, $socketPort));
$this->setProtocolName($socketInstance->getSocketProtocol());
$this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $socketAddress);
$this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $socketPort);
// Set socket here
- $this->setSocketInstance($socketInstance);
-
- // Debugging:
//* DEBUG-DIE: */ die(sprintf('[%s:%d]: socketInstance=%s', __METHOD__, __LINE__, print_r($socketInstance, TRUE)));
+ $this->setSocketInstance($socketInstance);
// Trace message
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: EXIT!');
* @throws LogicException If no info instance can be created
*/
protected function registerServerSocketInstance (StorableSocket $socketInstance) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=%s - CALLED!', strtoupper($this->getProtocolName()), $socketInstance->__toString()));
-
// First check if it is valid
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=%s - CALLED!', strtoupper($this->getProtocolName()), $socketInstance->__toString()));
if ($this->isServerSocketRegistered($socketInstance)) {
// Already registered
throw new SocketAlreadyRegisteredException(array($this, $socketInstance->getSocketResource()), self::EXCEPTION_SOCKET_ALREADY_REGISTERED);
$infoInstance->fillWithListenerInformation($this);
// Register the socket
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Registering socketInstance->socketResource=%s,socketType=%s', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource(), $socketInstance->getSocketType()));
$this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance);
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Setting socketInstance ... (socketResource=%s)', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource()));
-
// And set it here
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Setting socketInstance ... (socketResource=%s)', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource()));
$this->setSocketInstance($socketInstance);
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName())));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName())));
}
/**
* @return void
*/
public function monitorIncomingRawData () {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: CALLED!', strtoupper($this->getProtocolName())));
-
// Check if an incoming socket is registered
- if (!$this->getRegistryInstance()->isIncomingSocketRegistered()) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: No socket of type "%s" registered. - EXIT!', strtoupper($this->getProtocolName()), StorableSocket::CONNECTION_TYPE_INCOMING));
-
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: CALLED!', strtoupper($this->getProtocolName())));
+ if (!$this->getRegistryInstance()->isIncomingSocketRegistered($this)) {
// Skip further processing
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: No socket of type "%s" registered. - EXIT!', strtoupper($this->getProtocolName()), StorableSocket::CONNECTION_TYPE_INCOMING));
return;
- } // END - if
-
- // Debugging:
- //* DEBUG-DIE: */ $this->getRegistryInstance()->debugInstance();
+ }
// Get iterator from registry instance
- $iteratorInstance = $this->getRegistryInstance()->getIterator();
-
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: iteratorInstance[]=%s', strtoupper($this->getProtocolName()), gettype($iteratorInstance)));
+ //* DEBUG-DIE: */ $this->getRegistryInstance()->debugInstance();
+ $iteratorInstance = $this->getRegistryInstance()->getIterator([$this->getProtocolName()]);
// Should be valid instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: iteratorInstance[]=%s', strtoupper($this->getProtocolName()), gettype($iteratorInstance)));
if (is_null($iteratorInstance)) {
// Throw NPE
throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
} // END - if
// Is it valid?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: iteratorInstance->valid()=%d', strtoupper($this->getProtocolName()), intval($iteratorInstance->valid())));
if (!$iteratorInstance->valid()) {
// Then rewind it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Rewinding iterator ...', strtoupper($this->getProtocolName())));
$iteratorInstance->rewind();
} // END - if
// Get current instance
$socketInstance = $iteratorInstance->current();
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance[]=%s', strtoupper($this->getProtocolName()), gettype($socketInstance)));
-
// Is NULL returned?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance[]=%s', strtoupper($this->getProtocolName()), gettype($socketInstance)));
if (is_null($socketInstance)) {
- // Trace message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=NULL - EXIT!', strtoupper($this->getProtocolName())));
-
// Then abort here
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=NULL - EXIT!', strtoupper($this->getProtocolName())));
return;
} // END - if
$iteratorInstance->next();
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName())));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName())));
}
/**
* @throws LogicException If no info instance can be created
*/
protected function doListenSocketSelect ($peerSuffix) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: peerSuffix=%s - CALLED!', strtoupper($this->getProtocolName()), $peerSuffix));
-
// Check on all instances
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: peerSuffix=%s - CALLED!', strtoupper($this->getProtocolName()), $peerSuffix));
assert($this->getPoolInstance() instanceof Poolable);
assert($this->getSocketInstance()->isValidSocket());
// Get next socket instance from pool over the factory
$socketInstance = SocketFactory::createNextAcceptedSocketFromPool($this->getPoolInstance(), $this->getSocketInstance());
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance[]=%s accepted.', strtoupper($this->getProtocolName()), gettype($socketInstance)));
-
// Is socket instance set?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance[]=%s accepted.', strtoupper($this->getProtocolName()), gettype($socketInstance)));
if (!($socketInstance instanceof StorableSocket)) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: No new connection on listener ... - EXIT!', strtoupper($this->getProtocolName())));
-
// Nothing has changed on the listener
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: No new connection on listener ... - EXIT!', strtoupper($this->getProtocolName())));
return;
} // END - if
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance.socketResource=%s accepted.', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource()));
-
// Init peer address/port
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance->socketResource=%s accepted.', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource()));
$peerAddress = '0.0.0.0';
$peerPort = '0';
// Get peer name
if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) {
- // Handle this socket error with a faked recipientData array
+ // Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
// Get a connection info instance
$infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), StorableSocket::CONNECTION_TYPE_INCOMING);
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: infoInstance[]=%s', strtoupper($this->getProtocolName()), gettype($infoInstance)));
-
// Is the instance set?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: infoInstance[]=%s', strtoupper($this->getProtocolName()), gettype($infoInstance)));
if (!($infoInstance instanceof ShareableInfo)) {
// Should not happen
throw new LogicException(sprintf('infoInstance[]=%s does not implement ShareableInfo', gettype($infoInstance)));
// Will the info instance with listener data
$infoInstance->fillWithSocketPeerInformation($socketInstance);
- // Debugging:
- //* DEBUG-DIE: */ $infoInstance->debugInstance();
-
// Set all required data
+ //* DEBUG-DIE: */ $infoInstance->debugInstance();
$socketInstance->setSenderAddress($peerAddress . $peerSuffix);
$socketInstance->setSenderPort($peerPort);
- $socketInstance->setRecipientId($nodeInstance->getSessionId());
// Register the socket with the registry and with the faked array
$this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance);
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Calling this.handlerInstance.processRawDataFromSocketInstance(resource=%s) ...', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource()));
-
// Handle it here, if not main server socket
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Calling this.handlerInstance->processRawDataFromSocketInstance(resource=%s) ...', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource()));
$this->getHandlerInstance()->processRawDataFromSocketInstance($socketInstance);
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName())));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName())));
}
/**
$unlProtocol = $locatorInstance->getUnlProtocol();
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: locatorInstance.unlProtocol=%s,socketProtocol=%s', strtoupper($this->getProtocolName()), $unlProtocol, $socketProtocol));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: locatorInstance->unlProtocol=%s,socketProtocol=%s', strtoupper($this->getProtocolName()), $unlProtocol, $socketProtocol));
// Is same protocol?
$accepts = ($unlProtocol == $socketProtocol);
* @throws LogicException If the given handler instance is not implementing a required interface
*/
public function monitorIncomingRawData () {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER-DECORATOR: CALLED!');
-
// Get the handler instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER-DECORATOR: CALLED!');
$handlerInstance = $this->getListenerInstance()->getHandlerInstance();
/*
throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
} elseif (!$handlerInstance instanceof Networkable) {
// Bad, bad!
- throw new LogicException(sprintf('listenerInstance.handlerInstance[]=%s is not imlementing Networkable.', gettype($handlerInstance)));
+ throw new LogicException(sprintf('listenerInstance->handlerInstance[]=%s is not imlementing Networkable.', gettype($handlerInstance)));
}
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER-DECORATOR: handlerInstance=%s', strtoupper($this->getProtocolName()), $handlerInstance->__toString()));
-
// Does the handler have some decoded data pending?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER-DECORATOR: handlerInstance=%s', strtoupper($this->getProtocolName()), $handlerInstance->__toString()));
if (!$handlerInstance->isRawDataPending()) {
// No data is pending so skip further code silently
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER-DECORATOR: No data pending on handlerInstance=' . $handlerInstance->__toString() . ' - EXIT!');
* know if the decoded data origins from a TCP or UDP connection so it
* can just be passed over to the network package receiver.
*/
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER-DECORATOR: Going to handle over some raw data to receiverInstance=' . $receiverInstance->__toString() . ' ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER-DECORATOR: Going to handle over some raw data to receiverInstance=%s ...', strtoupper($this->getProtocolName()), $receiverInstance->__toString()));
$receiverInstance->addRawDataToIncomingStack($handlerInstance);
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER-DECORATOR: EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER-DECORATOR: EXIT!', strtoupper($this->getProtocolName())));
}
/**
*/
private function validateSocket (StorableSocket $socketInstance) {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: socketInstance.socketResource=%s - CALLED!', $socketInstance->getSocketResource()));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: socketInstance->socketResource=%s - CALLED!', $socketInstance->getSocketResource()));
// Is it a valid resource?
if (!$socketInstance->isValidSocket()) {
// Is it without any errors?
if ($errorCode > 0) {
- // Handle the socket error with a faked recipientData array
+ // Handle the socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
}
if (!$this->getListenerInstance()->getSocketInstance()->equals($socketInstance)) {
// Try to determine the peer's IP number
if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) {
- // Handle the socket error with a faked recipientData array
+ // Handle the socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
} // END - if
} else {
* @throws LogicException If an expected array element is missing
*/
public function getSocketFromPackageInstance (DeliverablePackage $packageInstance, $connectionType = NULL) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL:packageInstance=%s,connectionType[%s]=%s - CALLED!', $packageInstance->__toString(), gettype($connectionType), $connectionType));
-
// Default is no socket
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL:packageInstance=%s,connectionType[%s]=%s - CALLED!', $packageInstance->__toString(), gettype($connectionType), $connectionType));
$socketInstance = NULL;
// Resolve recipient (UNL) into a handler instance
// Get UNL data
$locatorInstance = $handlerInstance->getUniversalNodeLocatorInstance();
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),locatorInstance.unl=' . $locatorInstance->generateUnl() . ' ...');
-
// Default is all sockets
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),locatorInstance->unl=' . $locatorInstance->generateUnl() . ' ...');
$sockets = $this->getAllSockets();
// Get all sockets and check them, skip the server socket
foreach ($sockets as $socketArray) {
- // Debugging:
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketArray(%d)=%s', count($socketArray), implode(',', $socketArray)));
- //* DEBUG-DIE: */ die(__METHOD__.':connectionType='.$connectionType.',socketArray='.print_r($socketArray, TRUE));
-
// Is connection type set?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketArray(%d)=%s', count($socketArray), implode(',', $socketArray)));
+ //* DEBUG-DIE: */ die(__METHOD__.':connectionType='.$connectionType.',socketArray='.print_r($socketArray, TRUE));
if (!isset($socketArray[Poolable::SOCKET_ARRAY_INSTANCE])) {
// Important array element missing
throw new LogicException(sprintf('socketArray[%s] is not set.', Poolable::SOCKET_ARRAY_INSTANCE));
throw new InvalidConnectionTypeException(array($this, $connectionType), self::EXCEPTION_INVALID_CONNECTION_TYPE);
} elseif ((!empty($connectionType)) && ($socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE] !== $connectionType)) {
// Skip unwanted sockets
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: Skipping unwanted socket %s of type %s/%s ...', $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE], $connectionType));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: Skipping unwanted socket %s of type %s/%s ...', $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE], $connectionType));
continue;
}
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketInstance.socketResource=%s,socketInstance.socketProtocol=%s,socketArray[%s]=%s', $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketProtocol(), Poolable::SOCKET_ARRAY_CONN_TYPE, $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE]));
-
// Init peer address/port
$peerAddress = '0.0.0.0';
$peerPort = '0';
// Try to get the "peer"'s name
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketInstance->socketResource=%s,socketInstance->socketProtocol=%s,socketArray[%s]=%s', $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketProtocol(), Poolable::SOCKET_ARRAY_CONN_TYPE, $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE]));
if (!$socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketPeerName($peerAddress, $peerPort)) {
// Handle the socket error with given package data
$socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->handleSocketError(__METHOD__, __LINE__);
} // END - if
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: peerAddress=%s,peerPort=%d,locatorInstance.addressPart=%s', $peerAddress, $peerPort, $locatorInstance->getAddressPart()));
-
// If the "peer" IP and recipient is same, use it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: peerAddress=%s,peerPort=%d,locatorInstance->addressPart=%s', $peerAddress, $peerPort, $locatorInstance->getAddressPart()));
if ($peerAddress == $locatorInstance->getAddressPart()) {
// IPs match, so take the socket and quit this loop
$socketInstance = $socketArray[Poolable::SOCKET_ARRAY_INSTANCE];
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: peerAddress=%s matches with recipient IP address. Taking socketResource=%s,type=%s', $peerAddress, $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE]));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: peerAddress=%s matches with recipient IP address. Taking socketResource=%s,type=%s', $peerAddress, $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE]));
break;
} // END - if
} // END - foreach
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: socketInstance[]=' . gettype($socketInstance) . ' - EXIT!');
-
// Return the determined socket instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketInstance[]=%s - EXIT!', gettype($socketInstance)));
return $socketInstance;
}
// Import SPL stuff
use \InvalidArgumentException;
+use \LogicException;
/**
* A DHT recipient
$recipients = $discoverInstance->resolveRecipientsByPackageInstance($packageInstance);
// Now "walk" through all elements and add them to the list
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-RECIPIENT: recipients()=%d', count($recipients)));
foreach ($recipients as $recipient) {
// These array elements must exist for this loop:
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-RECIPIENT: recipient=' . $recipient);
- // @TODO Unfinished
- die(__METHOD__ . ':recipient=' . print_r($recipient, TRUE));
- assert(!empty($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS]));
-
- // Put ip and port both together
- $unl = $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS];
+ //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-RECIPIENT: recipient=%s', print_r($recipient, TRUE)));
+ //* DEBUG-DIE: */ die(sprintf('[%s:%d]: recipient=%s', __METHOD__, __LINE__, print_r($recipient, TRUE)));
+ if (empty($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS])) {
+ // External UNL needs to be set
+ throw new LogicException(sprintf('recipient[%s] is not set', NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS));
+ }
// Add it to the list
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-RECIPIENT: Calling listInstance->addEntry(' . $unl . ') ...');
- $listInstance->addEntry('unl', $unl);
- } // END - foreach
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-RECIPIENT: Calling listInstance->addEntry(%s) ...', $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS]));
+ $listInstance->addEntry('unl', $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS]);
+ }
- // Debug message
+ // Trace message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-RECIPIENT: EXIT!');
}
* @return void
*/
public function registerSocketInstance (ShareableInfo $infoInstance, StorableSocket $socketInstance) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ' - CALLED!');
-
// Is the socket already registered?
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ' - CALLED!');
if ($this->isSocketRegistered($infoInstance, $socketInstance)) {
// Throw the exception
throw new SocketAlreadyRegisteredException(array($infoInstance, $socketInstance->getSocketResource()), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
/**
* Checks whether a socket of a connection type 'incoming' is registered.
*
+ * @param $listenerInstance An instance of a Listenable class
* @return $isRegistered Whether such a socket is registered
- * @todo 0% done
+ * @todo May extract this loop into private method and invoke it with CONNECTION_TYPE_INCOMING for later expansion also on outgoing/file sockets?
*/
- public function isIncomingSocketRegistered () {
+ public function isIncomingSocketRegistered (Listenable $listenerInstance) {
// Default is not found
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: CALLED!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: listenerInstance=%s (protocol: %s) - CALLED!', $listenerInstance->__toString(), strtoupper($listenerInstance->getProtocolName())));
+ //* DEBUG-DIE: */ die(sprintf('[%s:%d]: this=%s', __METHOD__, __LINE__, print_r($this, TRUE)));
$isRegistered = FALSE;
+ // Search listener's socket sub-registry
+ $registryInstance = $this->getInstance($listenerInstance->getProtocolName());
+ //* DEBUG-DIE: */ die(sprintf('[%s:%d]: listenerInstance->protocolName=%s,registryInstance=%s', __METHOD__, __LINE__, $listenerInstance->getProtocolName(), print_r($registryInstance, TRUE)));
+
+ // Get all sockets from it
+ foreach ($registryInstance->getInstanceRegistry() as $instanceKey => $socketInstance) {
+ // Is this socket as requested?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: instanceKey=%s,socketInstance=%s,socketType=%s', $instanceKey, $socketInstance->__toString(), $socketInstance->getSocketType()));
+ //* DEBUG-DIE: */ die(sprintf('[%s:%d]: socketInstance=%s', __METHOD__, __LINE__, print_r($socketInstance, TRUE)));
+ if ($socketInstance->getSocketType() == StorableSocket::CONNECTION_TYPE_INCOMING) {
+ // Is found!
+ $isRegistered = TRUE;
+ break;
+ }
+ }
+
// Return status
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: isRegistered=%d - EXIT!', intval($isRegistered)));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: isRegistered=%d - EXIT!', intval($isRegistered)));
return $isRegistered;
}
* @return void
*/
public function visitListener (Listenable $listenerInstance) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAWDATA-MONITOR-VISITOR: listenerInstance=' . $listenerInstance->__toString() . ' - CALLED!');
-
/*
* The TCP/UDP listener have a socket registry instance set. These
* registered sockets needs to be checked for pending incoming data.
*/
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAWDATA-MONITOR-VISITOR: listenerInstance=%s - CALLED!', $listenerInstance->__toString()));
$listenerInstance->monitorIncomingRawData();
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAWDATA-MONITOR-VISITOR: EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAWDATA-MONITOR-VISITOR: EXIT!');
}
/**
*/
public function getSocketType ();
- /**
- * Setter for socket type
- *
- * @param $socketType Socket type
- * @return void
- */
- public function setSocketType ($socketType);
-
}
/**
* Checks whether a socket of a connection type 'incoming' is registered.
*
+ * @param $listenerInstance An instance of a Listenable class
* @return $isRegistered Whether such a socket is registered
*/
- function isIncomingSocketRegistered ();
+ function isIncomingSocketRegistered (Listenable $listenerInstance);
}
-Subproject commit c53e9c7252290aa3e4107717419429c0418a6945
+Subproject commit b4a83b1583b9b3947633bbaf62b25883bfc3f9ea