【负载均衡oj】(一)架构和公共模块

本文详细描述了一个实现在线做题系统的项目,包括通用头文件模块、后端服务的编译和运行服务,以及客户端的MVC架构。着重介绍了文件路径管理、编译时文件命名、唯一文件名生成以及文件操作的相关函数。
摘要由CSDN通过智能技术生成

一.目的

实现类似牛客网的在线做题,带有判题功能,总体分为三个模块,一个通用头文件模块,一个后端服务模块,用于编译客户的代码并测验数据。还有一个oj客户端,采用mvc架构,负责前后端交互,可以调用后端的编译功能并提供负载均衡选择,这样后端编译模块可以采用集群化部署,通过负载均衡选择负载最低的后端进行业务逻辑。

编译服务在细化分为,编译服务,运行服务,后端对上层提供的接口,后端编译的网络服务

二.公共模块编写

PathUtil:形成产生的临时文件名称

编译时产生的共有三个文件名,**.cpp,**.exe,**.stderr,分别是源文件,可执行文件,标准错误。(编译条件下)我们默认把所有的临时文件都放在temp目录下。

static const std::string temp_path = "./temp/";

    class PathUtil
    {
        // 编译时需要有文件产生,不同文件存放在不同的绝对路径
        // 构建源文件路径+后缀的完整文件名
        // 1234 -> ./temp/1234.cpp
    public:
        static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
        {
            std::string path_name = temp_path;
            path_name += file_name;
            path_name += suffix;
            return path_name;
        }
        // 编译时所需路径
        // 生成可运行文件路径
        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }

        // 生成可执行文件路径
        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name, ".exe");
        }

        // 生成错误日志
        static std::string CompilerError(const std::string &file_name)
        {
            return AddSuffix(file_name, ".compile_error");
        }

        // 运行时所需路径
        // 输入
        static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }

        // 输出
        static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }

        // 错误
        static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };

运行状态下需要的文件名对应是,输入,输出,和错误(3个),注意编译时报错和运行时报错文件的区分

判断文件是否存在

stat用于获取文件属性,输出型参数可以获取到文件时间,indoe等参数,返回值为0意味着获取成功,返回值-1获取失败。

 static bool IsFileExists(const std::string path_name)
        {
            // 通过stat判断状态文件是否存在
            struct stat st;
            if (stat(path_name.c_str(), &st) == 0)
            {
                // 文件存在
                return true;
            }
            return false;
        }

UniqFileName:生成唯一文件名

通过毫秒级时间戳和原子性递增实现

获取毫秒级时间:

// 获取时间戳毫秒秒级别
        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);

            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000); // 秒+微秒转换为毫秒
        }

生成文件名:

 static std::string UniqFileName() // 获取唯一的对象名
        {
            static std::atomic_uint id(0); // 唯一标识符
            id++;
            std::string uniq_id = to_string(id);
            std::string ms = TimeUtil::GetTimeMs();
            return ms + "_" + uniq_id; // 时间戳加原子性标识符保证唯一性
        }

根据文件名写入或者读取:

// 将代码写到指定文件
        static bool WriteFile(const std::string &filename, const std::string code)
        {
            std::ofstream out(filename); // 打开文件流(写操作)或者用fstream+open的选项
            if (!out.is_open())          // 判断打开是否正常
            {
                return false;
            }
            out.write(code.c_str(), code.size());
            out.close();
            return true;
        }

        // 将代买从指定文件上读出来,并根据选项判断知否保留格式
        static bool ReadFile(const std::string &filename, std::string *content, bool keep)
        {
            (*content).clear();
            std::ifstream in(filename); // 打开文件流(读操作)
            if (!in.is_open())          // 判断打开是否正常
            {
                return false;
            }
            std::string line;
            // getline:不保存行分割符,有些时候需要保留\n,
            // getline内部重载了强制类型转化,返回值可以当作bool类型使用
            while (std::getline(in, line))
            {
                *content += line; // 手动控制换行符
                *content += (keep ? "\n" : "");
            }
            in.close();
            return true;
        }

字符串分割:

class StringUtil
    {
    public: // 分割字符串,输出型参数
        static void SplitString(const std::string &str, std::vector<std::string> *target, const std::string &sep)
        {
                                    //分隔符                是否压缩分隔符
            boost::split(*target,str,boost::is_any_of(sep),boost::algorithm::token_compress_on);
        }
    };

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值