#include "cs225.h"
#include "libcs225.h"
#include <stdio.h>
#include <string.h>
#include "network/PrSocket.h"
#include "network/PrServerSocket.h"
#include "network/PrDataInput.h"
#include "network/PrDataOutput.h"
#include "utility/PrMatlab.h"

void main()
{
  PrServerSocket *serverUser = new PrServerSocket(CS225_USER_GATHER_TCP_PORT);
  if (serverUser->flag() != PR_SERVERSOCKET_OK)
    perror("server: can't create serverUser error");

  PrSocket *clientUser = serverUser->accept();
  PrDataInput *inUser = new PrDataInput(clientUser);
  PrDataOutput *outUser = new PrDataOutput(clientUser);


//  printf("\ngather::connecting servo...\n");
  PrSocket *clientServo = new PrSocket("localhost",
				       CS225_GATHER_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);

  int action,n,numTotal;
  int flag = PR_MATLAB_ERROR;
  char filename[CS225_BUFFER_SIZE];
  GatherMesg *mesg = NULL;

  PrMatlab *matlab = new PrMatlab;
  int done=0;

  int count=0;

  while(!done)
    {
      n=inUser->ready();
      if (n>=0)
	{
	  switch(inUser->type())
	    {
	    case CS225_TYPE_ACTION:
	      inUser->readInt(&action,1);
	      switch(action)
		{
		case CS225_ACTION_EXIT:
		  done = 1;
		  matlab->end();
		  if (mesg != NULL)
		    delete[] mesg;
		  break;
		default:
		  printf("unknown action\n");
		  break;
		}
	      break;
	    default:
	      printf("unknown type\n");
	      break;
	    }
	}
    

      n=inServo->ready();

      if (n>=0)
	{
	  switch(inServo->type())
	    {
	    case CS225_TYPE_ACTION:
	      inServo->readInt(&action,1);
	      switch(action)
		{
		case CS225_ACTION_GATHER_START:
		  inServo->readInt(&numTotal,1);
		  count = 0;
		  if (mesg != NULL)
		    delete[] mesg;

		  mesg = new GatherMesg[numTotal];
		  n-=4;
		  inServo->readChar(filename,n);
		  flag = matlab->start(filename);

		  if (flag == PR_MATLAB_OK)
		    {
		      if (numTotal>0)
			{
			  printf("\n\n<%s>: is the output file!\n\n",filename);
			  outServo->flush(CS225_TYPE_GATHER_DATA);
			}
		      else
			{
			  matlab->end();
			}
		    }
		  break;
		default:
		  printf("unknown action\n");
		  break;
		}
	      break;
	    case CS225_TYPE_GATHER_DATA:
	      if (flag == PR_MATLAB_OK)
		{
		  inServo->readFloat(mesg[count].data,CS225_GATHER_DATA_NUM);
		  count++;
		  if (count < numTotal)
		    {
		      outServo->flush(CS225_TYPE_GATHER_DATA);
		    }
		  else
		    {
		      matlab->save("A",PR_MATLAB_FLOAT,mesg,
				   numTotal,CS225_GATHER_DATA_NUM);
		      matlab->end();
		      delete[] mesg;
		      mesg = NULL;
		      flag = PR_MATLAB_ERROR;
		      printf("\n\n(%d) data are saved in <%s>\n\n",count,filename);

		      action = CS225_ACTION_GATHER_END;
		      n=outServo->writeInt(&action,1);
		      if (outServo->flush(CS225_TYPE_ACTION)!=n)
			{
			  printf("write error\n");
			}
		    }
		}
	      else
		printf("error: file not open\n");
	      break;
	    default:
	      printf("unknown type\n");
	      break;
	    }
	}
    }

  delete inServo;
  delete outServo;
  clientServo->close();
  delete clientServo;

  delete inUser;
  delete outUser;
  delete clientUser;
  serverUser->close();
  delete serverUser;
  delete matlab;
  if (mesg != NULL)
    delete[] mesg; 
}



