!
! Client part of RMIF's ability to send GET/RET packet using specified protocol
! (either RDP (default) or UDP) based on the QUALS={PROTOCOL="xxx"} qualifier
! in the message sent to the RMIF primitive for the specified remote.
!
! @author  Neon Ngo
! @bug 1997 RMIF: should allow for special processing of individual messages
! @since NeXtmidas 2.7.3
!
! <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=RMIFC/msgid=main/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort
    message FUNC=sendw ID=RMIFC NAME="OPEN" DATA={ID=CLIENT,HP="^host:^port"}

    ! timer to continue to send out GET via UDP every second until test case finishes
    res this.timer(0) 1.0
  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 "OPENED" then
    message FUNC=send ID=cntrl NAME=CLIENT_STARTED INFO=0 DATA={TEST=PASS}

  elseif msg.name eqs "START_TEST" then
    message FUNC=send ID=CLIENT NAME="GET" DATA={SFREQ=?} QUALS={PROTOCOL="UDP"}

  elseif msg.name eqs "TIMER" then
    message FUNC=send ID=CLIENT NAME="GET" DATA={SFREQ=?} QUALS={PROTOCOL="UDP"}

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

    message FUNC=send ID=cntrl NAME=CLIENT_DONE INFO=0 DATA={TEST=PASS}

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

  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
