! Client part of "closed" Remote should NOT send messages over network
!
! <cntrl> - The parent macro object
! <port>  - The KNOWN port of the server.  Client uses any available port.
!
! @bug     1633 RMIF: Closed remotes still send messages
! @author  Neon Ngo
! @since   NeXtMidas 2.7.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
  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
    pipe stop     ! sent by ctrl

  elseif msg.name eqs "START_TEST" then
    ! disconnect from Remote RMIF instance
    message FUNC=send ID=CLIENT NAME=CLOSE
    ! below SET message SHOULD not get sent to the Remote RMIF since we closed it
    message FUNC=send ID=CLIENT NAME=SET DATA={X=Remote_RMIF_should_not_get_this}

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

  elseif msg.name eqss "CLOSED" then
    ! We are now done with test case
    message FUNC=send ID=cntrl NAME=CLIENT_DONE INFO=0 DATA={TEST=PASS}

  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
