use of top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO in project HOJ by HimitZH.
the class RemoteJudgeContext method judge.
@Async
public void judge(ToJudge toJudge) {
String[] source = toJudge.getRemoteJudgeProblem().split("-");
String remoteOj = source[0];
String remoteProblemId = source[1];
RemoteJudgeDTO remoteJudgeDTO = RemoteJudgeDTO.builder().judgeId(toJudge.getJudge().getSubmitId()).uid(toJudge.getJudge().getUid()).cid(toJudge.getJudge().getCid()).pid(toJudge.getJudge().getPid()).username(toJudge.getUsername()).password(toJudge.getPassword()).oj(remoteOj).completeProblemId(remoteProblemId).userCode(toJudge.getJudge().getCode()).language(toJudge.getJudge().getLanguage()).serverIp(toJudge.getJudgeServerIp()).serverPort(toJudge.getJudgeServerPort()).submitId(toJudge.getJudge().getVjudgeSubmitId()).build();
initProblemId(remoteJudgeDTO);
Boolean isHasSubmitIdRemoteReJudge = toJudge.getIsHasSubmitIdRemoteReJudge();
RemoteJudgeStrategy remoteJudgeStrategy = buildJudgeStrategy(remoteJudgeDTO);
if (remoteJudgeStrategy != null) {
if (isHasSubmitIdRemoteReJudge != null && isHasSubmitIdRemoteReJudge) {
// 拥有远程oj的submitId远程判题的重判
remoteJudgeGetResult.process(remoteJudgeStrategy);
} else {
// 调用远程判题
boolean isSubmitOk = remoteJudgeToSubmit.process(remoteJudgeStrategy);
if (isSubmitOk) {
remoteJudgeGetResult.process(remoteJudgeStrategy);
}
}
}
}
use of top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO in project HOJ by HimitZH.
the class HduJudge method login.
@Override
public void login() {
RemoteJudgeDTO remoteJudgeDTO = getRemoteJudgeDTO();
HttpRequest homeRequest = HttpUtil.createGet(HOST);
HttpResponse homeResponse = homeRequest.execute();
if (homeResponse.getStatus() != 200) {
throw new RuntimeException("Failed to submit to HDU!!! The possible cause is connection failure, and the returned status code is " + homeResponse.getStatus());
}
String homePage = homeResponse.body();
if (homePage.contains("href=\"/userloginex.php?action=logout\"")) {
remoteJudgeDTO.setCookies(homeResponse.getCookies());
return;
}
HttpRequest request = HttpUtil.createPost(HOST + LOGIN_URL).addHeaders(headers);
HttpResponse response = request.form(MapUtil.builder(new HashMap<String, Object>()).put("username", remoteJudgeDTO.getUsername()).put("login", "Sign In").put("userpass", remoteJudgeDTO.getPassword()).map()).execute();
remoteJudgeDTO.setLoginStatus(response.getStatus());
remoteJudgeDTO.setCookies(response.getCookies());
}
use of top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO in project HOJ by HimitZH.
the class HduJudge method submit.
@Override
public void submit() {
RemoteJudgeDTO remoteJudgeDTO = getRemoteJudgeDTO();
if (remoteJudgeDTO.getCompleteProblemId() == null || remoteJudgeDTO.getUserCode() == null) {
return;
}
login();
List<HttpCookie> cookies = remoteJudgeDTO.getCookies();
HttpRequest request = HttpUtil.createPost(HOST + SUBMIT_URL).addHeaders(headers).cookie(cookies);
HttpResponse response = request.form(MapUtil.builder(new HashMap<String, Object>()).put("check", "0").put("language", getLanguage(remoteJudgeDTO.getLanguage())).put("problemid", remoteJudgeDTO.getCompleteProblemId()).put("usercode", remoteJudgeDTO.getUserCode() + getRandomBlankString()).map()).execute();
remoteJudgeDTO.setSubmitStatus(response.getStatus());
if (response.getStatus() != 200 && response.getStatus() != 302) {
log.error("进行题目提交时发生错误:提交题目失败," + HduJudge.class.getName() + ",题号:" + remoteJudgeDTO.getCompleteProblemId());
return;
}
// 下面的请求都是GET
request.setMethod(Method.GET);
// 获取提交的题目id
Long maxRunId = getMaxRunId(request, remoteJudgeDTO.getUsername(), remoteJudgeDTO.getCompleteProblemId());
if (maxRunId == -1L) {
// 等待2s再次查询,如果还是失败,则表明提交失败了
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
maxRunId = getMaxRunId(request, remoteJudgeDTO.getUsername(), remoteJudgeDTO.getCompleteProblemId());
}
remoteJudgeDTO.setCookies(cookies).setSubmitId(maxRunId);
}
use of top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO in project HOJ by HimitZH.
the class HduJudge method result.
@Override
public RemoteJudgeRes result() {
RemoteJudgeDTO remoteJudgeDTO = getRemoteJudgeDTO();
String url = HOST + String.format(QUERY_URL, remoteJudgeDTO.getSubmitId());
HttpRequest request = HttpUtil.createGet(url).cookie(remoteJudgeDTO.getCookies()).addHeaders(headers);
HttpResponse response = request.execute();
// 1提交时间 2结果 3执行时间 4执行空间 5代码长度
// 一般情况下 代码长度和提交时间不需要,想要也行,自行添加
Pattern pattern = Pattern.compile(">" + remoteJudgeDTO.getSubmitId() + "</td><td>[\\s\\S]*?</td><td>([\\s\\S]*?)</td><td>[\\s\\S]*?</td><td>(\\d*?)MS</td><td>(\\d*?)K</td>");
Matcher matcher = pattern.matcher(response.body());
// 找到时
Validate.isTrue(matcher.find());
String rawStatus = matcher.group(1).replaceAll("<[\\s\\S]*?>", "").trim();
Constants.Judge judgeStatus = statusTypeMap.getOrDefault(rawStatus, Constants.Judge.STATUS_PENDING);
RemoteJudgeRes remoteJudgeRes = RemoteJudgeRes.builder().status(judgeStatus.getStatus()).build();
if (judgeStatus == Constants.Judge.STATUS_PENDING) {
return remoteJudgeRes;
}
// 获取其他信息
String executionTime = matcher.group(2);
remoteJudgeRes.setTime(Integer.parseInt(executionTime));
String executionMemory = matcher.group(3);
remoteJudgeRes.setMemory(Integer.parseInt(executionMemory));
// 如果CE了,则还需要获得错误信息
if (judgeStatus == Constants.Judge.STATUS_COMPILE_ERROR) {
request.setUrl(HOST + String.format(ERROR_URL, remoteJudgeDTO.getSubmitId()));
String CEHtml = request.execute().body();
String compilationErrorInfo = ReUtil.get("<pre>([\\s\\S]*?)</pre>", CEHtml, 1);
remoteJudgeRes.setErrorInfo(HtmlUtil.unescape(compilationErrorInfo));
}
return remoteJudgeRes;
}
use of top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO in project HOJ by HimitZH.
the class RemoteJudgeToSubmit method process.
public boolean process(RemoteJudgeStrategy remoteJudgeStrategy) {
RemoteJudgeDTO remoteJudgeDTO = remoteJudgeStrategy.getRemoteJudgeDTO();
log.info("Ready Send Task to RemoteJudgeDTO => {}", remoteJudgeDTO);
String errLog = null;
try {
remoteJudgeStrategy.submit();
} catch (Exception e) {
log.error("Submit Failed! Error:", e);
errLog = e.getMessage();
}
Long submitId = remoteJudgeDTO.getSubmitId();
// 提交失败 前端手动按按钮再次提交 修改状态 STATUS_SUBMITTED_FAILED
if (submitId == null || submitId == -1L) {
// 将使用的账号放回对应列表
log.error("[{}] Submit Failed! Begin to return the account to other task!", remoteJudgeDTO.getOj());
remoteJudgeService.changeAccountStatus(remoteJudgeDTO.getOj(), remoteJudgeDTO.getUsername());
if (RemoteJudgeContext.openCodeforcesFixServer) {
if (remoteJudgeDTO.getOj().equals(Constants.RemoteJudge.GYM_JUDGE.getName()) || remoteJudgeDTO.getOj().equals(Constants.RemoteJudge.CF_JUDGE.getName())) {
// 对CF特殊,归还判题机权限
log.error("[{}] Submit Failed! Begin to return the Server Status to other task!", remoteJudgeDTO.getOj());
remoteJudgeService.changeServerSubmitCFStatus(remoteJudgeDTO.getServerIp(), remoteJudgeDTO.getServerPort());
}
}
// 更新此次提交状态为提交失败!
UpdateWrapper<Judge> judgeUpdateWrapper = new UpdateWrapper<>();
judgeUpdateWrapper.set("status", Constants.Judge.STATUS_SUBMITTED_FAILED.getStatus()).set("error_message", errLog).eq("submit_id", remoteJudgeDTO.getJudgeId());
judgeEntityService.update(judgeUpdateWrapper);
// 更新其它表
judgeContext.updateOtherTable(remoteJudgeDTO.getSubmitId(), Constants.Judge.STATUS_SYSTEM_ERROR.getStatus(), remoteJudgeDTO.getCid(), remoteJudgeDTO.getUid(), remoteJudgeDTO.getPid(), remoteJudgeDTO.getGid(), null, null);
return false;
}
// 提交成功顺便更新状态为-->STATUS_PENDING 等待判题中...
judgeEntityService.updateById(new Judge().setSubmitId(remoteJudgeDTO.getJudgeId()).setStatus(Constants.Judge.STATUS_PENDING.getStatus()).setVjudgeSubmitId(submitId).setVjudgeUsername(remoteJudgeDTO.getUsername()).setVjudgePassword(remoteJudgeDTO.getPassword()).setJudger(name));
log.info("[{}] Submit Successfully! The submit_id of remote judge is [{}]. Waiting the result of the task!", submitId, remoteJudgeDTO.getOj());
return true;
}
Aggregations