C++ - 负载均衡式在线OJ

ops/2024/10/10 13:22:28/

目录

一.项目的宏观结构

1.1 只实现类似 leetcode 的题⽬列表+在线编程功能

1.2 项⽬宏观结构

1.3编写思路

二.所⽤技术与开发环境

2.1 所用技术

2.2 开发环境

三.compiler 服务设计

3.1 编译功能

3.2 日志功能

3.3 运⾏功能

3.4 编译并运⾏功能

3.5 把编译并运⾏功能,形成⽹络服务

四.基于 MVC 结构的 oj 服务设计

4.1 ⽤⼾请求的服务路由功能

4.2 model功能,提供对数据的操作

4.3 control,逻辑控制模块

五.version1 ⽂件版题⽬设计

六.前端⻚⾯设计

5.1 首页

5.2 所有题⽬的列表

5.3 指定题⽬的编写代码的⻚⾯+代码提交

七.version2 MySQL版题⽬设计

八.最终测试


一.项目的宏观结构

我们的项⽬核⼼是三个模块
  1. comm : 公共模块
  2. compile_server : 编译与运⾏模块
  3. oj_server : 获取题⽬列表,查看题⽬编写题⽬界⾯,负载均衡,其他功能

1.1 只实现类似 leetcode 的题⽬列表+在线编程功能

1.2 项⽬宏观结构

1.3编写思路

  1. 先编写 compile_server
  2. oj_server
  3. version1 基于⽂件版的在线OJ
  4. 前端的⻚⾯设计
  5. version2 基于 MySQL 版的在线OJ

二.所⽤技术与开发环境

