/* engine.c
 * Copyright (C) 2009 Thunor <thunorsif@hotmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>	/* For mkfifo */
#include <errno.h>		/* For errno */
#include <fcntl.h>		/* For open, O_RDONLY and O_WRONLY */
#include <unistd.h>		/* For read and write */

/* Object IDs */
#define OBJECT_ROOT 1

/* Event IDs */
#define EVENT_INIT 1
#define EVENT_QUIT 2

/* Function IDs */
#define FUNC_INIT 1
#define FUNC_QUIT 2
#define FUNC_EXIT 3
#define FUNC_DO_SOMETHING 4

int main(int argc, char *argv[]) {
	char pipeout_filename[255], pipein_filename[255], pipebuffer[255];
	int retval, pipeoutfd, pipeinfd, loopcount, func_id, intparam[3];
	char error_message[256];
    
	/* Check command line */
	if (argc < 3) {
		printf("%s: PIPEIN or PIPEOUT missing from command line\n", argv[0]);
		exit (1);        
	}

	/* Store the pipe names passed via the command line */
	strcpy(pipein_filename, argv[1]);
	printf("%s: PIPEIN to be named %s\n", argv[0], argv[1]);
	strcpy(pipeout_filename, argv[2]);
	printf("%s: PIPEOUT to be named %s\n", argv[0], argv[2]);

	/* Create the pipe PIPEIN */
	retval = mkfifo(pipein_filename, 0666);
	if ((retval == -1) && (errno != EEXIST)) {
		sprintf(error_message, "%s: Error creating PIPEIN %s", argv[0], pipein_filename);
		perror(error_message);
		exit (1);
	}

	/* Create the pipe PIPEOUT */
	retval = mkfifo(pipeout_filename, 0666);
	if ((retval == -1) && (errno != EEXIST)) {
		sprintf(error_message, "%s: Error creating PIPEOUT %s", argv[0], pipeout_filename);
		perror(error_message);
		exit (1);
	}

	/* Open PIPEIN for reading (very important this is done first) */
	printf("%s: Opening PIPEIN for reading\n", argv[0]);
	pipeinfd = open(pipein_filename, O_RDONLY);
	printf("%s: PIPEIN is now open\n", argv[0]);

	/* Open PIPEOUT for writing (very important this is done second) */
	printf("%s: Opening PIPEOUT for writing\n", argv[0]);
	pipeoutfd = open(pipeout_filename, O_WRONLY);
	printf("%s: PIPEOUT is now open\n", argv[0]);

	printf("%s: Entering function processing loop\n", argv[0]);
	while (1) {
		/* This will proceed if nothing is read */
		retval = read(pipeinfd, pipebuffer, 255);
		/* printf("%s: Loop %i\n", argv[0], loopcount++); */	/* Uncomment this to see it looping */
		if (retval > 0) {
			pipebuffer[retval] = 0;	/* Must terminate string */
			func_id = 0;
			retval = sscanf(pipebuffer, "%i", &func_id);
			if (retval > 0) {
				if (func_id == FUNC_INIT) {
					printf("%s: Received FUNC_INIT\n", argv[0]);
					/* Send an init event to enable the game to initialise */
					sprintf(pipebuffer, "%i %i\n", OBJECT_ROOT, EVENT_INIT);	/* \n is very important */
					printf("%s: Sending EVENT_INIT\n", argv[0]);
					write(pipeoutfd, pipebuffer, strlen(pipebuffer));
				} else if (func_id == FUNC_QUIT) {
					printf("%s: Received FUNC_QUIT\n", argv[0]);
					/* Send a quit event to enable the game to do some tidying up */
					sprintf(pipebuffer, "%i %i\n", OBJECT_ROOT, EVENT_QUIT);	/* \n is very important */
					printf("%s: Sending EVENT_QUIT\n", argv[0]);
					write(pipeoutfd, pipebuffer, strlen(pipebuffer));
				} else if (func_id == FUNC_EXIT) {
					printf("%s: Received FUNC_EXIT\n", argv[0]);
					/* Shutdown the engine */
					break;
				} else if (func_id == FUNC_DO_SOMETHING) {
					intparam[0] = 0; intparam[1] = 0;
					sscanf(pipebuffer, "%i %i %i", &func_id, &intparam[0], &intparam[1]);
					printf("%s: Received FUNC_DO_SOMETHING %i %i\n", argv[0], intparam[0], intparam[1]);
					/* Send something back */
					sprintf(pipebuffer, "%i\n", intparam[0] + intparam[1]);		/* \n is very important */
					printf("%s: Replying to FUNC_DO_SOMETHING\n", argv[0]);
					write(pipeoutfd, pipebuffer, strlen(pipebuffer));
				} else {
					printf("%s: Received unknown func_id=%i\n", argv[0], func_id);
				}
			}
		}
	}

	/* Close PIPEIN */
	printf("%s: Closing PIPEIN\n", argv[0]);
	close(pipeinfd);

	/* Close PIPEOUT */
	printf("%s: Closing PIPEOUT\n", argv[0]);
	close(pipeoutfd);

	/* Remove PIPEIN */
	printf("%s: Removing PIPEIN\n", argv[0]);
	unlink(pipein_filename);

	/* Remove PIPEOUT */
	printf("%s: Removing PIPEOUT\n", argv[0]);
	unlink(pipeout_filename);

	printf("%s: Goodbye\n", argv[0]);

	return 0;
}


