!
! Server 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
  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

  pipe on
    rmif/id=RMIF/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 "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 "OPEN" then
    ! do nothing (waiting for GET message)

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

    ! fill in the table values with bogus number for the RET response data
    foreach key inTable msg.data
      set msg.data.^key 1234.5
    endfor
    ! send back the result to the client using RDP protocol (this is default, but want to be specify for test case)
    message FUNC=send ID=msg.fid NAME="RET" DATA=msg.data QUALS={PROTOCOL="RDP"}

    message FUNC=send ID=cntrl NAME=SERVER_DONE INFO=0 DATA={TEST=TRUE}

  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 error",TEST=FALSE}

  elseif msg.name eqs "NULL" then
    ! ignore

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

