目录
- [1. oj_model.hpp](#1. oj_model.hpp)
- [2. oj_view.hpp](#2. oj_view.hpp)
- [3. oj_control.hpp](#3. oj_control.hpp)
- [4. oj_server.cc](#4. oj_server.cc)
- [5. conf目录](#5. conf目录)
- [6. wwwroot目录](#6. wwwroot目录)
- [7. template_html目录](#7. template_html目录)
- [8. questions目录](#8. questions目录)
oj_server目录框架
oj_model.hpp:题目数据加载。oj_view.hpp:http界面渲染。oj_control.hpp:主机控制、负载均衡、序列化处理、http协议发送与接收。- oj_server.cc:主逻辑运行。
- makefile(make指令):make编译与清理。
- wwwroot文件夹:网页主界面
- template_html文件夹:题目目录和题目界面
- questions文件夹:存放题目内容、代码框架等
- conf文件夹:存放可用ip+port
1. oj_model.hpp
- 定义题目信息的属性为一个结构体
- Model类主要用来和数据进行交互,对外提供访问数据的接口:
- LoadQuestionList:加载题目列表
- GetAllQuestions:从类对象中提取完整的题目列表(包括每个题目的内容)
- GetOneQuestion:从类对象中提取单个题目:查找单个题目序号,并将题目内容输出
cpp
#pragma once
#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <cassert>
// 根据题目list文件,加载所有的题目信息到内存中
namespace ns_model
{
using namespace std;
using namespace ns_log;
using namespace ns_util;
// 1.oj题目的相关描述(字符创类型)
struct Question
{
std::string number; // 题目编号,唯一
std::string title; // 题目的标题
std::string star; // 题目的难度:简单、中等、困难
int cpu_limit; // 题目的时间要求(s)
int mem_limit; // 题目的空间要求(KB)
// 以下文件都在 question文件夹 下面的 序号文件夹 中
std::string desc; // 题目的描述
std::string header; // 题目的代码的预设
std::string tail; // 题目的测试用例,需要和header拼接,形成完整代码
};
// 题目列表及具体题目的 存储路径
const std::string questions_list_ = "./questions/questions.list";
const std::string questions_path_ = "./questions/";
// 2.model:主要用来和数据进行交互,对外提供访问数据的接口
class Model
{
private:
// 题号:题目的相关信息
unordered_map<string, Question> questions;
public:
// 2.1 类实例化时直接调用LoadQuestionList函数
Model()
{
assert(LoadQuestionList(questions_list_));
}
// 2.2 加载题目列表(将question.list加载到类对象中)
bool LoadQuestionList(const string &question_list)
{
// 加载配置文件:questions/questions.list + 题目编号文件
ifstream in(question_list);
if(!in.is_open())
{
LOG(FATAL) << " 加载题库失败, 请检查是否存在题库文件" << "\n";
return false;
}
string line;
/***************************************
* 1.按行读取题目列表信息
* 2.根据题目列表信息,读取各个题目的描述、代码、测试用例到Question类型的对象中
* 3.将题目所有内容insert进入Model的成员变量questions中
****************************************/
while(getline(in,line))
{
vector<string> tokens;
StringUtil::SplitString(line, &tokens, " "); // 字符串分割功能
// 1 判断回文数 简单 1 30000
if(tokens.size() != 5)
{
LOG(WARNING) << " 加载部分题目失败,请检查文件格式" << "\n";
continue;
}
// 填充Question结构体的每一个成员变量形成每一个题目的内容
Question q;
q.number = tokens[0];
q.title = tokens[1];
q.star = tokens[2];
q.cpu_limit = atoi(tokens[3].c_str());
q.mem_limit = atoi(tokens[4].c_str());
string path = questions_path_;
path += q.number;
path += "/";
FileUtil::ReadFile(path + "desc.txt", &(q.desc), true);
FileUtil::ReadFile(path + "header.cpp", &(q.header), true);
FileUtil::ReadFile(path + "tail.cpp", &(q.tail), true);
questions.insert({q.number, q});
}
LOG(INFO) << "加载题库..成功!" << "\n";
in.close();
return true;
}
// 2.3 从类对象中提取完整的题目列表(包括每个题目的内容),由vector<Question> *out类型的对象输出
// 按照对应的序号,输出成为vector类型对象的下标
bool GetAllQuestions(vector<Question> *out)
{
if(questions.size() == 0)
{
LOG(ERROR) << " 用户获取题库失败 " << "\n";
return false;
}
for(const auto &q : questions)
{
out->push_back(q.second); // first:key, second:value
}
return true;
}
// 2.4 从类对象中提取单个题目:查找单个题目序号,并将题目内容输出
bool GetOneQuestion(const std::string &number, Question *q)
{
const auto& iter = questions.find(number);
if(iter == questions.end())
{
LOG(ERROR) << "用户获取题目失败, 题目编号: " << number << "\n";
return false;
}
(*q) = iter->second;
return true;
}
~Model(){}
};
}
2. oj_view.hpp
主要用来渲染题目列表界面和题目界面,形成完整的html文件
cpp
#pragma once
#include <iostream>
#include <string>
#include <ctemplate/template.h>
#include "oj_model.hpp"
// #include "oj_model2.hpp"
namespace ns_view
{
using namespace ns_model;
const std::string template_path = "./template_html/";
class View
{
public:
View(){}
~View(){}
public:
// 一.渲染题目列表界面
void AllExpandHtml(const vector<struct Question> &questions, std::string *html)
{
// 题目的编号 题目的标题 题目的难度
// 推荐使用表格显示
// 1. 形成路径
std::string src_html = template_path + "all_questions.html";
// 2. 形成数字典
ctemplate::TemplateDictionary root("all_questions");
for (const auto& q : questions)
{
ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
sub->SetValue("number", q.number);
sub->SetValue("title", q.title);
sub->SetValue("star", q.star);
}
//3. 获取被渲染的html
ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);
//4. 开始完成渲染功能
tpl->Expand(html, &root);
}
// 二.渲染具体题目界面
void OneExpandHtml(const struct Question &q, std::string *html)
{
// 1. 形成路径
std::string src_html = template_path + "one_question.html";
// 2. 形成数字典
ctemplate::TemplateDictionary root("one_question");
root.SetValue("number", q.number);
root.SetValue("title", q.title);
root.SetValue("star", q.star);
root.SetValue("desc", q.desc);
root.SetValue("pre_code", q.header);
//3. 获取被渲染的html
ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);
//4. 开始完成渲染功能
tpl->Expand(html, &root);
}
};
}
3. oj_control.hpp
- 主机属性及管理的类:存储主机ip、port、负载数量。包含对主机负载进行增减的函数接口。
- 负载均衡模块:LoadBlance
- LoadConf:获取所有主机的参数
- SmartChoice:选取负载最小的主机,并通过输出型参数输出Machine m
- OfflineMachine:统一下线机器
- OnlineMachine:统一上线机器
- ShowMachines:打印 在线 & 离线 主机列表
- 这是我们的核心业务逻辑的控制器:Control类
- RecoveryMachine:恢复所有机器:统一上线所有机器(调用LoadBlance类中的OnlineMachine函数)
- AllQuestions:根据题目数据构建 题目列表网页
- Question:构建每个题目的具体网页
- Judge:对外接口
cpp
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <mutex>
#include <cassert>
#include <jsoncpp/json/json.h>
#include "../comm/httplib.h"
#include "../comm/log.hpp"
#include "../comm/util.hpp"
#include "oj_model.hpp"
#include "oj_view.hpp"
namespace ns_control
{
using namespace std;
using namespace ns_log;
using namespace ns_util;
using namespace ns_model;
using namespace ns_view;
using namespace httplib;
// 1.提供服务的主机
/***********************************
* 1.主机的相关配置
* 2.主机负载的调整函数
***********************************/
class Machine
{
public:
std::string ip; // 编译服务的ip
int port; // 编译服务的port
uint64_t load; // 编译服务的负载
std::mutex *mtx; // mutex禁止拷贝的,使用指针
public:
Machine()
: ip(""),
port(0),
load(0),
mtx(nullptr)
{
}
~Machine()
{}
public:
// 提升主机负载
void IncLoad()
{
if(mtx) mtx->lock();
++load;
if(mtx) mtx->unlock();
}
// 减少主机负载
void DecLoad()
{
if(mtx) mtx->lock();
--load;
if(mtx) mtx->unlock();
}
// 重置主机负载
void ResetLoad()
{
if(mtx) mtx->lock();
load = 0;
if(mtx) mtx->unlock();
}
// 获取主机负载,没有太大意义,只是为了统一接口
uint64_t Load()
{
uint64_t _load = 0;
if(mtx) mtx->lock();
_load = load;
if(mtx) mtx->unlock();
return _load;
}
};
// 所有有效的服务端机器: IP + port
const std::string service_machine = "./conf/service_machine.conf";
// 2.负载均衡模块
class LoadBlance
{
private:
/**************************************
* 四个成员变量
* 1.每一台主机都有自己的下标,充当当前主机的id
* 2.所有在线的主机id
* 3.所有离线的主机id
* 4.保证LoadBlance他的数据安全
**************************************/
std::vector<Machine> machines;
std::vector<int> online;
std::vector<int> offline;
std::mutex mtx;
public:
// 2.1 类实例化时直接调用LoadConf函数
LoadBlance()
{
assert(LoadConf(service_machine));
LOG(INFO) << "加载 " << service_machine << " 成功" << "\n";
}
~LoadBlance()
{}
public:
// 2.2 获取所有主机的参数
bool LoadConf(const std::string &machine_conf)
{
std::ifstream in(machine_conf);
if(!in.is_open())
{
LOG(FATAL) << "加载: " << machine_conf << " 失败" << "\n";
return false;
}
std::string line;
/***************************************
* 1.按行读取所有可使用的主机参数
* 2.将主机参数分割:IP+port分割开
* 3.将主机相关参数存入临时变量Machine m中
* 4.通过online.push_back()存入在线主机id到成员变量online,通过machines.push_back()存入主机参数到成员变量machines
******************************************/
while(std::getline(in, line))
{
std::vector<std::string> tokens;
StringUtil::SplitString(line, &tokens, ":");
if(tokens.size() != 2)
{
LOG(WARNING) << " 切分 " << line << " 失败" << "\n";
continue;
}
Machine m;
m.ip = tokens[0];
m.port = atoi(tokens[1].c_str());
m.load = 0;
m.mtx = new std::mutex();
online.push_back(machines.size());
machines.push_back(m);
}
in.close();
return true;
}
// 2.3 选取负载最小的主机,并通过输出型参数输出Machine m
// id: 输出型参数
// m : 输出型参数
bool SmartChoice(int *id, Machine **m)
{
// 1.使用我们选择好的主机(更新该主机的负载)
// 2.我们需要可能离线主机
mtx.lock();
// 负载均衡的算法
// 1.随机数+hash
// 2.轮询+hash
int online_num = online.size();
if(online_num == 0)
{
mtx.unlock();
LOG(FATAL) << " 所有的后端编译主机已经离线,请运维的同时尽快查看" << "\n";
return false;
}
// 通过遍历的方式,找到所有负载最小的机器
*id = online[0];
*m = &machines[online[0]];
uint64_t min_load = machines[online[0]].Load();
for(int i = 1; i < online_num; i++)
{
uint64_t curr_load = machines[online[i]].Load();
if(min_load > curr_load)
{
min_load = curr_load;
*id = online[i];
*m = &machines[online[i]];
}
}
mtx.unlock();
return true;
}
// 2.4 统一下线机器
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();
}
// 2.5 统一上线机器
void OnlineMachine()
{
mtx.lock();
online.insert(online.end(), offline.begin(), offline.end());
offline.erase(offline.begin(), offline.end());
mtx.unlock();
LOG(INFO) << "所有的主机都上线了" << "\n";
}
// 2.6 打印 在线 & 离线 主机列表
void ShowMachines()
{
mtx.lock();
std::cout << "当前在线主机列表: ";
for(auto &id : online)
{
std::cout << id << " ";
}
std::cout << std::endl;
std::cout << "当前离线主机列表: ";
for(auto &id : offline)
{
std::cout << id << " ";
}
std::cout << std::endl;
mtx.unlock();
}
};
// 3.这是我们的核心业务逻辑的控制器
class Control
{
private:
Model model_; // 提供后台数据: oj_model.hpp文件中的类
View view_; // 提供html渲染功能
LoadBlance load_blance_; // 核心负载均衡器: 本文件中的类
public:
Control()
{
}
~Control()
{
}
public:
// 3.1 恢复所有机器:统一上线所有机器(调用LoadBlance类中的OnlineMachine函数)
void RecoveryMachine()
{
load_blance_.OnlineMachine();
}
// 3.2 根据题目数据构建 题目列表网页
// html:输出型参数
bool AllQuestions(string *html)
{
bool ret = true;
vector<struct Question> all;
// a.获取所有题目的信息
if (model_.GetAllQuestions(&all))
{
// b.按照题目编号,将所有题目进行排序
sort(all.begin(), all.end(), [](const struct Question &q1, const struct Question &q2)
{ return atoi(q1.number.c_str()) < atoi(q2.number.c_str()); });
// c.获取题目信息成功,将所有的题目数据构建成网页
view_.AllExpandHtml(all, html);
}
else
{
*html = "获取题目失败,形成题目列表失败";
ret = false;
}
return ret;
}
// 3.3 构建每个题目的具体网页
bool Question(const string &number, string *html)
{
bool ret = true;
struct Question q;
// a.获取指定题目的详细信息
if (model_.GetOneQuestion(number, &q))
{
// b,获取指定题目信息成功, 将所有的题目数据构建成网页
view_.OneExpandHtml(q, html);
}
else
{
*html = "指定题目: " + number + " 不存在";
ret = false;
}
return ret;
}
// 3.4 对外接口
void Judge(const std::string &number, const std::string in_json, std::string *out_json)
{
// 0.根据题目编号,直接拿到对应的题目细节 (此处主要是题目的测试用例)
struct Question q;
model_.GetOneQuestion(number, &q);
// 1.in_json进行反序列化,得到题目的id,得到用户提交的源代码,input
Json::Reader reader;
Json::Value in_value;
reader.parse(in_json, in_value);
std::string code = in_value["code"].asString();
// 2.重新拼接用户代码+测试用例代码,形成新的代码
Json::Value compile_value;
compile_value["input"] = in_value["input"].asString();
compile_value["code"] = code + "\n" + q.tail;
compile_value["cpu_limit"] = q.cpu_limit;
compile_value["mem_limit"] = q.mem_limit;
Json::FastWriter writer;
std::string compile_string = writer.write(compile_value);
// 3.选择负载最低的主机(差错处理)
// 规则: 一直选择,直到主机可用,否则,就是全部挂掉
while(true)
{
int id = 0;
Machine *m = nullptr;
// SmartChoine()选择负载最小的主机
if(!load_blance_.SmartChoice(&id, &m))
{
break;
}
// 4.然后发起http请求,得到结果
Client cli(m->ip, m->port);
m->IncLoad(); // 被使用的机器负载+1
LOG(INFO) << " 选择主机成功,主机id:" << id << " 详情:" << m->ip << ":" << m->port <<
" 当前主机的负载是:" << m->Load() << "\n";
/********************************
* a.此处注册的Post请求是发送给compile_server.cc函数,用来编译运行程序,并将应答内容输出到res变量中
* b.提取res中的运行结果,并通过out_json对象输出
*/
if(auto res = cli.Post("/compile_and_run", compile_string, "application/json;charset=utf-8"))
{
// 5. 将结果赋值给out_json
if(res->status == 200)
{
*out_json = res->body;
m->DecLoad(); // 运行代码完毕,负载-1
LOG(INFO) << "请求编译和运行服务成功" << "\n";
break;
}
m->DecLoad();
}
else
{
// 请求失败
LOG(ERROR) << " 选择主机成功,主机id:" << id << " 详情:" << m->ip << ":" << m->port <<
" 当前主机的负载是:" << m->Load() << "\n";
load_blance_.OfflineMachine(id); // 如果运行失败,则当前负载离线
load_blance_.ShowMachines(); // 并显示所有在线的负载
}
}
}
};
}
4. oj_server.cc
-
获取题目列表界面
-
获取题目详细内容界面
-
判题执行
- 调用Judge函数,函数中根据记录中寻找负载最低的机器(机器负载是记录在oj_control文件的类对象中的)
- 将从网页中得到并解析完成的代码信息通过http协议发送给compile_server.cc中进行解析并执行。
注意:judge可以发送的ip+端口信息存放在oj_server目录下面的conf文件夹的service_machine.conf文件中。所以在运行compile_server可执行文件时必须使用存好的端口号才能使oj_server和compile_server进行互相通信!!!
cpp
#include <iostream>
#include <signal.h>
#include "../comm/httplib.h"
#include "oj_control.hpp"
using namespace httplib;
using namespace ns_control;
static Control *ctrl_ptr = nullptr;
void Recovery(int signo)
{
ctrl_ptr->RecoveryMachine();
}
int main()
{
signal(SIGQUIT, Recovery);
//用户请求的服务路由功能
Server svr;
Control ctrl;
ctrl_ptr = &ctrl;
// 注册以下三个请求逻辑
// 1.获取所有的题目列表
svr.Get("/all_questions", [&ctrl](const Request &req, Response &resp){
//返回一张包含有所有题目的html网页
std::string html;
ctrl.AllQuestions(&html);
//用户看到的是什么呢??网页数据 + 拼上了题目相关的数据
resp.set_content(html, "text/html; charset=utf-8");
});
// 2.用户要根据题目编号,获取题目的内容
// /question/100 -> 正则匹配
// R"()", 原始字符串raw string,保持字符串内容的原貌,不用做相关的转义
svr.Get(R"(/question/(\d+))", [&ctrl](const Request &req, Response &resp){
std::string number = req.matches[1];
std::string html;
ctrl.Question(number, &html);
resp.set_content(html, "text/html; charset=utf-8");
});
// 3.用户提交代码,使用我们的判题功能(1. 每道题的测试用例 2. compile_and_run)
svr.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &resp){
std::string number = req.matches[1];
std::string result_json;
ctrl.Judge(number, req.body, &result_json);
resp.set_content(result_json, "application/json;charset=utf-8");
// resp.set_content("指定题目的判题: " + number, "text/plain; charset=utf-8");
});
// 设置主页
svr.set_base_dir("./wwwroot");
// 监听具体端口信息: 监听任意ip的8080端口
svr.listen("0.0.0.0", 8080);
return 0;
}
5. conf目录
- 存放service_machine.conf文件,文件中包含可用机器的ip和port,oj_server只能使用文件中存在的机器进行后台编译运行。
6. wwwroot目录
- 存放index.html文件,是在线oj网页的主页。
7. template_html目录
- 存放all_question.html和one_question.html文件,分别是用来待渲染的题目目录和题目内容。
8. questions目录
- 存放题目序号的文件夹,每个文件夹中都包含有desc.txt、header.cpp、tail.cpp三个文件,分别是题目描述、题目代码框架、题目测试用例。
