/*****************************************************************/
/**                                                             **/
/**             (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;

   Array Integer(2) arrtype[10];

   Call call_in_out 6
      InOut arrtype;

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;

   int size_s1, size_s2;
   int size_r1, size_r2;
   arrtype arr_s1,arr_s2;
   arrtype arr_r1,arr_r2;

   arr_s1[0] = 0;
   arr_s1[1] = 1;
   arr_s1[2] = 2;
   arr_s1[3] = 3;
   arr_s1[4] = -4;
   size_s1 = 5;
   size_r1 = 10;

   arr_s2[0] = 1;
   arr_s2[1] = 2;
   arr_s2[2] = 3;
   arr_s2[3] = -4;
   size_s2 = 4;
   size_r2 = 10;

   /* 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+18002",
           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_in_out@inst(arr_s1{size_s1, size_s2});
      /*printf("call_in_out(arr_s{size_in, size_out}) has completed\n");*/
      if (size_s1 != 5) {
	 printf("size_s1 is %d (should be 5)\n",size_s1);
	 exit(1);
      }
      if (size_s2 != 4) {
	 printf("size_s2 is %d (should be 4)\n",size_s2);
	 exit(1);
      }
      if (arr_s1[0] != 1 ||
	  arr_s1[1] != 2 ||
	  arr_s1[2] != 3 ||
	  arr_s1[3] != -4) {
	 printf("   array values were bad\n");
	 exit(1);
      }

   }

   else

   {
      Accept inst
      {
	 On call_in_out(arr_r1{size_r1})
	    {
	    /*printf( "call_in_out(arr_r1{size_r1}) accepted\n");*/
	       if (size_r1 != 5) {
		  printf("size_r1 is %d (should be 5)\n",size_r1);
		  exit(1);
	       }
	       if (arr_r1[0] != 0 ||
		   arr_r1[1] != 1 ||
		   arr_r1[2] != 2 ||
		   arr_r1[3] != 3 ||
		   arr_r1[4] != -4) {
		  printf("   array values were bad\n");
		  exit(1);
	       }
	    }
      }
      Give inst {
	 call_in_out(arr_s2{size_s2});
	 /*printf( "call_in_out(arr_s2{size_s2}) given\n");*/
      } 
   }

   Unbind(inst);

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