diff options
-rw-r--r-- | switchctl.c | 122 |
1 files changed, 92 insertions, 30 deletions
diff --git a/switchctl.c b/switchctl.c index 5a7c94e..ad9d05b 100644 --- a/switchctl.c +++ b/switchctl.c @@ -204,6 +204,38 @@ int process_cmd_request(const char* cmd, cmd_id_t cmd_id, const char* param, int return 0; } +int swap_channels(const char* ch_from, const char* ch_to, int fd, cmd_t **cmd_q, options_t* opt) +{ + char* cmd_param = strdup("*0FDii*0FUii"); + + char* ch_nr_from = key_value_storage_find(&opt->alias_table_, ch_from); + if(!ch_nr_from || ch_nr_from[0] == 0 || ch_nr_from[1] == 0 || ch_nr_from[2] != 0) { + log_printf(ERROR, "invalid channel name or number: %s", ch_from); + send_response(fd, "EEE: channel: invalid channel name or number"); + free(cmd_param); + return 1; + } + + char* ch_nr_to = key_value_storage_find(&opt->alias_table_, ch_to); + if(!ch_nr_to || ch_nr_to[0] == 0 || ch_nr_to[1] == 0 || ch_nr_to[2] != 0) { + log_printf(ERROR, "invalid channel name or number: %s", ch_to); + send_response(fd, "EEE: channel: invalid channel name or number"); + free(cmd_param); + return 1; + } + + cmd_param[4] = ch_nr_from[0]; + cmd_param[5] = ch_nr_from[1]; + cmd_param[10] = ch_nr_to[0]; + cmd_param[11] = ch_nr_to[1]; + + log_printf(DEBUG, "enqueing command to switch: '%s'", cmd_param); + int ret = cmd_push(cmd_q, fd, CHANNEL, cmd_param); + free(cmd_param); + + return ret; +} + int process_cmd_channel(const char* cmd, const char* param, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) { client_t* c = client_find(client_lst, fd); @@ -232,7 +264,7 @@ int process_cmd_channel(const char* cmd, const char* param, int fd, cmd_t **cmd_ return 0; } - char* cmd_param = strdup("*0FDii*0FUii"); + channel_t old_channel = opt->channel_; char* ch_from = NULL; char* ch_to = NULL; if(!strcmp(param, "main")) { @@ -258,33 +290,14 @@ int process_cmd_channel(const char* cmd, const char* param, int fd, cmd_t **cmd_ opt->channel_ = CHAN_MUSIC; } + int ret = swap_channels(ch_from, ch_to, fd, cmd_q, opt); + if(ret) { + opt->channel_ = old_channel; + if(ret > 0) + return 0; - char* ch_nr_from = key_value_storage_find(&opt->alias_table_, ch_from); - if(!ch_nr_from || ch_nr_from[0] == 0 || ch_nr_from[1] == 0 || ch_nr_from[2] != 0) { - log_printf(ERROR, "invalid channel name or number: %s", ch_from); - send_response(fd, "EEE: channel: invalid channel name or number"); - free(cmd_param); - return 0; - } - - char* ch_nr_to = key_value_storage_find(&opt->alias_table_, ch_to); - if(!ch_nr_to || ch_nr_to[0] == 0 || ch_nr_to[1] == 0 || ch_nr_to[2] != 0) { - log_printf(ERROR, "invalid channel name or number: %s", ch_to); - send_response(fd, "EEE: channel: invalid channel name or number"); - free(cmd_param); - return 0; - } - - cmd_param[4] = ch_nr_from[0]; - cmd_param[5] = ch_nr_from[1]; - cmd_param[10] = ch_nr_to[0]; - cmd_param[11] = ch_nr_to[1]; - - log_printf(DEBUG, "enqueing command to switch: '%s'", cmd_param); - int ret = cmd_push(cmd_q, fd, CHANNEL, cmd_param); - free(cmd_param); - if(ret) return ret; + } log_printf(NOTICE, "command: %s", cmd); @@ -320,9 +333,10 @@ void process_cmd_type(const char* param, int fd, client_t* client_lst) } } - -void process_cmd_mode(const char* param, int fd, options_t* opt) +int process_cmd_mode(const char* param, int fd, cmd_t **cmd_q, options_t* opt) { + mode_t old_mode = opt->mode_; + if(param) { if(!strncmp(param, "master", 6)) opt->mode_ = MODE_MASTER; @@ -331,7 +345,48 @@ void process_cmd_mode(const char* param, int fd, options_t* opt) else { log_printf(DEBUG, "unkown mode '%s'", param); send_response(fd, "EEE: mode: unknown mode"); - return; + return 0; + } + + // swap master with standby channels + + char* ch_from = NULL; + if(old_mode == MODE_MASTER && opt->channel_ == CHAN_MAIN) + ch_from = "master_main"; + else if(old_mode == MODE_MASTER && opt->channel_ == CHAN_MUSIC) + ch_from = "master_music"; + else if(old_mode == MODE_STANDBY && opt->channel_ == CHAN_MAIN) + ch_from = "standby_main"; + else if(old_mode == MODE_STANDBY && opt->channel_ == CHAN_MUSIC) + ch_from = "standby_music"; + else { + opt->mode_ = old_mode; + log_printf(ERROR, "EEE: mode: old config is illegal?!"); + return 0; + } + + char* ch_to = NULL; + if(opt->mode_ == MODE_MASTER && opt->channel_ == CHAN_MAIN) + ch_to = "master_main"; + else if(opt->mode_ == MODE_MASTER && opt->channel_ == CHAN_MUSIC) + ch_to = "master_music"; + else if(opt->mode_ == MODE_STANDBY && opt->channel_ == CHAN_MAIN) + ch_to = "standby_main"; + else if(opt->mode_ == MODE_STANDBY && opt->channel_ == CHAN_MUSIC) + ch_to = "standby_music"; + else { + opt->mode_ = old_mode; + log_printf(ERROR, "EEE: mode: current config is illegal?!"); + return 0; + } + + int ret = swap_channels(ch_from, ch_to, fd, cmd_q, opt); + if(ret) { + opt->mode_ = old_mode; + if(ret > 0) + return 0; + + return ret; } } else { @@ -340,6 +395,8 @@ void process_cmd_mode(const char* param, int fd, options_t* opt) } log_printf(NOTICE, "new mode: %s", opt->mode_ == MODE_MASTER ? "master" : "standby"); + + return 0; } void process_cmd_listen(const char* param, int fd, client_t* client_lst) @@ -439,7 +496,12 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, op break; } case TYPE: process_cmd_type(param, fd, client_lst); break; - case MODE: process_cmd_mode(param, fd, opt); break; + case MODE: { + int ret = process_cmd_mode(param, fd, cmd_q, opt); + if(ret) + return ret; + break; + } case LOG: { if(param && param[0]) log_printf(NOTICE, "ext msg: %s", param); |