diff --git a/Makefile b/Makefile index 6c4bf1c..15ff0c2 100644 --- a/Makefile +++ b/Makefile @@ -3,46 +3,41 @@ # variables names # value # - - - - - - - - - - - - - - # ! name is case sensitive -SERVER = server -CLIENT = client -CC = gcc VPATH = srcs - -S_SRCS = server.c -C_SRCS = client.c - +CC = gcc ODIR = ./builds -S_OBJS = $(S_SRCS:%.c=$(ODIR)/%.o) -C_OBJS = $(C_SRCS:%.c=$(ODIR)/%.o) - -DEPS = ./includes/minitalk.h \ - ./libft/includes/libft.h -IDIR = $(dir $(DEPS)) -# $(dir PATH/TO/FILE) expands to "PATH/TO/" -> the directory of a file - +IDIR = ./includes ./libft/includes LDIR = ./libft -_LIBS = libft.a -LIBS = $(_LIBS:lib%.a=%) - +LIBS = ft CFLAGS = $(IDIR:%=-I%) CFLAGS += -g3 -Wall -Wextra -Werror LFLAGS = -L./libft -lft +S_NAME = server +S_SRCS = server.c +S_OBJS = $(S_SRCS:%.c=$(ODIR)/%.o) +S_DEPS = ./includes/server.h + +C_NAME = client +C_SRCS = client.c +C_OBJS = $(C_SRCS:%.c=$(ODIR)/%.o) +C_DEPS = ./includes/client.h + # - - - - - - - - - - - - - - # target: prerequisites | $@ : target # rules to execute # recipe | $< : 1st prerequisite # - - - - - - - - - - - - - - # recipe | $^ : all prerequisites -all: libft $(SERVER) $(CLIENT) +all: libft $(S_NAME) $(C_NAME) libft: @echo "hello" make -C $(LDIR) -$(SERVER): $(S_OBJS) $(DEPS) +$(S_NAME): $(S_OBJS) $(S_DEPS) $(CC) $(CFLAGS) -o $@ $< $(LFLAGS) -$(CLIENT): $(C_OBJS) $(DEPS) +$(C_NAME): $(C_OBJS) $(C_DEPS) $(CC) $(CFLAGS) -o $@ $< $(LFLAGS) $(ODIR)/%.o: %.c | $(ODIR) @@ -61,7 +56,7 @@ clean: fclean: clean /bin/rm -rf $(ODIR) - /bin/rm -f $(SERVER) $(CLIENT) + /bin/rm -f $(S_NAME) $(C_NAME) /bin/rm -rf a.out a.out.dSYM libfclean: diff --git a/builds/client.o b/builds/client.o new file mode 100644 index 0000000..58a2b53 Binary files /dev/null and b/builds/client.o differ diff --git a/builds/server.o b/builds/server.o new file mode 100644 index 0000000..5316af3 Binary files /dev/null and b/builds/server.o differ diff --git a/client b/client new file mode 100755 index 0000000..acb8105 Binary files /dev/null and b/client differ diff --git a/includes/client.h b/includes/client.h new file mode 100644 index 0000000..baeab8d --- /dev/null +++ b/includes/client.h @@ -0,0 +1,12 @@ +#ifndef CLIENT_H +# define CLIENT_H + +# include // for signal kill sigaction +# include // for getpid kill +# include // for getpid +# include "libft.h" + +int msg_received; + +#endif + diff --git a/includes/minitalk.h b/includes/server.h similarity index 88% rename from includes/minitalk.h rename to includes/server.h index bd1a41d..03898e1 100644 --- a/includes/minitalk.h +++ b/includes/server.h @@ -1,5 +1,5 @@ -#ifndef MINITALK_H -# define MINITALK_H +#ifndef SERVER_H +# define SERVER_H # include // for signal kill sigaction # include // for getpid kill diff --git a/server b/server new file mode 100755 index 0000000..b7577da Binary files /dev/null and b/server differ diff --git a/srcs/client.c b/srcs/client.c index fa88fc4..bdb3244 100644 --- a/srcs/client.c +++ b/srcs/client.c @@ -1,4 +1,4 @@ -#include "../includes/minitalk.h" +#include "client.h" int usage(void) { @@ -13,12 +13,15 @@ void send_char(int server_pid, char c) mask = 1 << 6; while (mask != 0) { - if ((c & mask) != 0) - kill(server_pid, SIGUSR1); - if ((c & mask) == 0) - kill(server_pid, SIGUSR2); - mask >>= 1; - usleep(1); + if (msg_received == 1) + { + if ((c & mask) != 0) + kill(server_pid, SIGUSR1); + if ((c & mask) == 0) + kill(server_pid, SIGUSR2); + msg_received = 0; + mask >>= 1; + } } } @@ -32,11 +35,19 @@ void send_message(int server_pid, char *msg) send_char(server_pid, *msg); } +void message_received(int sig_num) +{ + (void)sig_num; + msg_received = 1; +} + int main(int ac, char **av) { + signal(SIGUSR1, message_received); if (ac != 3) return (usage()); - send_message(ft_atoi(av[1]), av[2]); + msg_received = 1; ft_putnbrendl((int)getpid()); + send_message(ft_atoi(av[1]), av[2]); return (0); } diff --git a/srcs/server.c b/srcs/server.c index 5eaf8c0..89d633d 100644 --- a/srcs/server.c +++ b/srcs/server.c @@ -1,10 +1,10 @@ -#include "minitalk.h" +#include "server.h" void sig_handler_1(int sig_num, siginfo_t *info, void *context) { (void)sig_num; (void)context; - kill(info->sa__pid, SIGUSR1); + kill(info->si_pid, SIGUSR1); message.character ^= 1 << (6 - message.count_bits); message.count_bits++; } @@ -13,7 +13,20 @@ void sig_handler_2(int sig_num, siginfo_t *info, void *context) { (void)sig_num; (void)context; - kill(info->sa__pid, SIGUSR2); + kill(info->si_pid, SIGUSR1); + message.count_bits++; +} + +void handler_1(int sig_num) +{ + (void)sig_num; + message.character ^= 1 << (6 - message.count_bits); + message.count_bits++; +} + +void handler_2(int sig_num) +{ + (void)sig_num; message.count_bits++; } @@ -51,6 +64,10 @@ int main() act_2.sa_sigaction = sig_handler_2; sigaction(SIGUSR1, &act_1, NULL); sigaction(SIGUSR2, &act_2, NULL); + +// signal(SIGUSR1, handler_1); +// signal(SIGUSR2, handler_2); + ft_putnbrendl((int)getpid()); init_message(); while (1)