aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-01-08 20:27:17 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-01-08 20:27:17 +0100
commitc7c834e9944c94de3d07f21ce2d2d3f18a03b3b2 (patch)
treed6f712b6817232ec3c2ed6ba6bfd47b033777ab0
parentd65e74a20a50e2161e5ff2007f53aea8e3b105e2 (diff)
downloadrelabsd-c7c834e9944c94de3d07f21ce2d2d3f18a03b3b2.zip
relabsd-c7c834e9944c94de3d07f21ce2d2d3f18a03b3b2.tar.bz2
Implements remote "-t" and "-q" commands.
-rw-r--r--include/relabsd/config/parameters.h9
-rw-r--r--include/relabsd/config/parameters_types.h1
-rw-r--r--include/relabsd/device/axis_types.h1
-rw-r--r--src/client.c15
-rw-r--r--src/config/parameters/handle_remote_client_commands.c199
-rw-r--r--src/config/parameters/parameters.c22
-rw-r--r--src/server/handle_client.c44
7 files changed, 240 insertions, 51 deletions
diff --git a/include/relabsd/config/parameters.h b/include/relabsd/config/parameters.h
index 3290c56..ae33e0b 100644
--- a/include/relabsd/config/parameters.h
+++ b/include/relabsd/config/parameters.h
@@ -1,5 +1,8 @@
#pragma once
+/**** POSIX *******************************************************************/
+#include <stdio.h>
+
/**** LIBEVDEV ****************************************************************/
#include <libevdev/libevdev.h>
@@ -48,6 +51,12 @@ int relabsd_parameters_parse_config_file
struct relabsd_parameters parameters [const restrict static 1]
);
+int relabsd_parameters_handle_remote_client
+(
+ FILE socket_as_file [const static 1],
+ struct relabsd_parameters parameters [const restrict static 1]
+);
+
/**** Accessors ***************************************************************/
void relabsd_parameters_initialize_options
(
diff --git a/include/relabsd/config/parameters_types.h b/include/relabsd/config/parameters_types.h
index f7b82b1..5b5c8e8 100644
--- a/include/relabsd/config/parameters_types.h
+++ b/include/relabsd/config/parameters_types.h
@@ -23,4 +23,5 @@ struct relabsd_parameters
int use_timeout;
struct timeval timeout;
struct relabsd_axis axes[RELABSD_AXIS_VALID_AXES_COUNT];
+ int device_name_was_modified;
};
diff --git a/include/relabsd/device/axis_types.h b/include/relabsd/device/axis_types.h
index 5ae0145..d13ccd8 100644
--- a/include/relabsd/device/axis_types.h
+++ b/include/relabsd/device/axis_types.h
@@ -40,4 +40,5 @@ struct relabsd_axis
int is_enabled;
int previous_value;
int flags[RELABSD_AXIS_FLAGS_COUNT];
+ int attributes_were_modified;
};
diff --git a/src/client.c b/src/client.c
index 0b84fe7..6e6b036 100644
--- a/src/client.c
+++ b/src/client.c
@@ -121,6 +121,11 @@ static int send_commands
return -1;
}
+ if (fputc('\n', socket) == EOF)
+ {
+ // TODO: error
+ }
+
for
(
i++;
@@ -128,20 +133,16 @@ static int send_commands
j--, i++
)
{
- if (fputc(' ', socket) == EOF)
+ if (fputs(argv[i], socket) == EOF)
{
// TODO: error
}
- if (fputs(argv[i], socket) == EOF)
+
+ if (fputc('\n', socket) == EOF)
{
// TODO: error
}
}
-
- if (fputc('\0', socket) == EOF)
- {
- // TODO: error
- }
}
if (fputc('\n', socket) == EOF)
diff --git a/src/config/parameters/handle_remote_client_commands.c b/src/config/parameters/handle_remote_client_commands.c
new file mode 100644
index 0000000..5523e37
--- /dev/null
+++ b/src/config/parameters/handle_remote_client_commands.c
@@ -0,0 +1,199 @@
+/**** POSIX *******************************************************************/
+/*
+ * To get the POSIX 'getline' function.
+ * We don't know what POSIX version is set by default.
+ */
+#define _POSIX_C_SOURCE 200809L
+
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**** RELABSD *****************************************************************/
+#include <relabsd/debug.h>
+#include <relabsd/server.h>
+
+#include <relabsd/util/string.h>
+
+#include <relabsd/config/parameters.h>
+
+/******************************************************************************/
+/**** TYPES *******************************************************************/
+/******************************************************************************/
+struct relabsd_parameters_client_input
+{
+ FILE * socket_as_file;
+ ssize_t size;
+ char * buffer;
+ size_t buffer_size;
+};
+
+/******************************************************************************/
+/**** LOCAL FUNCTIONS *********************************************************/
+/******************************************************************************/
+static int get_next_argument
+(
+ struct relabsd_parameters_client_input input [const restrict static 1]
+)
+{
+ errno = 0;
+ input->size =
+ getline
+ (
+ &(input->buffer),
+ &(input->buffer_size),
+ input->socket_as_file
+ );
+
+ if (input->size < 1)
+ {
+ RELABSD_ERROR
+ (
+ "Unable to read line from client socket: %s.",
+ strerror(errno)
+ );
+
+ return -1;
+ }
+
+ input->buffer[(input->size - 1)] = '\0';
+
+ return 0;
+}
+
+static void initialize_client_input
+(
+ FILE socket_as_file [const static 1],
+ struct relabsd_parameters_client_input input [const restrict static 1]
+)
+{
+ input->socket_as_file = socket_as_file;
+ input->size = 0;
+ input->buffer = (char *) NULL;
+ input->buffer_size = 0;
+}
+
+static void finalize_client_input
+(
+ struct relabsd_parameters_client_input input [const restrict static 1]
+)
+{
+ free((void *) input->buffer);
+
+ input->buffer = (char *) NULL;
+ input->buffer_size = 0;
+}
+
+static int handle_timeout_change
+(
+ struct relabsd_parameters_client_input input [const restrict static 1],
+ struct relabsd_parameters parameters [const static 1]
+)
+{
+ int timeout_msec;
+
+ if (get_next_argument(input) < 0)
+ {
+ RELABSD_S_ERROR("Could not get timeout value from client.");
+
+ return -1;
+ }
+
+ if (relabsd_util_parse_int(input->buffer, 0, INT_MAX, &timeout_msec) < 0)
+ {
+ RELABSD_S_ERROR("Invalid timeout value from client.");
+
+ return -1;
+ }
+
+ relabsd_parameters_set_timeout(timeout_msec, parameters);
+
+ return 0;
+}
+
+static int handle_inputs
+(
+ struct relabsd_parameters_client_input input [const restrict static 1],
+ struct relabsd_parameters parameters [const static 1]
+)
+{
+ for (;;)
+ {
+ if (get_next_argument(input) < 0)
+ {
+ RELABSD_S_ERROR("Could not get next client command.");
+
+ return -1;
+ }
+
+ if ((input->buffer[0] == '\n') || (input->buffer[0] == '\0'))
+ {
+ return 0;
+ }
+ else if
+ (
+ RELABSD_STRING_EQUALS("-q", input->buffer)
+ || RELABSD_STRING_EQUALS("--quit", input->buffer)
+ )
+ {
+ relabsd_server_interrupt();
+ }
+ else if
+ (
+ RELABSD_STRING_EQUALS("-t", input->buffer)
+ || RELABSD_STRING_EQUALS("--timeout", input->buffer)
+ )
+ {
+ if (handle_timeout_change(input, parameters) < 0)
+ {
+ return 0;
+ }
+ }
+ else if
+ (
+ RELABSD_STRING_EQUALS("-n", input->buffer)
+ || RELABSD_STRING_EQUALS("--name", input->buffer)
+ )
+ {
+ /* TODO: implement */
+ RELABSD_PROG_ERROR("Unimplemented command \"%s\".", input->buffer);
+ }
+ else if
+ (
+ RELABSD_STRING_EQUALS("-m", input->buffer)
+ || RELABSD_STRING_EQUALS("--mod-axis", input->buffer)
+ )
+ {
+ /* TODO: implement */
+ RELABSD_PROG_ERROR("Unimplemented command \"%s\".", input->buffer);
+ }
+ else
+ {
+ RELABSD_ERROR("Unknown client command \"%s\"", input->buffer);
+
+ return -1;
+ }
+ }
+}
+
+/******************************************************************************/
+/**** EXPORTED FUNCTIONS ******************************************************/
+/******************************************************************************/
+int relabsd_parameters_handle_remote_client
+(
+ FILE socket_as_file [const static 1],
+ struct relabsd_parameters parameters [const restrict static 1]
+)
+{
+ struct relabsd_parameters_client_input input;
+
+ initialize_client_input(socket_as_file, &input);
+
+ (void) handle_inputs(&input, parameters);
+
+ finalize_client_input(&input);
+
+ return 0;
+}
diff --git a/src/config/parameters/parameters.c b/src/config/parameters/parameters.c
index 0e98016..84b348c 100644
--- a/src/config/parameters/parameters.c
+++ b/src/config/parameters/parameters.c
@@ -337,8 +337,8 @@ int relabsd_parameters_argument_count_for
{
if
(
- RELABSD_STRING_EQUALS("-n", option)
- || RELABSD_STRING_EQUALS("--name", option)
+ RELABSD_STRING_EQUALS("-q", option)
+ || RELABSD_STRING_EQUALS("--quit", option)
)
{
*result = 0;
@@ -373,7 +373,7 @@ int relabsd_parameters_argument_count_for
|| RELABSD_STRING_EQUALS("--mod-axis", option)
)
{
- *result = 7;
+ *result = 3;
}
else if
(
@@ -411,7 +411,8 @@ void relabsd_parameters_print_usage (const char exec [const restrict static 1])
"\t%s [-? | --compatible] <physical_device_file> [<CONF_OPTION>+]\n"
"\t\tDevice & configuration compatibility test.\n\n"
- "\t%s [-c | --client] <server_file> [(<CLIENT_OPTION>|<CONF_OPTION>)+]"
+ "\t%s [-c | --client] <server_file> "
+ "[(<CLIENT_OPTION>|<GLOBAL_CONF_OPTION>)+]"
"\n"
"\t\tSends the commands to a given server instance.\n\n"
@@ -423,21 +424,24 @@ void relabsd_parameters_print_usage (const char exec [const restrict static 1])
" [(<SERVER_OPTION>|<CONF_OPTION>)+]:\n"
"\t\tCreates an unnamed server instance.\n\n"
- "<CONF_OPTION>:\n"
+ "<GLOBAL_CONF_OPTION>:\n"
"\t[-n | --name] <relabsd_device_name>:\n"
"\t\tNames the virtual device.\n\n"
"\t[-t | --timeout] <timeout_in_ms>:\n"
"\t\tSets a zeroing timeout (0 to disable).\n\n"
+ "\t[-m | --mod-axis] <name> [min|max|fuzz|flat|resolution|enable]"
+ " [+|-|=]<value>:\n"
+ "\t\tModifies an axis.\n\n"
+
+ "<CONF_OPTION>:\n"
+ "\t<GLOBAL_CONF_OPTION>\n\n"
+
"\t[-a | --axis] <name> <min> <max> <fuzz> <flat> <resolution> "
"<options>:\n"
"\t\t(Re)defines an axis.\n\n"
- "\t[-m | --mod-axis] <name> [min|max|fuzz|flat|resolution]"
- " [+|-|=]<value>:\n"
- "\t\tModifies an axis.\n\n"
-
"\t[-f | --config] <config_file>"
"\t\tUse the options defined in <config_file>.\n\n"
diff --git a/src/server/handle_client.c b/src/server/handle_client.c
index bc18e95..5212440 100644
--- a/src/server/handle_client.c
+++ b/src/server/handle_client.c
@@ -15,17 +15,11 @@
#include <relabsd/debug.h>
#include <relabsd/server.h>
+#include <relabsd/config/parameters.h>
+
/******************************************************************************/
/**** LOCAL FUNCTIONS *********************************************************/
/******************************************************************************/
-static void handle_input
-(
- const ssize_t input_size __attribute__((unused)),
- const char input [const static 1] __attribute__((unused)),
- struct relabsd_server server [const static 1] __attribute__((unused))
-)
-{
-}
/******************************************************************************/
/**** EXPORTED FUNCTIONS ******************************************************/
@@ -37,10 +31,6 @@ int relabsd_server_handle_client
)
{
FILE * socket_as_file;
- /* FIXME: reallocating at every new connection is kind of wasteful. */
- char * input;
- ssize_t input_size;
- size_t input_buffer_size;
errno = 0;
socket_as_file = fdopen(socket, "r");
@@ -58,29 +48,13 @@ int relabsd_server_handle_client
return -1;
}
- errno = 0;
-
- input_size = getline(&input, &input_buffer_size, socket_as_file);
-
- if (input_size < 1)
- {
- RELABSD_ERROR
- (
- "Unable to read line from client socket: %s.",
- strerror(errno)
- );
-
- (void) free((void *) input);
-
- /* This also closes 'socket' */
- (void) fclose(socket_as_file);
-
- return -1;
- }
-
- handle_input(input_size, input, server);
-
- (void) free((void *) input);
+ pthread_mutex_lock(&(server->mutex));
+ (void) relabsd_parameters_handle_remote_client
+ (
+ socket_as_file,
+ &(server->parameters)
+ );
+ pthread_mutex_unlock(&(server->mutex));
/* This also closes 'socket' */
(void) fclose(socket_as_file);