!
! Client (sender) side of RMIF rogue CLOSED messages when closing a Remote
! test case.
!
! <cntrl>   - The parent control macro object
! <port>    - The KNOWN port of the server.  Client uses any available port.
!
! @bug     1054
! @author  Neon Ngo
! @since   NeXtMidas 2.5.3
!
startmacro O:cntrl L:port S:host["LOCALHOST"]
  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  ! save reference to parent control macro
  set L:anyAvailablePort -1
  set wait       3      ! Number of extra seconds to wait for extra messages
  set gotClosed1 false
  set gotClosed2 false

  pipe on
    ! startup client (sender) RMIF
    rmif/id=SENDER/msgid=MAIN/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort

    message FUNC=sendw ID=SENDER NAME="OPEN" DATA={ID="REMOTE1",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
    pipe stop       ! sent by ctrl

  elseif msg.name eqs "START_TEST" then
    message FUNC=send ID=REMOTE1 NAME="CLOSE" INFO=0

  elseif msg.name eqs "OPENED" then
    set remote msg.data
    if remote.id eqs "REMOTE1" then
      message FUNC=send ID=cntrl NAME=CLIENT_STARTED DATA={TEST=PASS}
    elseif remote.id eqs "REMOTE2" then
      ! We should be done now, but wait a few seconds to see if any rogue messages come in
      set this.timer(0) wait
    endif

  elseif msg.name eqs "CLOSE" then
    ! ignore

  elseif msg.name eqs "CLOSED" then
    set remote msg.data
    if remote.id eqs "REMOTE1" then
      set gotClosed1 true
      message FUNC=sendw ID=SENDER NAME="OPEN" DATA={ID="REMOTE2",HP="^host:^port"}
    elseif remote.id eqs "REMOTE2" then
     set gotClosed2 true
    endif

  elseif msg.name eqs "NULL"
    ! ignore

  elseif msg.name eqs "LINKERR" then
    WARN "Received a LINKERR. ^msg.data"
    ! notify control macro of link error
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 &
            DATA={TEXT="got unexpected LINKERR: ^{msg.data.text}.",TEST=FAIL}

  elseif msg.name eqs "TIMER" then
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 &
            DATA={TEXT="CLOSED message from REMOTE1.",TEST=^gotClosed1}

    set closed2Result TEST(gotClosed2,ISFALSE)
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 &
            DATA={TEXT="rogue CLOSED message from REMOTE2.",TEST=^closed2Result}

    set passFail TEST(gotClosed1,and,closed2Result)

    ! done waiting for any extra messages. Tell control macro client part of test is done
    message FUNC=send ID=cntrl NAME=CLIENT_DONE DATA={TEST=^passFail}

  else
    ! notify control macro of unknown message as a failed test
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 &
            DATA={TEXT="got unhandled message name=^msg.name data=^msg.data",TEST=FAIL}
  endif

return
