!
! Client 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 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=RMIFCIENT/msgid=main/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort
    message FUNC=sendw ID=RMIFCIENT NAME="OPEN" DATA={ID=RECEIVER,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
    ! ignore

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

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

  elseif msg.name eqs "NULL"
    ! ignore

  elseif msg.name eqs "GET" then
    ! send back the RET
    message FUNC=send ID=msg.fid NAME="RET" DATA={MYTESTKEYTOCLIENT="test value"}

    message FUNC=send ID=cntrl NAME=CLIENT_TEST DATA={TEXT="got GET request.",TEST=TRUE}
    ! client side of test case is now done
    message FUNC=send ID=cntrl NAME=CLIENT_DONE INFO=0 DATA={TEST=PASS}

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

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