parsing exit and reset <LPT pin> commands, rewrite client connection handler and return apropriate status from it

This commit is contained in:
Sergey Popov 2012-05-13 19:25:52 +04:00
parent 17274b11b9
commit 5686c50bdf

View File

@ -8,30 +8,68 @@
#include <string.h> #include <string.h>
#include <pthread.h> #include <pthread.h>
#include <stdbool.h>
#include "yasnd-log.h" #include "yasnd-log.h"
#include "yasnd-lpt.h"
const char sock_path[]="/var/run/yasnd.sock"; const char sock_path[]="/var/run/yasnd.sock";
pthread_t server_thread_handle; // server socket thread handle pthread_t server_thread_handle; // server socket thread handle
int socket_fd; // server socket descriptor int socket_fd; // server socket descriptor
#define STATUS_OK (void*)0
#define STATUS_ERROR (void*)1
void* connection_handler(void* param) void* connection_handler(void* param)
{ {
// Get connection descriptor // Get connection descriptor
int connection_fd=*(int*)param; int connection_fd=*(int*)param;
int nbytes; // Buffer for client command and command's actual length
char buffer[256]; char buffer[256];
char tmpbuf[512]; int nbytes;
nbytes = read(connection_fd, buffer, 256); // Token for string splitting
buffer[nbytes] = 0; char* token;
sprintf(tmpbuf,"MESSAGE FROM CLIENT: %s\n", buffer); // Status of command parsing (default is OK)
log_event(tmpbuf); 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); close(connection_fd);
pthread_exit(0); // Exit client thread and return apropriate status
pthread_exit(thread_status);
} }
void* server_socket(void* param) void* server_socket(void* param)