/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : SimulatedDevice.cc                                            */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ourTypes.h"
#include "SimulatedDevice.h"


SimulatedDevice::SimulatedDevice(const char *name,
				 const char *simServerHostName,
				 int simServerPort)
  : DeviceInterface(name)
{
  _simulated = True;

  _connected = False;

  _serverHost = strdup(simServerHostName);
  _serverPort = simServerPort;

  if ((_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("SimulatedDevice::SimulatedDevice() - socket()");
    return;
  }

  // Connect socket using simulation server's host name
  if ((_hostEnt = gethostbyname((char *)simServerHostName)) == 0) {
    fprintf(stderr, 
	    "SimulatedDevice::SimulatedDevice() - unknown host: \"%s\"",
	    simServerHostName);

    return;
  }

  _server.sin_family = AF_INET;
  memcpy(&_server.sin_addr, _hostEnt->h_addr, _hostEnt->h_length);

  fprintf(stderr, "SimulatedDevice::SimulatedDevice - connect()...");
  if (connect(_socket, (const struct sockaddr *)&_server, 
	      sizeof(_server)) < 0) {

    perror("SimulatedDevice::SimulatedDevice() - connect()");
  }
  else {
    _connected = True;
  }

  fprintf(stderr, "okay\n");

  
}


SimulatedDevice::~SimulatedDevice()
{
  free((void *)_serverHost);
}



int SimulatedDevice::write(char *buf, int nBytes)
{
  return 0;
}


int SimulatedDevice::read(char *buf, int maxBytes)
{
  static char msg[] = "Simulated message";

  strncpy(buf, msg, min(maxBytes, strlen(msg)));

  return strlen(buf);
}


void SimulatedDevice::reset()
{
}

