/*     
 * $RCSfile: common_test_rpc.c,v $
 *
 * x-kernel v3.3
 *
 * Copyright (c) 1993,1991,1990,1996  Arizona Board of Regents
 *
 * $Revision: 1.2 $
 * $Date: 1996/01/29 22:36:24 $
 */


#ifdef __STDC__
static 	XkReturn	testCallDemux( Protl, Sessn, Msg *, Msg * );
#else
static 	XkReturn	testCallDemux();
#endif /* __STDC__ */



static int
tryCallDefault( sessn, times, length )
  Sessn sessn;
  int times;
  int length;
{
    XkReturn ret_val;
    int i;
    Msg	savedMsg, request, reply;
    char *buf;
    int c = 0;
    
    buf = msgConstructAllocate(&savedMsg, length);
    msgConstructEmpty(&reply);
    msgConstructEmpty(&request);
    for (i=0; i<times; i++) {
	msgAssign(&request, &savedMsg);
	ret_val = xCall(sessn, &request, &reply);
	xIfTrace(prottest, TR_MAJOR_EVENTS) {
	    putchar('.');
	    if (! (++c % 50)) {
		putchar('\n');
	    }
	}
	if( ret_val == XK_FAILURE ) {
	    printf( "RPC call error %d\n" , ret_val );
	    goto abort;
	}
	if (msgLength(&reply) != length) {
	    printf("Bizarre reply length.  Expected %d, received %d\n",
		   length, msgLength(&reply));
	    goto abort;
	}
	msgTruncate(&reply, 0);
    }

abort:
    msgDestroy(&savedMsg);
    msgDestroy(&reply);
    msgDestroy(&request);
    return i;
}


/* 
 * RPC runTest
 */
static int
defaultRunTest( self, len, testNumber )
    Protl	self;
    int 	len;
    int		testNumber;
{
    int 	test;
    static int	noResponseCount = 0;
    Sessn	lls;
#ifdef TIME    
    XTime 	startTime, endTime, netTime;
#endif
    int		count;

    xAssert(xIsProtl(self));
    lls = ((PState *)self->state)->lls;
    xAssert(xIsSessn(lls));
    for (test = 0; test < TIMES; test++) {
	printf("Sending (%d) ...\n", testNumber);
	count = 0;
#ifdef PROFILE
	startProfile();
#endif
#ifdef TIME
	xGetTime(&startTime);
#endif
	if ( (count = tryCall(lls, trips, len)) == trips ) {
#ifdef TIME
	    xGetTime(&endTime);
	    subtime(&startTime, &endTime, &netTime);
	    printf("\nlen = %4d, %d trips: %6d.%06d\n", 
		   len, trips, netTime.sec, netTime.usec);
#else
	    printf("\nlen = %4d, %d trips\n", len, trips);
#endif
	}
#ifdef USE_CONTROL_SESSN
	if (xIsSessn(controlSessn)) {
	    Msg		m;
	    char	*strbuf;
	    
	    strbuf = msgConstructAllocate(&m, 80);
	    strbuf[0] = '\n';
	    sprintf(strbuf+1, "End of test %d", testNumber);
	    
	    xCall(controlSessn, &m, 0);
	    msgDestroy(&m);
	    
	}
#endif /* USE_CONTROL_SESSN */
	if ( count == 0 ) {
	    if ( noResponseCount++ == FAILURE_LIMIT ) {
		printf("Server looks dead.  I'm outta here.\n");
		return 1;
	    }
	} else {
	    noResponseCount = 0;
	}
	Delay(testdelay * 1000);
#ifdef XK_MEMORY_THROTTLE
	{
	  extern int max_thread_pool_used, min_memory_avail,
	             max_xk_threads_inuse;
	  printf("used %d incoming threads; %d regular threads; max %d bytes memory",
		 max_thread_pool_used,
		 max_xk_threads_inuse,
		 min_memory_avail);
	}
#endif /* XK_MEMORY_THROTTLE */
    }
    return 0;
}


static XkReturn
serverCallDemuxDefault(self, lls, dg, rMsg)
    Protl self;
    Sessn lls;
    Msg *dg, *rMsg;
{
    msgAssign(rMsg, dg);
    return XK_SUCCESS;
}


static XkReturn
testCallDemux( self, lls, dg, rMsg )
    Protl self;
    Sessn lls;
    Msg *dg, *rMsg;
{
    static int c = 0;
    
    xIfTrace(prottest, TR_MAJOR_EVENTS) {
	putchar('.');
	if (! (++c % 50)) {
	    putchar('\n');
	}
    }
    return serverCallDemux(self, lls, dg, rMsg);
}


static void
realmServerFuncs( self )
    Protl	self;
{
    self->calldemux = testCallDemux;
}


static void
realmClientFuncs( self )
    Protl	self;
{
}
