/*****************************************************************/
/**                                                             **/
/**             (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>
#define TRUE 1
#define FALSE 0

Interface sample;

   Invoke op           5 ;
   Result response_op  6 ;
   Invoke challenge_op 7 ;
   Error  error_op     8 ;

End;


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

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

   /* Parse command line args */
   if (argc < 3) {
     printf("Usage: %s <host> {'i','I','r','R'}\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+18004",
           ipvalue[0], ipvalue[1], ipvalue[2], ipvalue[3]);

   Init_puresocket_addr(&addr,r,addrbuffer);


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

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

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

   if (r == Initiator) { /* invoker-initiator */
      long invoke_id, *linked_id, *challenge_id;
      invoke_id = 7;
      Give inst /* performer */ {
	 op[invoke_id]();
	 looping = TRUE;
	 while(looping) {
	    Accept [challenge_id,linked_id] inst /* performer */ {
	       On challenge_op() {
		  printf("on challenge_op executing\n");
		  if (*linked_id == invoke_id) {
		     Give inst /* performer */ {
			response_op[*challenge_id]();
		     }
		     looping = FALSE;
		  }
		  else {
		     printf("spurious challenge\n");
		     continue;
		  }
	       }
	       On error_op() {
		  printf("on error_op executing\n");
	       }
	    }
	 }
      }
   }
   else { /* performer-responder */
      long *invoke_id, *linked_id, challenge_id = 40;
      Accept [invoke_id] inst /* invoker */ {
	 On op() {
	    Give inst /* invoker */ {
	       challenge_op[challenge_id, *invoke_id]();
	       Accept [linked_id] inst /* invoker */ {
		  On response_op() {
		     if (*linked_id != challenge_id) {
			Give inst /* invoker */ {
			   error_op[challenge_id]();
			}
		     }
		  }
	       }
	    }
	 }
      }
   }
   Unbind(inst);

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