Public Types | Public Member Functions | Static Private Member Functions

MarNet Class Reference

#include <marnet.h>

Inheritance diagram for MarNet:
Inheritance graph
[legend]

List of all members.

Public Types

typedef MarXF inherited

Public Member Functions

 MarNet (QObject *parent)
int net_open (int sk)
int net_close (int sk)
int net_data ()
int net_comm (int mode, char *buf)
int net_select (int sk)
int net_stat ()
int net_msg ()
virtual int process_status (char *buf)=0
virtual void print_msg (char *s)=0

Static Private Member Functions

static void stop_net (int signo)

Detailed Description

Definition at line 6 of file marnet.h.


Member Typedef Documentation

Reimplemented from MarXF.

Reimplemented in Command, MarClock, MarHW, MarStartup, QScan345Controller, and Scan345.

Definition at line 12 of file marnet.h.


Constructor & Destructor Documentation

MarNet::MarNet ( QObject *  parent)

Definition at line 65 of file marnet.cpp.

  : inherited(parent)
{
}

Member Function Documentation

int MarNet::net_close ( int  sk)

Definition at line 178 of file marnet.cpp.

References mar_socket, and netcontrol.

Referenced by MarHW::mar_quit(), and MarHW::mar_servo().

{
  if ( mar_socket[sk] < 0 || netcontrol < 1 ) return 0;
  close         ( mar_socket[sk] );
  mar_socket[sk] = -1;
  return 1;
}
int MarNet::net_comm ( int  mode,
char *  buf 
)

Definition at line 295 of file marnet.cpp.

References FD_MAX, i, j, mar_socket, net_timeout, netcontrol, SK_COMM, swaplong(), and timeout.

Referenced by MarHW::mar_command().

{
  int   i,j,nwrite,fail=0;
  int   sel;
  fd_set        r;

  if ( mar_socket[SK_COMM] < 0 || netcontrol < 1 ) return -1;

  /* Check that we can write to socket ... */
  FD_ZERO( &r );
  FD_SET ( mar_socket[SK_COMM], &r );

  if ( mode == 0 )      /* Something to read on socket ? */     
    sel = select(FD_MAX,&r,(fd_set *)0,(fd_set *)0,&timeout);
  else                  /* Something to write on socket ? */    
    sel = select(FD_MAX,(fd_set *)0,&r,(fd_set *)0,&timeout);

  if ( sel < 0 ) return -1;

  /* There are data to read on socket: USER PARAMETERS */
  if ( mode == 0 ) {
    return 64;
    nwrite = 0;
    while ( ( i=read(mar_socket[SK_COMM], buf, 64)) > 0 ) {
      nwrite += i;
    }
    return nwrite;
  }

  net_timeout = 0;
  i = nwrite  = 0;
  j = 60;

#if ( __linux__ || __osf__ )
  swaplong( buf+4, sizeof(char)*28 );
#endif

  while ( nwrite<j ) {
    i=write(mar_socket[SK_COMM], buf+nwrite, j);
    if  ( i < 1 ) {
      fail++;
      if ( fail > 100 ) {
        net_timeout++;
        return -1;
      }
      continue;
    }
    nwrite+=i;
    j     -=i;
  }

  return nwrite;
}
int MarNet::net_data ( )

Definition at line 190 of file marnet.cpp.

References bytes2xfer, DATA_SIZE, debug, FD_MAX, i, ict1, ict2, j, mar_socket, maximum_bytes, net_buf, netcontrol, scan_in_progress, SK_DATA, stat_blocks_scanned, stat_blocks_sent, swapshort(), timeout, and MarXF::Transform().

Referenced by MarHW::get_status(), and QScan345Controller::poll_status().

