!
! Server (receiver) 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 L:msgCnt
  switch TRACE   trace        GET DEF=false SDEF=true
  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   ! control macro

  set wait       3      ! Number of extra seconds to wait for extra messages
  set passFail "PASS"   ! Overall status
  set normalStop false  ! If this macro was stopped normally (internally)
  set gotClose   false

  pipe on
    ! startup RMIF server (to receive messages from client)
    rmif/id=RECEIVER/msgid=MAIN/STATEM=^statemachine/VERBOSE=^verbose ^port

    message FUNC=send ID=cntrl NAME=SERVER_STARTED INFO=0 DATA={TEST=PASS}
  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 "START_TEST" then
    ! do nothing, waiting for client to send packets

  elseif msg.name eqs "STOP_TEST" then
    ! STOP_TEST is sent by cntrl macro
    rem reg.cntrl  ! we put this in manually, now remove it
    pipe stop

  elseif msg.name eqs "OPEN" then
    if gotClose then
      ! this should be the OPEN from 2nd client
      ! 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
    set gotClose true

  elseif msg.name eqs "CLOSED" then
    ! ignore

  elseif msg.name eqs "NULL" then
    ! ignore
  elseif msg.name eqs "TIMER" then
    ! done waiting for any extra messages
    message FUNC=send ID=cntrl NAME=SERVER_DONE DATA={TEST=^passFail}

  elseif msg.name eqs "LINKERR" then
    ! notify control macro of link error as a failed test
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 &
            DATA={TEXT="got unexpected LINKERR data=^msg.data",TEST=FAIL}
  else
    ! notify control macro of unknown message as a failed test
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 &
            DATA={TEXT="got unhandled message name=^msg.name data=^msg.data",TEST=FAIL}
  endif
return
