public function mysql_save() { $new = false; if ($this->id == 0) { $new = true; } $lid = parent::mysql_save(); if ($new) { $ts = TestSession::from_mysql_id($lid); $ts->hash = TestSession::generate_hash($lid); $ts->mysql_save(); } return $lid; }
public function get_TestSession() { return TestSession::from_mysql_id($this->TestSession_id); }
private function get_client($client_sock, &$input) { $data = json_decode($input); $key = "sid" . $data->session_id; if (!array_key_exists($key, $this->clients)) { $session = TestSession::from_mysql_id($data->session_id); if ($session->serialized == 1) { include Ini::$path_internal . 'SETTINGS.php'; $code = "\r\n options(encoding='UTF-8')\r\n options(error=quote(stop(geterrmessage())))\r\n library(session)\r\n restore.session('" . $session->get_RSession_file_path() . "')\r\n\r\n CONCERTO_DB_HOST <- '" . $db_host . "'\r\n CONCERTO_DB_PORT <- as.numeric(" . ($db_port != "" ? $db_port : "3306") . ")\r\n CONCERTO_DB_LOGIN <- '" . $db_user . "'\r\n CONCERTO_DB_PASSWORD <- '" . $db_password . "'\r\n\r\n CONCERTO_DRIVER <- dbDriver('MySQL')\r\n for(CONCERTO_DB_CONNECTION in dbListConnections(CONCERTO_DRIVER)) { dbDisconnect(CONCERTO_DB_CONNECTION) }\r\n CONCERTO_DB_CONNECTION <- dbConnect(CONCERTO_DRIVER, user = CONCERTO_DB_LOGIN, password = CONCERTO_DB_PASSWORD, dbname = CONCERTO_DB_NAME, host = CONCERTO_DB_HOST, port = CONCERTO_DB_PORT, client.flag=CLIENT_MULTI_STATEMENTS)\r\n dbSendQuery(CONCERTO_DB_CONNECTION,statement = \"SET NAMES 'utf8';\")\r\n dbSendQuery(CONCERTO_DB_CONNECTION,statement = \"SET time_zone='" . Ini::$mysql_timezone . "';\")\r\n\r\n rm(CONCERTO_DB_HOST)\r\n rm(CONCERTO_DB_PORT)\r\n rm(CONCERTO_DB_LOGIN)\r\n rm(CONCERTO_DB_PASSWORD)\r\n "; $session->serialized = 0; $session->mysql_save(); $data->code = $code . $data->code; $input = json_encode($data); } $this->clients[$key] = array(); $this->clients[$key]["sock"] = $client_sock; if (self::$debug) { self::log_debug("TestServer->get_client() --- Client '{$key}' added"); } } else { if (is_resource($this->clients[$key]["sock"])) { //@socket_shutdown($this->clients[$key]["sock"]); socket_close($this->clients[$key]["sock"]); $this->clients[$key]["sock"] = $client_sock; } if (self::$debug) { self::log_debug("TestServer->get_client() --- Client '{$key}' loaded"); } } return $this->clients[$key]; }
public function start() { gc_enable(); if (file_exists(Ini::$path_unix_sock)) { unlink(Ini::$path_unix_sock); } $this->last_action_time = time(); if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- TestServer started"); } if (Ini::$server_socks_type == 1) { $this->main_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); } if (Ini::$server_socks_type == 0) { $this->main_sock = socket_create(AF_UNIX, SOCK_STREAM, 0); } if (!$this->main_sock) { if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- Error: (socket_create) " . socket_last_error() . " - " . socket_strerror(socket_last_error())); self::log_debug("TestServer->start() --- Server halted!"); } return; } if (!socket_set_option($this->main_sock, SOL_SOCKET, SO_REUSEADDR, 1)) { if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- Error: (socket_set_option) " . socket_last_error() . " - " . socket_strerror(socket_last_error())); self::log_debug("TestServer->start() --- Server halted!"); } $this->stop(); return; } $bind = null; if (Ini::$server_socks_type == 1) { $bind = socket_bind($this->main_sock, Ini::$server_host, Ini::$server_port); } if (Ini::$server_socks_type == 0) { $bind = socket_bind($this->main_sock, Ini::$path_unix_sock); } if (!$bind) { if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- Error: (socket_bind) " . socket_last_error() . " - " . socket_strerror(socket_last_error())); self::log_debug("TestServer->start() --- Server halted!"); } $this->stop(); return; } if (!socket_listen($this->main_sock)) { if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- Error: (socket_listen) " . socket_last_error() . " - " . socket_strerror(socket_last_error())); self::log_debug("TestServer->start() --- Server halted!"); } $this->stop(); return; } $this->clients = array(); $this->instances = array(); if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- TestServer initialized"); } if (!socket_set_nonblock($this->main_sock)) { if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- Error: (socket_set_nonblock)"); self::log_debug("TestServer->start() --- Server halted!"); } return; } $this->is_alive = true; do { gc_collect_cycles(); //serialization foreach ($this->clients as $k => $v) { if ($this->instances[$k] != null && $this->instances[$k]->is_timedout() && !$this->instances[$k]->is_serializing) { if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- Client '{$k}' timedout"); } TestSession::change_db($this->instances[$k]->UserWorkspace_id); $session = TestSession::from_mysql_id($this->instances[$k]->TestSession_id); if ($session->debug == 0) { $this->serialize_instance($k); } else { $this->close_instance($k, true); } } } if (time() - $this->last_action_time > Ini::$r_server_timeout) { if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- Reached max idle time"); } break; } //interpret data start foreach ($this->clients as $k => $v) { //read if ($this->instances[$k]->is_working) { $serialized = false; $response = $this->instances[$k]->read(); if ($this->instances[$k]->is_serialized) { $serialized = true; } if ($response !== null) { $this->last_action_time = time(); $this->instances[$k]->is_data_ready = false; $this->instances[$k]->is_working = false; if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- Client '{$k}' test data read"); if (Ini::$log_server_streams) { self::log_debug($response, true); if ($this->instances[$k]->error_response != "") { self::log_debug($this->instances[$k]->error_response, true); } } } TestSession::change_db($this->instances[$k]->UserWorkspace_id); $session = TestSession::from_mysql_id($this->instances[$k]->TestSession_id); if ($session->debug == 1 && $session->status == TestSession::TEST_SESSION_STATUS_WAITING && !$this->instances[$k]->debug_code_appended) { $this->instances[$k]->debug_code_appended = true; $this->instances[$k]->run(' concerto:::concerto.updateState() ', null, false); continue; } $this->instances[$k]->debug_code_appended = false; if (!$serialized) { $response = array("return" => $this->instances[$k]->code_execution_halted ? 1 : 0); $response = json_encode($response); if (!socket_write($this->clients[$k]["sock"], $response . chr(0))) { if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- Error: (socket_write) " . socket_last_error() . " - " . socket_strerror(socket_last_error())); } } else { if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- Client '{$k}' test response sent back"); if (Ini::$log_server_streams) { self::log_debug($response, false); } } } if ($this->instances[$k]->code_execution_halted) { $this->close_instance($k, true); } } } if ($serialized || array_key_exists($k, $this->instances) && $this->instances[$k]->is_finished) { $this->last_action_time = time(); $this->close_instance($k, true); } } } //interpret data end usleep(self::$sleep_microseconds); $client_sock = @socket_accept($this->main_sock); if (!$client_sock) { continue; } if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- socket accepted"); } $data = ""; while ($read = socket_read($client_sock, 4096)) { $len = strlen($read); if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- socket read (" . $len . ")"); } $data .= $read; if (substr($read, -1, 1) == chr(0)) { break; } } if ($read === false) { if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- Error: (socket_read) " . socket_last_error() . " - " . socket_strerror(socket_last_error())); } socket_close($client_sock); continue; } $input = trim($data); if ($input != "") { $authorization_required = true; if (Ini::$log_server_events) { self::log_debug("TestServer->start() --- data recieved"); if (Ini::$log_server_streams) { self::log_debug($input, true); } } $this->last_action_time = time(); if (!$authorization_required || $this->authorize_client($client_sock, $input)) { $client = $this->get_client($client_sock, $input); $this->interpret_input($client, $input); } } } while ($this->is_alive); $this->stop(); gc_collect_cycles(); gc_disable(); }
public function get_TestSession() { TestSession::change_db($this->UserWorkspace_id); return TestSession::from_mysql_id($this->TestSession_id); }
public function serialize() { if (TestServer::$debug) { TestServer::log_debug("TestInstance->serialize() --- Serializing #" . $this->session_id); } $session = TestSession::from_mysql_id($this->session_id); $this->is_serializing = true; $this->send(sprintf("\n save.session('%s')\n ", $session->get_RSession_file_path())); }