#include <../arch/arch.h>

int main(int argc,char *argv[])
{
	arch_pipe_t pipe;
	char msg[IPC_MSG_LEN] = "";
	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);
	if(argc == 2 && !strcmp(argv[1],"-server"))
	{
		if(!archInboundPipe("Hello",&pipe))
			return -1;
		do
		{
			puts("SERVER: waiting for input...");
			if(!archInboundPipeWait(&pipe,10000))
			{
				puts("SERVER: no messages within 10 seconds.");
				continue;
			}
			archInboundPipeRecv(&pipe,msg);
			printf("SERVER: received '%s'\n",msg);
		} while(strcmp(msg,"quit"));
		puts("SERVER: DONE");
		archPipeClose(&pipe);
	}
	else
	{
		if(argc != 1)
		{
			printf("Usage: %s [-server]\n",argv[0]);
			return -1;
		}
		if(!archOutboundPipe("Hello",&pipe))
			return -1;
		while(strcmp(msg,"quit"))
		{
			fgets(msg,IPC_MSG_LEN,stdin);
			strStripTrail(msg);
			archOutboundPipeSend(&pipe,msg);
		}
		archPipeClose(&pipe);
	}
	memPrintStats();
	return 0;
}
