From: Roland Häder Date: Sun, 21 May 2017 21:50:38 +0000 (+0200) Subject: Continued a bit: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d7b4394702add19286c2b09e3c073ed8610540de;p=hub.git Continued a bit: - some more logging - more constants for socket array indexes Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 5f95620d8..ff3bcb065 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -73,6 +73,10 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * @return $socketInstance An instance of this Container class */ public static final function createSocketContainer ($socketResource, $socketProtocol, array $packageData, ShareableInfo $infoInstance = NULL) { + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET: socketResource=%s,socketProtocol=%s,packageData()=%d,infoInstance[]=%s - CALLED!', $socketResource, $socketProtocol, count($packageData), gettype($infoInstance))); + /* DEBUG-PRINT: */ printf('[%s:%d]: packageData=%s', __METHOD__, __LINE__, print_r($packageData, TRUE)); + // Get a new instance $socketInstance = new SocketContainer(); @@ -83,9 +87,6 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable unset($packageData[NetworkPackage::PACKAGE_DATA_CONTENT]); unset($packageData[NetworkPackage::PACKAGE_DATA_HASH]); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-CONTAINER:socketResource=' . $socketResource . ',packageData='.print_r($packageData, true)); - // Is the info instance set? if ($infoInstance instanceof ShareableInfo) { // Get listener/helper from info class diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index b057981e1..b553e6905 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -107,7 +107,7 @@ class SocketFactory extends ObjectFactory { $socketFile = self::createTempPathForFile($listenerInstance->getConfigInstance()->getConfigEntry('ipc_socket_file_name')); // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: socketFile=' . $socketFile . ' ...'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: socketFile=' . $socketFile . ' ...'); // File name must not be empty assert(!empty($socketFile)); @@ -122,8 +122,7 @@ class SocketFactory extends ObjectFactory { $socketResource = socket_create(AF_UNIX, SOCK_STREAM, 0); // Get container from it - // @TODO Somehow handle $infoInstance to this factory - $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_FILE, $packageData, NULL)); + $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_FILE, $packageData)); // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FILE_LISTENER: socketInstance[]=%s', gettype($socketInstance))); @@ -143,7 +142,7 @@ class SocketFactory extends ObjectFactory { // Is the file there? if ((FrameworkBootstrap::isReachableFilePath($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE])) && (file_exists($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE]))) { // Old socket found - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: WARNING: Old socket at ' . $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE] . ' found. Will not start.'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: WARNING: Old socket at ' . $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE] . ' found. Will not start.'); // Shutdown this socket $socketInstance->shutdownSocket(); @@ -153,7 +152,7 @@ class SocketFactory extends ObjectFactory { } // END - if // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Binding to ' . $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE] . ' ...'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE] . ' ...'); // Try to bind to it if (!$socketInstance->bindSocketTo($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE])) { @@ -162,14 +161,14 @@ class SocketFactory extends ObjectFactory { } // END - if // Start listen for connections - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Listening for connections.'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.'); if (!$socketInstance->listenOnSocket()) { // Handle this socket error with a faked recipientData array $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData)); } // END - if // Allow non-blocking I/O - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Setting 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 $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData)); @@ -196,8 +195,7 @@ class SocketFactory extends ObjectFactory { //*NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Created socket ' . $socketResource . '(' . gettype($socketResource) . ') from class ' . $className . '.'); // Construct container class, this won't be reached if an exception is thrown - // @TODO Somehow handle $infoInstance to this factory - $socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_TCP, $packageData, NULL)); + $socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_TCP, $packageData)); // Is the socket resource valid? if (!$socketInstance->isValidSocket()) { @@ -216,6 +214,7 @@ class SocketFactory extends ObjectFactory { } // 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 $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0')); @@ -226,6 +225,7 @@ class SocketFactory extends ObjectFactory { * it. This is now the default behaviour for all connection helpers who * call initConnection(); . */ + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); if (!$socketInstance->enableSocketNonBlocking()) { // Handle this socket error with a faked recipientData array $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0')); @@ -246,8 +246,10 @@ class SocketFactory extends ObjectFactory { // Create a streaming socket, of type TCP/IP $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); - // @TODO Init fake package data with address/port from listener + // Init fake package data with address/port from listener $packageData = array( + StorableSocket::SOCKET_ARRAY_INDEX_ADDRESS => $listenerInstance->getListenAddress(), + StorableSocket::SOCKET_ARRAY_INDEX_PORT => $listenerInstance->getListenPort(), ); // Create socket instance @@ -267,6 +269,7 @@ class SocketFactory extends ObjectFactory { // Set the option to reuse the port // @TODO: , SOL_SOCKET, SO_REUSEADDR, 1 + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...'); if (!$socketInstance->enableSocketReuseAddress()) { // Handle this socket error with a faked recipientData array $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0')); @@ -277,21 +280,21 @@ class SocketFactory extends ObjectFactory { * that all connections on this port are now our resposibility to * send/recv data, disconnect, etc.. */ - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort()); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort()); if (!$socketInstance->bindSocketTo($listenerInstance->getListenAddress(), $listenerInstance->getListenPort())) { // Handle this socket error with a faked recipientData array $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0')); } // END - if // Start listen for connections - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Listening for connections.'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.'); if (!$socketInstance->listenToSocket()) { // Handle this socket error with a faked recipientData array $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0')); } // END - if // Now, we want non-blocking mode - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Setting 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 $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0')); diff --git a/application/hub/classes/statistics/connection/class_ConnectionStatisticsHelper.php b/application/hub/classes/statistics/connection/class_ConnectionStatisticsHelper.php index 7f839a6a7..285458ea1 100644 --- a/application/hub/classes/statistics/connection/class_ConnectionStatisticsHelper.php +++ b/application/hub/classes/statistics/connection/class_ConnectionStatisticsHelper.php @@ -106,7 +106,5 @@ class ConnectionStatisticsHelper extends BaseHubSystem { // @TODO last_update is not being used at the moment self::$connectionStatistics[$helperInstance->getProtocolName()][$helperInstance->__toString()]['last_update'] = time(); } -} -// [EOF] -?> +} diff --git a/application/hub/interfaces/container/socket/class_StorableSocket.php b/application/hub/interfaces/container/socket/class_StorableSocket.php index 13c8997cc..07de27e65 100644 --- a/application/hub/interfaces/container/socket/class_StorableSocket.php +++ b/application/hub/interfaces/container/socket/class_StorableSocket.php @@ -47,10 +47,10 @@ interface StorableSocket extends FrameworkInterface { const SOCKET_ERROR_OPERATION_NOT_SUPPORTED = 'operation_not_supported'; // 'Operation not supported' const SOCKET_CONNECTED = 'connected'; // Nothing errorous happens, socket is connected - /** - * Socket file array index - */ - const SOCKET_ARRAY_INDEX_FILE = 'socket_file'; + // Socket data array indexes + const SOCKET_ARRAY_INDEX_FILE = 'socket_file'; + const SOCKET_ARRAY_INDEX_ADDRESS = 'socket_address'; + const SOCKET_ARRAY_INDEX_PORT = 'socket_port'; // Socket protocols const SOCKET_PROTOCOL_INVALID = 'invalid';