0010 REM BujuClientPvx: Buju Client
0100 TEST:
0110 REM Test
0120 IF TCB(13)=0 THEN CALL PGM(-2); ESCAPE ; REM endif
0150 GOSUB TEST_RUN_
0160 EXIT 
0200 INIT_ARGS:
0210 REM Initializes argument string template.
0220 REM @return arg$: argument string template
0230 SETESC ESC_HANDLER_; SETERR ERR_HANDLER_
0240 ENTER ARG$
0250 GOSUB INIT_ARGS_
0260 EXIT 
1000 OPEN_CONNECTION:
1010 SETESC ESC_HANDLER_; SETERR ERR_HANDLER_
1020 ENTER ARG$
1030 LET U=UNT; OPEN (U)"[TCP]"+ARG.SOCKHOST$+";"+STR(ARG.SOCKPORT)+";secure;stream;nodelay"
1035 LET ARG.SOCKUNT=U
1040 LET RESULT$=FNEVAL$(ARG.LOGIN$),ARG.CONNECTED=1
1045 EXIT 
1050 CLOSE_CONNECTION:
1060 SETESC ESC_HANDLER_; SETERR ERR_HANDLER_
1070 ENTER ARG$,RESULT$
1080 GOSUB CHECK_CONNECTION_
1085 LET U=ARG.SOCKUNT,RESULT$=FNEVAL$(".close()"),ARG.SOCKUNT=0,ARG.CONNECTED=0
1090 CLOSE (U)
1095 EXIT 
1100 EVAL_SOURCE:
1110 SETESC ESC_HANDLER_; SETERR ERR_HANDLER_
1120 ENTER ARG$,SOURCE$,RESULT$
1130 GOSUB CHECK_CONNECTION_
1140 LET U=ARG.SOCKUNT,RESULT$=FNEVAL$(SOURCE$,ERR=EVAL_SOURCE_ERR_)
1150 EXIT 
1170 EVAL_SOURCE_ERR_:
1180 EXIT ERR
7000 CHECK_CONNECTION_:
7020 IF ARG.CONNECTED=0 THEN EXIT 14; REM endif
7090 RETURN 
7200 INIT_ARGS_:
7210 REM @return arg$: initialized
7220 DIM ARG$:IOL=ARG_IOL
7230 ARG_IOL: IOLIST CONNECTED,SOCKPORT,SOCKUNT,SOCKDEV$,SOCKHOST$,LOGIN$
7260 LET ARG.SOCKDEV$="N0"
7270 LET ARG.SOCKHOST$="localhost"
7280 LET ARG.SOCKPORT=2183
7290 RETURN 
8000 REM Functions
8060 DEF FNEVAL$(SOURCE$)
8062 LET RESULT$="",SOURCE$=CVS(SOURCE$,3),XLEN$=""
8064 IF SOURCE$="" THEN RETURN RESULT$; REM endif
8066 LET SOURCE$=BIN(LEN(SOURCE$),4)+SOURCE$
8068 WRITE RECORD (ARG.SOCKUNT)SOURCE$
8069 REM "NOT SURE WHY 1 READ,SIZ=4 DOESN'T WORK
8070 WHILE LEN(XLEN$)<4; READ RECORD (ARG.SOCKUNT,SIZ=1,TIM=30)X$; LET XLEN$=XLEN$+X$; WEND
8072  LET XLEN=DEC(XLEN$); IF XLEN<>0 THEN READ RECORD (ARG.SOCKUNT,SIZ=ABS(XLEN),TIM=5)RESULT$; REM endif
8074 IF XLEN<0 THEN ESCAPE 20; REM endif
8076 RETURN RESULT$
8078 END DEF
8700 ERR_HANDLER_:
8800 ESC_HANDLER_:
8810 EXIT ERR
10000 TEST_RUN_:
10010 SETESC TEST_ESC_HANDLER_; SETERR TEST_ERR_HANDLER_
10020 PRINT 'CS',"Buju Client: Pvx"
10040 CALL PGM(-2)+";init_args",ARG$
10050 LET XFIELD=0,FIELD_COUNT=4,HOSTF=1,PORTF=2,DEVICEF=3,LOGINF=4
10060 DIM XFIELD[FIELD_COUNT-1]
10070 LET XFIELD[0]=HOSTF,XFIELD[1]=PORTF,XFIELD[2]=DEVICEF,XFIELD[3]=LOGINF; REM Order of input
10100 WHILE XFIELD>=0 AND XFIELD<FIELD_COUNT
10110 SWITCH XFIELD[XFIELD]
10200 CASE HOSTF
10210 REM Host
10220 INPUT "Host (e.g.: localhost): ",X$
10230 IF X$="" THEN LET XFIELD=XFIELD-1; BREAK; REM endif
10240 LET ARG.SOCKHOST$=X$
10250 LET XFIELD=XFIELD+1
10260 BREAK
10300 CASE PORTF
10310 REM Port
10320 PORTF_INP: INPUT "Port (e.g.: 2183): ",X$
10330 IF X$="" THEN LET XFIELD=XFIELD-1; BREAK; REM endif
10340 LET ARG.SOCKPORT=INT(ABS(NUM(X$,ERR=PORTF_INP)))
10350 LET XFIELD=XFIELD+1
10360 BREAK
10400 CASE DEVICEF
10410 REM Device name
10420 rem INPUT "Device (e.g.: N0): ",X$
10430 rem IF X$="" THEN LET XFIELD=XFIELD-1; BREAK; REM endif
10440 rem LET ARG.SOCKDEV$=X$
10450 LET XFIELD=XFIELD+1
10460 BREAK
10500 CASE LOGINF
10510 REM Login arguments
10520 INPUT "Login arguments (e.g.: user=user passwd=user123):",'LF',X$
10530 IF X$="" THEN LET XFIELD=XFIELD-1; BREAK; REM endif
10540 LET ARG.LOGIN$=X$
10550 LET XFIELD=XFIELD+1
10560 BREAK
10900 DEFAULT 
10910 LET XFIELD=-1
10920 BREAK
10980 END SWITCH 
10990 WEND 
10995 IF XFIELD=-1 THEN RETURN ; REM endif
11100 REM Open connection to the server
11110 PRINT 'LF',"Opening connection..."
11120 CALL PGM(-2)+";open_connection",ARG$
11150 PRINT "Test client ready.",'LF',"At prompt, enter Java code to evaluate.",'LF',"Enter 'quit' to exit."
11900 INTERACTIVE_TEST_:
11910 REM Interactive send/receive
11920 LET RUNNING=1
11930 WHILE RUNNING
11940 INPUT "Send: ",X$
11950 IF X$="quit" THEN LET RUNNING=0; CONTINUE; REM endif
11960 PRINT FNEVAL$(X$,ERR=INTERACTIVE_TEST_ERR_)
11970 CONTINUE
11980 INTERACTIVE_TEST_ERR_:
11990 PRINT "Error ",ERR," ",RESULT$
11995 CONTINUE
12000 WEND 
14950 REM Close connection to the server
14960 PRINT 'LF',"Closing connection..."
14970 CALL PGM(-2)+";close_connection",ARG$,RESULT$
14980 PRINT RESULT$
14990 RETURN 
15700 TEST_ERR_HANDLER_:
15710 TEST_ESC_HANDLER_:
15720 IF ARG.SOCKUNT THEN CLOSE (ARG.SOCKUNT); REM endif
15790 EXIT ERR
