From 4eada97796ddda887c932ec65225faad31380a29 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Sat, 20 Jan 2024 18:21:29 +0100 Subject: [PATCH] Implement client, change Makefile accordingly Because of signals merging, client now waits for 1000 us between bits for server to "certainly" handle the signal. This could use some tuning but a better protocol for communications would be better. --- Makefile | 24 +++++++++++++++------ src/client.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/server.c | 5 +++-- 3 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 src/client.c diff --git a/Makefile b/Makefile index 658fbf2..e79a212 100644 --- a/Makefile +++ b/Makefile @@ -12,20 +12,31 @@ INCLUDE := $(addprefix -I, $(INCDIR) $(SUBINCDIR)) SRCDIR := src -SOURCES := server.c \ +CLISOURCES := client.c \ -SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) +CLISOURCES := $(addprefix $(SRCDIR)/, $(CLISOURCES)) -OBJECTS := $(SOURCES:.c=.o) +CLIOBJECTS := $(CLISOURCES:.c=.o) -NAME := server +SERVSOURCES := server.c \ + +SERVSOURCES := $(addprefix $(SRCDIR)/, $(SERVSOURCES)) + +SERVOBJECTS := $(SERVSOURCES:.c=.o) + +OBJECTS := $(SERVOBJECTS) $(CLIOBJECTS) + +NAME := server client all : $(NAME) debug : CFLAGS += -g debug : $(NAME) -$(NAME) : $(OBJECTS) Libft/libft.a +server : $(SERVOBJECTS) Libft/libft.a + $(CC) $(CFLAGS) -o $@ $^ + +client : $(CLIOBJECTS) Libft/libft.a $(CC) $(CFLAGS) -o $@ $^ Libft/libft.a : @@ -45,7 +56,8 @@ clean : $(RM) $(OBJECTS) $(foreach proj, $(SUBPROJECTS), $(MAKE) -C $(proj) clean) -fclean : clean +fclean : + $(RM) $(OBJECTS) $(RM) $(NAME) $(foreach proj, $(SUBPROJECTS), $(MAKE) -C $(proj) fclean) diff --git a/src/client.c b/src/client.c new file mode 100644 index 0000000..c9e2a37 --- /dev/null +++ b/src/client.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* client.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/20 17:15:06 by ljiriste #+# #+# */ +/* Updated: 2024/01/20 18:14:47 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include +#include + +void send(pid_t server_pid, const char *message) +{ + int char_bits_sent; + int char_ind; + + char_bits_sent = 0; + char_ind = 0; + while (1) + { + if (message[char_ind] & 1 << char_bits_sent) + kill(server_pid, SIGUSR1); + else + kill(server_pid, SIGUSR2); + ++char_bits_sent; + if (char_bits_sent == CHAR_BIT && message[char_ind] == '\0') + break; + if (char_bits_sent == CHAR_BIT) + ++char_ind; + char_bits_sent %= CHAR_BIT; + usleep(1000); + } + return ; +} + +int main(int argc, char **argv) +{ + pid_t server_pid; + + if (argc != 3) + { + ft_printf("Invalid number of args. Th call should look like:\n" + "client SERVER_PID MESSAGE\n"); + return (0); + } + server_pid = ft_atoi(argv[1]); + if (kill(server_pid, 0)) + { + ft_printf("%i is not a valid PID.\n", server_pid); + return (0); + } + send(server_pid, argv[2]); + return (0); +} diff --git a/src/server.c b/src/server.c index 98e9ba5..cc4aa65 100644 --- a/src/server.c +++ b/src/server.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* main.c :+: :+: :+: */ +/* server.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/20 15:12:04 by ljiriste #+# #+# */ -/* Updated: 2024/01/20 17:06:28 by ljiriste ### ########.fr */ +/* Updated: 2024/01/20 18:12:40 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,6 +68,7 @@ int main(void) if (g_transfer.transfer_done) { ft_printf(g_transfer.string.vec); + ft_printf("\n"); ft_vec_forget_range(&g_transfer.string, g_transfer.string.size, 0); g_transfer.transfer_done = 0; } -- 2.30.2