aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-01-06 07:07:55 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-01-06 07:07:55 +0100
commitf9a06303ce72aa6697c17447dfef545db72c2407 (patch)
tree7cbc0638bcf7879f0b978881f66b51192e290e08
parentee48214209e66aa8c6f353262b175895373f76df (diff)
downloadrelabsd-f9a06303ce72aa6697c17447dfef545db72c2407.zip
relabsd-f9a06303ce72aa6697c17447dfef545db72c2407.tar.bz2
Working on the client/server communication...
-rw-r--r--include/relabsd/server_types.h1
-rw-r--r--src/device/axis/axis_filter.c2
-rw-r--r--src/server/communication_node.c4
-rw-r--r--src/server/communication_thread.c90
-rw-r--r--src/server/conversion_main_loop.c12
-rw-r--r--src/server/daemon.c13
-rw-r--r--src/server/server.c21
7 files changed, 122 insertions, 21 deletions
diff --git a/include/relabsd/server_types.h b/include/relabsd/server_types.h
index 51a17de..3a0a6fc 100644
--- a/include/relabsd/server_types.h
+++ b/include/relabsd/server_types.h
@@ -11,6 +11,7 @@
struct relabsd_server
{
+ pthread_mutex_t mutex;
pthread_t communication_thread;
struct relabsd_parameters parameters;
struct relabsd_physical_device physical_device;
diff --git a/src/device/axis/axis_filter.c b/src/device/axis/axis_filter.c
index eb14edd..04dfdb7 100644
--- a/src/device/axis/axis_filter.c
+++ b/src/device/axis/axis_filter.c
@@ -122,8 +122,6 @@ int relabsd_axis_filter_new_value
return 0;
}
- /* TODO: handle conf->axis[axis].resolution */
-
if (axis->flags[RELABSD_DIRECT])
{
return direct_filter(axis, value);
diff --git a/src/server/communication_node.c b/src/server/communication_node.c
index 96f4af4..c621f06 100644
--- a/src/server/communication_node.c
+++ b/src/server/communication_node.c
@@ -14,7 +14,6 @@
/******************************************************************************/
/**** LOCAL FUNCTIONS *********************************************************/
/******************************************************************************/
-
static int create_socket (int result [const restrict static 1])
{
errno = 0;
@@ -80,6 +79,7 @@ static int bind_socket
return 0;
}
+
/*
static int set_socket_to_unblocking (const int socket)
{
@@ -150,7 +150,6 @@ int relabsd_server_create_communication_node
if (bind_socket(socket_name, *socket) < 0)
{
- /* TODO: err message. */
(void) close(*socket);
return -1;
@@ -158,7 +157,6 @@ int relabsd_server_create_communication_node
if (set_socket_as_listener(*socket) < 0)
{
- /* TODO: err message. */
(void) close(*socket);
return -1;
diff --git a/src/server/communication_thread.c b/src/server/communication_thread.c
index e226f17..0f367da 100644
--- a/src/server/communication_thread.c
+++ b/src/server/communication_thread.c
@@ -9,19 +9,93 @@
/******************************************************************************/
/**** LOCAL FUNCTIONS *********************************************************/
/******************************************************************************/
-/* TODO: implement. */
-/*
-void main_loop (struct relabsd_server server [const static 1])
+
+static void main_loop (struct relabsd_server server [const static 1])
{
+ int communication_socket, current_client_socket;
+ int interrupt_fd, highest_fd;
+ int ready_fds;
+ fd_set ready_to_read;
+
+ if
+ (
+ relabsd_server_create_communication_node
+ (
+ relabsd_parameters_get_communication_node_name(&(server->parameters)),
+ &communication_socket
+ )
+ < 0
+ )
+ {
+ relabsd_server_interrupt();
+
+ return;
+ }
+
+ interrupt_fd = relabsd_server_get_interruption_file_descriptor();
+
+ if (interrupt_fd > communication_socket)
+ {
+ highest_fd = (interrupt_fd + 1);
+ }
+ else
+ {
+ highest_fd = (communication_socket + 1);
+ }
+
+ for (;;)
+ {
+ FD_ZERO(&ready_to_read);
+ FD_SET(communication_socket, &ready_to_read);
+ FD_SET(interrupt_fd, &ready_to_read);
+
+ ready_fds =
+ select
+ (
+ highest_fd,
+ &ready_to_read,
+ (fd_set *) NULL,
+ (fd_set *) NULL,
+ (struct timeval *) NULL
+ );
+
+ /* TODO: select error handling. */
+
+ if (!relabsd_server_keep_running())
+ {
+ return;
+ }
+
+ errno = 0;
+
+ current_client_socket =
+ accept
+ (
+ communication_socket,
+ (struct sockaddr *) NULL,
+ (socklen_t *) NULL
+ );
+
+ if (current_client_socket == -1)
+ {
+ RELABSD_ERROR
+ (
+ "Unable to accept on the server's socket: %s.",
+ strerror(errno)
+ );
+
+ return -1;
+ }
+
+ (void) handle_client(current_client_socket, server);
+ }
}
-*/
-void * posix_main_loop (void * params)
+static void * posix_main_loop (void * params)
{
- /* main_loop((struct relabsd_server *) params);*/
- params = NULL;
+ main_loop((struct relabsd_server *) params);
- return params;
+ return NULL;
}
/******************************************************************************/
diff --git a/src/server/conversion_main_loop.c b/src/server/conversion_main_loop.c
index c948954..e36bbd0 100644
--- a/src/server/conversion_main_loop.c
+++ b/src/server/conversion_main_loop.c
@@ -150,10 +150,10 @@ static int wait_for_next_event
{
struct timeval curr_timeout;
- /* TODO: mutex lock */
+ pthread_mutex_lock(&(server->mutex));
/* call to select may alter timeout */
curr_timeout = relabsd_parameters_get_timeout(&(server->parameters));
- /* TODO: mutex unlock */
+ pthread_mutex_unlock(&(server->mutex));
ready_fds =
select
@@ -228,17 +228,17 @@ int relabsd_server_conversion_loop
)
)
{
- /* TODO: mutex lock */
+ pthread_mutex_lock(&(server->mutex));
convert_input(server);
- /* TODO: mutex unlock */
+ pthread_mutex_unlock(&(server->mutex));
}
break;
case 0:
- /* TODO: mutex lock */
+ pthread_mutex_lock(&(server->mutex));
reset_axes(server);
- /* TODO: mutex unlock */
+ pthread_mutex_unlock(&(server->mutex));
break;
}
}
diff --git a/src/server/daemon.c b/src/server/daemon.c
index 9740b80..c9d29d7 100644
--- a/src/server/daemon.c
+++ b/src/server/daemon.c
@@ -186,7 +186,18 @@ int relabsd_server_create_daemon (void)
strerror(errno)
);
- /* TODO: boop main process. */
+ errno = 0;
+
+ if (write(unnamed_pipe[0], (void *) "!", (size_t) 1) == -1)
+ {
+ RELABSD_ERROR
+ (
+ "Unable to write to writing end of an unnamed pipe during the"
+ " daemon creation process: %s.",
+ strerror(errno)
+ );
+ }
+
return -1;
}
diff --git a/src/server/server.c b/src/server/server.c
index 2988732..1b17d12 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -1,3 +1,6 @@
+/**** POSIX *******************************************************************/
+#include <string.h>
+
/**** RELABSD *****************************************************************/
#include <relabsd/config.h>
#include <relabsd/debug.h>
@@ -16,6 +19,8 @@ static int initialize
struct relabsd_server server [const restrict static 1]
)
{
+ int err;
+
if
(
relabsd_physical_device_open
@@ -47,6 +52,18 @@ static int initialize
return -2;
}
+ err =
+ pthread_mutex_init(&(server->mutex), (const pthread_mutexattr_t *) NULL);
+
+ if (err != 0)
+ {
+ RELABSD_FATAL
+ (
+ "Could not initialize the server's mutex: %s.",
+ strerror(err)
+ );
+ }
+
if
(
(
@@ -59,7 +76,7 @@ static int initialize
relabsd_virtual_device_destroy(&(server->virtual_device));
relabsd_physical_device_close(&(server->physical_device));
- return -3;
+ return -4;
}
return 0;
@@ -78,6 +95,8 @@ static void finalize (struct relabsd_server server [const static 1])
relabsd_virtual_device_destroy(&(server->virtual_device));
relabsd_physical_device_close(&(server->physical_device));
+
+ (void) pthread_mutex_destroy(&(server->mutex));
}
/******************************************************************************/