! Server part of an ADDRessed RMIF remote test case
!
! @bug     2138 RMIF (nxm273 to nxm280) can no longer send messages to existing
!               RMIF servers (nxm272 and prior) that have been ADDRressed.
! @author  Neon Ngo
!
! <cntrl>     - The parent macro object
! <port>      - The KNOWN port of the server.  Client uses any available port.
! <numPackets>- Number of packets expected from client.
!------------------------------------------------------------------------------!
startmacro O:cntrl L:port L:numPackets[5]
  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
  res serverproperty "Result value on RMIF server"
  set count 0

  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
    set isCountValid TEST(count,EQ,numPackets)
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="got ^count of ^numPackets SET packets",TEST=^isCountValid}
    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 "SET" then
    calc L:count count 1 +
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="got SET request #^count data=^msg.data",TEST=TRUE}
    ! send ACK back to the remote client
    message FUNC=send ID=msg.fid NAME="ACK" DATA=msg.data
    ! done testing on server side if we got expected number of packets
    if count ge numPackets then
      message FUNC=send ID=cntrl NAME=SERVER_DONE INFO=0 DATA={TEST=TRUE}
    endif

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

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

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