Skip to content

Commit

Permalink
for #319, support enable or disable vhost.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Sep 15, 2015
1 parent 631e76c commit e8a00de
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
67 changes: 58 additions & 9 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,16 +721,9 @@ int SrsConfig::reload_vhost(SrsConfDirective* old_root)

// ENABLED => DISABLED
if (get_vhost_enabled(old_vhost) && !get_vhost_enabled(new_vhost)) {
srs_trace("vhost %s removed, reload it.", vhost.c_str());
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
ISrsReloadHandler* subscribe = *it;
if ((ret = subscribe->on_reload_vhost_removed(vhost)) != ERROR_SUCCESS) {
srs_error("notify subscribes removed "
"vhost %s failed. ret=%d", vhost.c_str(), ret);
return ret;
}
if ((ret = do_reload_vhost_removed(vhost)) != ERROR_SUCCESS) {
return ret;
}
srs_trace("reload removed vhost %s success.", vhost.c_str());
continue;
}

Expand Down Expand Up @@ -2514,6 +2507,42 @@ int SrsConfig::raw_delete_vhost(string vhost, bool& applied)
return ret;
}

int SrsConfig::raw_disable_vhost(string vhost, bool& applied)
{
int ret = ERROR_SUCCESS;

applied = false;

SrsConfDirective* conf = root->get("vhost", vhost);
conf->get_or_create("enabled")->set_arg0("off");

if ((ret = do_reload_vhost_removed(vhost)) != ERROR_SUCCESS) {
return ret;
}

applied = true;

return ret;
}

int SrsConfig::raw_enable_vhost(string vhost, bool& applied)
{
int ret = ERROR_SUCCESS;

applied = false;

SrsConfDirective* conf = root->get("vhost", vhost);
conf->get_or_create("enabled")->set_arg0("on");

if ((ret = do_reload_vhost_added(vhost)) != ERROR_SUCCESS) {
return ret;
}

applied = true;

return ret;
}

int SrsConfig::do_reload_listen()
{
int ret = ERROR_SUCCESS;
Expand Down Expand Up @@ -2670,6 +2699,26 @@ int SrsConfig::do_reload_vhost_added(string vhost)
return ret;
}

int SrsConfig::do_reload_vhost_removed(string vhost)
{
int ret = ERROR_SUCCESS;

srs_trace("vhost %s removed, reload it.", vhost.c_str());

vector<ISrsReloadHandler*>::iterator it;
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
ISrsReloadHandler* subscribe = *it;
if ((ret = subscribe->on_reload_vhost_removed(vhost)) != ERROR_SUCCESS) {
srs_error("notify subscribes removed "
"vhost %s failed. ret=%d", vhost.c_str(), ret);
return ret;
}
}
srs_trace("reload removed vhost %s success.", vhost.c_str());

return ret;
}

string SrsConfig::config()
{
return config_file;
Expand Down
9 changes: 9 additions & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ class SrsConfig
* raw delete the disabled vhost.
*/
virtual int raw_delete_vhost(std::string vhost, bool& applied);
/**
* raw disable the enabled vhost.
*/
virtual int raw_disable_vhost(std::string vhost, bool& applied);
/**
* raw enable the disabled vhost.
*/
virtual int raw_enable_vhost(std::string vhost, bool& applied);
private:
virtual int do_reload_listen();
virtual int do_reload_pid();
Expand All @@ -400,6 +408,7 @@ class SrsConfig
virtual int do_reload_utc_time();
virtual int do_reload_pithy_print_ms();
virtual int do_reload_vhost_added(std::string vhost);
virtual int do_reload_vhost_removed(std::string vhost);
public:
/**
* get the config file path.
Expand Down
27 changes: 26 additions & 1 deletion trunk/src/app/srs_app_http_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
} else if (scope == "vhost") {
std::string param = r->query_get("param");
std::string data = r->query_get("data");
if (param != "create" && param != "update" && param != "delete") {
if (param != "create" && param != "update" && param != "delete" && param != "disable" && param != "enable") {
ret = ERROR_SYSTEM_CONFIG_RAW_NOT_ALLOWED;
srs_error("raw api query invalid scope=%s, param=%s. ret=%d", scope.c_str(), param.c_str(), ret);
return srs_api_response_code(w, r, ret);
Expand Down Expand Up @@ -1202,7 +1202,32 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
srs_error("raw api update vhost=%s, param=%s failed. ret=%d", value.c_str(), param.c_str(), ret);
return srs_api_response_code(w, r, ret);
}
} else if (param == "disable") {
// when disable, the vhost must exists and enabled.
SrsConfDirective* vhost = _srs_config->get_vhost(value, false);
if (param.empty() || !vhost || !_srs_config->get_vhost_enabled(vhost)) {
ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS;
srs_error("raw api update check vhost=%s, param=%s failed. ret=%d", value.c_str(), param.c_str(), ret);
return srs_api_response_code(w, r, ret);
}

if ((ret = _srs_config->raw_disable_vhost(value, applied)) != ERROR_SUCCESS) {
srs_error("raw api update vhost=%s, param=%s failed. ret=%d", value.c_str(), param.c_str(), ret);
return srs_api_response_code(w, r, ret);
}
} else if (param == "enable") {
// when enable, the vhost must exists and disabled.
SrsConfDirective* vhost = _srs_config->get_vhost(value, false);
if (param.empty() || !vhost || _srs_config->get_vhost_enabled(vhost)) {
ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS;
srs_error("raw api update check vhost=%s, param=%s failed. ret=%d", value.c_str(), param.c_str(), ret);
return srs_api_response_code(w, r, ret);
}

if ((ret = _srs_config->raw_enable_vhost(value, applied)) != ERROR_SUCCESS) {
srs_error("raw api update vhost=%s, param=%s failed. ret=%d", value.c_str(), param.c_str(), ret);
return srs_api_response_code(w, r, ret);
}
}
}

Expand Down

0 comments on commit e8a00de

Please sign in to comment.