/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : DropWeightServer.cc                                           */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include "AdaptiveSamplerServer.h"

AdaptiveSamplerServer::AdaptiveSamplerServer(const char *driverName, char *test) 
   : AdaptiveSamplerIF_SK()
{
  _adaptiveSamplerOutput = new AdaptiveSamplerOutput();
  for( int i=0; i<NGULPERS; i++ ) 
     _adaptiveSamplerOutput->data.gulpersToArm[i] = 0;
  _adaptiveSamplerOutput->write();
  _driverName = strdup(driverName);
  _test = strdup(test);
}

AdaptiveSamplerServer::~AdaptiveSamplerServer()
{
  free((void *)_driverName);
  free((void *)_test);
  delete _adaptiveSamplerOutput;
}

void AdaptiveSamplerServer::armGulper( AdaptiveSamplerIF::GULPERS_TO_ARM_T gulpersToArm)
{
   memcpy( _adaptiveSamplerOutput->data.gulpersToArm, gulpersToArm, 
	   sizeof( AdaptiveSamplerIF::GULPERS_TO_ARM_T) );
  _adaptiveSamplerOutput->write();
}

short AdaptiveSamplerServer::counter( void )
{
  _adaptiveSamplerOutput->read();
  return _adaptiveSamplerOutput->data.cntr;
}
void AdaptiveSamplerServer::state( short *gulperState )
{
  _adaptiveSamplerOutput->read();

//   memcpy( gulperState, _adaptiveSamplerOutput->data.gulperState, 
//	   sizeof( AdaptiveSamplerIF::GulperState ) );

   for(int j=0; j<NGULPERS; j++) 
      gulperState[j] = _adaptiveSamplerOutput->data.gulperState[j];
}

int AdaptiveSamplerServer::spawnAuxTasks()
{
  char *program = (char *)_driverName;

  char errorBuf[256];
  char buf[256];
 
  pid_t pid;
 
  switch ((pid = fork())) {
     
  case 0:
    // In child
    // Execute server program
    // execlp(program, program, "-serial", _serialParams->string(), 0);
    execlp(program, program, _test, 0, 0);

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

    perror(errorBuf);
 
    break;

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

  return 0;
}

