From 5686c50bdf10b9d9ec5e442dfdbe019ea61d8437 Mon Sep 17 00:00:00 2001 From: Sergey Popov Date: Sun, 13 May 2012 19:25:52 +0400 Subject: [PATCH] parsing exit and reset commands, rewrite client connection handler and return apropriate status from it --- daemon/yasnd-sock.c | 52 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/daemon/yasnd-sock.c b/daemon/yasnd-sock.c index d810876..c1b2e14 100644 --- a/daemon/yasnd-sock.c +++ b/daemon/yasnd-sock.c @@ -8,30 +8,68 @@ #include #include +#include + #include "yasnd-log.h" +#include "yasnd-lpt.h" const char sock_path[]="/var/run/yasnd.sock"; pthread_t server_thread_handle; // server socket thread handle int socket_fd; // server socket descriptor +#define STATUS_OK (void*)0 +#define STATUS_ERROR (void*)1 + void* connection_handler(void* param) { // Get connection descriptor int connection_fd=*(int*)param; - int nbytes; + // Buffer for client command and command's actual length char buffer[256]; - char tmpbuf[512]; + int nbytes; - nbytes = read(connection_fd, buffer, 256); - buffer[nbytes] = 0; + // Token for string splitting + char* token; - sprintf(tmpbuf,"MESSAGE FROM CLIENT: %s\n", buffer); - log_event(tmpbuf); + // Status of command parsing (default is OK) + void* thread_status = STATUS_OK; + while ( (nbytes = read(connection_fd, buffer, 256)) != -1 ) + { + buffer[nbytes] = 0; + + token = strtok(buffer, " "); + if (token == NULL) + { + // empty input from client + thread_status = STATUS_ERROR; + break; + } + + // Exit function + if (strncmp(token, "exit", 4) == 0) + { + // just break loop and thus close client connection + break; + } + // Reset function + if (strncmp(token, "reset", 5) == 0) + { + // Get parameter for reset call and convert it to long + long value = strtol(strtok(NULL, " "), (char **) NULL, 10); + // Call reset_pin function + reset_pin((int)value); + // continue waiting client commands + continue; + } + } + + // Close client connection descriptor close(connection_fd); - pthread_exit(0); + // Exit client thread and return apropriate status + pthread_exit(thread_status); } void* server_socket(void* param)