* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class SocketFactory extends BaseFactory {
+ /**
+ * "Cache" for socket factory
+ */
+ private static $socketFactoryInstance = NULL;
+
/**
* Protected constructor
*
parent::__construct(__CLASS__);
}
+ /**
+ * Some "static initializer
+ *
+ * @return void
+ */
+ public final static function staticInitializer () {
+ // Is it initialized?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: self::socketFactoryInstance[]=%s - CALLED!', gettype(self::$socketFactoryInstance)));
+ if (is_null(self::$socketFactoryInstance)) {
+ // No, then initialize it
+ self::$socketFactoryInstance = ObjectRegistry::getRegistry('factory');
+ }
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: self::socketFactoryInstance=%s - EXIT!', self::$socketFactoryInstance));
+ }
+
/**
* Creates a valid socket resource from given packae data and protocol
*
* @return $socketInstance An instance of a StorableSocket class
*/
public static function createSocketFromPackageInstance (DeliverablePackage $packageInstance, HandleableProtocol $protocolInstance) {
- // Init instance
+ // Invoke static initializer
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s,protocolInstance=%s - CALLED!', $packageInstance->__toString(), $protocolInstance->__toString()));
+ self::staticInitializer();
+
+ // Init instance
//* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE)));
$socketInstance = NULL;
// Is the key there?
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Trying to find a socket with registryKey=%s', $registryKey));
- if (ObjectRegistry::getRegistry('factory')->instanceExists($registryKey)) {
+ if (self::$socketFactoryInstance->instanceExists($registryKey)) {
// Get container instance
- $socketInstance = ObjectRegistry::getRegistry('factory')->getInstance($registryKey);
+ $socketInstance = self::$socketFactoryInstance->getInstance($registryKey);
// Debug message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Using socketResource[%s]=%s from registry.', gettype($socketInstance->getSocketResource()), $socketInstance->getSocketResource()));
$socketInstance = ObjectFactory::createObjectByConfiguredName(sprintf('%s_connection_helper_class', $protocolInstance->getProtocolName()), array($packageInstance));
// Register it with the registry
- ObjectRegistry::getRegistry('factory')->addInstance($registryKey, $socketInstance);
+ self::$socketFactoryInstance->addInstance($registryKey, $socketInstance);
// Debug message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Socket is now registered in registry.');
$socketInstance->setSocketFile($socketFile);
// Is the socket resource valid?
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance[]=%s', gettype($socketInstance)));
+ /* 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);
}
// Set the option to reuse the port
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...');
if (FrameworkBootstrap::getConfigurationInstance()->isEnabled('tcp_socket_enable_reuse_address') && !$socketInstance->enableSocketReuseAddress()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
* it. This is now the default behaviour for all connection helpers who
* call initConnection(); .
*/
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
}
// Return it
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}
*/
public static function createListenTcpSocket (Listenable $listenerInstance) {
// Create a streaming socket, of type TCP/IP
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString()));
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString()));
$socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Init fake "package" instance, the SocketContainer class requires this
}
// Return prepepared socket
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}
*/
public static function createListenUdpSocket (Listenable $listenerInstance) {
// Create a streaming socket, of type UDP
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString()));
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString()));
$socketResource = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
// Init fake package instance
}
// Return prepepared socket
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}
* @throws LogicException If the current instance is not valid
*/
public static final function createNextAcceptedSocketFromPool (Poolable $poolInstance, StorableSocket $socketInstance) {
+ // Invoke static initializer
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: poolInstance=%s,socketInstance->socketResource=%s - CALLED!', $poolInstance->__toString(), $socketInstance->getSocketResource()));
+ self::staticInitializer();
+
// Get socket protocol
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: poolInstance=%s,socketInstance->socketResource=%s - CALLED!', $poolInstance->__toString(), $socketInstance->getSocketResource()));
$socketProtocol = $socketInstance->getSocketProtocol();
// Is the an iterator instance?
- if (!ObjectRegistry::getRegistry('factory')->instanceExists('pool_iterator_' . $socketProtocol)) {
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: socketProtocol=%s', $socketProtocol));
+ if (!self::$socketFactoryInstance->instanceExists('pool_iterator_' . $socketProtocol)) {
// Create iterator instance
$iteratorInstance = $poolInstance->createListIteratorInstance($socketProtocol . '_pool');
// Now store it in registry
- ObjectRegistry::getRegistry('factory')->addInstance('pool_iterator', $iteratorInstance);
+ self::$socketFactoryInstance->addInstance('pool_iterator', $iteratorInstance);
} else {
// Get iterator from registry
- $iteratorInstance = ObjectRegistry::getRegistry('factory')->getInstance('pool_iterator_' . $socketProtocol);
+ $iteratorInstance = self::$socketFactoryInstance->getInstance('pool_iterator_' . $socketProtocol);
}
// Is it valid?
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: iteratorInstance=%s', $iteratorInstance->__toString()));
if (!$iteratorInstance->valid()) {
// Try to rewind it
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('SOCKET-FACTORY: Invoking iteratorInstance->rewind() ...');
$iteratorInstance->rewind();
}
$current = $iteratorInstance->current();
// Make sure it is valid
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: current[]=%s', gettype($current)));
if (!is_array($current)) {
// Opps, should not happen
throw new LogicException(sprintf('current[]=%s is not an array.', gettype($current)));
}
// Try to accept a new (incoming) socket from current listener instance
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: Invoking current[%s]->acceptNewIncomingSocket() ...', Poolable::SOCKET_ARRAY_INSTANCE));
$acceptedSocketInstance = $current[Poolable::SOCKET_ARRAY_INSTANCE]->acceptNewIncomingSocket();
// Return found socket instance
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: acceptedSocketInstance[]=%s - EXIT!', gettype($acceptedSocketInstance)));
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: acceptedSocketInstance[]=%s - EXIT!', gettype($acceptedSocketInstance)));
return $acceptedSocketInstance;
}