diff options
Diffstat (limited to 'switchctl.c')
-rw-r--r-- | switchctl.c | 69 |
1 files changed, 52 insertions, 17 deletions
diff --git a/switchctl.c b/switchctl.c index 65f439c..0e1b4ea 100644 --- a/switchctl.c +++ b/switchctl.c @@ -218,23 +218,26 @@ int process_cmd_request(const char* cmd, cmd_id_t cmd_id, const char* param, int return ret; if(cmd_id == STATUS) { - char buf[2][30]; + char buf[5][30]; snprintf(buf[0], 30, "Current Mode: %s", state_.mode_ == MODE_MASTER ? "Master" : "Standby"); send_response(fd, buf[0]); snprintf(buf[1], 30, "Master Channel: %s", state_.channel_master_ == CHAN_MAIN ? "Main" : "Music"); send_response(fd, buf[1]); - snprintf(buf[1], 30, "Standby Channel: %s", state_.channel_standby_ == CHAN_MAIN ? "Main" : "Music"); - send_response(fd, buf[1]); - snprintf(buf[1], 30, "Master Heartbeat: %s", state_.hb_state_master_ ? "present" : "timeout"); - send_response(fd, buf[1]); - snprintf(buf[1], 30, "Standby Heartbeat: %s", state_.hb_state_standby_ ? "present" : "timeout"); - send_response(fd, buf[1]); + snprintf(buf[2], 30, "Standby Channel: %s", state_.channel_standby_ == CHAN_MAIN ? "Main" : "Music"); + send_response(fd, buf[2]); + snprintf(buf[3], 30, "Master Heartbeat: %s", state_.hb_state_master_ ? "present" : "timeout"); + send_response(fd, buf[3]); + snprintf(buf[4], 30, "Standby Heartbeat: %s", state_.hb_state_standby_ ? "present" : "timeout"); + send_response(fd, buf[4]); client_t* client; int listener_cnt = 0; for(client = client_lst; client; client = client->next) if(client->status_listener && client->fd != fd) { send_response(client->fd, buf[0]); send_response(client->fd, buf[1]); + send_response(client->fd, buf[2]); + send_response(client->fd, buf[3]); + send_response(client->fd, buf[4]); listener_cnt++; } log_printf(DEBUG, "sent status to %d additional listeners", listener_cnt); @@ -492,9 +495,30 @@ int process_cmd_heartbeat(const char* param, int fd, cmd_t **cmd_q, client_t* cl if(param) { client_t* client = client_find(client_lst, fd); if(client) { + char* message = NULL; switch(client->type) { - case HB_MASTER: state_.hb_state_master_ = (param[0] == '1') ? TRUE : FALSE; break; - case HB_STANDBY: state_.hb_state_standby_ = (param[0] == '1') ? TRUE : FALSE; break; + case HB_MASTER: { + bool old_state = state_.hb_state_master_; + state_.hb_state_master_ = (param[0] == '1') ? TRUE : FALSE; + if(old_state != state_.hb_state_master_) { + if(state_.hb_state_master_) + message = "Master: heartbeat is back!"; + else + message = "Master: heartbeat timed out!"; + } + break; + } + case HB_STANDBY: { + bool old_state = state_.hb_state_standby_; + state_.hb_state_standby_ = (param[0] == '1') ? TRUE : FALSE; + if(old_state != state_.hb_state_standby_) { + if(state_.hb_state_standby_) + message = "Standby: heartbeat is back!"; + else + message = "Standby: heartbeat timed out!"; + } + break; + } default: { log_printf(ERROR, "unable to update heartbeat status: wrong client type"); send_response(fd, "EEE: heartbeat: wrong client type"); @@ -507,22 +531,29 @@ int process_cmd_heartbeat(const char* param, int fd, cmd_t **cmd_q, client_t* cl if(state_.hb_state_standby_) { state_.mode_ = MODE_STANDBY; return change_mode(old_mode, fd, cmd_q, opt, client_lst); - } else { - log_printf(ERROR, "both heartbeats are offline!!!!!!"); - //TODO: sent this to emergency listeners - } + } else + message = "both heartbeats are offline!"; } } else { if(!state_.hb_state_standby_) { if(state_.hb_state_master_) { state_.mode_ = MODE_MASTER; return change_mode(old_mode, fd, cmd_q, opt, client_lst); - } else { - log_printf(ERROR, "both heartbeats are offline!!!!!!"); - //TODO: sent this to emergency listeners - } + } else + message = "both heartbeats are offline!"; } } + if(message) { + log_printf(ERROR, message); + client_t* client; + int listener_cnt = 0; + for(client = client_lst; client; client = client->next) + if(client->heartbeat_listener && client->fd != fd) { + send_response(client->fd, message); + listener_cnt++; + } + log_printf(DEBUG, "sent heartbeat state to %d additional listeners", listener_cnt); + } } else { log_printf(ERROR, "unable to update heartbeat status: client not found"); @@ -548,6 +579,7 @@ void process_cmd_listen(const char* param, int fd, client_t* client_lst) listener->oc_listener = 1; listener->relay_listener = 1; listener->silence_listener = 1; + listener->heartbeat_listener = 1; } else { if(!strncmp(param, "request", 7)) @@ -564,6 +596,8 @@ void process_cmd_listen(const char* param, int fd, client_t* client_lst) listener->relay_listener = 1; else if(!strncmp(param, "silence", 7)) listener->silence_listener = 1; + else if(!strncmp(param, "heartbeat", 11)) + listener->heartbeat_listener = 1; else if(!strncmp(param, "none", 4)) { listener->request_listener = 0; listener->mode_listener = 0; @@ -572,6 +606,7 @@ void process_cmd_listen(const char* param, int fd, client_t* client_lst) listener->oc_listener = 0; listener->relay_listener = 0; listener->silence_listener = 0; + listener->heartbeat_listener = 0; } else { log_printf(DEBUG, "unkown listener type '%s'", param); |