`

OpenRTMFP/Cumulus Primer(5)CumulusServer启动流程分析(续1)

 
阅读更多

OpenRTMFP/Cumulus Primer(5)CumulusServer启动流程分析(续1)

  • 作者:柳大·Poechant(钟超)
  • 博客:Blog.CSDN.net/Poechant
  • 邮箱:zhongchao.ustc#gmail.com (# -> @)
  • 日期:April 14th, 2012

1 CumulusServer 是 ServerApplication 的子类

ServerApplication 对其子类有如下要求:

  • Subsystems must be registered in the constructor.
  • All non-trivial initializations must be made in the initialize() method.
  • At the end of the main() method, waitForTerminationRequest() should be called.

2 ServerApplication 是 Application 的子类

Application 对其子类的要求是,如下这些成员函数必须被覆盖:

  • initialize() (the one-argument, protected variant):上一篇已介绍过。
  • uninitialize():下面会介绍,Application 的 run() 函数会在调用 main() 函数后调用 uninitialize() 函数。
  • reinitialize()
  • defineOptions():定义命令行启动选项。
  • handleOption():响应相应的命令行选项。
  • main():

3 反初始化

CumulusServer是继承ServerApplication的,ServerApplication是继承Application的。Applicationrun()函数会先调用initialize(),然后调用main(),最后调用uninitialize。最后这个反初始化过程,在CumulusServer就是直接调用父类的uninitialize函数。

void uninitialize() {
    ServerApplication::uninitialize();
}

4 命令行选项设定

CumulusServer的命令行选项有:log(l)、dump(d)、cirrus(c)、middle(m)、help(h)。

void defineOptions(OptionSet& options) {
    ServerApplication::defineOptions(options);

设定日志级别(0 - 8,默认是 6,表示 info 级别)。

    options.addOption(
        Option("log", "l", "Log level argument, must be beetween 0 and 8 : \
            nothing, fatal, critic, error, warn, note, info, debug, trace. \
            Default value is 6 (info), all logs until info level are displayed.")
                .required(false)
                .argument("level")
                .repeatable(false));

其他一些选项:

    options.addOption(
        Option("dump", "d", "Enables packet traces in logs. Optional arguments \
            are 'middle' or 'all' respectively to displays just middle packet \
            process or all packet process. If no argument is given, just outside \
            packet process will be dumped.",false,"middle|all",false)
                .repeatable(false));

    options.addOption(
        Option("cirrus", "c", "Cirrus address to activate a 'man-in-the-middle' \
            developer mode in bypassing flash packets to the official cirrus \
            server of your choice, it's a instable mode to help Cumulus developers, \
            \"p2p.rtmfp.net:10000\" for example. By adding the 'dump' argument, \
            you will able to display Cirrus/Flash packet exchange in your logs \
            (see 'dump' argument).",false,"address",true)
                .repeatable(false));

    options.addOption(
        Option("middle", "m","Enables a 'man-in-the-middle' developer mode \
            between two peers. It's a instable mode to help Cumulus developers. \
            By adding the 'dump' argument, you will able to display Flash/Flash \
            packet exchange in your logs (see 'dump' argument).")
                .repeatable(false));

显示帮助信息的选项:

    options.addOption(
        Option("help", "h", "Displays help information about command-line usage.")
            .required(false)
            .repeatable(false));
}

OptionSetPoco::Util::OptionSet,调用addOption可以向其中增加选项Option。其中requiredrepeatable表示:

  • Sets whether the option is required (flag == true) or optional (flag == false).
  • Returns true if the option can be specified more than once, or false if at most once.

当需要显示帮助信息时,调用如下函数:

void displayHelp() {
    HelpFormatter helpFormatter(options());
    helpFormatter.setCommand(commandName());
    helpFormatter.setUsage("OPTIONS");
    helpFormatter.setHeader("CumulusServer, open source RTMFP server");
    helpFormatter.format(cout);
}
  • setCommand(): Sets the command name.
  • setUsage(): Sets the usage string.
  • setHeader(): Sets the header string.
  • format(): Writes the formatted help text to the given stream.

5 处理命令行选项

参数是选项名和选项值。

void handleOption(const std::string& name, const std::string& value) {
    ServerApplication::handleOption(name, value);

如果选项是帮助:

    if (name == "help")
        _helpRequested = true;

如果是cirrus,即该服务的 IP 和端口号,Poco::URI 中有协议名(Scheme)、IP 地址(Host)、端口号(Port)、查询串(Query)等等。

    else if (name == "cirrus") {
        try {
            URI uri("rtmfp://"+value);
            _pCirrus = new SocketAddress(uri.getHost(),uri.getPort());
            NOTE("Mode 'man in the middle' : the exchange will bypass to '%s'",value.c_str());
        } catch(Exception& ex) {
            ERROR("Mode 'man in the middle' error : %s",ex.message().c_str());
        }

如果选项是dump日志:

    } else if (name == "dump") {
        if(value == "all")
            Logs::SetDump(Logs::ALL);
        else if(value == "middle")
            Logs::SetDump(Logs::MIDDLE);
        else
            Logs::SetDump(Logs::EXTERNAL);

如果选项是middle

    } else if (name == "middle")
        _middle = true;

如果选项是log,表示设定日志级别:

    else if (name == "log")
        Logs::SetLevel(atoi(value.c_str()));
}

6 Dump logs

先加一个作用域锁,然后再向日志流写数据。

void dumpHandler(const UInt8* data,UInt32 size) {
    ScopedLock<FastMutex> lock(_logMutex);
    cout.write((const char*)data, size);
    _logStream.write((const char*)data,size);
    manageLogFile();
}

调用 manageLogFile,主要做一些日志大小超出限制的处理。

void manageLogFile() {

先判断是否超过日志文件的大小上线,LOG_SIZE1000000字节(即约 1 MB)。

    if(_pLogFile->getSize() > LOG_SIZE) {
        _logStream.close();
        int num = 10;

打开新日志文件:

        File file(_logPath + "10");

如果该文件已经存在,则先删除:

        if (file.exists())
            file.remove();

        while (--num >= 0) {
            file = _logPath + NumberFormatter::format(num);
            if (file.exists())
                file.renameTo(_logPath + NumberFormatter::format(num + 1));
        }
        _logStream.open(_pLogFile->path(), ios::in | ios::ate);
    }   
}

7 停止运行

CumulusServer继承了ApplicationKiller,该类中有纯虚函数kill()需要被实现,于是有:

void kill() {
    terminate();
}

ApplicationKiller的定义在ApplicationKiller.h中,如下:

class ApplicationKiller {
public:
    ApplicationKiller(){}
    virtual ~ApplicationKiller(){}

    virtual void kill()=0;
};

8 载入配置

initialize()函数中调用,上一篇已提到过。

void loadConfiguration(const string& path) {
    try {
        ServerApplication::loadConfiguration(path);
    } catch(...) {
    }
}

9 处理日志

void logHandler(Thread::TID threadId,
                const std::string& threadName,
                Priority priority,
                const char *filePath,
                long line, 
                const char *text) {

作用域锁:

    ScopedLock<FastMutex> lock(_logMutex);

    Path path(filePath);
    string file;
    if (path.getExtension() == "lua")
        file += path.directory(path.depth()-1) + "/";

如果是命令行交互模式(即不是 daemon 模式):

    if (_isInteractive)
        printf("%s  %s[%ld] %s\n",
            g_logPriorities[priority - 1],
            (file + path.getBaseName()).c_str(),
            line,
            text);

向日志流输出一句日志:

    _logStream << DateTimeFormatter::format(LocalDateTime(),"%d/%m %H:%M:%S.%c  ")
            << g_logPriorities[priority-1] 
            << '\t' << threadName 
            << '(' << threadId << ")\t"
            << (file + path.getFileName()) 
            << '[' << line << "]  " 
            << text << std::endl;

    _logStream.flush();

日志文件的善后处理(主要处理文件大小限制可能产生的问题):

    manageLogFile();
}

-

转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant

-

分享到:
评论

相关推荐

    OpenRTMFP Cumulus Primer()入门介绍与部署CumulusServer.pdf

    《OpenRTMFP Cumulus Primer 入门与CumulusServer部署》 OpenRTMFP (Real Time Media Flow Protocol) 是一种技术,它为Flash实时应用提供了高并发扩展能力。OpenRTMFP/Cumulus是基于GNU General Public License的...

    RTMFP服务器脚本CumulusServer.zip

    openrtmfp又名Cumulus Server是一个完全开源和跨平台的可扩展的RTMFP服务器脚本。Cumulus Server在GPL 框架下遵循速度、优势、跨平台、轻量和高质量代码。Cumulus Server的每一个版本都是通过严格测试和审核的。可...

    cumulus-linux-4.4.0-vx-amd64-qemu.zip

    1. **网络操作系统(NOS)**:Cumulus Linux提供了一个专门的网络操作系统,它支持标准的Linux命令行界面和开发工具,同时包含了网络特定的功能,如路由协议(BGP, OSPF等)、交换机配置和性能监控。 2. **开放网络...

    cumulus(浑天仪)使用手册

    Cumulus可能是基于Web的项目管理工具或者数据分析平台,而`swfobject.js`和`tagcloud.swf`这两个文件名暗示了它可能与Flash交互和数据可视化有关。 `swfobject.js`是一个JavaScript库,主要用于在网页中嵌入Adobe ...

    用Lua编写HelloWorld应用扩展CumulusServer源码

    1. **设置开发环境**:确保已经安装了CumulusServer和Lua解释器。在服务器配置文件中,指定Lua扩展的路径,以便服务器启动时加载。 2. **编写Lua脚本**:创建一个新的Lua文件,例如`hello_world.lua`。在这个文件中...

    wp-cumulus(支持中文标签)

    2. **易用性**:尽管拥有丰富的自定义选项,但`wp-cumulus`的基本安装和使用过程相当简单,适合新手和专业人士。 3. **多语言支持**:除了英文,`wp-cumulus`对中文的支持使得它在全球范围内具有更广泛的适用性。 ...

    Flex P2P 音视频流客户端(Cumulus支持)

    5. **错误处理和恢复**:如果在传输过程中出现错误,如网络中断,Cumulus会尝试恢复连接或者重新建立P2P链路。 6. **断开连接**:当通信结束时,客户端会关闭P2P连接,释放资源。 通过这样的机制,Flex P2P 音视频...

    HPCCloud:基于 CloudWeb 的仿真环境

    高性能计算云 目标 ...$ vi /opt/hpccloud/cumulus/cumulus/conf/config.json +- &gt; Fix host to be localhost +- &gt; baseUrl: " http://localhost:8080/api/v1 " , $ sudo service celeryd restar

    wp-cumulus

    在使用过程中,确保你的网站已经安装了Flash插件,因为"wp-cumulus" 在运行时需要依赖Flash支持。不过,随着HTML5技术的发展,一些现代版本可能提供了HTML5替代方案,以便在不支持Flash的设备上也能正常显示。 总的...

    Cumulus:一个基于网络编码的分布式文件系统.pdf

    5. **性能评估**:为了验证Cumulus 的性能,研究人员进行了实验比较,包括模拟网络环境下的数据恢复速度、存储效率和网络带宽利用率。实验结果表明,Cumulus 在保持或提高容错性的同时,显著降低了网络负载,并提升...

    cumulus-linux-cookbook:用于管理积云开关的手册

    访问 Cumulus HCL [1] 开关(Accton AS6701_32X,这就是这本食谱最初的目的) 属性 default [ :cumulus ] [ :dir ] = "/etc/cumulus" default [ :cumulus ] [ :model ] = "AS6701_32X" 用法 include_recipe ...

    wp-cumulus_3D云标签

    《WordPress中的wp-cumulus 3D云标签详解》 在WordPress这个强大的开源博客平台中,wp-cumulus是一款极具特色的3D云标签插件。它以其独特的视觉效果和交互体验,为用户提供了全新的标签展示方式,使得博客的分类和...

    Cumulus官网视频会议样例代码

    这里给出了cumulus/OpenRTMFP的git官网提到的视频会话样例的AS3代码,包括服务器端和客户端两部分,我已经在&lt;使用Cumulus和Flash Player搭建视频会议示例&gt;http://blog.csdn.net/tao_627/article/details/18041473中给...

    Cumulus:CloudApp 的开源替代品,利用您自己的 S3 进行存储

    积云Cumulus 是的免费、开源替代品,它利用您自己的 S3 进行存储。下载您可以在下载最新版本更改默认截图目录这是可选的。 默认情况下,OS X 会将屏幕截图放在您的桌面上。 但是,如果您希望他们去其他地方,您可以...

    cumulus:Cumulus框架+ Cumulus API

    积云框架 :open_book: 文献资料 最新文档。 文档。 更多信息 有关此项目的更多信息,以及有关NASA的地球观测系统数据和信息系统(EOSDIS)及其云工作的更多信息,请联系或访问 。...这是用于Cumulus开

    3D标签云单机中文版 WP-CUMULUS 1.23

    大名鼎鼎的WP-CUMULUS 3D标签云,已经改成支持中文标签,可在.htm自行添加标签链接,可以单机玩耍测试,不需要安装WordPress然后装插件~~ 主要是有些童鞋只是想要3D标签云动画,所以就提取重要文件出来稍作修改下.

    wp-cumulus.rar_TagCloud._TagCloud.as_flex cumul_wp cumulus_wp cu

    【标题】"wp-cumulus.rar" 是一个与WordPress相关的压缩包,其中包含"TagCloud._TagCloud.as",这表明它与创建和展示WordPress标签云的插件有关。"flex cumul_wp cumulus_wp cu"暗示这个插件可能是用Adobe Flex技术...

    cumulus-integration-tests:Cumulus工作流的集成测试[已弃用]

    Cumulus集成测试项目[已弃用] 注意:此存储库不再维护。 该存储库的内容已移至。 什么是积云? Cumulus是NASA未来地球科学数据流的基于云的数据提取,存档,分发和管理原型。 阅读 安装 nvm use npm install 在...

    Mellanox Cumulus 培训资源

    Mellanox Cumulus学习,基本使用,培训资源

    Python库 | cumulus-1.1.0.tar.gz

    或者,它可能包含了一些数据处理和分析的工具,帮助开发者快速处理大量数据。 Python库的版本管理也非常重要。版本号1.1.0意味着这是一个相对稳定的版本,相较于早期版本,它可能修复了一些已知问题,增加了新功能...

Global site tag (gtag.js) - Google Analytics