gitee地址:https://gitee.com/tgwTTT/load-balancing-online-judge

项目概述

在线判题系统(Online Judge,简称OJ)是一个允许用户在线编程、提交代码并实时获取评测结果的平台。本项目采用模块化设计,将系统分为三个核心模块,实现了代码编译、题目管理和负载均衡等关键功能。

系统架构设计

模块划分

  1. comm公共模块
    • 提供系统通用的工具类和函数
    • 包含日志记录、配置读取、公共数据结构等基础组件
    • 为其他模块提供统一的基础服务支持
  2. compile_server编译服务器模块
    • 负责用户提交代码的编译和执行
    • 实现安全沙箱环境,防止恶意代码对系统造成破坏
    • 支持多种编程语言(C/C++、Java、Python等)的编译运行
  3. oj_server核心业务模块
    • 题目列表获取和展示
    • 题目编辑和提交界面
    • 负载均衡功能,合理分配编译任务
    • 用户管理、提交记录、排名等业务功能(拓展)
  4. temp临时文件目录
    • 存储用户提交的代码文件
    • 保存编译过程中的临时文件

核心技术实现

前端

1. HTML模板渲染技术

项目使用ctemplate库实现动态网页生成:

#include 
#include 
#include 

int main()
{
    std::string in_html = "./test.html";
    std::string value = "比特就业课";
    
    // 形成数据字典
    ctemplate::TemplateDictionary root("test");
    root.SetValue("key", value);
    
    // 获取被渲染网页对象
    ctemplate::Template *tpl = ctemplate::Template::GetTemplate(in_html, ctemplate::DO_NOT_STRIP);
    
    // 添加字典数据到网页中
    std::string out_html;
    tpl->Expand(&out_html, &root);
    
    std::cout << out_html << std::endl;
}

后端

  • 后端框架: C++为主,结合多种系统调用
  • 字符串处理: Boost Algorithm库
  • 数据序列化: JSON格式用于数据传输
  • 安全机制: Linux资源限制(rlimit)和信号处理

1. 公共模块(comm)

公共模块

为整个系统提供基础支持,包含以下核心组件:

1.日志系统

2.配置管理

3.公共数据结构

2. 编译服务器模块(compile_server)

class CompileServer {
public:
    // 编译代码
    CompileResult CompileCode(const std::string& code, 
                             const std::string& language,
                             const std::string& work_dir);
    
    // 执行程序
    ExecuteResult ExecuteProgram(const std::string& executable_path,
                                const std::string& input,
                                int time_limit,
                                int memory_limit);
    
    // 清理工作目录
    void CleanWorkDir(const std::string& work_dir);
};

安全沙箱:

class SecuritySandbox {
private:
    // 设置资源限制
    void SetResourceLimit(int time_limit, int memory_limit);
    
    // 设置安全策略
    void SetSecurityPolicy();
    
public:
    // 在沙箱中运行程序
    SandboxResult RunInSandbox(const std::string& command,
                              const std::string& input,
                              int time_limit,
                              int memory_limit);
};

3. 业务逻辑服务器模块(oj_server)

HTTP请求处理

class HttpServer {
public:
    // 启动服务器
    void Start(int port);
    
    // 路由注册
    void AddRoute(const std::string& path, 
                 std::function handler);
    
private:
    // 请求路由
    void RouteRequest(const HttpRequest& req, HttpResponse& resp);
};

题目管理

class ProblemManager {
private:
    std::vector problems_;
    std::string problems_file_;
    
public:
    bool LoadProblems();
    Problem GetProblem(int problem_id);
    std::vector GetAllProblems();
    bool AddProblem(const Problem& problem);
};

负载均衡器

class LoadBalancer {
private:
std::vector servers_;
std::atomic current_index_{0};

public:
// 添加编译服务器
void AddServer(const CompileServerInfo& server);

// 选择服务器(轮询算法)
CompileServerInfo SelectServer();

// 健康检查
void HealthCheck();

};

以上就是简略的项目介绍了,完整代码请参靠gitee仓库中的代码

今天的更新就到这啦!!!