{
  static int    p_pixels= 9999;
  static int    size    = DATA_SIZE;
  static int    tot_pix = 0;
  static int    nbeg    = 0;
  int           nend    = 0;
  int           nread   = 0;
  int           ncop    = 0;
  int           ioff    = 0;
  int           j       = 0;
  int           i,n;
  char          *buf_ptr;
  char            rbuf[ DATA_SIZE ];
  fd_set                r;
  static unsigned short block_no;

  extern int    stat_blocks_scanned;
  extern int    stat_blocks_sent;
  extern int    ict1,ict2;
  extern int    bytes2xfer,maximum_bytes;

  ict1++;

  if ( mar_socket[SK_DATA] < 0 || netcontrol < 1 ) return 0;
  FD_ZERO( &r );
  FD_SET ( mar_socket[SK_DATA], &r );
  i = select(FD_MAX,&r,(fd_set *)0,(fd_set *)0,&timeout);
  if ( i < 1 ) return (-2);

  /* Read from socket */
  nread = read(mar_socket[SK_DATA], rbuf, size); 

  if ( nread<1 ) return -1;

  bytes2xfer    += nread;

  /* Increase pointer for end of this chunk of data */
  nend  = nbeg+nread;

  /* Make sure that we dont exceed 16386 kB */
  if ( nend > DATA_SIZE) {
    ioff = nend-DATA_SIZE;
    ncop = nread - ioff;
    nend = DATA_SIZE;
  }
  else {
    ioff = 0;
    ncop = nread;
  }

  /* Copy latest data into net_buf (16kB block) */
  memcpy( net_buf + nbeg, rbuf, ncop );

#ifdef DEBUG
  if (debug & 0x200 )
    printf("scan345: net_data %5d  block=%4d\n",nread,block_no);
#endif

  /* Increase pointer for begin of next chunk of data */
  nbeg += ncop;

  /* Block not yet finished: read more data */
  if ( nend < 16386 ) {
    block_no        = stat_blocks_sent;
    return nread;
  }

  /* Block of data finished: transform it ... */
#if ( __linux__ || __osf__ )
  swapshort( (char *)net_buf, nend);
#endif

  ict2++;

  /* Start of data block: get block no from first 2 bytes */
  memcpy( &block_no, net_buf, sizeof(short) );

  stat_blocks_sent        = block_no;
  buf_ptr                 = net_buf+2;
  n                       = (DATA_SIZE-2)/2;
  tot_pix                 += n;

  /* Transform ... */
  if ( scan_in_progress )
    Transform( n, block_no, (unsigned short *)buf_ptr);

  nbeg = 0;

  /* This time, there are still some data in rbuf that we can't
   * loose. Copy into start of net_buf
   */
  if ( ioff )  {
    memcpy( net_buf, rbuf+ncop, ioff);
    nbeg = ioff;
    ioff = 0;
  }

  return nread;
}
int MarNet::net_msg ( )

Definition at line 443 of file marnet.cpp.

References mar_socket, MESS_SIZE, msg_buf, net_timeout, netcontrol, print_msg(), and SK_MESS.

Referenced by MarHW::get_status(), and QScan345Controller::poll_status().

{
  int           sel;
  fd_set                r;

  if ( mar_socket[SK_MESS] < 0 || netcontrol < 1 || net_timeout ) return 0;

  /* We can't accumulate messages in the port, so always try to read
   * as much as possible 
   */

  while ( 1 ) {
    sel = read(mar_socket[SK_MESS], msg_buf, MESS_SIZE); 

    if ( sel < 1 ) break;

    print_msg( msg_buf );
  }

  return sel;
}
int MarNet::net_open ( int  sk)

Definition at line 74 of file marnet.cpp.

References i, mar_host, mar_port, mar_socket, MAX_TRY, MarXF::print_message(), sk_type, stop_net(), stop_trying, and timeout.

Referenced by MarStartup::marStartup().

