# define PRIVATE static
# define PUBLIC 

#include "ldc.hh"
#include <lotos.hh>
#include "swbus.h"

# include <stdio.h>
# include <signal.h>

PUBLIC int
int_val (n)
    kdatum n;
{
  kdatum sn;
  int i;

  sn= zeronat();
  i= 0;
  while (!kd_equal(sn, n)) {
    i++;
    sn= Succ(sn);
  }
  kd_free(sn);
  kd_free (n);
  return i;
}

PRIVATE void
onintr ()
{
  clean_kb ();
  clean_sockets ();
  exit (0);
}

main (argc, argv)
    int	argc;
    char* argv[];
{
  void* actual[4];
  kdatum aux;
  int n;
  int p;
  int side;

  srand (getpid ());
  konfig.trace = 0;
  if (argc != 5) {
    fprintf (stderr, "usage: %s <N> <P> side error_rate\n", argv[0]);
    exit (1);
  }
  kaos.debug =  TRUE;
  ldcinit ();
  n= atoi (argv[1]);
  aux= zeronat ();
  while (n-- > 0)
    aux= Succ (aux);
  actual[0]= (void*)aux;

  p= atoi (argv[2]);
  aux= zeronat ();
  while (p-- > 0)
    aux= Succ (aux);
  actual[1]= (void*)aux;

  signal (SIGINT, onintr);
  
  error_rate= atoi (argv[4]);

  side= atoi (argv[3]);       
  if (side % 2 == 1) {
    connect_one ();
    actual[2]= (void*)sap1 ();
    actual[3]= (void*)cmsap1 ();
    lbcinit (4, actual);
  }
  else {
    connect_two ();
    actual[2]= (void*)sap2 ();
    actual[3]= (void*)cmsap2 ();
    lbcinit (4, actual);
  }
  exit (sch ());
  }
