// PupTestBSP.bcpl - Pup package test program
// Copyright Xerox Corporation 1979, 1982
// Last modified February 15, 1982  5:27 PM by Boggs

get "Pup0.decl"
get "Pup1.decl"
get "PupRTP.decl"
get "PupBSP.decl"
get "PupTest.decl"

external
[
//outgoing procedures
BSPReceive; BSPSend

//incoming procedures
Zero; MoveBlock; BlockEq
Puts; Endofs; Closes; Block; Free; Allocate
OpenLevel1Socket; CloseLevel1Socket; SetAllocation
OpenRTPSocket; CloseRTPSocket; ComputeThruput
BSPReadBlock; BSPWriteBlock; CreateBSPStream
OpenPort; PutTemplate; GetString; GetPartner; Ws; Wss

//incoming statics
State; Cmmd; checksums; checkdata
keys; dsp; sysZone; pbiCount
checkBuffer; sendPort; tp; ctxQ
]

manifest
[
socketBSPTest = 6
realTimeClock = 430b
]

structure Byte↑0,0 byte

//----------------------------------------------------------------------------
let BSPReceive() be	//a context
//----------------------------------------------------------------------------
[
let inBuffer = Allocate(sysZone, 256)
let soc = vec lenBSPSoc
   [
   OpenLevel1Socket(soc, table [ 0; 0; socketBSPTest ])
   let total = pbiCount/2
   SetAllocation(soc, total, total-1, total-1)
   soc>>PupSoc.doChecksum = checksums
   OpenRTPSocket(soc, 0, modeListenAndReturn)
   Block() repeatuntil soc>>BSPSoc.state eq stateOpen
   let str = CreateBSPStream(soc)
      [
      if BSPReadBlock(str, inBuffer, 0, 512, 12000) ls 512 break  //2 min
      if checkdata then unless BlockEq(checkBuffer, inBuffer, 256) do
         [ Ws("*NRcvr Data Compare Error."); break ]
      ] repeat
   Closes(str)
   ] repeat
]

//----------------------------------------------------------------------------
and BSPSend() be
//----------------------------------------------------------------------------
[
Ws("BSP Send.  ")
let soc = vec lenBSPSoc
test Cmmd eq 0
   ifnot [ Cmmd = 0; OpenLevel1Socket(soc, 0, sendPort) ]
   ifso unless OpenPort("Receive Port: ", soc, 0, socketBSPTest) return
let total = pbiCount/2
SetAllocation(soc, total, total-1, total-1)
Ws("- Checksums "); Ws(checksums ? "enabled*N", "disabled*N")
soc>>PupSoc.doChecksum = checksums
test OpenRTPSocket(soc)
   ifso Ws("Connection open.  '!' = 10,000 bytes*N")
   ifnot [ Ws("Failed."); CloseLevel1Socket(soc); return ]
State = stateBSP
let str = CreateBSPStream(soc)
Zero(tp, lenTP)
tp>>TP.timer = @realTimeClock
   [
   unless BSPWriteBlock(str, checkBuffer, 0, 512) then
      [ Ws("*NBSPWriteBlock Failed"); break ]
   tp>>TP.count = tp>>TP.count + 512
   if tp>>TP.count ge 10000 then
      [
      tp>>TP.count = tp>>TP.count - 10000
      Puts(dsp, $!)
      tp>>TP.bigCount = tp>>TP.bigCount+1
      if tp>>TP.bigCount eq 50 then  //500,000 bytes
         [
         ComputeThruput(tp)
         tp>>TP.bigCount = 0
         tp>>TP.timer = @realTimeClock
         ]
      ]
   ] repeatwhile Endofs(keys) & soc>>BSPSoc.state eq stateOpen & Cmmd eq 0
test Closes(str)
   ifso Ws("*NClosed cleanly")
   ifnot Ws("*NClosed uncleanly")
]