! Client 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 to send to server and expected response packets
!
startmacro O:cntrl L:port S:host["localhost"] 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
  set L:anyAvailablePort -1
  set remotehp "^host:^port"
  set remoteid "RID"
  set count 0

  pipe on
    rmif/id=RMIFC/msgid=main/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort
    message FUNC=send ID=cntrl NAME=CLIENT_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=CLIENT_TEST INFO=0 DATA={TEXT="got ^count of ^numPackets ACK response 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
    ! address remote and then send a SET message to it
    message FUNC=sendw ID=RMIFC NAME="ADDR" DATA={ID="^remoteid",HP="^remotehp"}
    do ii 1 numPackets
      message FUNC=send ID=^remoteid NAME="SET" DATA={FREQ=97.^ii}
    enddo

  elseif msg.name eqs "ACK" then
    calc L:count count 1 +
    ! got "ACK" response from remote to our "SET" request
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 DATA={TEXT="got ACK response #^count to SET data=^msg.data",TEST=TRUE}
    ! done testing on client side if we got expected number of response packets
    if count ge numPackets then
      message FUNC=send ID=cntrl NAME=CLIENT_DONE DATA={TEST=PASS}
    endif

  elseif msg.name eqss "CLOSE" then
    ! ignore CLOSE and CLOSED messages
  elseif msg.name eqs "NULL" then
    ! ignore
  else
    warn "GOT unknown message: ^msg"
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 DATA={TEXT="got unhandled message name=^msg.name",TEST=FALSE}
  endif
return
