• 负载均衡在线oj


    1.项目源码🌹load-balanced-online-oj · fortianyang/project - 码云 - 开源中国 (gitee.com)

    2.相关技术⭐

    ⭕C++ STL 标准库
    ⭕Boost 准标准库(字符串切割)
    ⭕cpp-httplib 第三方开源网络库
    ⭕ctemplate 第三方开源前端网页渲染库
    ⭕jsoncpp 第三方开源序列化、反序列化库
    ⭕负载均衡设计
    ⭕多进程、多线程

    3. 开发环境 ⚙  

    ⭕quanCentos 7 云服务器
    ⭕vscode

    4.项目结构 🌼

    5. compile服务设计🌼

    compile: 编译并且运行代码,得到格式化的相关结果

    第一个功能compiler :编译功能❀ ❀❀

    ✍compile代码实现❤

    1. //compile.hpp
    2. #pragma once
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include "../comm/util.hpp"
    10. #include "../comm/log.hpp"
    11. // 只负责进行代码的编译
    12. namespace ns_compiler
    13. {
    14. // 引入路径拼接功能
    15. using namespace ns_util;
    16. using namespace ns_log;
    17. class Compiler
    18. {
    19. public:
    20. Compiler()
    21. {}
    22. ~Compiler()
    23. {}
    24. //返回值:编译成功:true,否则:false
    25. //输入参数:编译的文件名
    26. //file_name: 1234
    27. //1234 -> ./temp/1234.cpp
    28. //1234 -> ./temp/1234.exe
    29. //1234 -> ./temp/1234.stderr
    30. static bool Compile(const std::string &file_name)
    31. {
    32. pid_t pid = fork();
    33. if(pid < 0)
    34. {
    35. LOG(ERROR) << "内部错误,创建子进程失败" << "\n";
    36. return false;
    37. }
    38. else if (pid == 0)
    39. {
    40. umask(0);//⭐ 小细节 (*^_^*)
    41. int _stderr = open(PathUtil::CompilerError(file_name).c_str(), O_CREAT | O_WRONLY, 0644);
    42. if(_stderr < 0){
    43. LOG(WARNING) << "没有成功形成stderr文件" << "\n";
    44. exit(1);
    45. }
    46. //重定向标准错误到_stderr
    47. dup2(_stderr, 2);
    48. //程序替换,并不影响进程的文件描述符表
    49. //子进程: 调用编译器,完成对代码的编译工作
    50. //g++ -o target src -std=c++11
    51. execlp("g++", "g++", "-o", PathUtil::Exe(file_name).c_str(),\
    52. PathUtil::Src(file_name).c_str(),"-std=c++11", nullptr/*不要忘记*/);//♥
    53. // PathUtil::Src(file_name).c_str(), "-D", "COMPILER_ONLINE","-std=c++11", nullptr/*不要忘记*/);//♥
    54. LOG(ERROR) << "启动编译器g++失败,可能是参数错误" << "\n";
    55. exit(2);//♥
    56. }
    57. else{
    58. waitpid(pid, nullptr, 0);
    59. //编译是否成功,就看有没有形成对应的可执行程序
    60. if(FileUtil::IsFileExists(PathUtil::Exe(file_name))){
    61. LOG(INFO) << PathUtil::Src(file_name) << " 编译成功!" << "\n";
    62. return true;
    63. }
    64. }
    65. LOG(DEBUG)<Src(file_name)<<"\n";
    66. LOG(ERROR) << "编译失败,没有形成可执行程序" << "\n";
    67. return false;//♥
    68. }
    69. };
    70. }
    ✍Log功能♥
    1. //log.hpp
    2. #pragma once
    3. #include
    4. #include
    5. #include "util.hpp"
    6. namespace ns_log
    7. {
    8. using namespace ns_util;
    9. // 日志等级
    10. enum
    11. {
    12. INFO, //就是整数
    13. DEBUG,
    14. WARNING,
    15. ERROR,
    16. FATAL
    17. };
    18. inline std::ostream &Log(const std::string &level, const std::string &file_name, int line)
    19. {
    20. // 添加日志等级
    21. std::string message = "[";
    22. message += level;
    23. message += "]";
    24. // 添加报错文件名称
    25. message += "[";
    26. message += file_name;
    27. message += "]";
    28. // 添加报错行
    29. message += "[";
    30. message += std::to_string(line);
    31. message += "]";
    32. // 日志时间戳
    33. message += "[";
    34. message += TimeUtil::GetTimeStamp();
    35. message += "]";
    36. // cout 本质 内部是包含缓冲区的
    37. std::cout << message; //不要endl进行刷新
    38. return std::cout;
    39. }
    40. // LOG(INFo) << "message" << "\n";
    41. // 开放式日志
    42. #define LOG(level) Log(#level, __FILE__, __LINE__)
    43. }
    工具类♥
    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. namespace ns_util
    13. {
    14. class TimeUtil
    15. {
    16. public:
    17. static std::string GetTimeStamp()
    18. {
    19. struct timeval _time;
    20. gettimeofday(&_time, nullptr);
    21. return std::to_string(_time.tv_sec);
    22. }
    23. //获得毫秒时间戳
    24. static std::string GetTimeMs()
    25. {
    26. struct timeval _time;
    27. gettimeofday(&_time, nullptr);
    28. return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
    29. }
    30. };
    31. const std::string temp_path = "./temp/";
    32. class PathUtil
    33. {
    34. public:
    35. static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
    36. {
    37. std::string path_name = temp_path;
    38. path_name += file_name;
    39. path_name += suffix;
    40. return path_name;
    41. }
    42. // 编译时需要有的临时文件
    43. // 构建源文件路径+后缀的完整文件名
    44. // 1234 -> ./temp/1234.cpp
    45. static std::string Src(const std::string &file_name)
    46. {
    47. return AddSuffix(file_name, ".cpp");
    48. }
    49. // 构建可执行程序的完整路径+后缀名
    50. static std::string Exe(const std::string &file_name)
    51. {
    52. return AddSuffix(file_name, ".exe");
    53. }
    54. static std::string CompilerError(const std::string &file_name)
    55. {
    56. return AddSuffix(file_name, ".compile_error");
    57. }
    58. // 运行时需要的临时文件
    59. static std::string Stdin(const std::string &file_name)
    60. {
    61. return AddSuffix(file_name, ".stdin");
    62. }
    63. static std::string Stdout(const std::string &file_name)
    64. {
    65. return AddSuffix(file_name, ".stdout");
    66. }
    67. // 构建该程序对应的标准错误完整的路径+后缀名
    68. static std::string Stderr(const std::string &file_name)
    69. {
    70. return AddSuffix(file_name, ".stderr");
    71. }
    72. };
    73. class FileUtil
    74. {
    75. public:
    76. static bool IsFileExists(const std::string &path_name)
    77. {
    78. struct stat st;
    79. if (stat(path_name.c_str(), &st) == 0)
    80. {
    81. //获取属性成功,文件已经存在
    82. return true;
    83. }
    84. return false;
    85. }
    86. static std::string UniqFileName()
    87. {
    88. static std::atomic_uint id(0);
    89. id++;
    90. // 毫秒级时间戳+原子性递增唯一值: 来保证唯一性
    91. std::string ms = TimeUtil::GetTimeMs();
    92. std::string uniq_id = std::to_string(id);
    93. return ms + "_" + uniq_id;
    94. }
    95. static bool WriteFile(const std::string &target, const std::string &content)
    96. {
    97. std::ofstream out(target);
    98. if (!out.is_open())
    99. {
    100. return false;
    101. }
    102. out.write(content.c_str(), content.size());
    103. out.close();
    104. return true;
    105. }
    106. static bool ReadFile(const std::string &target, std::string *content, bool keep = false)
    107. {
    108. (*content).clear();
    109. std::ifstream in(target);
    110. if (!in.is_open())
    111. {
    112. return false;
    113. }
    114. std::string line;
    115. // getline:不保存行分割符,有些时候需要保留\n,
    116. // getline内部重载了强制类型转化
    117. while (std::getline(in, line))
    118. {
    119. (*content) += line;
    120. (*content) += (keep ? "\n" : "");
    121. }
    122. in.close();
    123. return true;
    124. }
    125. };
    126. class StringUtil
    127. {
    128. public:
    129. /*************************************
    130. * str: 输入型,目标要切分的字符串
    131. * target: 输出型,保存切分完毕的结果
    132. * sep: 指定的分割符
    133. * **********************************/
    134. static void SplitString(const std::string &str, std::vector *target, const std::string &sep)
    135. {
    136. //boost split
    137. boost::split((*target), str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
    138. }
    139. };
    140. }
    测试♥

    编译功能的基本代码已经完成,我们可以简单编写一个 temp目录,再在该目录下编写一个code.cpp才进行测试

    1. //code.cc
    2. #include
    3. int main()
    4. {
    5. //aaa--也可以加一点错误的信息,更好观察stderr的内容⭐
    6. std::cout<<"hello"<
    7. return 0;
    8. }
    1. //compile_server.cc
    2. #include"compiler.hpp"
    3. using namespace ns_compiler;
    4. #include "compile_run.hpp"
    5. int main(int argc, char *argv[])
    6. {
    7. std::string code="code";
    8. Compiler::Compile(code);
    9. return 0;
    10. }

    📕 📕运行结果
    📕 运行结果 1.0

    正常运行结束,stderr无输出。

    📕运行结果 2.0

    1. ./temp/code.cpp: In function ‘int main()’:
    2. ./temp/code.cpp:6:5: error: ‘aaa’ was not declared in this scope
    3. aaa
    4. ^
    5. ./temp/code.cpp:7:5: error: expected ‘;’ before ‘std’
    6. std::cout<<"hello"<<std::endl;
    7. ^

    第二个功能 runner :运行功能🌼

    功能分析

    程序运行:

    🐱      1. 代码跑完,结果正确

    🐱      2. 代码跑完,结果不正确

    🐱       3. 代码没跑完,异常了

    🐱        Run需要考虑代码跑完,结果正确与否吗??不考虑!

     ▷      结果正确与否:是由我们的测试用例决定的!

     ▷        我们只考虑:是否正确运行完毕

                 *

    🐱      我们必须知道可执行程序是谁?

     ▷        一个程序在默认启动的时候

             ▷       标准输入: 不处理

             ▷        标准输出: 程序运行完成,输出结果是什么

             ▷       标准错误: 运行时错误信息

    ❀基础版本

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include "../comm/log.hpp"
    10. #include "../comm/util.hpp"
    11. namespace ns_runner
    12. {
    13. using namespace ns_util;
    14. using namespace ns_log;
    15. class Runner
    16. {
    17. public:
    18. Runner() {}
    19. ~Runner() {}
    20. public:
    21. // 指明文件名即可,不需要代理路径,不需要带后缀
    22. /*******************************************
    23. * 返回值 > 0: 程序异常了,退出时收到了信号,返回值就是对应的信号编号
    24. * 返回值 == 0: 正常运行完毕的,结果保存到了对应的临时文件中
    25. * 返回值 < 0: 内部错误
    26. *
    27. * cpu_limit: 该程序运行的时候,可以使用的最大cpu资源上限
    28. * mem_limit: 改程序运行的时候,可以使用的最大的内存大小(KB)
    29. * *****************************************/
    30. static int Run(const std::string &file_name)
    31. {
    32. /*********************************************
    33. * 程序运行:
    34. * 1. 代码跑完,结果正确
    35. * 2. 代码跑完,结果不正确
    36. * 3. 代码没跑完,异常了
    37. * Run需要考虑代码跑完,结果正确与否吗??不考虑!
    38. * 结果正确与否:是由我们的测试用例决定的!
    39. * 我们只考虑:是否正确运行完毕
    40. *
    41. * 我们必须知道可执行程序是谁?
    42. * 一个程序在默认启动的时候
    43. * 标准输入: 不处理
    44. * 标准输出: 程序运行完成,输出结果是什么
    45. * 标准错误: 运行时错误信息
    46. * *******************************************/
    47. std::string _execute = PathUtil::Exe(file_name); // 可执行程序名
    48. std::string _stdin = PathUtil::Stdin(file_name); // 标准输入
    49. std::string _stdout = PathUtil::Stdout(file_name); // 标准输出
    50. std::string _stderr = PathUtil::Stderr(file_name); // 标准错误
    51. umask(0);
    52. int _stdin_fd = open(_stdin.c_str(), O_CREAT | O_RDONLY, 0644);
    53. int _stdout_fd = open(_stdout.c_str(), O_CREAT | O_WRONLY, 0644);
    54. int _stderr_fd = open(_stderr.c_str(), O_CREAT | O_WRONLY, 0644);
    55. if (_stdin_fd < 0 || _stdout_fd < 0 || _stderr_fd < 0)
    56. {
    57. LOG(ERROR) << "运行时打开标准文件失败"
    58. << "\n";
    59. return -1; // 代表打开文件失败
    60. }
    61. pid_t pid = fork();
    62. if (pid < 0)
    63. {
    64. LOG(ERROR) << "运行时创建子进程失败"
    65. << "\n";
    66. close(_stdin_fd);
    67. close(_stdout_fd);
    68. close(_stderr_fd);
    69. return -2; // 代表创建子进程失败
    70. }
    71. else if (pid == 0)
    72. {
    73. dup2(_stdin_fd, 0);
    74. dup2(_stdout_fd, 1);
    75. dup2(_stderr_fd, 2);
    76. execl(_execute.c_str() /*我要执行谁*/, _execute.c_str() /*我想在命令行上如何执行该程序*/, nullptr);
    77. exit(1);
    78. }
    79. else
    80. {
    81. close(_stdin_fd);
    82. close(_stdout_fd);
    83. close(_stderr_fd);
    84. int status = 0;
    85. waitpid(pid, &status, 0);
    86. // 程序运行异常,一定是因为因为收到了信号!
    87. LOG(INFO) << "运行完毕, info: " << (status & 0x7F) << "\n";
    88. return status & 0x7F;
    89. }
    90. }
    91. };
    92. }

    ✍ 测试

    1. #include"compiler.hpp"
    2. #include"runner.hpp"
    3. using namespace ns_compiler;
    4. using namespace ns_runner;
    5. int main(int argc, char *argv[])
    6. {
    7. std::string code="code";
    8. Compiler::Compile(code);
    9. Runner::Run(code);
    10. return 0;
    11. }

    测试结果

    程序正常运行结束,code.compile_error无输出

    ✍ 添加测试资源限制

    小测试

    我们通过下面这个简单的小实验来测试限制运行资源和时间资源,避免一些死循环或者资源消耗过大的测试恶意攻击

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. void handler(int signo)
    9. {
    10. std::cout<<"signo : "<
    11. }
    12. int main()
    13. {
    14. // 资源不足导致os终止进程,是通过信号终止的
    15. for(int i;i<=31;i++)
    16. {
    17. signal(i,handler);
    18. }
    19. // //限制累计运行时长
    20. // struct rlimit r;
    21. // r.rlim_cur=1;
    22. // r.rlim_max=RLIM_INFINITY;
    23. // setrlimit(RLIMIT_CPU,&r);
    24. // while (1) ;
    25. //限制使用的空间大小
    26. struct rlimit r;
    27. r.rlim_cur=1024*1024*40;//20M
    28. r.rlim_max=RLIM_INFINITY;
    29. setrlimit(RLIMIT_AS,&r);
    30. int count=0;
    31. while(true)
    32. {
    33. int *p=new int[1024*1024];
    34. count++;
    35. std::cout<<"size: "<
    36. sleep(1);
    37. }
    38. return 0;
    39. }
    测试结果

    添加资源限制功能到runner中

    代码实现

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include //⭐
    10. #include //⭐
    11. #include "../comm/log.hpp"
    12. #include "../comm/util.hpp"
    13. namespace ns_runner
    14. {
    15. using namespace ns_util;
    16. using namespace ns_log;
    17. class Runner
    18. {
    19. public:
    20. Runner() {}
    21. ~Runner() {}
    22. public:
    23. // 提供设置进程占用资源大小的接口
    24. static void SetProcLimit(int _cpu_limit, int _mem_limit)
    25. {
    26. // 设置CPU时长
    27. struct rlimit cpu_rlimit;
    28. cpu_rlimit.rlim_max = RLIM_INFINITY;
    29. cpu_rlimit.rlim_cur = _cpu_limit;
    30. setrlimit(RLIMIT_CPU, &cpu_rlimit);
    31. // 设置内存大小
    32. struct rlimit mem_rlimit;
    33. mem_rlimit.rlim_max = RLIM_INFINITY;
    34. mem_rlimit.rlim_cur = _mem_limit * 1024; // 转化成为KB
    35. setrlimit(RLIMIT_AS, &mem_rlimit);
    36. }
    37. // 指明文件名即可,不需要代理路径,不需要带后缀
    38. /*******************************************
    39. * 返回值 > 0: 程序异常了,退出时收到了信号,返回值就是对应的信号编号
    40. * 返回值 == 0: 正常运行完毕的,结果保存到了对应的临时文件中
    41. * 返回值 < 0: 内部错误
    42. *
    43. * cpu_limit: 该程序运行的时候,可以使用的最大cpu资源上限
    44. * mem_limit: 改程序运行的时候,可以使用的最大的内存大小(KB)
    45. * *****************************************/
    46. static int Run(const std::string &file_name, int cpu_limit, int mem_limit)
    47. {
    48. /*********************************************
    49. * 程序运行:
    50. * 1. 代码跑完,结果正确
    51. * 2. 代码跑完,结果不正确
    52. * 3. 代码没跑完,异常了
    53. * Run需要考虑代码跑完,结果正确与否吗??不考虑!
    54. * 结果正确与否:是由我们的测试用例决定的!
    55. * 我们只考虑:是否正确运行完毕
    56. *
    57. * 我们必须知道可执行程序是谁?
    58. * 一个程序在默认启动的时候
    59. * 标准输入: 不处理
    60. * 标准输出: 程序运行完成,输出结果是什么
    61. * 标准错误: 运行时错误信息
    62. * *******************************************/
    63. std::string _execute = PathUtil::Exe(file_name); // 可执行程序名
    64. std::string _stdin = PathUtil::Stdin(file_name); // 标准输入
    65. std::string _stdout = PathUtil::Stdout(file_name); // 标准输出
    66. std::string _stderr = PathUtil::Stderr(file_name); // 标准错误
    67. umask(0);
    68. int _stdin_fd = open(_stdin.c_str(), O_CREAT | O_RDONLY, 0644);
    69. int _stdout_fd = open(_stdout.c_str(), O_CREAT | O_WRONLY, 0644);
    70. int _stderr_fd = open(_stderr.c_str(), O_CREAT | O_WRONLY, 0644);
    71. if (_stdin_fd < 0 || _stdout_fd < 0 || _stderr_fd < 0)
    72. {
    73. LOG(ERROR) << "运行时打开标准文件失败"
    74. << "\n";
    75. return -1; // 代表打开文件失败
    76. }
    77. pid_t pid = fork();
    78. if (pid < 0)
    79. {
    80. LOG(ERROR) << "运行时创建子进程失败"
    81. << "\n";
    82. close(_stdin_fd);
    83. close(_stdout_fd);
    84. close(_stderr_fd);
    85. return -2; // 代表创建子进程失败
    86. }
    87. else if (pid == 0)
    88. {
    89. dup2(_stdin_fd, 0);
    90. dup2(_stdout_fd, 1);
    91. dup2(_stderr_fd, 2);
    92. SetProcLimit(cpu_limit, mem_limit);//⭐
    93. execl(_execute.c_str() /*我要执行谁*/, _execute.c_str() /*我想在命令行上如何执行该程序*/, nullptr);
    94. exit(1);
    95. }
    96. else
    97. {
    98. close(_stdin_fd);
    99. close(_stdout_fd);
    100. close(_stderr_fd);
    101. int status = 0;
    102. waitpid(pid, &status, 0);
    103. // 程序运行异常,一定是因为因为收到了信号!
    104. LOG(INFO) << "运行完毕, info: " << (status & 0x7F) << "\n";
    105. return status & 0x7F;
    106. }
    107. }
    108. };
    109. }
    编译并运行 compile_run 模块

    通常我们的code.cpp并不是直接写在我们的某个文件夹中的,需要我们从网络中获取,这就需要我们了解一点json相关的知识了

    安装json

    sudo yum install -y jsoncpp-devel

    使用json

    ✍ 代码示例

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. int main()
    8. {
    9. //序列化
    10. //Value是Json的一个中间类,可以填充k,v对象
    11. Json::Value root;
    12. root["value"]="mycode";
    13. root["user"]="hty";
    14. root["age"]="22";
    15. // Json::StyledWriter writer;
    16. //反序列化
    17. Json::FastWriter writer;
    18. std::string str=writer.write(root);
    19. std::cout<
    20. return 0;
    21. }

    运行结果

    compile_run 模块

    分析

         ⭕输入:

                🐟 code: 用户提交的代码

                🐟 input: 用户给自己提交的代码对应的输入,不做处理

                🐟cpu_limit: 时间要求

                🐟 mem_limit: 空间要求

       

        ⭕输出:

            🐱 必填

                🐟status: 状态码

                🐟 reason: 请求结果

            🐱 选填:

                🐟stdout: 我的程序运行完的结果

                🐟stderr: 我的程序运行完的错误结果

    ✍compile_run.hpp代码
    1. #pragma once
    2. #include "compiler.hpp"
    3. #include "runner.hpp"
    4. #include "../comm/log.hpp"
    5. #include "../comm/util.hpp"
    6. #include
    7. #include
    8. #include
    9. namespace ns_compile_and_run
    10. {
    11. using namespace ns_log;
    12. using namespace ns_util;
    13. using namespace ns_compiler;
    14. using namespace ns_runner;
    15. class CompileAndRun
    16. {
    17. public:
    18. // code > 0 : 进程收到了信号导致异常奔溃
    19. // code < 0 : 整个过程非运行报错(代码为空,编译报错等)
    20. // code = 0 : 整个过程全部完成
    21. //待完善
    22. static std::string CodeToDesc(int code, const std::string &file_name)
    23. {
    24. std::string desc;
    25. switch (code)
    26. {
    27. case 0:
    28. desc = "编译运行成功";
    29. break;
    30. case -1:
    31. desc = "提交的代码是空";
    32. break;
    33. case -2:
    34. desc = "未知错误";
    35. break;
    36. case -3:
    37. // desc = "代码编译的时候发生了错误";
    38. FileUtil::ReadFile(PathUtil::CompilerError(file_name), &desc, true);
    39. break;
    40. case SIGABRT: // 6
    41. desc = "内存超过范围";
    42. break;
    43. case SIGXCPU: // 24
    44. desc = "CPU使用超时";
    45. break;
    46. case SIGFPE: // 8
    47. desc = "浮点数溢出";
    48. break;
    49. default:
    50. desc = "未知: " + std::to_string(code);
    51. break;
    52. }
    53. return desc;
    54. }
    55. /***************************************
    56. * 输入:
    57. * code: 用户提交的代码
    58. * input: 用户给自己提交的代码对应的输入,不做处理
    59. * cpu_limit: 时间要求
    60. * mem_limit: 空间要求
    61. *
    62. * 输出:
    63. * 必填
    64. * status: 状态码
    65. * reason: 请求结果
    66. * 选填:
    67. * stdout: 我的程序运行完的结果
    68. * stderr: 我的程序运行完的错误结果
    69. *
    70. * 参数:
    71. * in_json: {"code": "#include...", "input": "","cpu_limit":1, "mem_limit":10240}
    72. * out_json: {"status":"0", "reason":"","stdout":"","stderr":"",}
    73. * ************************************/
    74. static void Start(const std::string &in_json, std::string *out_json)
    75. {
    76. Json::Value in_value;
    77. Json::Reader reader;
    78. reader.parse(in_json, in_value); //最后在处理差错问题
    79. std::string code = in_value["code"].asString();
    80. std::string input = in_value["input"].asString();
    81. int cpu_limit = in_value["cpu_limit"].asInt();
    82. int mem_limit = in_value["mem_limit"].asInt();
    83. int status_code = 0;
    84. Json::Value out_value;//返回数据
    85. int run_result = 0;
    86. std::string file_name; //需要内部形成的唯一文件名
    87. if (code.size() == 0)
    88. {
    89. status_code = -1; //代码为空
    90. goto END;
    91. }
    92. // 形成的文件名只具有唯一性,没有目录没有后缀
    93. // 毫秒级时间戳+原子性递增唯一值: 来保证唯一性
    94. file_name = FileUtil::UniqFileName();
    95. //形成临时src文件
    96. if (!FileUtil::WriteFile(PathUtil::Src(file_name), code))
    97. {
    98. status_code = -2; //未知错误
    99. goto END;
    100. }
    101. if (!Compiler::Compile(file_name))
    102. {
    103. //编译失败
    104. status_code = -3; //代码编译的时候发生了错误
    105. goto END;
    106. }
    107. run_result = Runner::Run(file_name, cpu_limit, mem_limit);
    108. if (run_result < 0)
    109. {
    110. status_code = -2; //未知错误
    111. }
    112. else if (run_result > 0)
    113. {
    114. //程序运行崩溃了
    115. status_code = run_result;
    116. }
    117. else
    118. {
    119. //运行成功
    120. status_code = 0;
    121. }
    122. END:
    123. out_value["status"] = status_code;
    124. out_value["reason"] = CodeToDesc(status_code, file_name);
    125. if (status_code == 0)
    126. {
    127. // 整个过程全部成功
    128. std::string _stdout;
    129. FileUtil::ReadFile(PathUtil::Stdout(file_name), &_stdout, true);
    130. out_value["stdout"] = _stdout;
    131. std::string _stderr;
    132. FileUtil::ReadFile(PathUtil::Stderr(file_name), &_stderr, true);
    133. out_value["stderr"] = _stderr;
    134. }
    135. Json::StyledWriter writer;
    136. *out_json = writer.write(out_value);
    137. }
    138. };
    139. }
    测试
    ✍代码示例
    1. #include "compile_run.hpp"
    2. using namespace ns_compiler;
    3. using namespace ns_runner;
    4. using namespace ns_compile_and_run;
    5. int main(int argc, char *argv[])
    6. {
    7. //通过Http 让client给我们上传一个json string
    8. std::string in_json;
    9. Json::Value in_value;
    10. in_value["code"]=R"(#include
    11. int main(){
    12. std::cout << "你可以看见我了" << std::endl;
    13. //aaaaaaaa
    14. return 0;
    15. })";
    16. in_value["input"]="";
    17. in_value["cpu_cimit"]=1;
    18. in_value["mem_limit"]=10240*3;
    19. Json::FastWriter writer;
    20. in_json=writer.write(in_value);
    21. std::cout<
    22. // CompileAndRun::Start();
    23. return 0;
    24. }
    📕 输出结果1.0:

    📕输出结果2.0:(源文件里有错误的代码)

    添加清理功能

    1. static void RemoveTempFile(const std::string &file_name)
    2. {
    3. //清理文件的个数是不确定的,但是有哪些我们是知道的
    4. std::string _src = PathUtil::Src(file_name);
    5. if(FileUtil::IsFileExists(_src)) unlink(_src.c_str());
    6. std::string _compiler_error = PathUtil::CompilerError(file_name);
    7. if(FileUtil::IsFileExists(_compiler_error)) unlink(_compiler_error.c_str());
    8. std::string _execute = PathUtil::Exe(file_name);
    9. if(FileUtil::IsFileExists(_execute)) unlink(_execute.c_str());
    10. std::string _stdin = PathUtil::Stdin(file_name);
    11. if(FileUtil::IsFileExists(_stdin)) unlink(_stdin.c_str());
    12. std::string _stdout = PathUtil::Stdout(file_name);
    13. if(FileUtil::IsFileExists(_stdout)) unlink(_stdout.c_str());
    14. std::string _stderr = PathUtil::Stderr(file_name);
    15. if(FileUtil::IsFileExists(_stderr)) unlink(_stderr.c_str());
    16. }

    将函数调用添加到END部分即可,为了观察现象比较明显,暂时屏蔽掉

    第三个功能: 把编译并运行功能,形成网络服务

    引入httplib第三方库
    最新的cpp-httplib在使用的时候,如果gcc不是特别新的话有可能会有运行时错误的问题
    建议:cpp-httplib 0.7.15
    下载zip安装包,上传到服务器即可
    cpp-httplib gitee链接:https://gitee.com/yuanfeng1897/cpp-httplib?_from=gitee_search
    v0.7.15版本链接: https://gitee.com/yuanfeng1897/cpp-httplib/tree/v0.7.15
    httplib.h拷贝到我们的项目中即可,就这么简单(可以用vscode打开笨的下载好的文件夹,直接将文件夹拷贝到云服务器上,最好是单的的目录下,再拷贝对应的httplib.h文件到comm目录下)

    另外,使用这个文件需要将gcc进行升级

    升级 gcc
    百度搜索: scl gcc devsettool 升级 gcc
    安装 scl
    $ sudo yum install centos-release-scl scl-utils-build
    安装新版本 gcc ,这里也可以把 7 换成 8 或者 9 ,我用的是 9 ,也可以都安装
    1. $ sudo yum install -y devtoolset-7-gcc devtoolset-7-gcc-c++
    2. $ ls /opt/rh/

    启动: 细节,命令行启动只能在本会话有效

    1. $ scl enable devtoolset-7 bash
    2. $ gcc -v

    可选:如果想每次登陆的时候,都是较新的gcc,需要把上面的命令添加到你的~/.bash_profile

    1. $ cat ~/.bash_profile
    2. # .bash_profile
    3. # Get the aliases and functions
    4. if [ -f ~/.bashrc ]; then
    5. . ~/.bashrc
    添加下面的命令,每次启动的时候,都会执行这个scl命令
    1. scl enable devtoolset-7 bash
    2. or
    3. scl enable devtoolset-8 bash
    4. or
    5. scl enable devtoolset-9 bash

    获得下面的结果gcc 就升级好啦 🌹🌹🌹🌹🌹

    httplib文件测试

    ✍ 代码示例

    1. #include "compile_run.hpp"
    2. #include"../comm/httplib.h"
    3. using namespace httplib;
    4. using namespace ns_compiler;
    5. using namespace ns_runner;
    6. using namespace ns_compile_and_run;
    7. int main(int argc, char *argv[])
    8. {
    9. //使用cpp-httplib
    10. Server svr;
    11. svr.Get("/hello",[](const Request &req, Response &resp){
    12. // 用来进行基本测试
    13. resp.set_content("hello httplib,你好 httplib!", "text/plain;charset=utf-8");
    14. });
    15. svr.listen("0.0.0.0",8080);
    16. return 0;
    17. }

    📕 输出结果

    ✍代码示例2

    wwroot目录下的index.html文件(仅作测试使用)

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>测试title>
    6. head>
    7. <body>
    8. <p>这是一个段落。p>
    9. <p>这是一个段落。p>
    10. <p>这是一个段落。p>
    11. body>
    12. html>

    compile_server.cc

    1. #include "compile_run.hpp"
    2. #include"../comm/httplib.h"
    3. using namespace httplib;
    4. using namespace ns_compiler;
    5. using namespace ns_runner;
    6. using namespace ns_compile_and_run;
    7. int main(int argc, char *argv[])
    8. {
    9. //使用cpp-httplib
    10. Server svr;
    11. svr.Get("/hello",[](const Request &req, Response &resp){
    12. // 用来进行基本测试
    13. resp.set_content("hello httplib,你好 httplib!", "text/plain;charset=utf-8");
    14. });
    15. svr.set_base_dir("./wwwroot");
    16. svr.listen("0.0.0.0",8080);
    17. return 0;
    18. }

    📕输出结果

    用postman软件进行测试

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

    🌼🌼🌼 本质:建立一个小型网站 🌼🌼🌼
    ⭕ 1. 获取首页,用题目列表充当
    ⭕ 2. 编辑区域页面
    ⭕ 3. 提交判题功能(编译并运行)
    🐟 M: Model,通常是和数据交互的模块,比如,对题库进行增删改查(文件版,MySQL)
    🐟 V: view, 通常是拿到数据之后,要进行构建网页,渲染网页内容,展示给用户的(浏览器)
    🐟 C: control, 控制器,就是我们的核心业务逻辑

    ✍第一个功能:用户请求的服务路由功能

    1. #include
    2. #include
    3. #include "../comm/httplib.h"
    4. #include "oj_control.hpp"
    5. using namespace httplib;
    6. using namespace ns_control;
    7. static Control *ctrl_ptr = nullptr;
    8. void Recovery(int signo)
    9. {
    10. ctrl_ptr->RecoveryMachine();
    11. }
    12. int main()
    13. {
    14. signal(SIGQUIT, Recovery);
    15. //用户请求的服务路由功能
    16. Server svr;
    17. Control ctrl;
    18. ctrl_ptr = &ctrl;
    19. // 获取所有的题目列表
    20. svr.Get("/all_questions", [&ctrl](const Request &req, Response &resp){
    21. //返回一张包含有所有题目的html网页
    22. std::string html;
    23. ctrl.AllQuestions(&html);
    24. //用户看到的是什么呢??网页数据 + 拼上了题目相关的数据
    25. resp.set_content(html, "text/html; charset=utf-8");
    26. });
    27. // 用户要根据题目编号,获取题目的内容
    28. // /question/100 -> 正则匹配
    29. // R"()", 原始字符串raw string,保持字符串内容的原貌,不用做相关的转义
    30. svr.Get(R"(/question/(\d+))", [&ctrl](const Request &req, Response &resp){
    31. std::string number = req.matches[1];
    32. std::string html;
    33. ctrl.Question(number, &html);
    34. resp.set_content(html, "text/html; charset=utf-8");
    35. });
    36. // 用户提交代码,使用我们的判题功能(1. 每道题的测试用例 2. compile_and_run)
    37. svr.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &resp){
    38. std::string number = req.matches[1];
    39. std::string result_json;
    40. ctrl.Judge(number, req.body, &result_json);
    41. resp.set_content(result_json, "application/json;charset=utf-8");
    42. // resp.set_content("指定题目的判题: " + number, "text/plain; charset=utf-8");
    43. });
    44. svr.set_base_dir("./wwwroot");
    45. svr.listen("0.0.0.0", 8080);
    46. return 0;
    47. }

    第二个功能:题目设计

    📕分析

    ⭕   题目的编号
    ⭕   题目的标题
    ⭕   题目的难度
    ⭕   题目的描述 , 题面
    ⭕   时间要求 ( 内部处理 )
    ⭕   空间要求 ( 内部处理 )
    🐱两批文件构成
            🐟 第一个: questions . list : 题目列表(不需要题目的内容)
            🐟 第二个:题目的描述,题目的预设置代码 ( header . cpp ), 测试用例代码 ( tail . cpp )

    测试用例的设计详细参见  测试用例设计    

    更多测试用例的编辑可以参考 题目以及测试用例

    boost库引入(用于分割字符串)

    安装boost库 🆒

    sudo yum install -y boost-devel //是boost 开发库

    ✍小实验

    1. #include
    2. #include
    3. #include
    4. int main()
    5. {
    6. std::vector tokens;
    7. const std::string str = "1 判断回文数 简单 30000";
    8. const std::string sep = " ";
    9. // boost::split(tokens,str,boost::is_any_of(sep),boost::algorithm::token_compress_off);
    10. boost::split(tokens, str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
    11. for (auto &iter : tokens)
    12. {
    13. std::cout << iter << std::endl;
    14. }
    15. return 0;
    16. }

    📕 运行结果

    项目中的具体使用

    第三个功能:control,逻辑控制模块

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include "../comm/util.hpp"
    11. #include "../comm/log.hpp"
    12. #include "../comm/httplib.h"
    13. #include "oj_model.hpp"
    14. #include "oj_view.hpp"
    15. namespace ns_control
    16. {
    17. using namespace std;
    18. using namespace ns_log;
    19. using namespace ns_util;
    20. using namespace ns_model;
    21. using namespace ns_view;
    22. using namespace httplib;
    23. // 提供服务的主机
    24. class Machine
    25. {
    26. public:
    27. std::string ip; // 编译服务的ip
    28. int port; // 编译服务的port
    29. uint64_t load; // 编译服务的负载
    30. std::mutex *mtx; // mutex禁止拷贝的,使用指针
    31. public:
    32. Machine() : ip(""), port(0), load(0), mtx(nullptr)
    33. {
    34. }
    35. ~Machine()
    36. {
    37. }
    38. public:
    39. // 提升主机负载
    40. void IncLoad()
    41. {
    42. if (mtx)
    43. mtx->lock();
    44. ++load;
    45. if (mtx)
    46. mtx->unlock();
    47. }
    48. // 减少主机负载
    49. void DecLoad()
    50. {
    51. if (mtx)
    52. mtx->lock();
    53. --load;
    54. if (mtx)
    55. mtx->unlock();
    56. }
    57. void ResetLoad()
    58. {
    59. if (mtx)
    60. mtx->lock();
    61. load = 0;
    62. if (mtx)
    63. mtx->unlock();
    64. }
    65. // 获取主机负载,没有太大的意义,只是为了统一接口
    66. uint64_t Load()
    67. {
    68. uint64_t _load = 0;
    69. if (mtx)
    70. mtx->lock();
    71. _load = load;
    72. if (mtx)
    73. mtx->unlock();
    74. return _load;
    75. }
    76. };
    77. const std::string service_machine = "./conf/service_machine.conf";
    78. // 负载均衡模块
    79. class LoadBlance
    80. {
    81. private:
    82. // 可以给我们提供编译服务的所有的主机
    83. // 每一台主机都有自己的下标,充当当前主机的id
    84. std::vector machines;
    85. // 所有在线的主机id
    86. std::vector<int> online;
    87. // 所有离线的主机id
    88. std::vector<int> offline;
    89. // 保证LoadBlance它的数据安全
    90. std::mutex mtx;
    91. public:
    92. LoadBlance()
    93. {
    94. // assert(LoadConf(service_machine));
    95. LOG(INFO) << "加载 " << service_machine << " 成功"
    96. << "\n";
    97. }
    98. ~LoadBlance()
    99. {
    100. }
    101. public:
    102. bool LoadConf(const std::string &machine_conf)
    103. {
    104. std::ifstream in(machine_conf);
    105. if (!in.is_open())
    106. {
    107. LOG(FATAL) << " 加载: " << machine_conf << " 失败"
    108. << "\n";
    109. return false;
    110. }
    111. std::string line;
    112. while (std::getline(in, line))
    113. {
    114. std::vector tokens;
    115. StringUtil::SplitString(line, &tokens, ":");
    116. if (tokens.size() != 2)
    117. {
    118. LOG(WARNING) << " 切分 " << line << " 失败"
    119. << "\n";
    120. continue;
    121. }
    122. Machine m;
    123. m.ip = tokens[0];
    124. m.port = atoi(tokens[1].c_str());
    125. m.load = 0;
    126. m.mtx = new std::mutex();
    127. online.push_back(machines.size());
    128. machines.push_back(m);
    129. }
    130. in.close();
    131. return true;
    132. }
    133. // id: 输出型参数
    134. // m : 输出型参数
    135. bool SmartChoice(int *id, Machine **m)
    136. {
    137. // 1. 使用选择好的主机(更新该主机的负载)
    138. // 2. 我们需要可能离线该主机
    139. mtx.lock();
    140. // 负载均衡的算法
    141. // 1. 随机数+hash
    142. // 2. 轮询+hash
    143. int online_num = online.size();
    144. if (online_num == 0)
    145. {
    146. mtx.unlock();
    147. LOG(FATAL) << " 所有的后端编译主机已经离线, 请运维的同事尽快查看"
    148. << "\n";
    149. return false;
    150. }
    151. // 通过遍历的方式,找到所有负载最小的机器
    152. *id = online[0];
    153. *m = &machines[online[0]];
    154. uint64_t min_load = machines[online[0]].Load();
    155. for (int i = 1; i < online_num; i++)
    156. {
    157. uint64_t curr_load = machines[online[i]].Load();
    158. if (min_load > curr_load)
    159. {
    160. min_load = curr_load;
    161. *id = online[i];
    162. *m = &machines[online[i]];
    163. }
    164. }
    165. mtx.unlock();
    166. return true;
    167. }
    168. void OfflineMachine(int which)
    169. {
    170. mtx.lock();
    171. for (auto iter = online.begin(); iter != online.end(); iter++)
    172. {
    173. if (*iter == which)
    174. {
    175. machines[which].ResetLoad();
    176. // 要离线的主机已经找到啦
    177. online.erase(iter);
    178. offline.push_back(which);
    179. break; // 因为break的存在,所有我们暂时不考虑迭代器失效的问题
    180. }
    181. }
    182. mtx.unlock();
    183. }
    184. void OnlineMachine()
    185. {
    186. // 我们统一上线,后面统一解决
    187. mtx.lock();
    188. online.insert(online.end(), offline.begin(), offline.end());
    189. offline.erase(offline.begin(), offline.end());
    190. mtx.unlock();
    191. LOG(INFO) << "所有的主机有上线啦!"
    192. << "\n";
    193. }
    194. // for test
    195. void ShowMachines()
    196. {
    197. mtx.lock();
    198. std::cout << "当前在线主机列表: ";
    199. for (auto &id : online)
    200. {
    201. std::cout << id << " ";
    202. }
    203. std::cout << std::endl;
    204. std::cout << "当前离线主机列表: ";
    205. for (auto &id : offline)
    206. {
    207. std::cout << id << " ";
    208. }
    209. std::cout << std::endl;
    210. mtx.unlock();
    211. }
    212. };
    213. // 这是我们的核心业务逻辑的控制器
    214. class Control
    215. {
    216. private:
    217. Model model_; // 提供后台数据
    218. View view_; // 提供html渲染功能
    219. LoadBlance load_blance_; // 核心负载均衡器
    220. public:
    221. Control()
    222. {
    223. }
    224. ~Control()
    225. {
    226. }
    227. public:
    228. void RecoveryMachine()
    229. {
    230. load_blance_.OnlineMachine();
    231. }
    232. // 根据题目数据构建网页
    233. // html: 输出型参数
    234. bool AllQuestions(string *html)
    235. {
    236. bool ret = true;
    237. vector<struct Question> all;
    238. if (model_.GetAllQuestions(&all))
    239. {
    240. sort(all.begin(), all.end(), [](const struct Question &q1, const struct Question &q2)
    241. { return atoi(q1.number.c_str()) < atoi(q2.number.c_str()); });
    242. // 获取题目信息成功,将所有的题目数据构建成网页
    243. view_.AllExpandHtml(all, html);
    244. }
    245. else
    246. {
    247. *html = "获取题目失败, 形成题目列表失败";
    248. ret = false;
    249. }
    250. return ret;
    251. }
    252. bool Question(const string &number, string *html)
    253. {
    254. bool ret = true;
    255. struct Question q;
    256. if (model_.GetOneQuestion(number, &q))
    257. {
    258. // 获取指定题目信息成功,将所有的题目数据构建成网页
    259. view_.OneExpandHtml(q, html);
    260. }
    261. else
    262. {
    263. *html = "指定题目: " + number + " 不存在!";
    264. ret = false;
    265. }
    266. return ret;
    267. }
    268. // code: #include...
    269. // input: ""
    270. void Judge(const std::string &number, const std::string in_json, std::string *out_json)
    271. {
    272. // LOG(DEBUG) << in_json << " \nnumber:" << number << "\n";
    273. // 0. 根据题目编号,直接拿到对应的题目细节
    274. struct Question q;
    275. model_.GetOneQuestion(number, &q);
    276. // 1. in_json进行反序列化,得到题目的id,得到用户提交源代码,input
    277. Json::Reader reader;
    278. Json::Value in_value;
    279. reader.parse(in_json, in_value);
    280. std::string code = in_value["code"].asString();
    281. // 2. 重新拼接用户代码+测试用例代码,形成新的代码
    282. Json::Value compile_value;
    283. compile_value["input"] = in_value["input"].asString();
    284. compile_value["code"] = code + "\n" + q.tail;
    285. compile_value["cpu_limit"] = q.cpu_limit;
    286. compile_value["mem_limit"] = q.mem_limit;
    287. Json::FastWriter writer;
    288. std::string compile_string = writer.write(compile_value);
    289. // 3. 选择负载最低的主机(差错处理)
    290. // 规则: 一直选择,直到主机可用,否则,就是全部挂掉
    291. while (true)
    292. {
    293. int id = 0;
    294. Machine *m = nullptr;
    295. if (!load_blance_.SmartChoice(&id, &m))
    296. {
    297. break;
    298. }
    299. // 4. 然后发起http请求,得到结果
    300. Client cli(m->ip, m->port);
    301. m->IncLoad();
    302. LOG(INFO) << " 选择主机成功, 主机id: " << id << " 详情: " << m->ip << ":" << m->port << " 当前主机的负载是: " << m->Load() << "\n";
    303. if (auto res = cli.Post("/compile_and_run", compile_string, "application/json;charset=utf-8"))
    304. {
    305. // 5. 将结果赋值给out_json
    306. if (res->status == 200)
    307. {
    308. *out_json = res->body;
    309. m->DecLoad();
    310. LOG(INFO) << "请求编译和运行服务成功..."
    311. << "\n";
    312. break;
    313. }
    314. m->DecLoad();
    315. }
    316. else
    317. {
    318. // 请求失败
    319. LOG(ERROR) << " 当前请求的主机id: " << id << " 详情: " << m->ip << ":" << m->port << " 可能已经离线"
    320. << "\n";
    321. load_blance_.OfflineMachine(id);
    322. load_blance_.ShowMachines(); // 仅仅是为了用来调试
    323. }
    324. }
    325. }
    326. };
    327. }

    附加功能:需要有数据渲染
    小实验✍
    代码示例
    1. //git clone https://hub.fastgit.xyz/OlafvdSpek/ctemplate
    2. #include
    3. #include
    4. #include
    5. int main()
    6. {
    7. std::string in_html="./test.html";
    8. std::string value="学习";
    9. //形成数据字典
    10. ctemplate::TemplateDictionary root("test");
    11. root.SetValue("key",value);
    12. //获取被渲染网页对象
    13. ctemplate::Template *tpl=ctemplate::Template::GetTemplate(in_html,ctemplate::DO_NOT_STRIP);
    14. //添加字典数据到网页中
    15. std::string out_html;
    16. tpl->Expand(&out_html,&root);
    17. std::cout<
    18. return 0;
    19. }

    📕运行结果

    如果后续引入了ctemplate ,一旦对网页结构进行修改,尽量的每次想看到结果,将 server 重启一下。 ctemplate 有 自己的优化加速策略,可能在内存中存在缓存网页数据(old)
    ⭐ 当我们完成全部功能之后,需要注意:
    要给编译模块添加 —D 条件编译掉测试用例中的头文件 inclde

    前端页面设计请移步 首页设计  和     后续网页设计  

    🌼🌼🌼🌼🌼🌼🌼最终显示结果🌼🌼🌼🌼🌼🌼🌼

    ​项目介绍就到这里,谢谢你那么忙。还花时间看到这个,把花花送给这么支持我的你

  • 相关阅读:
    [微前端实战]---035react16-资讯,视频,视频详情
    mac inter 芯片遇到程序无法打开(无法验证开发者)
    【CentOS 】DHCP 更改为静态 IP 地址并且遇到无法联网
    C语言程序设计 复习总结[持续更新ing]
    微软首款AI芯片代号“雅典娜”;马斯克四年内将让“星舰”上火星丨 RTE 开发者日报 Vol.61
    mac通过docker一键部署Jenkins
    数据分析基础之《jupyter notebook工具》
    笔试强训Day2
    苹果 AR/VR 头显售价 12000 元起,网友:“买来能干啥?”
    鸿鹄工程项目管理系统 Spring Cloud+Spring Boot+Mybatis+Vue+ElementUI+前后端分离构建工程项目管理系统
  • 原文地址:https://blog.csdn.net/qq_59293418/article/details/133090556