Implement client, change Makefile accordingly
authorLukas Jiriste <ljiriste@student.42prague.com>
Sat, 20 Jan 2024 17:21:29 +0000 (18:21 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 20 Jan 2024 17:21:29 +0000 (18:21 +0100)
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
src/client.c [new file with mode: 0644]
src/server.c

index 658fbf2149164aa2611a1130eb08eee2ce2c76ca..e79a2124f4e907493837dec0ed9d4ab5a09ac171 100644 (file)
--- 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 (file)
index 0000000..c9e2a37
--- /dev/null
@@ -0,0 +1,60 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   client.c                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/01/20 17:15:06 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/01/20 18:14:47 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include <limits.h>
+#include <signal.h>
+#include <unistd.h>
+
+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);
+}
index 98e9ba5c37b974d9b9b5576ae3b0cdc24eb98912..cc4aa65ce93c49b423ddd33bc0e4e63401400369 100644 (file)
@@ -1,12 +1,12 @@
 /* ************************************************************************** */
 /*                                                                            */
 /*                                                        :::      ::::::::   */
-/*   main.c                                             :+:      :+:    :+:   */
+/*   server.c                                           :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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;
                }