#include "cs225.h"
#include "libcs225.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "network/PrSocket.h"
#include "network/PrDataInput.h"
#include "network/PrDataOutput.h"
#include "driver/PrUtility.h"

int GetLine(FILE *stream,char *line,int max)
{
  int i,c=EOF;

  for (i = 0; i < max-1 && (c = fgetc(stream)) != EOF && c != '\n'; i++) {
    if (c == '\b' && i>0) {
        i -= 2;
    } else {
        line[i] = (char)c;
    }
  }
  line[i] = '\0';
  if (c == EOF && i == 0) return -1;
  else return i;
}

void main()
{
  PrUtility *util = new PrUtility;

  printf("connecting to servo...\n");
  sleep(1);
  PrSocket *clientServo = new PrSocket("localhost",
				       CS225_USER_SERVO_TCP_PORT);
  if (clientServo->flag() != PR_SOCKET_OK)
    perror("client: can't create error");
  
  PrDataInput *inServo = new PrDataInput(clientServo);
  PrDataOutput *outServo = new PrDataOutput(clientServo);

  printf("connecting to gather...\n");
  sleep(1);
  PrSocket *clientGather = new PrSocket("localhost",
				   CS225_USER_GATHER_TCP_PORT);
  if (clientGather->flag() != PR_SOCKET_OK)
    perror("client: can't create error");
  
  PrDataInput *inGather = new PrDataInput(clientGather);
  PrDataOutput *outGather = new PrDataOutput(clientGather);

  int action,n;
  char buffer[CS225_BUFFER_SIZE];
  int done=0;

  printf("start: User\n");
  fflush(stdout);
  int count = 1;
  int bufferlen;
  int msec=0;

  while(done != 1)
    {
      memset(buffer,CS225_BUFFER_SIZE,NULL);

      printf("\nCS225: %d> ",count);
      fflush(stdout);
      bufferlen=GetLine(stdin,buffer,CS225_BUFFER_SIZE);
      if (bufferlen > 0) 
	{
	  if (strncmp(buffer,"wait",4)==0)
	    {
	      msec = atoi(buffer+4);
	      util->miliBlock(msec);
	    }
	  else if (strncmp(buffer,"load",4)==0)
	    {
	      char filename[CS225_BUFFER_SIZE];
	      strcpy(filename,buffer+5);

	      FILE *fd = fopen(filename,"r"); 
	      if (fd == NULL)
		{
		  printf("Error: couldn't open file(%s)\n",filename);
		}
	      else
		{
		  fflush(stdout);
		  while(1)
		    {
		      memset(buffer,CS225_BUFFER_SIZE,NULL);
		      bufferlen=GetLine(fd,buffer,CS225_BUFFER_SIZE);
		      if (bufferlen>0)
			{
			  printf("executing (%s)...\n",buffer);

			  if (strncmp(buffer,"wait",4)==0)
			    {
			      msec = atoi(buffer+4);
			      util->miliBlock(msec);
			    }
			  else
			    {
			      n = outServo->writeChar(buffer,bufferlen);
			      if (outServo->flush(CS225_TYPE_CHAR)!=n)
				{
				  printf("write error\n");
				} 
			    }
			}
		      else
			{
			  printf("done loading from file(%s)\n",filename);
			  break;
			}
		      util->microBlock(2000);
		    }
		  fclose(fd);
		}
	    }
	  else
	    {
	      if (strncmp(buffer,"exit",4)==0)
		{
		  action = CS225_ACTION_EXIT;
		  n = outGather->writeInt(&action,1);
		  if (outGather->flush(CS225_TYPE_ACTION) != n)
		    {
		      printf("write error\n");
		    }
		  done = 1;
		}
	      n = outServo->writeChar(buffer,bufferlen);
	      if (outServo->flush(CS225_TYPE_CHAR)!=n)
		{
		  printf("write error\n");
		} 
	    }
	  count++;
	}
      util->microBlock(2000);
    }
  delete inServo;
  delete outServo;
  clientServo->close();
  delete clientServo;
  delete inGather;
  delete outGather;
  clientGather->close();
  delete clientGather;
  delete util;
}
