*/
public function getSocketRecipientUnl () {
// Should be valid socket
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
+ //* 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);
$packageInstance = $this->getPackageDataInstance();
// Return it
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: packageInstance.recipient=%s - EXIT!', strtoupper($this->getSocketProtocol()), $packageInstance->getRecipientUnl()));
+ //* 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 () {
// Should be valid socket
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
+ //* 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
- $recipient = $this->getSocketRecipientUnl();
+ $recipientUnl = $this->getSocketRecipientUnl();
// Generate UNL instance for it
- $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient);
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientUnl=%s', strtoupper($this->getSocketProtocol()), $recipientUnl));
+ $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipientUnl);
// Get address part
$recipientAddress = $locatorInstance->getUnlAddress();
// Return it
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientAddress=%s - EXIT!', strtoupper($this->getSocketProtocol()), $recipientAddress));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientAddress=%s - EXIT!', strtoupper($this->getSocketProtocol()), $recipientAddress));
return $recipientAddress;
}
*/
public function getSocketRecipientPort () {
// Should be valid socket
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
+ //* 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
- $recipient = $this->getSocketRecipientUnl();
+ // Get recipient UNL
+ $recipientUnl = $this->getSocketRecipientUnl();
// Generate UNL instance for it
- $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient);
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientUnl=%s', strtoupper($this->getSocketProtocol()), $recipientUnl));
+ $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipientUnl);
// Get port part
$recipientPort = $locatorInstance->getUnlPort();
// Return it
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientPort=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($recipientPort)));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientPort=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($recipientPort)));
return $recipientPort;
}
*/
public function isValidSocket () {
// Is it valid?
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
// @TODO maybe add more checks? is_resource() is still to less
$isValidSocket = (is_resource($this->getSocketResource()));
// Return status
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: isValidSocket=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($isValidSocket)));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: isValidSocket=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($isValidSocket)));
return $isValidSocket;
}
*/
public function getLastSocketErrorCode () {
// Should be valid socket
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
+ //* 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);
$errorCode = socket_last_error($this->getSocketResource());
// Return it
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($errorCode)));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($errorCode)));
return $errorCode;
}
}
// Try to listen on socket
- $result = socket_listen($this->getSocketResource());
+ $result = socket_listen($this->getSocketResource(), FrameworkBootstrap::getConfigurationInstance()->getConfigEntry(sprintf('%s_socket_listen_backlog', $this->getSocketProtocol())));
// Return result
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d,errorCode=%s - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result), $this->getLastSocketErrorCode()));
$this->clearRecipients();
// Is the UNL empty but id given?
- if (empty($packageInstance->getRecipientUnl()) && empty($packageInstance->getRecipientId())) {
- // Uh, both is empty!
- throw new InvalidArgumentException('packageInstance has both recipientUnl and recipientId empty.');
- } elseif (empty($packageInstance->getRecipientUnl()) && !empty($packageInstance->getRecipientId())) {
+ //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE)));
+ if (empty($packageInstance->getRecipientUnl()) && (empty($packageInstance->getRecipientId()) || $packageInstance->getRecipientId() == 'invalid')) {
+ // Uh, both is empty or invalid!
+ throw new InvalidArgumentException('packageInstance has both recipientUnl and recipientId empty or invalid.');
+ } elseif (empty($packageInstance->getRecipientUnl()) && $packageInstance->getRecipientId() != 'invalid') {
// Id is set, this might be resolved to an UNL
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RECIPIENT-DISCOVERY: Resolving recipientId=%s to UNL ...', $packageInstance->getRecipientId()));
$recipientUnl = HubTools::resolveSessionIdToUnl($packageInstance->getRecipientId());
* @return $handlerInstance A protocol handler instance
* @throws InvalidArgumentException If protocolType is not valid
*/
- private static function createProtocolHandlerByType ($protocolType) {
- // Is it valid?
- if (empty($protocolType)) {
- // Throw excption
- throw new InvalidArgumentException('protocolType is empty');
- }
-
+ private static function createProtocolHandlerByType (string $protocolType) {
// Do we have an instance in the registry?
if (GenericRegistry::getRegistry()->instanceExists($protocolType . '_protocol_handler')) {
// Then use this instance
// The UNL should be set but not recipientId
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: recipientUnl(%d)=%s,recipientId(%d)=%s', strlen($packageInstance->getRecipientUnl()), $packageInstance->getRecipientUnl(), strlen($packageInstance->getRecipientId()), $packageInstance->getRecipientId()));
- if (!empty($packageInstance->getRecipientUnl()) && empty($packageInstance->getRecipientId())) {
+ if (!empty($packageInstance->getRecipientUnl()) && (empty($packageInstance->getRecipientId()) || $packageInstance->getRecipientId() == 'invalid')) {
// Need to resolve UNL -> session id ("recipient id"), so prepare UNL instance
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: recipientUnl=%s is being solved into a session id ...', $packageInstance->getRecipientUnl()));
$locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipientUnl());
namespace Org\Shipsimu\Hub\Handler\Protocol\Network\IpV4\Tcp;
// Import application-specific stuff
+use Org\Shipsimu\Hub\Container\Socket\StorableSocket;
use Org\Shipsimu\Hub\Handler\Protocol\Network\IpV4\BaseIpV4ProtocolHandler;
use Org\Shipsimu\Hub\Handler\Protocol\HandleableProtocol;
// Set handler and protocol type
$this->setHandlerName('tcp_protocol');
- $this->setProtocolName('tcp');
+ $this->setProtocolName(StorableSocket::SOCKET_PROTOCOL_TCP);
}
/**
$this->setErrorCode(StorableSocket::SOCKET_ERROR_UNHANDLED);
// 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()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-RAW-DATA-HANDLER: Handling TCP package from socketResource=%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)));
parent::__construct(__CLASS__);
// Set handler name
- $this->setHandlerName('udp');
+ $this->setHandlerName(StorableSocket::SOCKET_PROTOCOL_UDP);
}
/**
$this->setErrorCode(StorableSocket::SOCKET_ERROR_UNHANDLED);
// Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UDP-HANDLER: Handling UDP package from resource=' . $socketInstance->getSocketResource() . ',last error=' . $socketInstance->getLastSocketErrorMessage());
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UDP-HANDLER: Handling UDP package from socketResource=%s,last error=%s', $socketInstance->getSocketResource(), $socketInstance->getLastSocketErrorMessage()));
$this->partialStub('Please rewrite this part.');
return;
}
*
* @return $recipientId Receipient id (mostly session id)
*/
- protected final function setRecipientId ($recipientId) {
+ protected final function setRecipientId (string $recipientId) {
$this->recipientId = $recipientId;
}
*
* @return $recipientType Type of a recipient (the helper is made for)
*/
- protected final function setRecipientType ($recipientType) {
+ protected final function setRecipientType (string $recipientType) {
$this->recipientType = $recipientType;
}
namespace Org\Shipsimu\Hub\Helper\Connection;
// Import application-specific stuff
-use Org\Shipsimu\Hub\Container\Socket\StorableSocket;
use Org\Shipsimu\Hub\Factory\Fragmenter\FragmenterFactory;
use Org\Shipsimu\Hub\Factory\Handler\Network\NetworkPackageHandlerFactory;
use Org\Shipsimu\Hub\Factory\Network\NetworkPackageFactory;
parent::__construct(__CLASS__);
// Set the protocol to TCP
- $this->setProtocolName('tcp');
+ $this->setProtocolName(StorableSocket::SOCKET_PROTOCOL_TCP);
}
/**
namespace Org\Shipsimu\Hub\Resolver\Protocol\Tcp;
// Import application-specific stuff
+use Org\Shipsimu\Hub\Container\Socket\StorableSocket;
use Org\Shipsimu\Hub\Database\Frontend\Node\Information\NodeInformationDatabaseFrontend;
use Org\Shipsimu\Hub\Factory\Network\Locator\UniversalNodeLocatorFactory;
use Org\Shipsimu\Hub\Node\Node;
parent::__construct(__CLASS__);
// Set protocol type
- $this->setProtocolName('tcp');
+ $this->setProtocolName(StorableSocket::SOCKET_PROTOCOL_TCP);
}
/**
namespace Org\Shipsimu\Hub\Helper\Connection;
// Import application-specific stuff
+use Org\Shipsimu\Hub\Container\Socket\StorableSocket;
use Org\Shipsimu\Hub\Generic\BaseHubSystem;
use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper;
* Statistics array
* @TODO Add more protocols to use
*/
- private static $connectionStatistics = array(
+ private static $connectionStatistics = [
// Statistics for TCP connections
- 'tcp' => array(
+ StorableSocket::SOCKET_PROTOCOL_TCP => [
// Tried TCP connection attempts
- 'retry_count' => array(),
- ),
+ 'retry_count' => [],
+ ],
// Statistics for UDP connections
- 'udp' => array(
+ StorableSocket::SOCKET_PROTOCOL_UDP => [
// Tried UDP connection attempts
- 'retry_count' => array(),
- )
- );
+ 'retry_count' => [],
+ ]
+ ];
/**
* Protected constructor
// CFG: TCP-SOCKET-ENABLE-REUSE-ADDRESS
$cfg->setConfigEntry('tcp_socket_enable_reuse_address', true);
+// CFG: FILE-SOCKET-LISTEN-BACKLOG
+$cfg->setConfigEntry('file_socket_listen_backlog', 0);
+
+// CFG: TCP-SOCKET-LISTEN-BACKLOG
+$cfg->setConfigEntry('tcp_socket_listen_backlog', 1000);
+
// CFG: NODE-STATE-CHECKED-PACKAGE-CLASS
$cfg->setConfigEntry('node_state_checked_package_class', 'NewConnectionNodeState');
// Own namespace
namespace Org\Shipsimu\Hub\Generic;
-// Import application-specific stuff
-use Org\Shipsimu\Hub\Container\Socket\StorableSocket;
-
// Inport frameworks stuff
use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;