/** * Class constructor. * * @since 5.1 */ public function __construct() { $this->host = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext')->getConfig()->getKey('server_address'); if (!$this->host) { $this->host = 'localhost'; } $this->port = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext')->getConfig()->getKey('server_port'); if (!$this->port) { $this->port = 9000; } $this->socket = new \Innomatic\Net\Socket\Socket(); }
/** * Starts the server socket. * * @since 5.1 * @return void */ public function start() { $context = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext'); $port = $context->getConfig()->getKey('server_port'); if (!strlen($port)) { $port = '9000'; } $bindAddress = $context->getConfig()->getKey('server_address'); if (!strlen($bindAddress)) { $bindAddress = '127.0.0.1'; } $server = new \Innomatic\Net\Socket\SequentialServerSocket($bindAddress, $port); $server->setHandler(new ModuleServerSocketHandler()); $server->start(); }
/** * Launches a server process and watches it. * * @since 5.1 * @param string $command Command for launching server to be watched. * @return void */ public function watch($command) { print 'Module server started and monitored by watch dog.' . "\n"; while (true) { $result = $this->run($command); if (!strpos($result, 'Module server failed.') and strpos($result, 'Module server stopped.')) { break; } $context = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext'); $logger = new ModuleServerLogger($context->getHome() . 'core/log/module-watchdog.log'); $logger->logEvent('------------------------------------------------------'); $logger->logEvent($result); print 'Module server restarted by watch dog.' . "\n"; } print 'Module server and watch dog stopped.' . "\n"; }
/** * Actions executed at socket server startup. * * During this method execution, loggers are started and garbage collecting * process is launched. * * @since 5.1 */ public function onStart() { $context = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext'); if ($context->getConfig()->getKey('log_server_events') == 1 or $context->getConfig()->useDefaults()) { $this->serverLogEnabled = true; $this->serverLogger = new ModuleServerLogger($context->getHome() . 'core/log' . DIRECTORY_SEPARATOR . 'module-server.log'); } if ($context->getConfig()->getKey('log_access_events') == 1) { $this->accessLogEnabled = true; $this->accessLogger = new ModuleServerLogger($context->getHome() . 'core/log' . DIRECTORY_SEPARATOR . 'module-access.log'); } \Innomatic\Module\Session\ModuleSessionGarbageCollector::clean(); $this->authenticator = ModuleServerAuthenticator::instance('ModuleServerAuthenticator'); if ($this->serverLogEnabled) { $this->serverLogger->logEvent('Start'); } register_shutdown_function(array($this, 'onHalt')); print 'Module server started and listening at port ' . $this->serversocket->getPort() . "\n"; }
/** * Executes garbage collection. * * @since 5.1 * @return void */ public static function clean() { // Obtains Modules list. $context = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext'); $module_list = $context->getModuleList(); // Cleans session files for each Module context. foreach ($module_list as $module) { if (!file_exists($context->getHome() . 'modules/' . $module . '/sessions')) { continue; } if (!($dh = opendir($context->getHome() . 'modules/' . $module . '/sessions/'))) { continue; } while (($file = readdir($dh)) !== false) { if ($file != '.' and $file != '..' and is_file($context->getHome() . 'modules/' . $module . '/sessions/' . $file)) { unlink($context->getHome() . 'modules/' . $module . '/sessions/' . $file); } } closedir($dh); } }
/** * Gets an instance of a local Module object. * * This static method retrieves a Module located in local context, without * using the Module server. Module authentication is needed anyway. * * The method also builds the configuration for the Module. * * @since 5.1 * @param ModuleLocator $locator Module locator object. * @return ModuleObject The Module object. */ public static function getLocalModule(ModuleLocator $locator) { $location = $locator->getLocation(); // Authenticates $authenticator = ModuleServerAuthenticator::instance('ModuleServerAuthenticator'); if (!$authenticator->authenticate($locator->getUsername(), $locator->getPassword())) { throw new ModuleException('Not authorized'); } $context = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext'); // Checks if Module exists $classes_dir = $context->getHome() . 'core/modules/' . $location . '/classes/'; if (!is_dir($classes_dir)) { throw new ModuleException('Module does not exists'); } // Checks if configuration file exists $moduleXml = $context->getHome() . 'core/modules/' . $location . '/setup/module.xml'; if (!file_exists($moduleXml)) { throw new ModuleException('Missing module.xml configuration file'); } $cfg = \Innomatic\Module\Util\ModuleXmlConfig::getInstance($moduleXml); $fqcn = $cfg->getFQCN(); // Builds Module Data Access Source Name $dasn_string = $authenticator->getDASN($locator->getUsername(), $locator->getLocation()); if (strpos($dasn_string, 'context:')) { $module_context = new ModuleContext($location); $dasn_string = str_replace('context:', $module_context->getHome(), $dasn_string); } $cfg->setDASN(new DataAccessSourceName($dasn_string)); // Adds Module classes directory to classpath // TODO: Should add include path only if not already available set_include_path($classes_dir . PATH_SEPARATOR . get_include_path()); require_once $fqcn . '.php'; // Retrieves class name from Fully Qualified Class Name $class = strpos($fqcn, '/') ? substr($fqcn, strrpos($fqcn, '/') + 1) : $fqcn; // Instantiates the new class and returns it return new $class($cfg); }
/** * stops the pinger * (starts the pinger shuting down sequence) * * @see ModulePinger.php * @since 5.1 * @return string Server result string. */ private function stopPinger() { $result = ''; $auth = ModuleServerAuthenticator::instance('ModuleServerAuthenticator'); $context = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext'); $this->socket->connect('127.0.0.1', $context->getConfig()->getKey('pinger_port')); $request = 'SHUTDOWN Module/1.0' . "\r\n"; $request .= 'User: admin' . "\r\n"; $request .= 'Password: '******'admin') . "\r\n"; $this->socket->write($request); $result = $this->socket->readAll(); $this->socket->disconnect(); }
/** * Actions executed when receiving data. * * The current implementation of the server recognizes the following * commands: * * - SHUTDOWN: the shutdown procedure is called; * - STATUS: a message about the status of the server is sent; * - REFRESH: refreshes the loaded Module configuration; * - INVOKE: a command is requested to be executed. * * @since 5.1 */ public function onReceiveData($clientId = null, $data = null) { $response = new ModuleServerResponse(); $raw_request = explode("\n", $data); $headers = array(); $body = ''; $body_start = false; $command_line = ''; foreach ($raw_request as $line) { $line = trim($line); if (!$body_start and $line == '') { $body_start = true; continue; } if ($body_start) { $body .= $line . "\n"; } else { if (strlen($command_line)) { $headers[substr($line, 0, strpos($line, ':'))] = trim(substr($line, strpos($line, ':') + 1)); } else { $command_line = $line; } } } if (!isset($headers['User'])) { $headers['User'] = ''; } if (!isset($headers['Password'])) { $headers['Password'] = ''; } if ($this->authenticator->authenticate($headers['User'], $headers['Password'])) { $command = explode(' ', $command_line); switch ($command[0]) { case 'GET_REGISTRY': //OK if ($this->authenticator->authorizeAction($headers['User'], 'get_registry')) { $this->logAccess($clientId, $headers['User'], $command_line); $context = ModuleServerContext::instance('\\Innomatic\\Module\\Server\\ModuleServerContext'); if ($file_content = file_get_contents($context->getHome() . 'core/conf/modules-netregistry.xml')) { $response->setBuffer($file_content); } else { $response->setBuffer("Net Registry not found"); } } else { $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized'); } break; case 'SHUTDOWN': if ($this->authenticator->authorizeAction($headers['User'], 'shutdown')) { $this->serversocket->sendData($clientId, "Shutdown requested.\n", true); $this->logAccess($clientId, $headers['User'], $command_line); $this->serversocket->shutDown(); return; } else { $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized'); } break; case 'STATUS': if ($this->authenticator->authorizeAction($headers['User'], 'status')) { $response->setBuffer("Module: services-extension up and ready.\n"); } else { $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized'); } break; case 'REFRESH': //refresh configuration and net-registry (following ini-file parameters) if ($this->authenticator->authorizeAction($headers['User'], 'status')) { $this->authenticator->parseConfig(); $registryHandler = new ModuleRegistryHandler(); $registryHandler->parseRegistry(); $response->setBuffer("Module: services-extension registry reloaded successfully.\n"); print "Module: services-extension registry reloaded successfully.\n"; } else { $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Action not authorized'); } break; case 'INVOKE': if ($this->authenticator->authorizeModule($headers['User'], $command[1])) { $request = new ModuleServerRequest(); $request->setCommand($command_line); $request->setHeaders($headers); $request->setPayload($body); $server = new ModuleServerXmlRpcProcessor(); $server->process($request, $response); } else { $response->sendWarning(ModuleServerResponse::SC_FORBIDDEN, 'Module not authorized'); } break; } } else { $response->sendWarning(ModuleServerResponse::SC_UNAUTHORIZED, 'Authentication needed'); } $this->serversocket->sendData($clientId, $response->getResponse(), true); $this->logAccess($clientId, $headers['User'], $command_line); $this->serversocket->closeConnection(); }