! Client part of sending pipe data over RMIF test
!
! @bug     2146 RMIF: incorrect internal packet buffer size
! @author  Neon Ngo
!
! <cntrl> - The parent macro object
! <port>  - The KNOWN port of the server.  Client uses any available port.
! <frameSize> - Size of frame for served type 2000 pipe (in bytes)
!
! Switches:
!   /savepipetofile - (for debugging), set to filename to save remote pipe
!                     locally (remember to uses quotes to keep filename case).
!                     if used as a state switch, then will use default file name.
!
startmacro O:cntrl L:port S:host["LOCALHOST"]  L:frameSize
  switch SAVEPIPETOFILE savepipetofile GET DEF=""    SDEF="test_rmif_pipe_received_data.tmp"
  switch DEBUG          debug          GET DEF=false SDEF=true
  switch VERBOSE        verbose        GET DEF=false SDEF=true
  switch STATEM         statemachine   GET DEF=false SDEF=true

  set reg.cntrl cntrl
  set L:anyAvailablePort -1

  pipe on
    rmif/id=RMIFC/msgid=main/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort
    message FUNC=sendw ID=RMIFC NAME="OPEN" DATA={ID=CLIENT,HP="^host:^port"}
  pipe off

endmacro

!******************************************************************************!
procedure processMessage m:msg
  if debug then
    say "^reg.main.name: Got message name=^msg.name fid=^msg.fid info=^msg.info data=^msg.data"
  endif

  if msg.name eqs "STOP_TEST" then
    rem reg.cntrl ! we put this in manually, now remove it
    say "!!!!! It is OK to see a warning that Remote is CLOSED not sending packet func=DBUF... !!!!!"
    pipe stop     ! sent by ctrl

  elseif msg.name eqs "START_TEST" then
    ! wait one second for remote pipe to get setup
    set this.timer(0) 1.0

  elseif msg.name eqs "OPENED" then
    ! add channel #1 to pipe on remote server
    send "CLIENT" "ADDC" {_remotepipe=_pipe} 1

    ! for debugging, save remote pipe data to local file
    if savepipetofile neqs "" then
      noop/bg _remotepipe savepipetofile
      info "Saving remote pipe to file: ^savepipetofile"
    endif

    ! we now ready to start test
    sendto cntrl "CLIENT_STARTED" DATA={TEST=PASS}

  elseif msg.name eqs "TIMER" then
    if _remotepipe fexists then
      set this.timer(0) 0.0           ! turn off timer
      sendto cntrl "CLIENT_TEST" DATA={TEXT="Got remote PIPE",TEST=TRUE}
      ! get remote pipe information and verify header
      status _remotepipe type=type format=form frameSize=fs
      set isType2000 TEST(type,eq,2000)
      sendto cntrl "CLIENT_TEST" DATA={TEXT="Remote PIPE type (^type) is 2000",TEST=^isType2000}
      set isFormatSB TEST(form,eqs,"SB")
      sendto cntrl "CLIENT_TEST" DATA={TEXT="Remote PIPE format (^form) is SB",TEST=^isFormatSB}
      set isValidFrameSize TEST(fs,eq,frameSize)
      sendto cntrl "CLIENT_TEST" DATA={TEXT="Remote PIPE Frame Size (^fs) is ^frameSize",TEST=^isValidFrameSize}

      ! done waiting for remote pipe data to be served to us
      sendto cntrl "CLIENT_DONE" DATA={TEST=PASS}
    endif

  elseif msg.name eqss "CLOSE" then
    ! ignore CLOSE and CLOSED messages
  elseif msg.name eqs "NULL" then
    ! ignore
  else
    warn "GOT unknown message: ^msg"
    sendto cntrl "CLIENT_TEST" DATA={TEXT="Got unhandled message name=^msg.name",TEST=FALSE}
  endif
return
