!
!  Purpose: Test the NeXtMidas ZPIPE primitive
!  Author: Jay Hallam
!  TODO: This test is dependent on X-Midas, so the user GUI need to change to
!  allow the user to run it only if X-Midas is available
!------------------------------------------------------------------------------ 
startmacro L:nels[1e5]
  switch INTERACTIVE interAct GET DEF=TRUE
  switch BASELINE    baseline GET DEF=TRUE

  set port 25000
  set xfer 16k
  set format "SL"
  if baseline and interAct then
    set testFile "test_zpipe_ramp"
    if testfile NFEXISTS or /REGEN isTRUE then
      info "^reg.main.name: generation test file..."
      if /SIN then
        info "^reg.main.name: generating SINEWAVE... "
        waveform testfile FORM=format ELEM=nels
      else
        info "^reg.main.name: generating RAMP... "
        ramp out=testfile FORM=format ELEM=nels
      endif
    endif
    pipe on
      if /ONESHOT then
        info "^reg.main.name: ONESHOT noop... "
        noop/rt/rate=xfer testfile _p
      else
        info "^reg.main.name: /WRAP noop... "
        noop/rt/rate=xfer/wrap testfile _p
      endif
      if /CLIENT then
        zpipe/xm_in/tl=xfer/format=format IN=myfileout HOST=localhost PORT=25000 /verbose /debug
      else
        zpipe/tl=xfer/verbose/format=format IN=_p PORT=port
      endif

      panel/msgid=MAIN/setup/controls=gc
      !!!plot/id=staticplot testfile 
      plot _p scale=NONE x1=0 x2=xfer y1=0 y2=nels

      gcontrol menu main "" "EXIT" /tleft
      set gc.main.itemcolors {EXIT=RED}
      gcontrol lval numconns "Num Conn" 0 1e6 1 /MON=reg.ZPIPE.numconnections /msg=f
      set instructions "Run the command &
                        zpipe/xm_in/tl=^{xfer}/format=^{format} myfileout 25000 localhost  &
                        in an X-Midas session"
      gcontrol text instruct "Instructions" "^instructions" 24 10
      gcontrol PIPEMON _p _p
    pipe off
  else
    info "^{reg.main.name}: Must be run in baseline and interactive mode"
  endif

  ! Cleanup test files
  erase testfile
endmacro

!******************************************************************************!
procedure processMessage
  if msg.name EQS MAIN then
    pipe stop
  else 
    warn "^reg.main.name: unrecognized message ^msg.name from ^msg.fid"
  endif
return
