#include "client.h"

int	usage(void)
{
	ft_printf("usage: ./client [server pid] [message]\n");
	return (0);
}

void	send_char(int server_pid, char c)
{
	int mask;

	mask = 1 << 6;
	while (mask != 0)
	{
		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;
		}
	}
}

void	send_message(int server_pid, char *msg)
{
	while (*msg)
	{
		send_char(server_pid, *msg);
		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());
	msg_received = 1;
	ft_putnbrendl((int)getpid());
	send_message(ft_atoi(av[1]), av[2]);
	return (0);
}
