/* ----------------------------------------------------------------
   PWS16NET written by L. Granroth
            modified by R. Baines

   This module contains the Begin, Command, Data and End functions
   for net communication.

      Begin   - opens communication.

      Command - instructs server on what data is req'd and how to
                process it.

      Data    - returns one channel of data.

      End     - closes communication.

   ---------------------------------------------------------------- */

#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include "pwstruct.h"
#include "pwsglobl.h"
#include "proto.h"

/* Internal Global Var */
int net;                       /* network file handle */

/* External Global Var */
extern char node_loc[];


/*---------------------------------------------------------------------

  pws16_begin written by L. Granroth  08-24-88
  to initialize network connections for the PWS16PC program.

  ---------------------------------------------------------------------*/

void pws16_net_begin (void)
{
  long int status;

  /* Set Initial Values for cmd, Zmin and Zmax */
  printf("\nPWS16PC version %s\n",VERSION);

  /* initialize command structure */
  cmd_reset();

  /* open network file */
  puts ("Attempting to open network connection . . .");

  if ((net=open(node_loc,O_BINARY|O_RDWR,0))==-1)
	error (13);
  if ((write(net,&Cmd,Cmd.length<<2))!=(Cmd.length<<2))
	error (14);
  if ((read(net,&status,4))!=4)
     error (15);
  if (status)
     error(16);
  puts ("Successful network dialog established.");
  puts ("\nEnter plot parameters (or help)");
}

/*---------------------------------------------------------------------

  pws16_command written by L. Granroth  08-30-88
  to pass data requests from the PWS16PC program to PWS16_SERVER.
  Nonzero is returned after a successfully transmitted request and
  acknowledgment, otherwise zero is returned.

  ---------------------------------------------------------------------*/

int pws16_net_command (void)
{
  long int status;

  if ((write(net,&Cmd,Cmd.length<<2))!=(Cmd.length<<2))
  {
    perror ("Error sending data request");
    return 0;
  }
  if ((read(net,&status,4))!=4)
  {
    perror ("Error receiving acknowledgement of data request");
    return 0;
  }
  return (int) status;

} /* pws16_command */


/*---------------------------------------------------------------------

  pws16_data written by L. Granroth  09-06-88
  to receive data packets for the PWS16PC program.

  ---------------------------------------------------------------------*/

int pws16_net_data (void)
{
  if (!(read(net,&Dat,2064)))
  {
    exit_graph();
    if (Dat.position != Cmd.nchan)
      perror ("Error reading data");
    return (0);
  }

  if ((Dat.length>4)&&(Dat.length<=516)) return (1);

  if (Dat.length!=0L) printf ("Bad data packet size: %li\n",Dat.length);
  {
   exit_graph();
   return (0);
  }

} /* pws16_data */


/*---------------------------------------------------------------------

  pws16_end written by L. Granroth  09-06-88
  to close connection.

  ---------------------------------------------------------------------*/

void pws16_net_end (void)
{
  puts ("Network connection closing.");
  close (net);
} /* pws16_end */
