!
! Client part of test that messages from RMIF remote's are not uniquely
! identified on the server side of RMIF.
!
! @bug     1238
! @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["RID"]
  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 passFail false ! overal test results
  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
    if passFail ISFALSE then
      message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 &
          DATA={TEXT="got ACK and SET from remote server.",TEST=FALSE}
    endif

    remove 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
    set gotAck TEST(msg.fid,EQS,remoteID)
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 DATA={TEXT="got ACK from ^remoteID",TEST=^gotAck}

    if passFail ISFALSE then     ! only check passFail when it hasn't passed yet
      set passFail TEST(gotAck,and,gotSet)
      if passFail then           ! if done with test then send done message
        message FUNC=send ID=cntrl NAME=CLIENT_DONE INFO=0 DATA={TEST=^passFail}
      endif
    endif

  elseif msg.name eqs "SET" then
    if msg.fid EQS ^remoteID then
      set gotSet true
      message FUNC=send ID=^remoteID NAME="ACK" INFO=0 DATA={TEXT="ACKING msg.data=^msg.data"}
    endif
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 DATA={TEXT="got SET from ^remoteID",TEST=^gotSet}

    if passFail ISFALSE then     ! only check passFail when it hasn't passed yet
      set passFail TEST(gotAck,and,gotSet)
      if passFail then           ! if done with test then send done message
        message FUNC=send ID=cntrl NAME=CLIENT_DONE INFO=0 DATA={TEST=^passFail}
      endif
    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
