forked from QingdaoU/JudgeServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJudgeClient.php
More file actions
170 lines (148 loc) · 4.49 KB
/
Copy pathJudgeClient.php
File metadata and controls
170 lines (148 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
class JudgeClient
{
private $ch = null;
private $serverBaseUrl = '';
private $token;
private static $languageConfigs = [];
public function __construct($token, $serverBaseUrl)
{
$this->serverBaseUrl = rtrim($serverBaseUrl, '/');
$this->token = hash('sha256', $token);
}
public function ping()
{
return $this->post($this->serverBaseUrl . '/ping');
}
/**
* 调用判题 api
* @param string $src 提交的源代码
* @param string $language 使用的编程语言
* @param string $testCaseId test_case_id
* @param array $config 额外配置
* @return array
* @throws Exception
*/
public function judge($src, $language, $testCaseId, $config = [])
{
$languageConfig = static::getLanguageConfigByLanguage($language);
if (is_null($languageConfig)) {
throw new Exception("don't support \"$language\" language!");
}
$default = [
'language_config' => $languageConfig,
'src' => $src,
'test_case_id' => $testCaseId,
'max_cpu_time' => $languageConfig['compile']['max_cpu_time'],
'max_memory' => $languageConfig['compile']['max_memory'],
'spj_version' => null,
'spj_config' => null,
'spj_compile_config' => null,
'spj_src' => null,
'output' => false
];
return $this->post($this->serverBaseUrl . '/judge', array_merge($default, $config));
}
public function compileSpj($src, $spjVersion, $spjCompileConfig)
{
$data = [
'src' => $src,
'spj_version' => $spjVersion,
'spj_compile_config' => $spjCompileConfig,
];
return $this->post($this->serverBaseUrl . '/compile_spj', $data);
}
public static function loadLanguageConfigs()
{
if (empty(static::$languageConfigs))
static::$languageConfigs = require('languages.php');
}
public static function getLanguageConfigByLanguage($language)
{
return static::getLanguageConfigByKey($language . '_lang_config');
}
public static function getLanguageConfigByKey($key)
{
return isset(static::$languageConfigs[$key]) ? static::$languageConfigs[$key] : null;
}
private function needCreateCurl()
{
return is_null($this->ch);
}
/**
* 获取 curl 资源
* @return null|resource
*/
private function getCurl()
{
if ($this->needCreateCurl()) {
$this->ch = curl_init();
$defaults = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
// set HTTP request header
CURLOPT_HTTPHEADER => [
'Content-type: application/json',
'X-Judge-Server-Token: ' . $this->token
],
];
curl_setopt_array($this->ch, $defaults);
}
return $this->ch;
}
/**
* 发送 GET 请求
* @param string $url 请求的url
* @param array $data 请求参数
* @return array
*/
private function get($url, $data = [])
{
return $this->request('GET', $url, $data);
}
/**
* 发送 POST 请求
* @param string $url 请求的url
* @param array $data 请求参数
* @return array
*/
private function post($url, $data = [])
{
return $this->request('POST', $url, $data);
}
/**
* 发送 http 请求
* @param string $method http method
* @param string $url 请求的url
* @param array $data 请求参数
* @return array
*/
private function request($method, $url, $data = [])
{
$ch = $this->getCurl();
$method = strtoupper($method);
if (in_array($method, ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', 'PATCH']))
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, empty($data) ? '{}' : json_encode($data));
if (!$result = curl_exec($this->ch)) {
trigger_error(curl_error($this->ch));
}
return json_decode($result, true);
}
/**
* 关闭 curl 资源
*/
public function close()
{
if (is_resource($this->ch)) {
curl_close($this->ch);
}
$this->ch = null;
}
public function __destruct()
{
$this->close();
}
}
JudgeClient::loadLanguageConfigs();