Create project, implement server
authorLukas Jiriste <ljiriste@student.42prague.com>
Sat, 20 Jan 2024 16:14:18 +0000 (17:14 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 20 Jan 2024 16:14:18 +0000 (17:14 +0100)
.gitignore [new file with mode: 0644]
.gitmodules [new file with mode: 0644]
Libft [new submodule]
Makefile [new file with mode: 0644]
src/server.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..1f84a6b
--- /dev/null
@@ -0,0 +1,3 @@
+server
+client
+*.o
diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..626d139
--- /dev/null
@@ -0,0 +1,3 @@
+[submodule "Libft"]
+       path = Libft
+       url = git://ljiriste.work/Libft
diff --git a/Libft b/Libft
new file mode 160000 (submodule)
index 0000000..a4212a0
--- /dev/null
+++ b/Libft
@@ -0,0 +1 @@
+Subproject commit a4212a0f1de2cf7553f3cde8931c90fe006f3cd4
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..82bb9d5
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,53 @@
+CC := gcc
+#CFLAGS = -std=c99 -Wall -Wextra -Werror -Wpedantic
+CFLAGS = -Wall -Wextra -Werror -Wpedantic
+
+RM := rm -f
+
+SUBPROJECTS := Libft
+
+INCDIR := inc
+SUBINCDIR := $(addsuffix /inc, $(SUBPROJECTS));
+INCLUDE := $(addprefix -I, $(INCDIR) $(SUBINCDIR))
+
+SRCDIR := src
+
+SOURCES :=     main.c                  \
+
+SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
+
+OBJECTS := $(SOURCES:.c=.o)
+
+NAME := server
+
+all : $(NAME)
+
+debug : CFLAGS += -g
+debug : $(NAME)
+
+$(NAME) : $(OBJECTS) Libft/libft.a
+       $(CC) $(CFLAGS) -o $@ $^
+
+Libft/libft.a :
+ifneq (,$(findstring debug, $(MAKECMDGOALS)))
+       $(MAKE) -C Libft debug
+else
+       $(MAKE) -C Libft
+endif
+
+%.o : %.c | $(SUBINCDIR)
+       $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE)
+
+%/inc :
+       git submodule update --init $($@%/inc=%)
+
+clean :
+       $(RM) $(OBJECTS)
+       $(foreach proj, $(SUBPROJECTS), $(MAKE) -C $(proj) clean)
+
+fclean : clean
+       $(RM) $(NAME)
+       $(foreach proj, $(SUBPROJECTS), $(MAKE) -C $(proj) fclean)
+
+re : fclean
+       $(MAKE) all
diff --git a/src/server.c b/src/server.c
new file mode 100644 (file)
index 0000000..98e9ba5
--- /dev/null
@@ -0,0 +1,76 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.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       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include <limits.h>
+#include <unistd.h>
+#include <signal.h>
+
+typedef struct s_transfer
+{
+       t_vec   string;
+       int             bits_written;
+       int             transfer_done;
+}                      t_transfer;
+
+t_transfer     g_transfer;
+
+void   signal_bit_decoder(int signum)
+{
+       const char      null_char = 0;
+
+       if (g_transfer.bits_written == 0)
+               ft_vec_append(&g_transfer.string, &null_char);
+       if (signum == SIGUSR1)
+               *(char *)ft_vec_access(&g_transfer.string, g_transfer.string.size - 1) |= 1 << g_transfer.bits_written;
+       ++g_transfer.bits_written;
+       g_transfer.transfer_done = (g_transfer.bits_written == CHAR_BIT && !(*(char *)ft_vec_access(&g_transfer.string, g_transfer.string.size - 1)));
+       g_transfer.bits_written %= CHAR_BIT;
+       return ;
+}
+
+void   setup_handler(void)
+{
+       struct sigaction        sa;
+
+       ft_memset(&sa, 0, sizeof(sa));
+       sigemptyset(&sa.sa_mask);
+       sigaddset(&sa.sa_mask, SIGUSR1);
+       sigaddset(&sa.sa_mask, SIGUSR2);
+       sa.sa_handler = signal_bit_decoder;
+       sigaction(SIGUSR1, &sa, NULL);
+       sigaction(SIGUSR2, &sa, NULL);
+       return ;
+}
+
+int    main(void)
+{
+       ft_vec_init(&g_transfer.string, sizeof(char));
+       setup_handler();
+       ft_printf("Hello, I'm a minitalk server.\n"
+                       "You can contact me through my PID: %i.\n"
+                       "For legibility I'll leave one empty line "
+                       "after this initial message.\n"
+                       "I'll also append newline to every message recieved.\n"
+                       "I'm starting to print the messages now:\n\n", getpid());
+       while (1)
+       {
+               pause();
+               if (g_transfer.transfer_done)
+               {
+                       ft_printf(g_transfer.string.vec);
+                       ft_vec_forget_range(&g_transfer.string, g_transfer.string.size, 0);
+                       g_transfer.transfer_done = 0;
+               }
+       }
+       return (0);
+}