#include "docking.h"

int csock, dsock;					// chinput socket, and docking socket
int init_ready;

void dock_cleanup (int, siginfo_t*, void*);
void dock_signal_handler (int, siginfo_t*, void*);

int main (int argc, char *argv[])
{
	size_t write_cnt;
	size_t read_cnt;
	DockMessage to_dmess, from_dmess;
	fd_set r_fdset;
	struct timeval tv;

	if (create_socket (&dsock, HZDSOCK_ADDR))
		exit (1);

	if (connect_peer (&csock, HZCSOCK_ADDR))
		exit (1);

	if (set_signal_handler (SIGTERM, &dock_cleanup))
		exit (1);

	if (send_init (csock))
		exit (1);

	if (set_sock_attr (dsock))
		exit (1);

	if (set_signal_handler (SIGIO, &dock_signal_handler))
		exit (1);

	while (1);
}

void dock_signal_handler (int signum, siginfo_t *sig_info, void *misc)
{
	int read_cnt;
	DockMessage from_mess;

	if (sig_info->si_signo != SIGIO)
	{
		fprintf (stderr, "dock_signal_handler (): Other than SIGIO\n");
		return;
	}

	if (sig_info->si_fd != dsock)
	{
		fprintf (stderr, "dock_signal_handler (): Other than dsock\n");
		return;
	}

	read_cnt = read (dsock, &from_mess, sizeof (DockMessage));
	if (read_cnt < 0)
	{
		perror ("dock_signal_handler () >> read () :");
		return;
	}
	else if (read_cnt == 0)
	{
		fprintf (stderr, "dock_signal_handler () >> read () : read_cnt == 0");
		return;
	}
	else
	{
		fprintf (stderr, "Signal #%d got!\n", from_mess.message_type);
		switch (from_mess.message_type)
		{
			case HZ_DOCK_INIT_ACK:
				if (store_print_init_ack ())
					fprintf (stderr, "store_print_init_ack error\n");
				break;
			case HZ_DOCK_REPLY_LIST:
				if (store_print_list (from_mess))
					fprintf (stderr, "store_print_list ERROR\n");
				break;
			case HZ_DOCK_CHANGE_IM:
				break;
			case HZ_DOCK_SYNC_REPLY:
				break;
			default:
				break;
		}
	}
}

int store_print_init_ack (void)
{
	int write_cnt;
	DockMessage to_mess;

	fprintf (stderr, "store_print_init_ack ()\n");
	fprintf (stderr, "Prepare to send HZ_DOCK_REQUEST_LIST.\n");
	
	memset (&to_mess, 0, sizeof (DockMessage));
	to_mess.message_type = HZ_DOCK_REQUEST_LIST;
	write_cnt = write (csock, &to_mess, sizeof (DockMessage));
	if (write_cnt < 0)
	{
		perror ("store_print_init_ack () >> read ()");
		return 1;
	}
	else if (write_cnt == 0)
	{
		perror ("store_print_init_ack () >> read (): Warning write_cnt == 0");
		return 1;
	}
	else
	{
		fprintf (stderr, "HZ_DOCK_REQUEST_LIST sent already!\n");
	}
	return 0;
}

int store_print_list (DockMessage mess)
{
}

int store_print_im (DockMessage mess)
{
}

int store_print_sync_reply (DockMessage mess)
{
}

int send_init (int sock)
{
	DockMessage to_dmess;
	int write_cnt;
	
	memset (&to_dmess, 0, sizeof (DockMessage));
	to_dmess.message_type = HZ_DOCK_INIT;

	write_cnt = write (sock, &to_dmess, sizeof (DockMessage));
	if (write_cnt < 0)
	{
		perror ("send_init () >> write ()");
		return (1);
	}
	else if (write_cnt == 0)
	{
		perror ("send_init () >> write () >> warning, write_cnt == 0");
	}
	return 0;
}

void dock_cleanup (int signum, siginfo_t *sig_info, void *misc)
{
	if (sig_info->si_code != SIGTERM &&
			sig_info->si_code != SIGINT)
		return;

	close (csock);
	close (dsock);
}
