From: Christian Pointner Date: Thu, 10 Sep 2009 00:59:51 +0000 (+0000) Subject: added request and error listeners X-Git-Url: https://git.realraum.at/?a=commitdiff_plain;h=273898a7a155895de69cde048b96f6ffb5f1fc47;p=svn42.git added request and error listeners --- diff --git a/door_daemon/client_list.c b/door_daemon/client_list.c index d989687..2c4c41c 100644 --- a/door_daemon/client_list.c +++ b/door_daemon/client_list.c @@ -46,6 +46,8 @@ int client_add(client_t** first, int fd) new_client->fd = fd; new_client->status_listener = 0; + new_client->error_listener = 0; + new_client->request_listener = 0; new_client->next = NULL; new_client->buffer.offset = 0; diff --git a/door_daemon/client_list.h b/door_daemon/client_list.h index 8bb14f9..d35c433 100644 --- a/door_daemon/client_list.h +++ b/door_daemon/client_list.h @@ -26,6 +26,8 @@ struct client_struct { int fd; int status_listener; + int error_listener; + int request_listener; struct client_struct* next; read_buffer_t buffer; }; diff --git a/door_daemon/command_queue.h b/door_daemon/command_queue.h index bc153fe..1b26e3a 100644 --- a/door_daemon/command_queue.h +++ b/door_daemon/command_queue.h @@ -23,7 +23,7 @@ #include -enum cmd_id_enum { OPEN, CLOSE, TOGGLE, RESET, STATUS, LOG }; +enum cmd_id_enum { OPEN, CLOSE, TOGGLE, RESET, STATUS, LOG , LISTEN }; typedef enum cmd_id_enum cmd_id_t; struct cmd_struct { diff --git a/door_daemon/door_daemon.c b/door_daemon/door_daemon.c index e9c02a1..f7bb2f8 100644 --- a/door_daemon/door_daemon.c +++ b/door_daemon/door_daemon.c @@ -154,15 +154,7 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst) else if(!strncmp(cmd, "log", 3)) cmd_id = LOG; else if(!strncmp(cmd, "listen", 6)) { - client_t* listener = client_find(client_lst, fd); - if(listener) { - log_printf(DEBUG, "adding status listener %d", fd); - listener->status_listener = 1; - } - else - log_printf(ERROR, "unable to add status listener %d", fd); - - return 0; + cmd_id = LISTEN; } else { log_printf(WARNING, "unknown command '%s'", cmd); @@ -172,6 +164,21 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst) if(param) param++; + if(cmd_id == OPEN || cmd_id == CLOSE || cmd_id == TOGGLE) { + char* resp; + asprintf(&resp, "Request: %s", cmd); + if(resp) { + char* linefeed = strchr(resp, '\n'); + if(linefeed) linefeed[0] = 0; + client_t* client; + for(client = client_lst; client; client = client->next) + if(client->request_listener && client->fd != fd) + send_response(client->fd, resp); + free(resp); + } +// else silently ignore memory alloc error + } + switch(cmd_id) { case OPEN: case CLOSE: @@ -192,6 +199,33 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst) 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; + } } return 0; @@ -269,6 +303,13 @@ int process_door(read_buffer_t* buffer, int door_fd, cmd_t **cmd_q, client_t* cl if(client->status_listener && client->fd != cmd_fd) send_response(client->fd, buffer->buf); } + + if(!strncmp(buffer->buf, "Error:", 6)) { + client_t* client; + for(client = client_lst; client; client = client->next) + if(client->error_listener && client->fd != cmd_fd) + send_response(client->fd, buffer->buf); + } cmd_pop(cmd_q); buffer->offset = 0;