diff --git a/client/PHP/JudgeClient.php b/client/PHP/JudgeClient.php new file mode 100644 index 0000000..f8c8cef --- /dev/null +++ b/client/PHP/JudgeClient.php @@ -0,0 +1,88 @@ +serverBaseUrl = rtrim($serverBaseUrl, '/'); + $this->ch = curl_init(); + $defaults = [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_HEADER => 0, + CURLOPT_HTTPHEADER => [ + 'Content-type: application/json', + 'X-Judge-Server-Token: ' . $token + ], + //POST方式 + CURLOPT_POST => 1 + ]; + curl_setopt_array($this->ch, $defaults); + } + + /** + * 发送http请求 + * @param $url string 请求的url + * @param $data array 请求参数 + */ + private function request($url, $data = []) + { + curl_setopt($this->ch, CURLOPT_URL, $url); + curl_setopt($this->ch, CURLOPT_POSTFIELDS, empty($data) ? '{}' : json_encode($data)); + if (!$result = curl_exec($this->ch)) { + trigger_error(curl_error($this->ch)); + } + return json_decode($result); + } + + public function ping() + { + return $this->request($this->serverBaseUrl . '/ping'); + } + + public function judge($src, $language_config, $max_cpu_time, $max_memory, $test_case_id, $output = false, $spj_version = null, + $spj_config = null, $spj_compile_config = null, $spj_src = null) + { + $data = [ + 'language_config' => $language_config, + 'src' => $src, + 'max_cpu_time' => $max_cpu_time, + 'max_memory' => $max_memory, + 'test_case_id' => $test_case_id, + 'spj_version' => $spj_version, + 'spj_config' => $spj_config, + 'spj_compile_config' => $spj_compile_config, + 'spj_src' => $spj_src, + 'output' => $output + ]; + return $this->request($this->serverBaseUrl . '/judge', $data); + + } + + public function compileSpj($src, $spj_version, $spj_compile_config, $test_case_id) + { + $data = [ + 'src' => $src, + 'spj_version' => $spj_version, + 'spj_compile_config' => $spj_compile_config, + 'test_case_id' => $test_case_id + ]; + return $this->request($this->serverBaseUrl . '/compile_spj', $data); + } + + public function close() + { + if (is_resource($this->ch)) { + curl_close($this->ch); + } + } + + public function __destruct() + { + $this->close(); + } +} diff --git a/client/PHP/client.php b/client/PHP/client.php new file mode 100644 index 0000000..88d8b0b --- /dev/null +++ b/client/PHP/client.php @@ -0,0 +1,74 @@ + +int main(){ + int a, b; + scanf("%d%d", &a, &b); + printf("%d", a+b); + return 0; +} +EOD; + +$c_spj_src = << +int main(){ + return 1; +} +EOD; + +$cpp_src = << + +using namespace std; + +int main() +{ + int a,b; + cin >> a >> b; + cout << a+b << endl; + return 0; +} +EOD; + +$java_src = <<ping()); + +echo "\n\ncompile_spj:\n"; +print_r($judgeClient->compileSpj($c_spj_src, '2', $languages['c_lang_spj_compile'], 'spj')); + +echo "\n\njudge c:\n"; +print_r($judgeClient->judge($c_src, $languages['c_lang_config'], 1000, 1024 * 1024 * 128, 'normal', true)); + +echo "\n\njudge cpp:\n"; +print_r($judgeClient->judge($cpp_src, $languages['cpp_lang_config'], 1000, 1024 * 1024 * 128, 'normal')); + +echo "\n\njudge java:\n"; +print_r($judgeClient->judge($java_src, $languages['java_lang_config'], 1000, 1024 * 1024 * 128, 'normal')); + +echo "\n\njudge python2:\n"; +print_r($judgeClient->judge($py2_src, $languages['py2_lang_config'], 1000, 1024 * 1024 * 128, 'normal')); diff --git a/client/PHP/languages.php b/client/PHP/languages.php new file mode 100644 index 0000000..a153df3 --- /dev/null +++ b/client/PHP/languages.php @@ -0,0 +1,76 @@ + [ + 'compile' => [ + 'src_name' => 'main.c', + 'exe_name' => 'main', + 'max_cpu_time' => 3000, + 'max_real_time' => 5000, + 'max_memory' => 128 * 1024 * 1024, + 'compile_command' => '/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 -static {src_path} -lm -o {exe_path}', + ], + 'run' => [ + 'command' => '{exe_path}', + 'seccomp_rule' => 'c_cpp', + ] + ], + 'c_lang_spj_compile' => [ + 'src_name' => 'spj-{spj_version}.c', + 'exe_name' => 'spj-{spj_version}', + 'max_cpu_time' => 3000, + 'max_real_time' => 5000, + 'max_memory' => 1024 * 1024 * 1024, + 'compile_command' => '/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 {src_path} -lm -o {exe_path}' + ], + 'c_lang_spj_config' => [ + 'exe_name' => 'spj-{spj_version}', + 'command' => '{exe_path} {in_file_path} {user_out_file_path}', + 'seccomp_rule' => 'c_cpp' + ], + 'cpp_lang_config' => [ + 'name' => 'cpp', + 'compile' => [ + 'src_name' => 'main.cpp', + 'exe_name' => 'main', + 'max_cpu_time' => 3000, + 'max_real_time' => 5000, + 'max_memory' => 128 * 1024 * 1024, + 'compile_command' => '/usr/bin/g++ -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c++11 {src_path} -lm -o {exe_path}', + ], + 'run' => [ + 'command' => '{exe_path}', + 'seccomp_rule' => 'c_cpp', + ] + ], + 'java_lang_config' => [ + 'name' => 'java', + 'compile' => [ + 'src_name' => 'Main.java', + 'exe_name' => 'Main', + 'max_cpu_time' => 3000, + 'max_real_time' => 5000, + 'max_memory' => -1, + 'compile_command' => '/usr/bin/javac {src_path} -d {exe_dir} -encoding UTF8' + ], + 'run' => [ + 'command' => '/usr/bin/java -cp {exe_dir} -Xss1M -XX:MaxPermSize=16M -XX:PermSize=8M -Xms16M -Xmx{max_memory}k -Djava.security.manager -Djava.security.policy==/etc/java_policy -Djava.awt.headless=true Main', + 'seccomp_rule' => null, + 'env' => ['MALLOC_ARENA_MAX=1'] + ] + ], + 'py2_lang_config' => [ + 'compile' => [ + 'src_name' => 'solution.py', + 'exe_name' => 'solution.pyc', + 'max_cpu_time' => 3000, + 'max_real_time' => 5000, + 'max_memory' => 128 * 1024 * 1024, + 'compile_command' => '/usr/bin/python -m py_compile {src_path}', + ], + 'run' => [ + 'command' => '/usr/bin/python {exe_path}', + 'seccomp_rule' => null, + ] + ] +];