{
  int                   i,retry=0;
  int                   protocol;
  int                   new_socket;
  struct sockaddr_in    sin;
  struct sockaddr*        sinPtr;
  struct hostent                *hp=NULL;
  extern int            mar_port;
  extern char           mar_host[32];

  /* Get host address */
  hp = gethostbyname(mar_host);
  if (hp == NULL) {
    return(0);
  }
  if ( strlen( mar_host ) < 1 ) return 0;

  if ( mar_socket[sk]  != -1 ) {
    close( mar_socket[sk] );
    mar_socket[sk] = -1;
  }


  /* Set timeout for socket op's */
  timeout.tv_sec  = 0;
  timeout.tv_usec = 0;

  /*****************************************************
   * Create a socket: try 10 times 
   *****************************************************/
  protocol   = getprotobyname("tcp")->p_proto;

  signal( SIGINT,  stop_net);
  stop_trying = 0;

  while ( retry < MAX_TRY ) {

    if ( stop_trying ) {
      retry = MAX_TRY;
      break;
    }

    new_socket = socket(AF_INET, SOCK_STREAM, protocol);

    if ( new_socket < 0 ) {
      fprintf( stdout, "scan345: Cannot open socket %s at port %d\n",sk_type[sk],mar_port+sk);
      return(0);
    }

    /*****************************************************
     * Connect to the server.
     *****************************************************/

    sin.sin_family      = AF_INET;
    sin.sin_port        = htons(mar_port+sk);
    bcopy (hp->h_addr, &(sin.sin_addr.s_addr), hp->h_length);
    sinPtr              = (struct sockaddr *)&sin;

    if ( ::connect(new_socket, sinPtr, sizeof(sin)) < 0) {
      fprintf(stdout, "scan345: %d. try to connect to host '%s' ...\n",retry,mar_host);
      if ( retry%5 == 0 ) 
        fprintf(stdout, "        To interrupt, press Ctrl+C\n");
      sleep( 1 );
      close( new_socket );
      retry++;
    }
    else 
      break;
  }

//   signal( SIGINT,  mar_kill);
//   signal( SIGKILL, mar_kill );
//   signal( SIGTERM, mar_kill );
//   signal( SIGQUIT, mar_abort);

  if ( retry >= MAX_TRY ) {
    fprintf(stdout, "scan345: Giving up ...\n");
    close(new_socket);
    new_socket = -1;
    return 0;
  }

//   fprintf(stdout,"scan345: Connected to host '%s' on port %d (%s)\n",mar_host,mar_port+sk,sk_type[sk]);
  emit print_message(tr("scan345: Connected to host '%1' on port %2 (%3)\n").arg(mar_host).arg(mar_port+sk).arg(sk_type[sk]));

  /* We don't need the previous socket any more, close it */
  if ( mar_socket[sk] != -1 )
    close( mar_socket[sk] );

  mar_socket[sk] = new_socket;

  /* Set socket NON BLOCKING */
  if ( fcntl( mar_socket[sk], F_SETFL, O_NONBLOCK ) < 0 ) {
    fprintf(stdout,"scan345: Socket %s cannot be set to NON BLOCKING\n",sk_type[sk]);
  }

  return 1;
}
int MarNet::net_select ( int  sk)

Definition at line 353 of file marnet.cpp.

References FD_MAX, mar_socket, netcontrol, and timeout.

{
  int   sel;
  fd_set        r;

  if ( mar_socket[sk] < 0 || netcontrol < 1 ) return -1;

  FD_ZERO( &r );
  FD_SET ( mar_socket[sk], &r );

  sel = select(FD_MAX,(fd_set *)0,&r,(fd_set *)0,&timeout);

  if ( sel < 0 )
    return -1;

  return sel;
}
int MarNet::net_stat ( )

Definition at line 385 of file marnet.cpp.

References debug, FD_MAX, i, j, mar_socket, net_timeout, netcontrol, process_status(), SK_STAT, STAT_SIZE, swaplong(), and timeout.

Referenced by MarHW::get_status(), and QScan345Controller::poll_status().

{
  register unsigned int j;
  static char   s1buf[STAT_SIZE];
  int           nread,i,size,sel;
  time_t                t1, t2;
  fd_set                r;

  if ( mar_socket[SK_STAT] < 0 || netcontrol < 1 || net_timeout ) return 0;

  /* Check that we can read from socket ... */

  FD_ZERO( &r );
  FD_SET ( mar_socket[SK_STAT], &r );
  sel = select(FD_MAX,&r,(fd_set *)0,(fd_set *)0,&timeout);

  if ( sel < 1 ) return sel;

  size  = STAT_SIZE;
  nread         = 0;
  j     = 0;
  t1    = time(NULL);

  while ( nread < STAT_SIZE ) {
    j++;
    i     = read(mar_socket[SK_STAT], s1buf+nread, size); 
    if ( j > 10000000 ) {
      t2    = time(NULL);
      if ( difftime( t2, t1 ) > 5.0 ) break;
      continue;
    }
    if ( i < 0 ) continue;
    nread += i;
    size  -= i;
  }

#ifdef DEBUG
  if (debug & 0x800 )
    printf("scan345: net_stat %d\n",i);
#endif

#if ( __linux__ || __osf__ )
  swaplong( s1buf, (int)STAT_SIZE);
#endif

  if ( nread >= STAT_SIZE )
    i = process_status( s1buf+4 );
  else {
    fprintf( stdout, "scan345: Only %d bytes in status block !!!\n",nread);
  }

  return nread;
}
virtual void MarNet::print_msg ( char *  s) [pure virtual]

Implemented in MarHW.

Referenced by net_msg().

virtual int MarNet::process_status ( char *  buf) [pure virtual]

Implemented in MarHW.

Referenced by net_stat().

void MarNet::stop_net ( int  signo) [static, private]

Definition at line 375 of file marnet.cpp.

References stop_trying.

Referenced by net_open().

{
  printf( "scan345: signal SIGINT caught\n");
  stop_trying = 1;
}

The documentation for this class was generated from the following files: