获取页面代码和题号
用jQuery
function submit(){
// alert("嘿嘿!");
// 1. 收集当前页面的有关数据, 1. 题号 2.代码
// 2. 构建json,并通过ajax向后台发起基于http的json请求
// 3. 得到结果,解析并显示到 result中
}
var code = editor.getSession().getValue();
console.log(code);
var number = $(".container .part1 .left_desc h3 #number").text();
// console.log(number);
var judge_url = "/judge/" + number;
console.log(judge_url);
完成发起和获取结果请求
$.ajax({
method: 'Post', // 向后端发起请求的方式
url: judge_url, // 向后端指定的url发起请求
dataType: 'json', // 告知server,我需要什么格式
contentType: 'application/json;charset=utf-8', // 告知server,我给你的是什么格式
data: JSON.stringify({
'code':code,
'input': ''
}),
success: function(data){
//成功得到结果
// console.log(data);
show_result(data);
}
});
在oj_control.hpp的Judge函数里添加一个LOG
void Judge(const std::string &number, const std::string in_json, std::string *out_json)
{
LOG(DEBUG) << in_json << " \nnumber:" << number << "\n";
// 0. 根据题目编号,直接拿到对应的题目细节
struct Question q;
model_.GetOneQuestion(number, &q);
显示结果到网页
function submit(){
// alert("嘿嘿!");
// 1. 收集当前页面的有关数据, 1. 题号 2.代码
var code = editor.getSession().getValue();
// console.log(code);
var number = $(".container .part1 .left_desc h3 #number").text();
// console.log(number);
var judge_url = "/judge/" + number;
// console.log(judge_url);
// 2. 构建json,并通过ajax向后台发起基于http的json请求
$.ajax({
method: 'Post', // 向后端发起请求的方式
url: judge_url, // 向后端指定的url发起请求
dataType: 'json', // 告知server,我需要什么格式
contentType: 'application/json;charset=utf-8', // 告知server,我给你的是什么格式
data: JSON.stringify({
'code':code,
'input': ''
}),
success: function(data){
//成功得到结果
// console.log(data);
show_result(data);
}
});
// 3. 得到结果,解析并显示到 result中
function show_result(data)
{
// console.log(data.status);
// console.log(data.reason);
// 拿到result结果标签
var result_div = $(".container .part2 .result");
// 清空上一次的运行结果
result_div.empty();
// 首先拿到结果的状态码和原因结果
var _status = data.status;
var _reason = data.reason;
var reason_lable = $( "<p>",{
text: _reason
});
reason_lable.appendTo(result_div);
if(status == 0){
// 请求是成功的,编译运行过程没出问题,但是结果是否通过看测试用例的结果
var _stdout = data.stdout;
var _stderr = data.stderr;
var stdout_lable = $("<pre>", {
text: _stdout
});
var stderr_lable = $("<pre>", {
text: _stderr
})
stdout_lable.appendTo(result_div);
stderr_lable.appendTo(result_div);
}
else{
// 编译运行出错,do nothing
}
}
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
int Max(const vector<int> &v)
{
//将你的代码写在下面
int max = v[0];
for(int i = 1; i < v.size(); i++)
{
if(max < v[i]){
max = v[i];
}
}
return max;
}
};
上线与综合测试
负载测试
上线处理
oj_control.hpp
class Machine
{
public:
void ResetLoad()
{
if(mtx) mtx->lock();
load = 0;
if(mtx) mtx->unlock();
}
};
void OnlineMachine()
{
//我们统一上线,后面统一解决
mtx.lock();
online.insert(online.end(), offline.begin(), offline.end());
offline.erase(offline.begin(), offline.end());
mtx.unlock();
LOG(INFO) << "所有的主机有上线啦!" << "\n";
}
void OfflineMachine(int which)
{
mtx.lock();
for(auto iter = online.begin(); iter != online.end(); iter++)
{
if(*iter == which)
{
machines[which].ResetLoad();
//要离线的主机已经找到啦
online.erase(iter);
offline.push_back(which);
break; //因为break的存在,所有我们暂时不考虑迭代器失效的问题
}
}
mtx.unlock();
}
public:
void RecoveryMachine()
{
load_blance_.OnlineMachine();
}
oj_server.cc
static Control *ctrl_ptr = nullptr;
void Recovery(int signo)
{
ctrl_ptr->RecoveryMachine();
}
int main()
{
signal(SIGQUIT, Recovery);
ctrl_ptr = &ctrl;
}
使用ctrl+\,一键上线