/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

#include <netdb.h>

Interface sample;

   Call call_none 6 ;

   Call call_in 7
      In Integer(2);

   Call call_out 8
      Out Integer(2);

End;


main(argc,argv)
   int argc;
   char *argv[];
{
   int timeout = 3000;
   char *ip;
   char addrbuffer[64];
   unsigned char *ipvalue;
   struct hostent *hostip;

   Bind_role r;
   Bind_address addr;
   Control (sample) inst;

   Integer(2) int2_s = 3;
   Integer(2) int2_r;

   /* Parse command line args */
   if (argc < 3) {
     printf("Usage: <host> {'i','I','r','R'} [ <mask> ]\n",argv[0]); 
     exit(1); 
   } 
 
   ip = argv[1];
 
   hostip = gethostbyname(ip);

   if (hostip == NULL) {
     printf("Invalid host server name: %s\n",ip);
     exit(1);
   }

   if (argv[2][0] == 'i' || argv[2][0] == 'I') 
      r = Initiator; 
   else if (argv[2][0] == 'r' || argv[2][0] == 'R') 
      r = Responder; 
   else 
   { 
      printf("2nd argument must be 'i' or 'r'\n"); 
      exit(1); 
   } 

   /* Setup the bind_address */ 

   ipvalue = (unsigned char *)(hostip->h_addr); 

   sprintf(addrbuffer,"%ld.%ld.%ld.%ld+18001",
           ipvalue[0], ipvalue[1], ipvalue[2], ipvalue[3]);

   Init_puresocket_addr(&addr,r,addrbuffer);


   /*** BEGIN client/server activity ***/

   printf("Binding..."); fflush(stdout);

   Bind_addr( inst, Asn_1_ber, &addr, timeout );

   if (r == Initiator)
   {
      call_none@inst();
      /*printf( "1st call_none() has completed\n")*/;
      call_none@inst();
      /*printf( "2nd call_none() has completed\n")*/;
      call_in@inst(int2_s);
      /*printf( "1st call_in(int2_s) has completed\n")*/;
      call_in@inst(int2_s);
      /*printf( "2nd call_in(int2_s) has completed\n")*/;
      int2_r = 0; /* will change to int2_s after call */
      call_out@inst(int2_r);
      if (int2_r != int2_s) {
            printf("   int2_r had a bad value (1st call_out)\n");
            exit(1);
         }
      /*printf( "1st call_out(int2_r) has completed\n")*/;
      int2_r = 0; /* will change to int2_s after call */
      call_out@inst(int2_r);
      if (int2_r != int2_s) {
            printf("   int2_r had a bad value (2nd call_out)\n");
            exit(1);
         }
      /*printf( "2nd call_out(int2_r) has completed\n")*/;
      call_out@inst(...);
      /*printf( "1st call_out(...) has completed\n")*/;
      call_out@inst(...);
      /*printf( "2nd call_out(...) has completed\n")*/;
      call_in@inst();
      /*printf( "1st call_in() has completed\n")*/;
      call_in@inst();
      /*printf( "2nd call_in() has completed\n")*/;
      call_out@inst();
      /*printf( "1st call_out() has completed\n")*/;
      call_out@inst();
      /*printf( "2nd call_out() has completed\n")*/;
   }
   else
   {
      /* 1st call */
      Accept inst
      {
	 On call_none()
	    /*printf( "call_none() accepted\n")*/;
      }
      Give inst {
	 call_none();
	 /*printf( "1st call_none() given\n")*/;
      } 
      /* 2nd call */
      Accept inst
      {
	 On call_none(...)
	    /*printf( "call_none(...) accepted\n")*/;
      }
      Give inst {
	 call_none();
	 /*printf( "2nd call_none() given\n")*/;
      } 
      /* 3rd call */
      Accept inst
      {
         On call_in(int2_r)
            {
            /*printf( "call_in(int2_r) accepted\n")*/;
            if (int2_r != int2_s) {
                  printf("   int_r had a bad value\n");
                  exit(1);
               }
            }
      }
      Give inst {
         call_in();
         /*printf( "1st call_in() given\n")*/;
      }
      /* 4th call */
      Accept inst
      {
         On call_in(...)
            /*printf( "call_in(...) accepted\n")*/;
      }
      Give inst {
         call_in();
         /*printf( "2nd call_in() given\n")*/;
      }
      /* 5th call */
      Accept inst
      {
         On call_out()
            /*printf( "1st call_out() accepted\n")*/;
      }
      Give inst {
         call_out(int2_s);
         /*printf( "1st call_out(int2_s) given\n")*/;
      }
      /* 6th call */
      Accept inst
      {
         On call_out(...)
            /*printf( "call_out(...) accepted\n")*/;
      }
      Give inst {
         call_out(int2_s);
         /*printf( "2nd call_out(int2_s) given\n")*/;
      }
      /* 7th call */
      Accept inst
      {
         On call_out()
            /*printf( "2nd call_out() accepted\n")*/;
      }
      Give inst {
         call_out(int2_s);
         /*printf( "3rd call_out(int2_s) given\n")*/;
      }
      /* 8th call */
      Accept inst
      {
         On call_out(...)
            /*printf( "2nd call_out(...) accepted\n")*/;
      }
      Give inst {
         call_out();
         /*printf( "1st call_out() given\n")*/;
      }
      /* 9th call */
      Accept inst
      {
         On call_in(...)
            /*printf( "2nd call_in(...) accepted\n")*/;
      }
      Give inst {
         call_in();
         /*printf( "2nd call_in() given\n")*/;
      }
      /* 10th call */
      Accept inst
      {
         On call_in()
            /*printf( "call_in(...) accepted\n")*/;
      }
      Give inst {
         call_in();
         /*printf( "3rd call_in() given\n")*/;
      }
      /* 11th call */
      Accept inst
      {
         On call_out(...)
            /*printf( "3rd call_out(...) accepted\n")*/;
      }
      Give inst {
         call_out();
         /*printf( "2nd call_out() given\n")*/;
      }
      /* 12th call */
      Accept inst
      {
         On call_out()
            /*printf( "3rd call_out() accepted\n")*/;
      }
      Give inst {
         call_out();
         /*printf( "3rd call_out() given\n")*/;
      }
   }

   Unbind(inst);

   printf("Unbound\n");
   printf("test completed\n");
   return (0); 
}