2.1 所用技术

  • C++ STL 标准库
  • Boost 准标准库(字符串切割)
  • cpp-httplib 第三⽅开源⽹络库
  • ctemplate 第三⽅开源前端⽹⻚渲染库
  • jsoncpp 第三⽅开源序列化、反序列化库
  • 负载均衡设计
  • 多进程、多线程
  • MySQL C connect
  • Ace前端在线编辑器(了解)
  • html/css/js/jquery/ajax (了解

2.2 开发环境

  • Centos 7 云服务器
  • vscode
  • Mysql Workbench

三.compiler 服务设计

提供的服务:编译并运⾏代码,得到格式化的相关的结果

3.1 编译功能

compiler.hpp文件

对文件进行编译,是一个简单的编译器封装,用于编译 C++ 源代码文件。它通过创建子进程来执行编译操作,并将编译过程中的错误信息重定向dup2()到一个单独的文件中。父进程等待子进程完成,并根据生成的可执行文件判断编译是否成功。

日志和文件拼接后面会给大家写出

Compile 静态成员函数

  • 这是一个静态函数,用于编译给定的文件名 file_name
  • 函数首先尝试创建一个子进程。
  • 如果 fork() 失败,记录错误并返回 false
  • 如果 fork() 成功,子进程将尝试执行编译操作。
  • 在子进程中,使用 open 打开或创建一个错误文件(stderr),并将标准错误重定向到这个文件描述符。
  • 使用 execlp 替换当前子进程的映像,执行 g++ 编译器,编译源文件并生成可执行文件。编译参数包括输出的可执行文件名、源文件名和编译标准。
  • 父进程调用 waitpid 等待子进程结束。
  • 检查生成的可执行文件是否存在,如果存在则记录编译成功并返回 true
  • 如果编译失败(即没有生成可执行文件),记录错误并返回 false
#pragma once
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "../comm/util.hpp"
#include "../comm/log.hpp"
// 只负责进⾏代码的编译
namespace ns_compiler
{// 引⼊路径拼接功能using namespace ns_util;using namespace ns_log;class Compiler{public:Compiler(){}~Compiler(){}// 返回值:编译成功:true,否则:false// 输⼊参数:编译的⽂件名// file_name: 1234// 1234 -> ./temp/1234.cpp// 1234 -> ./temp/1234.exe// 1234 -> ./temp/1234.stderrstatic bool Compile(const std::string &file_name){pid_t pid = fork();if (pid < 0){LOG(ERROR) << "内部错误,创建⼦进程失败" << "\n";return false;}else if (pid == 0){umask(0);int _stderr = open(PathUtil::Stderr(file_name).c_str(),O_CREAT | O_WRONLY, 0644);if (_stderr < 0){LOG(WARNING) << "没有成功形成stderr⽂件" << "\n";exit(1);} // 重定向标准错误到_stderrdup2(_stderr, 2);// 程序替换,并不影响进程的⽂件描述符表// ⼦进程: 调⽤编译器,完成对代码的编译⼯作// g++ -o target src -std=c++11execlp("g++", "g++", "-o", PathUtil::Exe(file_name).c_str(),PathUtil::Src(file_name).c_str(), "-std=c++11", nullptr /*不要忘记*/);LOG(ERROR) << "启动编译器g++失败,可能是参数错误" << "\n";exit(2);}else{waitpid(pid, nullptr, 0);// 编译是否成功,就看有没有形成对应的可执⾏程序if (FileUtil::IsFileExists(PathUtil::Exe(file_name))){LOG(INFO) << PathUtil::Src(file_name) << " 编译成功!" << "\n";return true;}}LOG(ERROR) << "编译失败,没有形成可执⾏程序" << "\n";return false;}};
}

3.2 日志功能

comm里的log.hpp文件,实现的就是日志功能

#pragma once#include <iostream>
#include <string>
#include "util.hpp"namespace ns_log
{using namespace ns_util;// 日志等级enum{INFO, //就是整数DEBUG,WARNING,ERROR,FATAL};inline std::ostream &Log(const std::string &level, const std::string &file_name, int line){// 添加日志等级std::string message = "[";message += level;message += "]";// 添加报错文件名称message += "[";message += file_name;message += "]";// 添加报错行message += "[";message += std::to_string(line);message += "]";// 日志时间戳message += "[";message += TimeUtil::GetTimeStamp();message += "]";// cout 本质 内部是包含缓冲区的std::cout << message; //不要endl进行刷新return std::cout;}// LOG(INFo) << "message" << "\n";// 开放式日志#define LOG(level) Log(#level, __FILE__, __LINE__)
}

3.3 运⾏功能

runner.hpp文件

Runner 类提供了一个运行程序并限制其资源使用的机制。它通过创建子进程来执行程序,并将程序的标准输入、输出和错误重定向到指定的文件。同时,它还设置了进程的CPU时间和内存使用限制SetProcLimit(),以防止程序过度占用系统资源。通过这种方式,可以有效地管理和监控程序的运行

SetProcLimit 静态成员函数

  • 这个函数用于设置进程的资源限制。它接受两个参数:_cpu_limit(CPU时间限制,单位秒)和 _mem_limit(内存限制,单位KB)。
  • 使用 setrlimit 函数设置进程的CPU时间和内存使用限制。

Run 静态成员函数

  • 这个函数用于运行程序并限制其资源使用。它接受三个参数:file_name(要运行的程序的文件名)、cpu_limit(CPU时间限制)和 mem_limit(内存限制)。
  • 函数首先根据 file_name 生成标准输入、标准输出、标准错误文件的路径。
  • 使用 open 函数创建或打开这些文件,并获取文件描述符。
  • 如果打开文件失败,记录错误并返回 -1
  • 如果创建子进程失败,记录错误并返回 -2
  • 在子进程中,使用 dup2 函数将标准输入、标准输出和标准错误重定向到相应的文件描述符。
  • 调用 SetProcLimit 函数设置资源限制。
  • 使用 execl 函数执行程序。如果 execl 失败,子进程将退出并返回状态码 1
  • 父进程使用 waitpid 函数等待子进程结束,并获取子进程的退出状态。
  • 如果子进程异常退出(即收到了信号),将返回信号编号。
#pragma once#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>#include "../comm/log.hpp"
#include "../comm/util.hpp"namespace ns_runner
{using namespace ns_util;using namespace ns_log;class Runner{public:Runner() {}~Runner() {}public://提供设置进程占用资源大小的接口static void SetProcLimit(int _cpu_limit, int _mem_limit){// 设置CPU时长struct rlimit cpu_rlimit;cpu_rlimit.rlim_max = RLIM_INFINITY;cpu_rlimit.rlim_cur = _cpu_limit;setrlimit(RLIMIT_CPU, &cpu_rlimit);// 设置内存大小struct rlimit mem_rlimit;mem_rlimit.rlim_max = RLIM_INFINITY;mem_rlimit.rlim_cur = _mem_limit * 1024; //转化成为KBsetrlimit(RLIMIT_AS, &mem_rlimit);}// 指明文件名即可,不需要代理路径,不需要带后缀/******************************************** 返回值 > 0: 程序异常了,退出时收到了信号,返回值就是对应的信号编号* 返回值 == 0: 正常运行完毕的,结果保存到了对应的临时文件中* 返回值 < 0: 内部错误* * cpu_limit: 该程序运行的时候,可以使用的最大cpu资源上限* mem_limit: 改程序运行的时候,可以使用的最大的内存大小(KB)* *****************************************/static int Run(const std::string &file_name, int cpu_limit, int mem_limit){/********************************************** 程序运行:* 1. 代码跑完,结果正确* 2. 代码跑完,结果不正确* 3. 代码没跑完,异常了* Run需要考虑代码跑完,结果正确与否吗??不考虑!* 结果正确与否:是由我们的测试用例决定的!* 我们只考虑:是否正确运行完毕** 我们必须知道可执行程序是谁?* 一个程序在默认启动的时候* 标准输入: 不处理* 标准输出: 程序运行完成,输出结果是什么* 标准错误: 运行时错误信息* *******************************************/std::string _execute = PathUtil::Exe(file_name);std::string _stdin   = PathUtil::Stdin(file_name);std::string _stdout  = PathUtil::Stdout(file_name);std::string _stderr  = PathUtil::Stderr(file_name);umask(0);int _stdin_fd = open(_stdin.c_str(), O_CREAT|O_RDONLY, 0644);int _stdout_fd = open(_stdout.c_str(), O_CREAT|O_WRONLY, 0644);int _stderr_fd = open(_stderr.c_str(), O_CREAT|O_WRONLY, 0644);if(_stdin_fd < 0 || _stdout_fd < 0 || _stderr_fd < 0){LOG(ERROR) << "运行时打开标准文件失败" << "\n";return -1; //代表打开文件失败}            pid_t pid = fork();if (pid < 0){LOG(ERROR) << "运行时创建子进程失败" << "\n";close(_stdin_fd);close(_stdout_fd);close(_stderr_fd);return -2; //代表创建子进程失败}else if (pid == 0){dup2(_stdin_fd, 0);dup2(_stdout_fd, 1);dup2(_stderr_fd, 2);SetProcLimit(cpu_limit, mem_limit);execl(_execute.c_str()/*我要执行谁*/, _execute.c_str()/*我想在命令行上如何执行该程序*/, nullptr);exit(1);}else{close(_stdin_fd);close(_stdout_fd);close(_stderr_fd);int status = 0;waitpid(pid, &status, 0);// 程序运行异常,一定是因为因为收到了信号!LOG(INFO) << "运行完毕, info: " << (status & 0x7F) << "\n"; return status & 0x7F;}}};
}

3.4 编译并运⾏功能

compile_run.hpp文件

CompileAndRun 类提供了一个从接收用户代码、编译、运行到返回运行结果的完整流程。它使用JSON作为输入和输出的格式,方便与前端或其他系统进行交互。通过这种方式,可以有效地管理和监控用户代码的执行过程,并提供清晰的反馈信息。

RemoveTempFile 静态成员函数

  • 用于清理编译和运行过程中生成的临时文件,如源文件、编译错误文件、可执行文件、标准输入输出错误文件。

CodeToDesc 静态成员函数

  • 根据状态码生成对应的描述信息,用于向用户解释程序的运行结果。

Start 静态成员函数

  • 这是类的主要函数,用于处理用户提交的代码:
    • 接受一个JSON格式的字符串 in_json,其中包含了用户代码、输入、CPU和内存限制。
    • 输出一个JSON格式的字符串 out_json,其中包含了程序的运行状态、原因、标准输出和标准错误。
#pragma once#include "compiler.hpp"
#include "runner.hpp"
#include "../comm/log.hpp"
#include "../comm/util.hpp"#include <signal.h>
#include <unistd.h>
#include <jsoncpp/json/json.h>namespace ns_compile_and_run
{using namespace ns_log;using namespace ns_util;using namespace ns_compiler;using namespace ns_runner;class CompileAndRun{public:static void RemoveTempFile(const std::string &file_name){//清理文件的个数是不确定的,但是有哪些我们是知道的std::string _src = PathUtil::Src(file_name);if(FileUtil::IsFileExists(_src)) unlink(_src.c_str());std::string _compiler_error = PathUtil::CompilerError(file_name);if(FileUtil::IsFileExists(_compiler_error)) unlink(_compiler_error.c_str());std::string _execute = PathUtil::Exe(file_name);if(FileUtil::IsFileExists(_execute)) unlink(_execute.c_str());std::string _stdin = PathUtil::Stdin(file_name);if(FileUtil::IsFileExists(_stdin)) unlink(_stdin.c_str());std::string _stdout = PathUtil::Stdout(file_name);if(FileUtil::IsFileExists(_stdout)) unlink(_stdout.c_str());std::string _stderr = PathUtil::Stderr(file_name);if(FileUtil::IsFileExists(_stderr)) unlink(_stderr.c_str());}// code > 0 : 进程收到了信号导致异常奔溃// code < 0 : 整个过程非运行报错(代码为空,编译报错等)// code = 0 : 整个过程全部完成//待完善static std::string CodeToDesc(int code, const std::string &file_name){std::string desc;switch (code){case 0:desc = "编译运行成功";break;case -1:desc = "提交的代码是空";break;case -2:desc = "未知错误";break;case -3:// desc = "代码编译的时候发生了错误";FileUtil::ReadFile(PathUtil::CompilerError(file_name), &desc, true);break;case SIGABRT: // 6desc = "内存超过范围";break;case SIGXCPU: // 24desc = "CPU使用超时";break;case SIGFPE: // 8desc = "浮点数溢出";break;default:desc = "未知: " + std::to_string(code);break;}return desc;}/**************************************** 输入:* code: 用户提交的代码* input: 用户给自己提交的代码对应的输入,不做处理* cpu_limit: 时间要求* mem_limit: 空间要求** 输出:* 必填* status: 状态码* reason: 请求结果* 选填:* stdout: 我的程序运行完的结果* stderr: 我的程序运行完的错误结果** 参数:* in_json: {"code": "#include...", "input": "","cpu_limit":1, "mem_limit":10240}* out_json: {"status":"0", "reason":"","stdout":"","stderr":"",}* ************************************/static void Start(const std::string &in_json, std::string *out_json){Json::Value in_value;Json::Reader reader;reader.parse(in_json, in_value); //最后在处理差错问题std::string code = in_value["code"].asString();std::string input = in_value["input"].asString();int cpu_limit = in_value["cpu_limit"].asInt();int mem_limit = in_value["mem_limit"].asInt();int status_code = 0;Json::Value out_value;int run_result = 0;std::string file_name; //需要内部形成的唯一文件名if (code.size() == 0){status_code = -1; //代码为空goto END;}// 形成的文件名只具有唯一性,没有目录没有后缀// 毫秒级时间戳+原子性递增唯一值: 来保证唯一性file_name = FileUtil::UniqFileName();//形成临时src文件if (!FileUtil::WriteFile(PathUtil::Src(file_name), code)){status_code = -2; //未知错误goto END;}if (!Compiler::Compile(file_name)){//编译失败status_code = -3; //代码编译的时候发生了错误goto END;}run_result = Runner::Run(file_name, cpu_limit, mem_limit);if (run_result < 0){status_code = -2; //未知错误}else if (run_result > 0){//程序运行崩溃了status_code = run_result;}else{//运行成功status_code = 0;}END:out_value["status"] = status_code;out_value["reason"] = CodeToDesc(status_code, file_name);if (status_code == 0){// 整个过程全部成功std::string _stdout;FileUtil::ReadFile(PathUtil::Stdout(file_name), &_stdout, true);out_value["stdout"] = _stdout;std::string _stderr;FileUtil::ReadFile(PathUtil::Stderr(file_name), &_stderr, true);out_value["stderr"] = _stderr;}Json::StyledWriter writer;*out_json = writer.write(out_value);RemoveTempFile(file_name);}};
}

3.5 把编译并运⾏功能,形成⽹络服务

compile_server.cc文件

引入了httplib.h 这是一个轻量级的C++ HTTP库,用于创建HTTP服务器和客户端 这个自行网络下载就好

#include "compile_run.hpp"
#include "../comm/httplib.h"using namespace ns_compile_and_run;
using namespace httplib;void Usage(std::string proc)
{std::cerr << "Usage: " << "\n\t" << proc << " port" << std::endl;
}
//编译服务随时可能被多个人请求,必须保证传递上来的code,形成源文件名称的时候,要具有
//唯一性,要不然多个用户之间会互相影响
//./compile_server port
int main(int argc, char *argv[])
{if(argc != 2){Usage(argv[0]);return 1;}Server svr;svr.Post("/compile_and_run", [](const Request &req, Response &resp){// 用户请求的服务正文是我们想要的json stringstd::string in_json = req.body;std::string out_json;if(!in_json.empty()){CompileAndRun::Start(in_json, &out_json);resp.set_content(out_json, "application/json;charset=utf-8");}});svr.listen("0.0.0.0", atoi(argv[1])); //启动http服务return 0;
}

四.基于 MVC 结构的 oj 服务设计

本质:建⽴⼀个⼩型⽹站
  1. 获取⾸⻚,⽤题⽬列表充当
  2. 2. 编辑区域⻚⾯
  3. 3. 提交判题功能(编译并运⾏)
  • M: Model,通常是和数据交互的模块,⽐如,对题库进⾏增删改查(⽂件版,MySQL
  • V: view, 通常是拿到数据之后,要进⾏构建⽹⻚,渲染⽹⻚内容,展⽰给⽤⼾的(浏览器)
  • C: control, 控制器,就是我们的核⼼业务逻辑

4.1 ⽤⼾请求的服务路由功能

这段代码实现了一个在线编程评判系统的HTTP接口,包括获取题目列表、获取题目详情和代码评判功能。通过设置路由和处理函数,服务器能够响应不同类型的HTTP请求,并与业务逻辑控制器交互,返回相应的结果。此外,通过信号处理,程序能够在接收到特定信号时执行特定的操作,如恢复机器状态。

#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;// 获取所有的题目列表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");});// 用户要根据题目编号,获取题目的内容// /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");});// 用户提交代码,使用我们的判题功能(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");svr.listen("0.0.0.0", 8080);return 0;
}

4.2 model功能,提供对数据的操作

Model 类提供了加载题目信息、获取所有题目和获取单个题目的功能。它通过读取题目列表文件和对应的题目文件,将题目信息存储在内存中,以便于其他部分的程序访问和使用

Question 结构体

  • 定义了一个结构体 Question,用于存储题目的详细信息,包括编号、标题、难度、CPU和内存限制、描述、头部代码和尾部代码。
  • questins_list 和 questins_path 分别定义了题目列表文件的路径和题目文件的存放路径。

Model 类

  • Model 类包含一个私有成员 questions,是一个哈希表,用于存储题目信息。

构造函数

  • 在构造函数中,使用 assert 断言调用 LoadQuestionList 成功加载题目列表。

LoadQuestionList 函数

  • 从题目列表文件中加载题目信息,并读取每个题目的描述、头部代码和尾部代码,存储到 questions 哈希表中。
  • 使用 ifstream 打开并读取文件,使用 StringUtil::SplitString 分割每行数据。
  • 如果文件打开失败或格式不正确,将记录日志并返回 false

GetAllQuestions 函数

  • 将 questions 哈希表中的所有题目信息复制到输出参数 out 指向的 vector<Question> 中。

GetOneQuestion 函数

  • 根据题目编号从 questions 哈希表中获取单个题目信息,如果找到则复制到参数 q 指向的 Question 结构体中。
#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文件,加载所有的题目信息到内存中
// model: 主要用来和数据进行交互,对外提供访问数据的接口namespace ns_model
{using namespace std;using namespace ns_log;using namespace ns_util;struct Question{std::string number; //题目编号,唯一std::string title;  //题目的标题std::string star;   //难度: 简单 中等 困难int cpu_limit;      //题目的时间要求(S)int mem_limit;      //题目的空间要去(KB)std::string desc;   //题目的描述std::string header; //题目预设给用户在线编辑器的代码std::string tail;   //题目的测试用例,需要和header拼接,形成完整代码};const std::string questins_list = "./questions/questions.list";const std::string questins_path = "./questions/";class Model{private://题号 : 题目细节unordered_map<string, Question> questions;public:Model(){assert(LoadQuestionList(questins_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;while(getline(in, line)){vector<string> tokens;StringUtil::SplitString(line, &tokens, " ");// 1 判断回文数 简单 1 30000if(tokens.size() != 5){LOG(WARNING) << "加载部分题目失败, 请检查文件格式" << "\n";continue;}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 = questins_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;}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;}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(){}};
} // namespace ns_model

4.3 control,逻辑控制模块

这段代码实现了一个在线编程评判系统的核心功能,包括主机管理、负载均衡、题目信息管理和用户代码的编译运行。通过智能选择主机和错误处理机制,确保了服务的稳定性和可用性。

Machine 类

  • Machine 类用于表示提供编译服务的主机,包含 IP 地址、端口、负载和互斥锁。

  • 提供了增加和减少负载的方法,以及获取当前负载的方法。

LoadBlance 类

  • LoadBlance 类实现了负载均衡器的功能,维护了所有主机的状态(在线或离线)。

  • 通过 LoadConf 方法加载配置文件,初始化主机列表。

  • SmartChoice 方法用于智能选择负载最低的主机。

  • OfflineMachine 和 OnlineMachine 方法用于管理主机的在线和离线状态。

  • ShowMachines 方法用于调试,显示当前在线和离线的主机列表。

Control 类

  • Control 类是业务逻辑控制器,集成了模型(Model)、视图(View)和负载均衡器(LoadBlance)的功能。

  • RecoveryMachine 方法用于恢复所有主机为在线状态。

  • AllQuestions 和 Question 方法用于获取所有题目或指定题目的详细信息,并构建成 HTML 页面。

  • Judge 方法用于处理用户的代码提交,选择一个主机进行代码编译和运行,并获取结果。

主要功能流程

  • 加载配置LoadBlance 类在构造时加载服务主机的配置信息。
  • 智能选择SmartChoice 方法选择负载最低的主机进行任务分配。
  • 编译和运行Judge 方法处理用户提交的代码,选择主机,发起 HTTP 请求进行编译和运行,处理结果。
  • 在线和离线管理OfflineMachine 和 OnlineMachine 方法用于管理主机状态,确保服务的高可用性。
#pragma once#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <mutex>
#include <cassert>
#include <jsoncpp/json/json.h>#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include "../comm/httplib.h"#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;// 提供服务的主机class Machine{public:std::string ip;  //编译服务的ipint port;        //编译服务的portuint64_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;}};const std::string service_machine = "./conf/service_machine.conf";// 负载均衡模块class LoadBlance{private:// 可以给我们提供编译服务的所有的主机// 每一台主机都有自己的下标,充当当前主机的idstd::vector<Machine> machines;// 所有在线的主机idstd::vector<int> online;// 所有离线的主机idstd::vector<int> offline;// 保证LoadBlance它的数据安全std::mutex mtx;public:LoadBlance(){assert(LoadConf(service_machine));LOG(INFO) << "加载 " << service_machine << " 成功"<< "\n";}~LoadBlance(){}public: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;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;}// id: 输出型参数// m : 输出型参数bool SmartChoice(int *id, Machine **m){// 1. 使用选择好的主机(更新该主机的负载)// 2. 我们需要可能离线该主机mtx.lock();// 负载均衡的算法// 1. 随机数+hash// 2. 轮询+hashint 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;}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();}void OnlineMachine(){//我们统一上线,后面统一解决mtx.lock();online.insert(online.end(), offline.begin(), offline.end());offline.erase(offline.begin(), offline.end());mtx.unlock();LOG(INFO) << "所有的主机有上线啦!" << "\n";}//for testvoid 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();}};// 这是我们的核心业务逻辑的控制器class Control{private:Model model_; //提供后台数据View view_;   //提供html渲染功能LoadBlance load_blance_; //核心负载均衡器public:Control(){}~Control(){}public:void RecoveryMachine(){load_blance_.OnlineMachine();}//根据题目数据构建网页// html: 输出型参数bool AllQuestions(string *html){bool ret = true;vector<struct Question> all;if (model_.GetAllQuestions(&all)){sort(all.begin(), all.end(), [](const struct Question &q1, const struct Question &q2){return atoi(q1.number.c_str()) < atoi(q2.number.c_str());});// 获取题目信息成功,将所有的题目数据构建成网页view_.AllExpandHtml(all, html);}else{*html = "获取题目失败, 形成题目列表失败";ret = false;}return ret;}bool Question(const string &number, string *html){bool ret = true;struct Question q;if (model_.GetOneQuestion(number, &q)){// 获取指定题目信息成功,将所有的题目数据构建成网页view_.OneExpandHtml(q, html);}else{*html = "指定题目: " + number + " 不存在!";ret = false;}return ret;}// code: #include...// input: ""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);// 1. in_json进行反序列化,得到题目的id,得到用户提交源代码,inputJson::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;if(!load_blance_.SmartChoice(&id, &m)){break;}// 4. 然后发起http请求,得到结果Client cli(m->ip, m->port);m->IncLoad();LOG(INFO) << " 选择主机成功, 主机id: " << id << " 详情: " << m->ip << ":" << m->port << " 当前主机的负载是: " << m->Load() << "\n";if(auto res = cli.Post("/compile_and_run", compile_string, "application/json;charset=utf-8")){// 5. 将结果赋值给out_jsonif(res->status == 200){*out_json = res->body;m->DecLoad();LOG(INFO) << "请求编译和运行服务成功..." << "\n";break;}m->DecLoad();}else{//请求失败LOG(ERROR) << " 当前请求的主机id: " << id << " 详情: " << m->ip << ":" << m->port << " 可能已经离线"<< "\n";load_blance_.OfflineMachine(id);load_blance_.ShowMachines(); //仅仅是为了用来调试}}}};
} // namespace name

五.version1 ⽂件版题⽬设计

  1.  题⽬的编号
  2. 题⽬的标题
  3. 题⽬的难度
  4. 题⽬的描述,题⾯
  5. 时间要求(内部处理)
  6. 空间要求(内部处理)

两批⽂件构成

1. 第⼀个: questions.list : 题⽬列表(不需要题⽬的内容)
2. 第⼆个:题⽬的描述(desc.txt),题⽬的预设置代码(header.cpp), 测试⽤例代码 (tail.cpp)
1. 当⽤⼾提交⾃⼰的代码的时候:header.cpp

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>using namespace std;
class Solution
{
public:bool isPalindrome(int x){// 将你的代码写在下⾯// code// code// code// code// codereturn true;}
};
2. OJ 不是只把上⾯的代码提交给 compile_and_run , ⽽是
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
class Solution
{
public:bool isPalindrome(int x){// 将你的代码写在下⾯// code// code//code// code// codereturn true;}
};
该题号对应的测试⽤例 : tail.cpp
#ifndef COMPILER_ONLINE
#include "header.cpp"
#endifvoidTest1()
{// 通过定义临时对象,来完成⽅法的调⽤bool ret = Solution().isPalindrome(121);if (ret){std::cout << "通过⽤例1, 测试121通过 ... OK!" << std::endl;}else{std::cout << "没有通过⽤例1, 测试的值是: 121" << std::endl;}
}
void Test2()
{// 通过定义临时对象,来完成⽅法的调⽤bool ret = Solution().isPalindrome(-10);if (!ret){std::cout << "通过⽤例2, 测试-10通过 ... OK!" << std::endl;}else{std::cout << "没有通过⽤例2, 测试的值是: -10" << std::endl;}
}
int main()
{Test1();Test2();return 0;
}
最终提交给后台编译运⾏服务的代码是:
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
class Solution
{
public:bool isPalindrome(int x){// 将你的代码写在下⾯// code// code// code// code// codereturn true;}
};
// 下⾯的代码,我们不想让编译器编译的时候,保留它,⽽是裁剪掉(g++ -D COMPILER_ONLINE)
// 仅仅是为了让我们设计测试⽤例的时候,不要报错
#ifndef COMPILER_ONLINE
#include "header.cpp"
#endif
void Test1()
{// 通过定义临时对象,来完成⽅法的调⽤bool ret = Solution().isPalindrome(121);if (ret){std::cout << "通过⽤例1, 测试121通过 ... OK!" << std::endl;}else{std::cout << "没有通过⽤例1, 测试的值是: 121" << std::endl;}
}
void Test2()
{// 通过定义临时对象,来完成⽅法的调⽤bool ret = Solution().isPalindrome(-10);if (!ret){std::cout << "通过⽤例2, 测试-10通过 ... OK!" << std::endl;}else{std::cout << "没有通过⽤例2, 测试的值是: -10" << std::endl;}
}
int main()
{Test1();Test2();return 0;
}

六.前端⻚⾯设计

由于我们主要是后端编写 前端就不详细解释了

5.1 首页

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>这是我的个人OJ系统</title><style>/* 起手式, 100%保证我们的样式设置可以不受默认影响 */* {/* 消除网页的默认外边距 */margin: 0px;/* 消除网页的默认内边距 */padding: 0px;}html,body {width: 100%;height: 100%;}.container .navbar {width: 100%;height: 50px;background-color: black;/* 给父级标签设置overflow,取消后续float带来的影响 */overflow: hidden;}.container .navbar a {/* 设置a标签是行内块元素,允许你设置宽度 */display: inline-block;/* 设置a标签的宽度,a标签默认行内元素,无法设置宽度 */width: 80px;/* 设置字体颜色 */color: white;/* 设置字体的大小 */font-size: large;/* 设置文字的高度和导航栏一样的高度 */line-height: 50px;/* 去掉a标签的下划线 */text-decoration: none;/* 设置a标签中的文字居中 */text-align: center;}/* 设置鼠标事件 */.container .navbar a:hover {background-color: green;}.container .navbar .login {float: right;}.container .content {/* 设置标签的宽度 */width: 800px;/* 用来调试 *//* background-color: #ccc; *//* 整体居中 */margin: 0px auto;/* 设置文字居中 */text-align: center;/* 设置上外边距 */margin-top: 200px;}.container .content .font_ {/* 设置标签为块级元素,独占一行,可以设置高度宽度等属性 */display: block;/* 设置每个文字的上外边距 */margin-top: 20px;/* 去掉a标签的下划线 */text-decoration: none;/* 设置字体大小font-size: larger; */}</style>
</head><body><div class="container"><!-- 导航栏, 功能不实现--><div class="navbar"><a href="/">首页</a><a href="/all_questions">题库</a><a href="#">竞赛</a><a href="#">讨论</a><a href="#">求职</a><a class="login" href="#">登录</a></div><!-- 网页的内容 --><div class="content"><h1 class="font_">欢迎来到我的OnlineJudge平台</h1><p class="font_">这个我个人独立开发的一个在线OJ平台</p><a class="font_" href="/all_questions">点击我开始编程啦!</a></div></div>
</body></html>

5.2 所有题⽬的列表

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>在线OJ-题目列表</title><style>/* 起手式, 100%保证我们的样式设置可以不受默认影响 */* {/* 消除网页的默认外边距 */margin: 0px;/* 消除网页的默认内边距 */padding: 0px;}html,body {width: 100%;height: 100%;}.container .navbar {width: 100%;height: 50px;background-color: black;/* 给父级标签设置overflow,取消后续float带来的影响 */overflow: hidden;}.container .navbar a {/* 设置a标签是行内块元素,允许你设置宽度 */display: inline-block;/* 设置a标签的宽度,a标签默认行内元素,无法设置宽度 */width: 80px;/* 设置字体颜色 */color: white;/* 设置字体的大小 */font-size: large;/* 设置文字的高度和导航栏一样的高度 */line-height: 50px;/* 去掉a标签的下划线 */text-decoration: none;/* 设置a标签中的文字居中 */text-align: center;}/* 设置鼠标事件 */.container .navbar a:hover {background-color: green;}.container .navbar .login {float: right;}.container .question_list {padding-top: 50px;width: 800px;height: 100%;margin: 0px auto;/* background-color: #ccc; */text-align: center;}.container .question_list table {width: 100%;font-size: large;font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;margin-top: 50px;background-color: rgb(243, 248, 246);}.container .question_list h1 {color: green;}.container .question_list table .item {width: 100px;height: 40px;font-size: large;font-family:'Times New Roman', Times, serif;}.container .question_list table .item a {text-decoration: none;color: black;}.container .question_list table .item a:hover {color: blue;text-decoration:underline;}.container .footer {width: 100%;height: 50px;text-align: center;line-height: 50px;color: #ccc;margin-top: 15px;}</style>
</head><body><div class="container"><!-- 导航栏, 功能不实现--><div class="navbar"><a href="/">首页</a><a href="/all_questions">题库</a><a href="#">竞赛</a><a href="#">讨论</a><a href="#">求职</a><a class="login" href="#">登录</a></div><div class="question_list"><h1>OnlineJuge题目列表</h1><table><tr><th class="item">编号</th><th class="item">标题</th><th class="item">难度</th></tr>{{#question_list}}<tr><td class="item">{{number}}</td><td class="item"><a href="/question/{{number}}">{{title}}</a></td><td class="item">{{star}}</td></tr>{{/question_list}}</table></div><div class="footer"><!-- <hr> --><h4>@比特就业课</h4></div></div></body></html>

5.3 指定题⽬的编写代码的⻚⾯+代码提交

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>{{number}}.{{title}}</title><!-- 引入ACE插件 --><!-- 官网链接:https://ace.c9.io/ --><!-- CDN链接:https://cdnjs.com/libraries/ace --><!-- 使用介绍:https://www.iteye.com/blog/ybc77107-2296261 --><!-- https://justcode.ikeepstudying.com/2016/05/ace-editor-%E5%9C%A8%E7%BA%BF%E4%BB%A3%E7%A0%81%E7%BC%96%E8%BE%91%E6%9E%81%E5%85%B6%E9%AB%98%E4%BA%AE/ --><!-- 引入ACE CDN --><script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript"charset="utf-8"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript"charset="utf-8"></script><!-- 引入jquery CDN --><script src="http://code.jquery.com/jquery-2.1.1.min.js"></script><style>* {margin: 0;padding: 0;}html,body {width: 100%;height: 100%;}.container .navbar {width: 100%;height: 50px;background-color: black;/* 给父级标签设置overflow,取消后续float带来的影响 */overflow: hidden;}.container .navbar a {/* 设置a标签是行内块元素,允许你设置宽度 */display: inline-block;/* 设置a标签的宽度,a标签默认行内元素,无法设置宽度 */width: 80px;/* 设置字体颜色 */color: white;/* 设置字体的大小 */font-size: large;/* 设置文字的高度和导航栏一样的高度 */line-height: 50px;/* 去掉a标签的下划线 */text-decoration: none;/* 设置a标签中的文字居中 */text-align: center;}/* 设置鼠标事件 */.container .navbar a:hover {background-color: green;}.container .navbar .login {float: right;}.container .part1 {width: 100%;height: 600px;overflow: hidden;}.container .part1 .left_desc {width: 50%;height: 600px;float: left;overflow: scroll;}.container .part1 .left_desc h3 {padding-top: 10px;padding-left: 10px;}.container .part1 .left_desc pre {padding-top: 10px;padding-left: 10px;font-size: medium;font-family:'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;}.container .part1 .right_code {width: 50%;float: right;}.container .part1 .right_code .ace_editor {height: 600px;}.container .part2 {width: 100%;overflow: hidden;}.container .part2 .result {width: 300px;float: left;}.container .part2 .btn-submit {width: 120px;height: 50px;font-size: large;float: right;background-color: #26bb9c;color: #FFF;/* 给按钮带上圆角 *//* border-radius: 1ch; */border: 0px;margin-top: 10px;margin-right: 10px;}.container .part2 button:hover {color:green;}.container .part2 .result {margin-top: 15px;margin-left: 15px;}.container .part2 .result pre {font-size: large;}</style>
</head><body><div class="container"><!-- 导航栏, 功能不实现--><div class="navbar"><a href="/">首页</a><a href="/all_questions">题库</a><a href="#">竞赛</a><a href="#">讨论</a><a href="#">求职</a><a class="login" href="#">登录</a></div><!-- 左右呈现,题目描述和预设代码 --><div class="part1"><div class="left_desc"><h3><span id="number">{{number}}</span>.{{title}}_{{star}}</h3><pre>{{desc}}</pre></div><div class="right_code"><pre id="code" class="ace_editor"><textarea class="ace_text-input">{{pre_code}}</textarea></pre></div></div><!-- 提交并且得到结果,并显示 --><div class="part2"><div class="result"></div><button class="btn-submit" onclick="submit()">提交代码</button></div></div><script>//初始化对象editor = ace.edit("code");//设置风格和语言(更多风格和语言,请到github上相应目录查看)// 主题大全:http://www.manongjc.com/detail/25-cfpdrwkkivkikmk.htmleditor.setTheme("ace/theme/monokai");editor.session.setMode("ace/mode/c_cpp");// 字体大小editor.setFontSize(16);// 设置默认制表符的大小:editor.getSession().setTabSize(4);// 设置只读(true时只读,用于展示代码)editor.setReadOnly(false);// 启用提示菜单ace.require("ace/ext/language_tools");editor.setOptions({enableBasicAutocompletion: true,enableSnippets: true,enableLiveAutocompletion: true});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}}}</script>
</body></html>

七.version2 MySQL版题⽬设计

Model 类提供了一个从MySQL数据库中查询题目信息的接口,通过 GetAllQuestionsGetOneQuestion 函数可以获取所有题目或根据题目编号获取单个题目的详细信息。使用MySQL C API进行数据库操作,并通过日志记录了操作过程中可能出现的错误。

Model 类

  • Model 类提供了从MySQL数据库查询题目信息的功能。

QueryMySql 函数

  • 执行给定的SQL语句,并返回结果集到 out 指向的 vector<Question> 中。

  • 使用 mysql_init 初始化MySQL连接句柄。

  • 使用 mysql_real_connect 连接到数据库。

  • 设置连接的字符集为 utf8,以避免乱码问题。

  • 执行SQL语句,并使用 mysql_store_result 获取结果集。

  • 遍历结果集,将每行数据转换为 Question 结构体,并添加到输出向量中。

  • 释放结果集空间,并关闭MySQL连接。

GetAllQuestions 函数

  • 构造一个SQL查询语句,选择 oj_questions 表中的所有记录,并调用 QueryMySql 执行查询。

GetOneQuestion 函数

  • 构造一个SQL查询语句,根据题目编号 number 从 oj_questions 表中选择记录,并调用 QueryMySql 执行查询。

  • 如果查询结果恰好有一条记录,则将其赋值给 q 参数。

#pragma once
// MySQL 版本
#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include "include/mysql.h"
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <cassert>
// 根据题⽬list⽂件,加载所有的题⽬信息到内存中
// model: 主要⽤来和数据进⾏交互,对外提供访问数据的接⼝
namespace ns_model
{using namespace std;using namespace ns_log;using namespace ns_util;struct Question{std::string number; // 题⽬编号,唯⼀std::string title;  // 题⽬的标题std::string star;   // 难度: 简单 中等 困难std::string desc;   // 题⽬的描述std::string header; // 题⽬预设给⽤⼾在线编辑器的代码std::string tail;   // 题⽬的测试⽤例,需要和header拼接,形成完整代码int cpu_limit;      // 题⽬的时间要求(S)int mem_limit;      // 题⽬的空间要去(KB)};const std::string oj_questions = "oj_questions";const std::string host = "127.0.0.1";const std::string user = "oj_client";const std::string passwd = "123456";const std::string db = "oj";const int port = 3306;class Model{public:Model(){}bool QueryMySql(const std::string &sql, vector<Question> *out){// 创建mysql句柄MYSQL *my = mysql_init(nullptr);// 连接数据库if (nullptr == mysql_real_connect(my, host.c_str(), user.c_str(),passwd.c_str(), db.c_str(), port, nullptr, 0)){LOG(FATAL) << "连接数据库失败!" << "\n";return false;}// ⼀定要设置该链接的编码格式, 要不然会出现乱码问题mysql_set_character_set(my, "utf8");LOG(INFO) << "连接数据库成功!" << "\n";// 执⾏sql语句if (0 != mysql_query(my, sql.c_str())){LOG(WARNING) << sql << " execute error!" << "\n";return false;}// 提取结果MYSQL_RES *res = mysql_store_result(my);// 分析结果int rows = mysql_num_rows(res);   // 获得⾏数量int cols = mysql_num_fields(res); // 获得列数量Question q;for (int i = 0; i < rows; i++){MYSQL_ROW row = mysql_fetch_row(res);q.number = row[0];q.title = row[1];q.star = row[2];q.desc = row[3];q.header = row[4];q.tail = row[5];q.cpu_limit = atoi(row[6]);q.mem_limit = atoi(row[7]);out->push_back(q);}// 释放结果空间free(res);// 关闭mysql连接mysql_close(my);return true;}bool GetAllQuestions(vector<Question> *out){std::string sql = "select * from ";sql += oj_questions;return QueryMySql(sql, out);}bool GetOneQuestion(const std::string &number, Question *q){bool res = false;std::string sql = "select * from ";sql += oj_questions;sql += " where number=";sql += number;vector<Question> result;if (QueryMySql(sql, &result)){if (result.size() == 1){*q = result[0];res = true;}}return res;}~Model(){}};
}

八.最终测试

mysql和文件版的录题需要自己手动弄一下

CREATE TABLE IF NOT EXISTS `questions`(
id int PRIMARY KEY AUTO_INCREMENT COMMENT '题⽬的ID',
title VARCHAR(64) NOT NULL COMMENT '题⽬的标题',
star VARCHAR(8) NOT NULL COMMENT '题⽬的难度',
question_desc TEXT NOT NULL COMMENT '题⽬描述',
header TEXT NOT NULL COMMENT '题⽬头部,给⽤⼾看的代码',
tail TEXT NOT NULL COMMENT '题⽬尾部,包含我们的测试⽤例',
time_limit int DEFAULT 1 COMMENT '题⽬的时间限制',
mem_limit int DEFAULT 5000000 COMMENT '题⽬的空间限制'
)ENGINE=INNODB DEFAULT CHARSET=utf8;

以上就是所有的内容了,如果途中遇到什么问题,欢迎评论区进行提出!!!


http://www.ppmy.cn/ops/86976.html

相关文章

教程系列4 | 趋动云『社区项目』极速体验 LivePortrait 人脸表情“移花接木”大法

LivePortrait LivePortrait 由快手可灵大模型团队开源&#xff0c;只需 1 张原图就能生成动态视频。 LivePortrait 的核心优势在于其卓越的表情"迁移"技术&#xff0c;能够令静态图像中的人物瞬间焕发活力&#xff0c;无论是眨眼、微笑还是转头&#xff0c;皆栩栩如…

浅学爬虫-python爬虫基础

介绍与应用 Python爬虫是指利用Python编写程序从互联网上自动获取信息的技术。爬虫广泛应用于数据收集、价格监控、内容聚合、市场分析等领域。其基本原理是模拟浏览器发送HTTP请求获取网页数据&#xff0c;并通过解析HTML来提取所需的信息。 基本工具 Python中有许多强大的…

C++ | string

前言 本篇博客讲解c中的string类的使用(常用接口) &#x1f493; 个人主页&#xff1a;普通young man-CSDN博客 ⏩ 文章专栏&#xff1a;C_普通young man的博客-CSDN博客 ⏩ 本人giee:普通小青年 (pu-tong-young-man) - Gitee.com 若有问题 评论区见&#x1f4dd; &#x1f389…

火山引擎VeDI数据技术分享:两个步骤,为Parquet降本提效

更多技术交流、求职机会&#xff0c;欢迎关注字节跳动数据平台微信公众号&#xff0c;回复【1】进入官方交流群 作者&#xff1a;王恩策、徐庆 火山引擎 LAS 团队 火山引擎数智平台 VeDI 是火山引擎推出的新一代企业数据智能平台&#xff0c;基于字节跳动数据平台多年的“数据…

C++11 可变参数模板

C11的新特性可变参数模板能够创建可以接受可变参数的函数模板和类模板&#xff0c;相比C98/03&#xff0c;类模版和函数模版中只能含固定数量的模版参数&#xff0c;可变模版参数无疑是一个巨大的改进。然而由于可变模版参数比较抽象&#xff0c;使用起来需要一定的技巧&#x…

Spark实时(六):Output Sinks案例演示

文章目录 Output Sinks案例演示 一、​​​​​​​File sink 二、​​​​​​​​​​​​​​Memory Sink 三、​​​​​​​​​​​​​​Foreach Sink 1、​​​​​​​foreachBatch 2、​​​​​​​​​​​​​​foreach Output Sinks案例演示 当我们对流式…

测桃花运(算姻缘)的网站系统源码

简介&#xff1a; 站长安装本源码后只要有人在线测算&#xff0c;就可以获得收入哦。是目前市面上最火的变现利器。 本版本无后台&#xff0c;无数据。本版本为开发的逗号联盟接口版本。直接对接逗号联盟&#xff0c;修改ID就可以直接运营收费赚钱。 安装环境&#xff1a;PH…

TVM、LLVM、CUDA 是三个不同的项目或技术

TVM、LLVM、CUDA 是三个不同的项目或技术,它们在编程和计算领域中各有其独特的用途和优势。以下是它们之间的关系及各自用途的详细说明: TVM TVM (Tensor Virtual Machine) 是一个端到端的深度学习编译栈,用于优化和部署深度学习模型到各种硬件后端。其主要功能包括: 模型…