parsing exit and reset <LPT pin> commands, rewrite client connection handler and return apropriate status from it
This commit is contained in:
parent
17274b11b9
commit
5686c50bdf
@ -8,30 +8,68 @@
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#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);
|
||||
// Token for string splitting
|
||||
char* token;
|
||||
|
||||
// Status of command parsing (default is OK)
|
||||
void* thread_status = STATUS_OK;
|
||||
|
||||
while ( (nbytes = read(connection_fd, buffer, 256)) != -1 )
|
||||
{
|
||||
buffer[nbytes] = 0;
|
||||
|
||||
sprintf(tmpbuf,"MESSAGE FROM CLIENT: %s\n", buffer);
|
||||
log_event(tmpbuf);
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user