!
! Server part of RMIF test
!
! @author  Jay Hallam / Neon Ngo
! @bug 1541 & 1631: Reopen not quite right/restarting a remote
!
! <cntrl> - The parent macro object
! <port>  - The KNOWN port of the server.  Client uses any available port.
!
startmacro o:cntrl l:port
  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 killedRmif false
  set wait       1.0   ! Number of seconds to wait for extra messages at end
  set rmifid     "RMIFSRV"

  pipe on
    ! start RMIF in foreground for pipe section
    call startRMIF false
    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 "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
    ! do nothing

  elseif msg.name eqs "TIMER" then
    ! done waiting for any extra messages, let control macro know that we are done
    message FUNC=send ID=cntrl NAME=SERVER_DONE DATA={TEST=TRUE}

  elseif msg.name eqs "OPEN" then
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="Got open. killedRmif=^{killedRmif}",TEST=TRUE}

    if killedRmif isTrue then
      ! on 2nd OPEN msg, send a GET request to client
      message FUNC=send ID=^{msg.data.id} NAME="GET" DATA={MYTESTKEYTOCLIENT=?}
      ! break out of processMessage
      return
    endif

    ! kill RMIF instance (for testing only)
    mess FUNC=SENDW ID=^{RMIFID} NAME=KILL_FOR_TESTING_ONLY  ! This message is for testing only, do not use in your macros.
    set killedRmif true

    ! give a tiny amount of time for killed RMIF to shutdown
    pause 0.25

    ! verify that RMIF was KILLED for test case
    set isRmifNotInRegistry TEST(reg.^{RMIFID},nrexists)
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="killed RMIF server instance for testing",TEST=^isRmifNotInRegistry}

    ! start RMIF in background
    call startRMIF true

  elseif msg.name eqs "RET" then
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="got RET response.",TEST=TRUE}

    ! wait a few seconds to see if extra messages are sent.
    set this.timer(0) wait

  elseif msg.name eqss "CLOSE" then
    ! ignore CLOSE and CLOSED messages

  elseif msg.name eqs "LINKERR" then
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="got unexpected LINK ERROR: ^{msg.data}",TEST=FALSE}

  elseif msg.name eqs "NULL"
    ! ignore

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

!--------------------------------------------------------------------------------------------------!
procedure startRMIF s:sr_background[false]
  if sr_background isTRUE then
    rmif/id=^{RMIFID}/msgid=main/STATEM=^statemachine/VERBOSE=^verbose/BG ^port
    if reg.^{RMIFID} REXISTS then
      info "Restarted RMIF on port ^reg.^{RMIFID}.port"
    else
      warn "FAILED to restart RMIF for testing"
    endif
  else
    rmif/id=^{RMIFID}/msgid=main/STATEM=^statemachine/VERBOSE=^verbose ^port
  endif
return
