* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class NetworkMessageFactory extends BaseFactory {
+ /**
+ * "Cache" for message factory
+ */
+ private static $messageFactoryInstance = 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__)->traceMessage(sprintf('NETWORK-MESSAGE-FACTORY: self::messageFactoryInstance[]=%s - CALLED!', gettype(self::$messageFactoryInstance)));
+ if (is_null(self::$messageFactoryInstance)) {
+ // No, then initialize it
+ self::$messageFactoryInstance = ObjectRegistry::getRegistry('factory');
+ }
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NETWORK-MESSAGE-FACTORY: self::messageFactoryInstance=%s - EXIT!', self::$messageFactoryInstance));
+ }
+
/**
* Returns a singleton (registry-based) DeliverableMessage instance
*
* @return $messageInstance An instance of a DeliverableMessage class
*/
public static final function createNetworkMessageInstanceFromPackage (DeliverablePackage $packageInstance) {
- // If there is no handler?
+ // Invoke static initializer
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NETWORK-MESSAGE-FACTORY: packageInstance=%s - CALLED!', $packageInstance->__toString()));
- if (ObjectRegistry::getRegistry('factory')->instanceExists('network_message')) {
+ self::staticInitializer();
+
+ // If there is no handler?
+ if (self::$messageFactoryInstance->instanceExists('network_message')) {
// Get handler from registry
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('NETWORK-MESSAGE-FACTORY: Getting messageInstance from registry ...');
- $messageInstance = ObjectRegistry::getRegistry('factory')->getInstance('network_message');
+ $messageInstance = self::$messageFactoryInstance->getInstance('network_message');
} else {
// Get the handler instance
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('NETWORK-MESSAGE-FACTORY: Creating new messageInstance ...');
// Add it to the registry
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('NETWORK-MESSAGE-FACTORY: Adding messageInstance=%s to registy ...', $messageInstance->__toString()));
- ObjectRegistry::getRegistry('factory')->addInstance('network_message', $messageInstance);
+ self::$messageFactoryInstance->addInstance('network_message', $messageInstance);
}
+ // Parse package instance's XML
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NETWORK-MESSAGE-FACTORY: Invoking messageInstance->parsePackageXmlToMessageFields(=%s) ...', $packageInstance->__toString()));
+ $messageInstance->parsePackageXmlToMessageFields($packageInstance);
+
// Return the instance
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NETWORK-MESSAGE-FACTORY: messageInstance=%s - EXIT!', $messageInstance->__toString()));
return $messageInstance;
*/
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)));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(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));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: self::socketFactoryInstance=%s - EXIT!', self::$socketFactoryInstance));
}
/**
*/
public static function createSocketFromPackageInstance (DeliverablePackage $packageInstance, HandleableProtocol $protocolInstance) {
// Invoke static initializer
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s,protocolInstance=%s - CALLED!', $packageInstance->__toString(), $protocolInstance->__toString()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: packageInstance=%s,protocolInstance=%s - CALLED!', $packageInstance->__toString(), $protocolInstance->__toString()));
self::staticInitializer();
// Init instance
);
// Is the key there?
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Trying to find a socket with registryKey=%s', $registryKey));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: Trying to find a socket with registryKey=%s', $registryKey));
if (self::$socketFactoryInstance->instanceExists($registryKey)) {
// Get container instance
$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()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: Using socketResource[%s]=%s from registry.', gettype($socketInstance->getSocketResource()), $socketInstance->getSocketResource()));
} else {
// Invoke inner factory
$socketInstance = ObjectFactory::createObjectByConfiguredName(sprintf('%s_connection_helper_class', $protocolInstance->getProtocolName()), array($packageInstance));
self::$socketFactoryInstance->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__)->debugMessage('SOCKET-FACTORY: Socket is now registered in registry.');
}
// Return the socket (container) instance
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}
*/
public static final function createListenFileSocket (Listenable $listenerInstance) {
// Create SplFileInfo
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString()));
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString()));
$fileInfo = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('ipc_socket_file_name'));
// Create file name
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: socketFile=' . $socketFile . ' ...');
if (empty($socketFile)) {
// Something unexpected
- throw new UnexpectedValueException('socketFile is empty');
+ throw new UnexpectedValueException('socketFile is empty', FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE);
}
// Init package instance
$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__)->debugMessage(sprintf('SOCKET-FACTORY: socketInstance[]=%s', gettype($socketInstance)));
if (!$socketInstance->isValidSocket()) {
// Something bad happened
throw new InvalidSocketException(array($listenerInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
// Is the file there?
if ((FrameworkBootstrap::isReachableFilePath($socketInstance->getSocketFile())) && (file_exists($socketInstance->getSocketFile()))) {
// Old socket found
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: WARNING: Old socket at ' . $socketInstance->getSocketFile() . ' found. Will not start.');
+ self::createDebugInstance(__CLASS__, __LINE__)->warningMessage('SOCKET-FACTORY: WARNING: Old socket at ' . $socketInstance->getSocketFile() . ' found. Will not start.');
// Shutdown this socket
$socketInstance->shutdownSocket();
}
// Try to bind to it
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $socketInstance->getSocketFile() . ' ...');
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SOCKET-FACTORY: Binding to ' . $socketInstance->getSocketFile() . ' ...');
if (!$socketInstance->bindSocketToFile()) {
// Handle error here
$socketInstance->handleSocketError(__METHOD__, __LINE__);
}
// Start listen for connections
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.');
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SOCKET-FACTORY: Listening for connections.');
if (!$socketInstance->listenToSocket()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
}
// Allow non-blocking I/O
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SOCKET-FACTORY: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
}
// Return socket instance
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}
*/
public static final function createTcpOutgoingSocketFromPackageInstance (DeliverablePackage $packageInstance) {
// Create a socket instance
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s - CALLED!', $packageInstance->__toString()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: packageInstance=%s - CALLED!', $packageInstance->__toString()));
$socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Construct container class, this won't be reached if an exception is thrown
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Created socketResource[%s]=%s ...', gettype($socketResource), $socketResource));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: Created socketResource[%s]=%s ...', gettype($socketResource), $socketResource));
$socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', [
$socketResource,
StorableSocket::SOCKET_PROTOCOL_TCP,
}
// 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__)->debugMessage('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__)->debugMessage('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__)->traceMessage(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__)->traceMessage(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
}
// Set the option to reuse the port
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SOCKET-FACTORY: Enabling re-use address ...');
if (!$socketInstance->enableSocketReuseAddress()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
* that all connections on this port are now our resposibility to
* send/recv data, disconnect, etc..
*/
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
+ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
if (!$socketInstance->bindSocketToListener()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
}
// Start listen for connections
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SOCKET-FACTORY: Listening for connections.');
if (!$socketInstance->listenToSocket()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
}
// Now, we want non-blocking mode
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SOCKET-FACTORY: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
}
// Return prepepared socket
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(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__)->traceMessage(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString()));
$socketResource = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
// Init fake package instance
}
// Set the option to reuse the port
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SOCKET-FACTORY: Enabling re-use address ...');
if (!$socketInstance->enableSocketReuseAddress()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
* that all connections on this port are now our resposibility to
* send/recv data, disconnect, etc..
*/
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
+ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
if (!$socketInstance->bindSocketToListener()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
}
// Now, we want non-blocking mode
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SOCKET-FACTORY: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
}
// Return prepepared socket
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}