mirror of
https://github.com/QingdaoU/JudgeServer.git
synced 2024-12-28 21:31:43 +00:00
add judge client for php (#1)
* add a simple judge client for php * 把JudgeClient类单独抽出来 * Unify code style * Unify code style
This commit is contained in:
parent
3a6d0cb56a
commit
58eee54e40
88
client/PHP/JudgeClient.php
Normal file
88
client/PHP/JudgeClient.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
|
||||
class JudgeClient
|
||||
{
|
||||
|
||||
private $ch = null;
|
||||
private $serverBaseUrl = '';
|
||||
|
||||
public function __construct($token, $serverBaseUrl)
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
74
client/PHP/client.php
Normal file
74
client/PHP/client.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
require('JudgeClient.php');
|
||||
|
||||
$c_src = <<<EOD
|
||||
#include <stdio.h>
|
||||
int main(){
|
||||
int a, b;
|
||||
scanf("%d%d", &a, &b);
|
||||
printf("%d", a+b);
|
||||
return 0;
|
||||
}
|
||||
EOD;
|
||||
|
||||
$c_spj_src = <<<EOD
|
||||
#include <stdio.h>
|
||||
int main(){
|
||||
return 1;
|
||||
}
|
||||
EOD;
|
||||
|
||||
$cpp_src = <<<EOD
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int a,b;
|
||||
cin >> a >> b;
|
||||
cout << a+b << endl;
|
||||
return 0;
|
||||
}
|
||||
EOD;
|
||||
|
||||
$java_src = <<<EOD
|
||||
import java.util.Scanner;
|
||||
public class Main{
|
||||
public static void main(String[] args){
|
||||
Scanner in=new Scanner(System.in);
|
||||
int a=in.nextInt();
|
||||
int b=in.nextInt();
|
||||
System.out.println(a + b);
|
||||
}
|
||||
}
|
||||
EOD;
|
||||
|
||||
$py2_src = <<<EOD
|
||||
s = raw_input()
|
||||
s1 = s.split(" ")
|
||||
print int(s1[0]) + int(s1[1])
|
||||
EOD;
|
||||
|
||||
|
||||
$judgeClient = new JudgeClient(hash('sha256', 'token'), 'http://123.57.151.42:12358');
|
||||
$languages = require('languages.php');
|
||||
|
||||
echo "ping:\n";
|
||||
print_r($judgeClient->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'));
|
76
client/PHP/languages.php
Normal file
76
client/PHP/languages.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'c_lang_config' => [
|
||||
'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,
|
||||
]
|
||||
]
|
||||
];
|
Loading…
Reference in New Issue
Block a user