Skip to content

Commit

Permalink
for #319, raw api support update all globals.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Sep 9, 2015
1 parent 6aafd07 commit c4feb8f
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 21 deletions.
166 changes: 147 additions & 19 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"))) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2461,6 +2533,57 @@ int SrsConfig::do_reload_srs_log_file()
return ret;
}

int SrsConfig::do_reload_max_connections()
{
int ret = ERROR_SUCCESS;

vector<ISrsReloadHandler*>::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<ISrsReloadHandler*>::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<ISrsReloadHandler*>::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;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,27 @@ class SrsConfig
* 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.
Expand Down Expand Up @@ -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:
Expand Down
41 changes: 40 additions & 1 deletion trunk/src/app/srs_app_http_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion trunk/src/app/srs_app_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ SrsFastLog::SrsFastLog()

fd = -1;
log_to_file_tank = false;
utc = false;
}

SrsFastLog::~SrsFastLog()
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions trunk/src/app/srs_app_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class SrsFastLog : public ISrsLog, public ISrsReloadHandler
int fd;
// whether log to file tank
bool log_to_file_tank;
// whether use utc time.
bool utc;
public:
SrsFastLog();
virtual ~SrsFastLog();
Expand All @@ -85,6 +87,7 @@ class SrsFastLog : public ISrsLog, public ISrsReloadHandler
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();
Expand Down
5 changes: 5 additions & 0 deletions trunk/src/app/srs_app_reload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_reload.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ISrsReloadHandler
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();
Expand Down
5 changes: 5 additions & 0 deletions trunk/src/app/srs_app_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading

0 comments on commit c4feb8f

Please sign in to comment.