From c4feb8f6ed92cd329955728e04e27506fc3d1fb5 Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 9 Sep 2015 23:32:02 +0800 Subject: [PATCH] for #319, raw api support update all globals. --- trunk/src/app/srs_app_config.cpp | 166 +++++++++++++++++++++++++---- trunk/src/app/srs_app_config.hpp | 20 ++++ trunk/src/app/srs_app_http_api.cpp | 41 ++++++- trunk/src/app/srs_app_log.cpp | 11 +- trunk/src/app/srs_app_log.hpp | 3 + trunk/src/app/srs_app_reload.cpp | 5 + trunk/src/app/srs_app_reload.hpp | 1 + trunk/src/app/srs_app_utility.cpp | 5 + trunk/src/app/srs_app_utility.hpp | 5 + 9 files changed, 236 insertions(+), 21 deletions(-) diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 69decbdae..4ef39c62e 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -922,18 +922,6 @@ int SrsConfig::reload_conf(SrsConfig* conf) // chunk_size, ff_log_dir, // bandcheck, http_hooks, heartbeat, // security - - // merge config: max_connections - if (!srs_directive_equals(root->get("max_connections"), old_root->get("max_connections"))) { - for (it = subscribes.begin(); it != subscribes.end(); ++it) { - ISrsReloadHandler* subscribe = *it; - if ((ret = subscribe->on_reload_max_conns()) != ERROR_SUCCESS) { - srs_error("notify subscribes reload max_connections failed. ret=%d", ret); - return ret; - } - } - srs_trace("reload max_connections success."); - } // merge config: listen if (!srs_directive_equals(root->get("listen"), old_root->get("listen"))) { @@ -970,16 +958,25 @@ int SrsConfig::reload_conf(SrsConfig* conf) } } + // merge config: max_connections + if (!srs_directive_equals(root->get("max_connections"), old_root->get("max_connections"))) { + if ((ret = do_reload_max_connections()) != ERROR_SUCCESS) { + return ret; + } + } + + // merge config: utc_time + if (!srs_directive_equals(root->get("utc_time"), old_root->get("utc_time"))) { + if ((ret = do_reload_utc_time()) != ERROR_SUCCESS) { + return ret; + } + } + // merge config: pithy_print_ms if (!srs_directive_equals(root->get("pithy_print_ms"), old_root->get("pithy_print_ms"))) { - for (it = subscribes.begin(); it != subscribes.end(); ++it) { - ISrsReloadHandler* subscribe = *it; - if ((ret = subscribe->on_reload_pithy_print()) != ERROR_SUCCESS) { - srs_error("notify subscribes pithy_print_ms listen failed. ret=%d", ret); - return ret; - } + if ((ret = do_reload_pithy_print_ms()) != ERROR_SUCCESS) { + return ret; } - srs_trace("reload pithy_print_ms success."); } // merge config: http_api @@ -2376,6 +2373,81 @@ int SrsConfig::raw_set_srs_log_file(string srs_log_file, bool& applied) return ret; } +int SrsConfig::raw_set_max_connections(string max_connections, bool& applied) +{ + int ret = ERROR_SUCCESS; + + applied = false; + + + SrsConfDirective* conf = root->get_or_create("max_connections"); + + if (conf->arg0() == max_connections) { + return ret; + } + + conf->args.clear(); + conf->args.push_back(max_connections); + + if ((ret = do_reload_max_connections()) != ERROR_SUCCESS) { + return ret; + } + + applied = true; + + return ret; +} + +int SrsConfig::raw_set_utc_time(string utc_time, bool& applied) +{ + int ret = ERROR_SUCCESS; + + applied = false; + + + SrsConfDirective* conf = root->get_or_create("utc_time"); + + if (conf->arg0() == utc_time) { + return ret; + } + + conf->args.clear(); + conf->args.push_back(utc_time); + + if ((ret = do_reload_utc_time()) != ERROR_SUCCESS) { + return ret; + } + + applied = true; + + return ret; +} + +int SrsConfig::raw_set_pithy_print_ms(string pithy_print_ms, bool& applied) +{ + int ret = ERROR_SUCCESS; + + applied = false; + + + SrsConfDirective* conf = root->get_or_create("pithy_print_ms"); + + if (conf->arg0() == pithy_print_ms) { + return ret; + } + + conf->args.clear(); + conf->args.push_back(pithy_print_ms); + + if ((ret = do_reload_pithy_print_ms()) != ERROR_SUCCESS) { + return ret; + } + + applied = true; + + return ret; +} + int SrsConfig::do_reload_listen() { int ret = ERROR_SUCCESS; @@ -2461,6 +2533,57 @@ int SrsConfig::do_reload_srs_log_file() return ret; } +int SrsConfig::do_reload_max_connections() +{ + int ret = ERROR_SUCCESS; + + vector::iterator it; + for (it = subscribes.begin(); it != subscribes.end(); ++it) { + ISrsReloadHandler* subscribe = *it; + if ((ret = subscribe->on_reload_max_conns()) != ERROR_SUCCESS) { + srs_error("notify subscribes reload max_connections failed. ret=%d", ret); + return ret; + } + } + srs_trace("reload max_connections success."); + + return ret; +} + +int SrsConfig::do_reload_utc_time() +{ + int ret = ERROR_SUCCESS; + + vector::iterator it; + for (it = subscribes.begin(); it != subscribes.end(); ++it) { + ISrsReloadHandler* subscribe = *it; + if ((ret = subscribe->on_reload_utc_time()) != ERROR_SUCCESS) { + srs_error("notify subscribes utc_time failed. ret=%d", ret); + return ret; + } + } + srs_trace("reload utc_time success."); + + return ret; +} + +int SrsConfig::do_reload_pithy_print_ms() +{ + int ret = ERROR_SUCCESS; + + vector::iterator it; + for (it = subscribes.begin(); it != subscribes.end(); ++it) { + ISrsReloadHandler* subscribe = *it; + if ((ret = subscribe->on_reload_pithy_print()) != ERROR_SUCCESS) { + srs_error("notify subscribes pithy_print_ms failed. ret=%d", ret); + return ret; + } + } + srs_trace("reload pithy_print_ms success."); + + return ret; +} + string SrsConfig::config() { return config_file; @@ -5989,6 +6112,11 @@ bool srs_stream_caster_is_flv(string caster) return caster == "flv"; } +string srs_config_bool2switch(const string& sbool) +{ + return sbool == "true"? "on":"off"; +} + int srs_config_transform_vhost(SrsConfDirective* root) { int ret = ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 0742053a3..5f9056f24 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -361,12 +361,27 @@ public: * raw set the global log file path for file tank. */ virtual int raw_set_srs_log_file(std::string srs_log_file, bool& applied); + /** + * raw set the global max connections of srs. + */ + virtual int raw_set_max_connections(std::string max_connections, bool& applied); + /** + * raw set the global whether use utc time. + */ + virtual int raw_set_utc_time(std::string utc_time, bool& applied); + /** + * raw set the global pithy print interval in ms. + */ + virtual int raw_set_pithy_print_ms(std::string pithy_print_ms, bool& applied); private: virtual int do_reload_listen(); virtual int do_reload_pid(); virtual int do_reload_srs_log_tank(); virtual int do_reload_srs_log_level(); virtual int do_reload_srs_log_file(); + virtual int do_reload_max_connections(); + virtual int do_reload_utc_time(); + virtual int do_reload_pithy_print_ms(); public: /** * get the config file path. @@ -1278,6 +1293,11 @@ extern bool srs_stream_caster_is_udp(std::string caster); extern bool srs_stream_caster_is_rtsp(std::string caster); extern bool srs_stream_caster_is_flv(std::string caster); +/** + * convert bool in str to on/off + */ +extern std::string srs_config_bool2switch(const std::string& sbool); + /** * parse loaded vhost directives to compatible mode. * for exmaple, SRS1/2 use the follow refer style: diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index 3cfebeb22..cc5444e7f 100755 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -993,6 +993,9 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) // srs_log_tank file the tank to log, file or console. // srs_log_level trace the level of log, verbose, info, trace, warn, error. // srs_log_file ./objs/srs.log the log file when tank is file. + // max_connections 1000 the max connections of srs. + // utc_time false whether enable utc time. + // pithy_print_ms 10000 the pithy print interval in ms. if (rpc == "update") { if (!allow_update) { ret = ERROR_SYSTEM_CONFIG_RAW_DISABLED; @@ -1009,7 +1012,8 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) } if (scope != "listen" && scope != "pid" && scope != "chunk_size" && scope != "ff_log_dir" && scope != "srs_log_tank" && scope != "srs_log_level" - && scope != "srs_log_file" + && scope != "srs_log_file" && scope != "max_connections" && scope != "utc_time" + && scope != "pithy_print_ms" ) { ret = ERROR_SYSTEM_CONFIG_RAW_NOT_ALLOWED; srs_error("raw api query invalid scope=%s. ret=%d", scope.c_str(), ret); @@ -1106,6 +1110,41 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) srs_error("raw api update srs_log_file=%s failed. ret=%d", value.c_str(), ret); return srs_api_response_code(w, r, ret); } + } else if (scope == "max_connections") { + int mcv = ::atoi(value.c_str()); + if (mcv < 10 || mcv > 65535 || !srs_is_digit_number(value)) { + ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS; + srs_error("raw api update check max_connections=%s/%d failed. ret=%d", value.c_str(), mcv, ret); + return srs_api_response_code(w, r, ret); + } + + if ((ret = _srs_config->raw_set_max_connections(value, applied)) != ERROR_SUCCESS) { + srs_error("raw api update max_connections=%s/%d failed. ret=%d", value.c_str(), mcv, ret); + return srs_api_response_code(w, r, ret); + } + } else if (scope == "utc_time") { + if (!srs_is_boolean(value)) { + ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS; + srs_error("raw api update check utc_time=%s failed. ret=%d", value.c_str(), ret); + return srs_api_response_code(w, r, ret); + } + + if ((ret = _srs_config->raw_set_utc_time(srs_config_bool2switch(value), applied)) != ERROR_SUCCESS) { + srs_error("raw api update utc_time=%s failed. ret=%d", value.c_str(), ret); + return srs_api_response_code(w, r, ret); + } + } else if (scope == "pithy_print_ms") { + int ppmv = ::atoi(value.c_str()); + if (ppmv < 100 || ppmv > 300000 || !srs_is_digit_number(value)) { + ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS; + srs_error("raw api update check pithy_print_ms=%s/%d failed. ret=%d", value.c_str(), ppmv, ret); + return srs_api_response_code(w, r, ret); + } + + if ((ret = _srs_config->raw_set_pithy_print_ms(value, applied)) != ERROR_SUCCESS) { + srs_error("raw api update pithy_print_ms=%s/%d failed. ret=%d", value.c_str(), ppmv, ret); + return srs_api_response_code(w, r, ret); + } } // whether the config applied. diff --git a/trunk/src/app/srs_app_log.cpp b/trunk/src/app/srs_app_log.cpp index 5cc6ed14d..2062b1776 100644 --- a/trunk/src/app/srs_app_log.cpp +++ b/trunk/src/app/srs_app_log.cpp @@ -86,6 +86,7 @@ SrsFastLog::SrsFastLog() fd = -1; log_to_file_tank = false; + utc = false; } SrsFastLog::~SrsFastLog() @@ -111,6 +112,7 @@ int SrsFastLog::initialize() log_to_file_tank = _srs_config->get_log_tank_file(); _level = srs_get_log_level(_srs_config->get_log_level()); + utc = _srs_config->get_utc_time(); } return ret; @@ -221,6 +223,13 @@ void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...) write_log(fd, log_data, size, SrsLogLevel::Error); } +int SrsFastLog::on_reload_utc_time() +{ + utc = _srs_config->get_utc_time(); + + return ERROR_SUCCESS; +} + int SrsFastLog::on_reload_log_tank() { int ret = ERROR_SUCCESS; @@ -291,7 +300,7 @@ bool SrsFastLog::generate_header(bool error, const char* tag, int context_id, co // to calendar time struct tm* tm; - if (_srs_config && _srs_config->get_utc_time()) { + if (utc) { if ((tm = gmtime(&tv.tv_sec)) == NULL) { return false; } diff --git a/trunk/src/app/srs_app_log.hpp b/trunk/src/app/srs_app_log.hpp index a9900b914..cd4be8e02 100644 --- a/trunk/src/app/srs_app_log.hpp +++ b/trunk/src/app/srs_app_log.hpp @@ -73,6 +73,8 @@ private: int fd; // whether log to file tank bool log_to_file_tank; + // whether use utc time. + bool utc; public: SrsFastLog(); virtual ~SrsFastLog(); @@ -85,6 +87,7 @@ public: virtual void error(const char* tag, int context_id, const char* fmt, ...); // interface ISrsReloadHandler. public: + virtual int on_reload_utc_time(); virtual int on_reload_log_tank(); virtual int on_reload_log_level(); virtual int on_reload_log_file(); diff --git a/trunk/src/app/srs_app_reload.cpp b/trunk/src/app/srs_app_reload.cpp index 3afaef5fd..a3276eea3 100644 --- a/trunk/src/app/srs_app_reload.cpp +++ b/trunk/src/app/srs_app_reload.cpp @@ -45,6 +45,11 @@ int ISrsReloadHandler::on_reload_max_conns() return ERROR_SUCCESS; } +int ISrsReloadHandler::on_reload_utc_time() +{ + return ERROR_SUCCESS; +} + int ISrsReloadHandler::on_reload_pid() { return ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_reload.hpp b/trunk/src/app/srs_app_reload.hpp index 0e094886e..ecbcd61e1 100644 --- a/trunk/src/app/srs_app_reload.hpp +++ b/trunk/src/app/srs_app_reload.hpp @@ -45,6 +45,7 @@ public: virtual ~ISrsReloadHandler(); public: virtual int on_reload_max_conns(); + virtual int on_reload_utc_time(); virtual int on_reload_listen(); virtual int on_reload_pid(); virtual int on_reload_log_tank(); diff --git a/trunk/src/app/srs_app_utility.cpp b/trunk/src/app/srs_app_utility.cpp index 3fc18b092..3365fb204 100644 --- a/trunk/src/app/srs_app_utility.cpp +++ b/trunk/src/app/srs_app_utility.cpp @@ -1367,6 +1367,11 @@ bool srs_is_digit_number(const string& str) return v / powv >= 1 && v / powv <= 9; } +bool srs_is_boolean(const string& str) +{ + return str == "true" || str == "false"; +} + void srs_api_dump_summaries(SrsAmf0Object* obj) { SrsRusage* r = srs_get_system_rusage(); diff --git a/trunk/src/app/srs_app_utility.hpp b/trunk/src/app/srs_app_utility.hpp index c37e099b4..bc8661e37 100644 --- a/trunk/src/app/srs_app_utility.hpp +++ b/trunk/src/app/srs_app_utility.hpp @@ -675,6 +675,11 @@ extern std::string srs_get_peer_ip(int fd); // is_digit("1234567890a") === false // is_digit("a1234567890") === false extern bool srs_is_digit_number(const std::string& str); +// whether string is boolean +// is_bool("true") == true +// is_bool("false") == true +// otherwise, false. +extern bool srs_is_boolean(const std::string& str); // dump summaries for /api/v1/summaries. extern void srs_api_dump_summaries(SrsAmf0Object* obj);