diff options
author | Christian Pointner <equinox@helsinki.at> | 2009-11-04 20:39:44 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2009-11-04 20:39:44 (GMT) |
commit | a328c855ccd5c8d86eaef2439a73b73c348925ca (patch) | |
tree | 08d698a57092c7958b93862283e21d1fcd8ae4d1 | |
parent | 2a0b2f4b57c2b6241154ff58fc842f21d1c2e04b (diff) |
cleanup
-rw-r--r-- | switchctl.c | 177 |
1 files changed, 99 insertions, 78 deletions
diff --git a/switchctl.c b/switchctl.c index 83ea464..b293015 100644 --- a/switchctl.c +++ b/switchctl.c @@ -76,6 +76,100 @@ int send_response(int fd, const char* response) return ret; } +int process_cmd_request(const char* cmd, cmd_id_t cmd_id, const char* param, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) +{ + char* tmp = key_value_storage_find(&opt->alias_table_, param); + int ret = cmd_push(cmd_q, fd, cmd_id, tmp != NULL ? tmp : param); + if(ret) + return ret; + + if(cmd_id == STATUS) { + char buf[30]; + snprintf(buf, 30, "Current Mode: %s", opt->mode_ == MODE_MASTER ? "Master" : "Standby"); + send_response(fd, buf); + 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); + listener_cnt++; + } + log_printf(DEBUG, "sent status to %d additional listeners", listener_cnt); + } + + log_printf(NOTICE, "command: %s", cmd); + + return 0; +} + +void process_cmd_type(const char* param, int fd, client_t* client_lst) +{ + if(param) { + client_t* client = client_find(client_lst, fd); + if(client) { + if(!strncmp(param, "master", 6)) + client->type = MASTER; + else if(!strncmp(param, "standby", 7)) + client->type = STANDBY; + else if(!strncmp(param, "heartbeat", 9)) + client->type = HEARTBEAT; + else { + log_printf(DEBUG, "unkown client type '%s'", param); + return; + } + log_printf(DEBUG, "client %d type set to %s", fd, param); + } + else + log_printf(ERROR, "unable to set client type for %d: client not found", fd); + } + else + log_printf(ERROR, "unable to set client type for %d: empty parameter", fd); +} + + +void process_cmd_mode(const char* param, options_t* opt) +{ + if(param) { + if(!strncmp(param, "master", 6)) + opt->mode_ = MODE_MASTER; + else if(!strncmp(param, "standby", 7)) + opt->mode_ = MODE_STANDBY; + else { + log_printf(DEBUG, "unkown mode '%s'", param); + return; + } + } + else + log_printf(ERROR, "unable to set mode: empty parameter"); +} + +void process_cmd_listen(const char* param, int fd, client_t* client_lst) +{ + client_t* listener = client_find(client_lst, fd); + if(listener) { + if(!param) { + listener->status_listener = 1; + listener->error_listener = 1; + listener->request_listener = 1; + } + else { + if(!strncmp(param, "status", 6)) + listener->status_listener = 1; + else if(!strncmp(param, "error", 5)) + listener->error_listener = 1; + else if(!strncmp(param, "request", 7)) + listener->request_listener = 1; + else { + log_printf(DEBUG, "unkown listener type '%s'", param); + return; + } + } + log_printf(DEBUG, "listener %d requests %s messages", fd, param ? param:"all"); + } + else + log_printf(ERROR, "unable to add listener %d", fd); +} + int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) { log_printf(DEBUG, "processing command from %d", fd); @@ -105,7 +199,7 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, op if(param) param++; - if(cmd_id == SWITCH) { + if(cmd_id == SWITCH || cmd_id == MODE) { char* resp; asprintf(&resp, "Request: %s", cmd); if(resp) { @@ -127,60 +221,13 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, op switch(cmd_id) { case SWITCH: case STATUS: { - char* tmp = key_value_storage_find(&opt->alias_table_, param); - int ret = cmd_push(cmd_q, fd, cmd_id, tmp != NULL ? tmp : param); + int ret = process_cmd_request(cmd, cmd_id, param, fd, cmd_q, client_lst, opt); if(ret) return ret; - - if(cmd_id == STATUS) { - char buf[30]; - snprintf(buf, 30, "Current Mode: %s", opt->mode_ == MODE_MASTER ? "Master" : "Standby"); - send_response(fd, buf); - } - - log_printf(NOTICE, "command: %s", cmd); - break; - } - case TYPE: { - if(param) { - client_t* client = client_find(client_lst, fd); - if(client) { - if(!strncmp(param, "master", 6)) - client->type = MASTER; - else if(!strncmp(param, "standby", 7)) - client->type = STANDBY; - else if(!strncmp(param, "heartbeat", 9)) - client->type = HEARTBEAT; - else { - log_printf(DEBUG, "unkown client type '%s'", param); - break; - } - log_printf(DEBUG, "client %d type set to %s", fd, param); - } - else - log_printf(ERROR, "unable to set client type for %d: client not found", fd); - } - else - log_printf(ERROR, "unable to set client type for %d: empty parameter", fd); - - break; - } - case MODE: { - if(param) { - if(!strncmp(param, "master", 6)) - opt->mode_ = MODE_MASTER; - else if(!strncmp(param, "standby", 7)) - opt->mode_ = MODE_STANDBY; - else { - log_printf(DEBUG, "unkown mode '%s'", param); - break; - } - } - else - log_printf(ERROR, "unable to set mode: empty parameter", fd); - break; } + case TYPE: process_cmd_type(param, fd, client_lst); break; + case MODE: process_cmd_mode(param, opt); break; case LOG: { if(param && param[0]) log_printf(NOTICE, "ext msg: %s", param); @@ -188,33 +235,7 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, op log_printf(DEBUG, "ignoring empty ext log message"); break; } - case LISTEN: { - client_t* listener = client_find(client_lst, fd); - if(listener) { - if(!param) { - listener->status_listener = 1; - listener->error_listener = 1; - listener->request_listener = 1; - } - else { - if(!strncmp(param, "status", 6)) - listener->status_listener = 1; - else if(!strncmp(param, "error", 5)) - listener->error_listener = 1; - else if(!strncmp(param, "request", 7)) - listener->request_listener = 1; - else { - log_printf(DEBUG, "unkown listener type '%s'", param); - break; - } - } - log_printf(DEBUG, "listener %d requests %s messages", fd, param ? param:"all"); - } - else { - log_printf(ERROR, "unable to add listener %d", fd); - } - break; - } + case LISTEN: process_cmd_listen(param, fd, client_lst); break; } return 0; |