/*     
 * $RCSfile: tcptest.c,v $
 *
 * x-kernel v3.3
 *
 * Copyright (c) 1993,1991,1990,1996  Arizona Board of Regents
 *
 * $Revision: 1.2 $
 * $Date: 1996/02/01 15:36:43 $
 */

/*
 * Ping-pong test of TCP
 */

#include "xkernel.h"
#include "tcp.h"
#include "ip.h"
/* #include "site.h" */
#include "xsim.h"

/*
 * These definitions describe the lower protocol
 */
#define HOST_TYPE IPhost
#define INIT_FUNC tcptest_init
#define TRACE_VAR tcptestp
#define PROT_STRING "tcp"
#define TCP_DUMPTRACE ((TCP_CTL + 1)*MAXOPS - 1)

/* 
 * If a host is booted without client/server parameters and matches
 * one of these addresses, it will come up in the appropriate role.
 */
static HOST_TYPE ServerAddr = { 0, 0, 0, 0 };
static HOST_TYPE ClientAddr = { 0, 0, 0, 0 };

#define TRIPS 1
#define TIMES 1
#define DELAY 1
/*
 * Define to do timing calculations
 */
#define TIME


long	serverPort = 2001;
long	clientPort = ANY_PORT;

#define CUSTOM_ASSIGN

static int lens[] = { 
  1, 4000, 8000, 16000
};


#define BUFFER_SIZE	4096

#define CUSTOM_OPENDONE
#define CLIENT_OPENDONE
#define CUSTOM_SERVER_DEMUX
#define CUSTOM_CLIENT_DEMUX


#define STREAM_TEST

static void clientSetPart();
static void serverSetPart();
static XkReturn customServerDemux(Protl, Sessn, Msg *);
static XkReturn customClientDemux(Protl, Sessn, Msg *);
static XkReturn customOpenDone( Protl, Protl, Sessn, Protl );

#define XNETSIM
#define TCP_SAVE_TRACE
#include "common_test.c"

static void
clientSetPart( v, p )
    Vars	*v;
    Part	*p;
{
    partInit(p, 1);
    partPush(p[0], &v->serverAddr, sizeof(IPhost));
    partPush(p[0], &serverPort, sizeof(long));
    /* 
     * If we don't specify the second participant, TCP will select a port
     * for us. 
     */
#if 0
    /* 
     * NOTE -- if you use two participants, make sure the second argument to 
     * the above call to partInit is 2
     */
    partPush(p[1], ANY_HOST, 0);
    partPush(p[1], &clientPort, sizeof(long));
#endif
    printf("[%s] Client sending to: %s, port:%d\n", v->hostStr, 
	   ipHostStr(&v->serverAddr), serverPort);
}

static void
serverSetPart( v, p )
    Vars  	*v;
    Part	*p;
{
    partInit(p, 1);
    partPush(*p, ANY_HOST, 0);
    partPush(*p, &serverPort, sizeof(long));
    printf("[%s] Server responding to: %s, port:%d\n", v->hostStr, "ANY_HOST",
	   serverPort);
}

static XkReturn
customOpenDone( self, llp, s, hlpType )
    Protl self, llp, hlpType;
    Sessn s;
{
    u_short space = BUFFER_SIZE;
    Vars *v=(Vars *)self->state;

    xsimDbg(TET_FLAG, 
	    printf("[%s] customOpenDone\n", v->hostStr));
    xTrace0(prottest, TR_MAJOR_EVENTS, "tcp test program openDone");
    xDuplicate(s);

    if (xControlSessn(s, TCP_SETRCVBUFSIZE, (char*)&space, sizeof(space)) < 0) {
	xError("saveServerSessn: TCP_SETRCVBUFSIZE failed");
    } /* if */
    if (xControlSessn(s, TCP_SETRCVBUFSPACE, (char*)&space, sizeof(space)) < 0){
	xError("saveServerSessn: TCP_SETRCVBUFSPACE failed");
    } /* if */

    return XK_SUCCESS;
}
    
static XkReturn
customClientDemux( self, lls, dg )
    Protl	self;
    Sessn	lls;
    Msg		*dg;
{
    u_short space = BUFFER_SIZE;
    Vars *v=(Vars *)self->state;

    xsimDbg(TET_FLAG, 
	    printf("[%s] customClientDemux\n", v->hostStr));
    xTrace1(prottest, TR_DETAILED, "TCP custom demux resets buffer to size %d",
	    BUFFER_SIZE);
    if (xControlSessn(lls, TCP_SETRCVBUFSPACE, (char*)&space, sizeof(space)) < 0) {
	xError("TCP custom test_demux: TCP_SETRCVBUFSPACE failed");
    } /* if */
    return XK_SUCCESS;
}

static XkReturn
customServerDemux( self, lls, dg )
    Protl	self;
    Sessn	lls;
    Msg		*dg;
{
    Vars *v=(Vars *)self->state;

    xsimDbg(TET_FLAG, 
	    printf("[%s] customServerDemux\n", v->hostStr));
    return customClientDemux( self, lls, dg );
}

static void
testInit()
{
}
