!
! Server part of test that messages from RMIF remote's are not uniquely
! identified on the server side of RMIF.
!
! @bug     1238
! @author  Neon Ngo
!
! <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 passFail         false ! overal test results
  set gotAck           false
  set gotSet           false
  set S:remoteClientID "initialRemoteClientId"

  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
    if passFail ISFALSE then
      message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 &
              DATA={TEXT="Got ACK and SET from remote client.",TEST=FALSE}
    endif
    remove 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 "SET" then
    ! got SET from remote client, send back ACK message
    message FUNC=send ID=^msg.fid NAME="ACK" INFO=0 DATA={RESPONSE="Got SET message=^msg.data",FID=^msg.fid}
    set gotSet TEST(msg.fid,EQS,remoteClientID)  ! verify Remote ID of message
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="got SET from ^{remoteClientID}.",TEST=^gotSet}

    if passFail ISFALSE then     ! only check passFail when it hasn't passed yet
      set passFail TEST(gotAck,and,gotSet)
      if passFail then           ! if done with test then send done message
        message FUNC=send ID=cntrl NAME=SERVER_DONE INFO=0 DATA={TEST=^passFail}
      endif
    endif

  elseif msg.name eqs "ACK" then
    ! got ACK from remote client from our SET message
    set gotAck TEST(msg.fid,EQS,remoteClientID)  ! verify Remote ID of message
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="got ACK from ^{remoteClientID}.",TEST=^gotAck}

    if passFail ISFALSE then     ! only check passFail when it hasn't passed yet
      set passFail TEST(gotAck,and,gotSet)
      ! check if done with test
      if passFail then           ! if done with test then send done message
        message FUNC=send ID=cntrl NAME=SERVER_DONE INFO=0 DATA={TEST=^passFail}
      endif
    endif

  elseif msg.name eqs "OPEN" then
    ! Got OPEN from remote client, change its default FID of "REMOTE" to desired scheme for test
    set remote msg.data  ! remote object is in the DATA field of the message table
    set tmpNewId "REMOTE^{remote.host}_P_^{remote.port}"
    sedit tmpNewId remoteClientID  GSUB "." "_"
    set remote.id remoteClientID

    ! after OPEN from remote client, send it a SET message using its newly designated remote ID
    message FUNC=send ID=^remoteClientID NAME="SET" INFO=0 &
            DATA={TEXT="SERVER test message to remote client using new ID"}

  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
