diff --git a/client.c b/client.c new file mode 100644 index 0000000..e4ac751 --- /dev/null +++ b/client.c @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* client.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/07/18 16:28:03 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/07/27 14:43:17 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include +#define SLEEP 100 + +static volatile int g_ack = 0; + +size_t ft_strlen(const char *s) +{ + size_t length; + + length = 0; + while (*s++) + length++; + return (length); +} + +void ft_putstr(char *str) +{ + write(1, str, ft_strlen(str)); +} + +int ft_isspace(int c) +{ + return (' ' == c || '\f' == c || '\n' == c + || '\r' == c || '\t' == c || '\v' == c); +} + +int ft_isdigit(char c) +{ + return (c >= '0' && c <= '9'); +} + +int ft_atoi(const char *nptr) +{ + int sign; + int res; + + while (ft_isspace(*nptr)) + nptr++; + res = 0; + sign = 1; + while (*nptr == '-' || *nptr == '+') + { + if (*nptr == '-') + sign *= -1; + nptr++; + } + while (ft_isdigit(*nptr)) + res = 10 * res + sign * (*nptr++ - '0'); + return (res); +} + +int send_byte(int pid, unsigned char c) +{ + int i; + + i = 0; + while (i < 8) + { + if ((c & 128) == 128) + kill(pid, SIGUSR1); + else + kill(pid, SIGUSR2); + c = c << 1; + usleep(SLEEP); + while (!g_ack) + pause(); + g_ack = 0; + i++; + } + return (1); +} + +int send_message(int pid, char *msg) +{ + while (*msg) + { + send_byte(pid, *msg); + msg++; + } + send_byte(pid, '\0'); + return (1); +} + +void sig(int signum) +{ + (void)signum; + g_ack = 1; +} + +int main(int argc, char *argv[]) +{ + int pid; + + if (argc != 3) + ft_putstr("Usage: ./client "); + pid = ft_atoi(argv[1]); + if (pid <= 1) + return (1); + g_ack = 0; + signal(SIGUSR1, sig); + send_message(pid, argv[2]); + return (0); +} diff --git a/server.c b/server.c new file mode 100644 index 0000000..6ae4dcf --- /dev/null +++ b/server.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* server.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/07/18 17:24:54 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/07/27 14:40:08 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +void ft_putstr(char *str) +{ + int i; + + i = 0; + while (str[i]) + { + i++; + } + write(1, str, i); +} + +void signal_handler(int signum, siginfo_t *info, void *context) +{ + static int count = 0; + static char c = '\0'; + static pid_t client_pid; + + (void) context; + if (client_pid != info->si_pid) + { + count = 0; + c = '\0'; + client_pid = info->si_pid; + } + c = c << 1; + if (signum == SIGUSR1) + c |= 1; + count++; + if (count == 8) + { + if (c) + write(1, &c, 1); + else + write(1, "\n", 1); + c = '\0'; + count = 0; + } + kill(info->si_pid, SIGUSR1); + return ; +} + +int main(void) +{ + struct sigaction sa; + + printf("MiniTalk Server started on pid: %d\n", getpid()); + sa.sa_sigaction = signal_handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_SIGINFO; + sigaction(SIGUSR1, &sa, NULL); + sigaction(SIGUSR2, &sa, NULL); + while (1) + pause(); +}