/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : CathxServer.cc                                                */
/* Author   : Henthorn                                                      */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 09/07/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/

#include "CathxServer.h"
#include "CathxM12Msg.h"
#include "Syslog.h"
#include "System.h"


/** Initializing constructor
    @param driverName driver name
*/
CathxServer::CathxServer(const char *driverName){
  _driverName = strdup(driverName);
  _msgQ = new CathxM12Msg();
}

/** Default destructor */
CathxServer::~CathxServer()
{
  if (_driverName) free((void *)_driverName);
  if (_msgQ) delete _msgQ;
}

/** Get camera camera output data 
    @param data pointer to destination Data object
*/
void CathxServer::get( CathxIF::Data *data )
{
}

void CathxServer::actions(CathxIF::Actions *orders)
{
  CathxM12Msg::Message msg(orders->profileNumber,
                           orders->imageCapture);
  _msgQ->write(&msg);
}

/** Forks a driver task */
int CathxServer::spawnAuxTasks()
{
  char *program = (char *)_driverName;

  char errorBuf[256];
  char buf[256];
 
  pid_t pid;
 
  switch ((pid = fork())) {
     
  case 0:
    // In child
    // Execute server program
     if(_test) 
       execlp(program, program, "-test", 0);
     else if(_sim) 
       execlp(program, program, "-sim", 0);
     else 
       execlp(program, program, 0);

    sprintf(errorBuf, 
	    "Task::spawnServer() - execlp() of \"%s\" failed", program);

    perror(errorBuf);
 
    break;

  case -1:
    perror("Task::spawnServer() - fork() failed");
    return -1;
  }

  return 0;
}

int main(int argc, char** argv)
{
  // Set task priority
  if (System::setTaskPriority(CriticalServerPriority) == -1)
    return 1;

  char configFileName[256];
  strcpy(configFileName, System::configurationFile("cathxServer.cfg"));

  try {
    Syslog::write("cathxServer - initializing..." );
    CathxServer *cathxServer = new CathxServer("cathxm12");
    //cathxServer->initialize();
    
    Syslog::write("cathxServer - running..." );
    cathxServer->run();
  }
  catch( Exception e ) {
    Syslog::write("cathxServer -- Caught exception: %s\n", e.msg );
  }
  catch( ... ) {
    Syslog::write("cathxServer -- Caught exception!\n");
  }

  kill(0, SIGTERM);
  return 0;
}
