From 8a2709dd2c411266dd7a013da38e39bcd803b25e Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 31 Aug 2015 12:06:22 +0800 Subject: [PATCH] for #319, do not apply when config not changed. --- trunk/src/app/srs_app_config.cpp | 71 ++++++++++++++++++++++++++---- trunk/src/app/srs_app_config.hpp | 8 +++- trunk/src/app/srs_app_http_api.cpp | 12 +++-- 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 065e1daea..63afd9268 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -67,9 +67,46 @@ using namespace _srs_internal; // '\r' #define SRS_CR (char)SRS_CONSTS_CR -// dumps the engine to amf0 object. +/** + * dumps the ingest/transcode-engine in @param dir to amf0 object @param engine. + * @param dir the transcode or ingest config directive. + * @param engine the amf0 object to dumps to. + */ int srs_config_dumps_engine(SrsConfDirective* dir, SrsAmf0Object* engine); +/** + * whether the two vector actual equals, for instance, + * srs_vector_actual_equals([0, 1, 2], [0, 1, 2]) ==== true + * srs_vector_actual_equals([0, 1, 2], [2, 1, 0]) ==== true + * srs_vector_actual_equals([0, 1, 2], [0, 2, 1]) ==== true + * srs_vector_actual_equals([0, 1, 2], [0, 1, 2, 3]) ==== false + * srs_vector_actual_equals([1, 2, 3], [0, 1, 2]) ==== false + */ +template +bool srs_vector_actual_equals(const vector& a, const vector& b) +{ + // all elements of a in b. + for (int i = 0; i < (int)a.size(); i++) { + const T& e = a.at(i); + if (::find(b.begin(), b.end(), e) == b.end()) { + return false; + } + } + + // all elements of b in a. + for (int i = 0; i < (int)b.size(); i++) { + const T& e = b.at(i); + if (::find(a.begin(), a.end(), e) == a.end()) { + return false; + } + } + + return true; +} + +/** + * whether the ch is common space. + */ bool is_common_space(char ch) { return (ch == ' ' || ch == '\t' || ch == SRS_CR || ch == SRS_LF); @@ -900,14 +937,9 @@ int SrsConfig::reload_conf(SrsConfig* conf) // merge config: listen if (!srs_directive_equals(root->get("listen"), old_root->get("listen"))) { - for (it = subscribes.begin(); it != subscribes.end(); ++it) { - ISrsReloadHandler* subscribe = *it; - if ((ret = subscribe->on_reload_listen()) != ERROR_SUCCESS) { - srs_error("notify subscribes reload listen failed. ret=%d", ret); - return ret; - } + if ((ret = do_reload_listen()) != ERROR_SUCCESS) { + return ret; } - srs_trace("reload listen success."); } // merge config: pid @@ -2175,13 +2207,34 @@ int SrsConfig::raw_to_json(SrsAmf0Object* obj) return ret; } -int SrsConfig::raw_set_listen(const vector& eps) +int SrsConfig::raw_set_listen(const vector& eps, bool& applied) { int ret = ERROR_SUCCESS; + applied = false; + SrsConfDirective* listen = root->get("listen"); + + // not changed, ignore. + if (srs_vector_actual_equals(listen->args, eps)) { + return ret; + } + + // changed, apply and reload. listen->args = eps; + if ((ret = do_reload_listen()) != ERROR_SUCCESS) { + return ret; + } + + applied = true; + return ret; +} + +int SrsConfig::do_reload_listen() +{ + int ret = ERROR_SUCCESS; + // force to reload the memory server. vector::iterator it; for (it = subscribes.begin(); it != subscribes.end(); ++it) { diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 66d80e07e..1beb577b3 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -331,8 +331,14 @@ public: virtual int raw_to_json(SrsAmf0Object* obj); /** * raw set the global listen. + * @param applied whether the config is applied. */ - virtual int raw_set_listen(const std::vector& eps); + virtual int raw_set_listen(const std::vector& eps, bool& applied); +private: + /** + * do reload listen, for reload from signal or raw api. + */ + virtual int do_reload_listen(); public: /** * get the config file path. diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index 30dd32a69..f94da5217 100755 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -992,6 +992,7 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) return srs_api_response_code(w, r, ret); } + bool applied = false; if (scope == "global.listen") { vector eps = srs_string_split(value, ","); @@ -1010,14 +1011,19 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) return srs_api_response_code(w, r, ret); } - if ((ret = _srs_config->raw_set_listen(eps)) != ERROR_SUCCESS) { + if ((ret = _srs_config->raw_set_listen(eps, applied)) != ERROR_SUCCESS) { srs_error("raw api update global.listen=%s failed. ret=%d", value.c_str(), ret); return srs_api_response_code(w, r, ret); } } - server->on_signal(SRS_SIGNAL_PERSISTENCE_CONFIG); - srs_trace("raw api update %s=%s ok.", scope.c_str(), value.c_str()); + // whether the config applied. + if (applied) { + server->on_signal(SRS_SIGNAL_PERSISTENCE_CONFIG); + srs_trace("raw api update %s=%s ok.", scope.c_str(), value.c_str()); + } else { + srs_warn("raw api update not applied %s=%s.", scope.c_str(), value.c_str()); + } return srs_api_response(w, r, obj->to_json()); }