!
! Client part of test for sending and receiving to and from an RMIF remote with
! the remote ID (RID) set on a client's OPEN message.
!
! @author  Neon Ngo
!
! <cntrl>    - The parent macro object
! <port>     - The KNOWN port of the server.  Client uses any available port.
! <remoteID> - ID to use for remote.
!
startmacro O:cntrl L:port S:host["LOCALHOST"] S:remoteID[TESTREMOTEID]
  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
  set gotAck false
  set gotSet false

  pipe on
    rmif/id=RMIF/msgid=main/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort
    message FUNC=sendw ID=RMIF NAME="OPEN" DATA={ID=^remoteID,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
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 DATA={TEXT="Got ACK from ^remoteID",TEST=^gotAck}
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 DATA={TEXT="Got SET from ^remoteID",TEST=^gotSet}
    rem reg.cntrl ! we put this in manually, now remove it
    pipe stop     ! sent by ctrl

  elseif msg.name eqs "START_TEST" then
    message FUNC=send ID=^remoteID NAME="SET" INFO=0 DATA="{text=test message to remote ^remoteID}"

  elseif msg.name eqs "ACK" then
    if msg.fid EQS ^remoteID then
      set gotAck true
    endif

  elseif msg.name eqs "SET" then
    if msg.fid EQS ^remoteID then
      set gotSet true
      message FUNC=send ID=cntrl NAME=CLIENT_DONE INFO=0 DATA={TEST=PASS}
      message FUNC=send ID=^remoteID NAME="ACK" INFO=0
    endif

  elseif msg.name eqs "OPENED" then
    message FUNC=send ID=cntrl NAME=CLIENT_STARTED INFO=0 DATA={TEST=PASS}

  elseif msg.name eqs "CLOSED" then
    ! ignore

  elseif msg.name eqs "NULL" then
    ! ignore

  else
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 &
            DATA={TEXT="Got unhandled message name=^msg.name",TEST=FALSE}
  endif
return
