!
! Client (sender) side of RMIF RDP receive buffer test. The RMIF instance used to
! send messages here uses a large window size to increase the likelyhood for the
! test case to show the problem.
!
! <cntrl>   - The parent control macro object
! <port>    - The KNOWN port of the server.  Client uses any available port.
! <msgCnt>  - The number of messages the client (sender) will send out
! <sizeMul> - Packet size multipler for messages to send
!
! @bug     1193
! @author  Neon Ngo
! @since   NeXtMidas 2.5.1
!
!L:msgCnt[1000] D:timeout[40] L:size[250]

startmacro O:cntrl L:port L:msgCnt[1000] L:sizeMul[250] S:host["localhost"]
  switch WINDOW  window       GET DEF=64
  switch DEBUG   debug        GET DEF=false SDEF=true
  switch VERBOSE verbose      GET DEF=false SDEF=true
  switch STATEM  statemachine GET DEF=false SDEF=true

  invoke headless nxm.sys.lib.Shell.isHeadless()
  if statemachine and headless then
    info "SKIPPING ^reg.main.name because this test intermittently fails with /STATEM in HEADLESS mode."
  else
    set reg.cntrl  cntrl    ! save reference to parent control macro
    set L:droppedPackets 0  ! Total number of dropped packets (messages)
    set datamsg ""
    do i 1 ^sizeMul
      set datamsg "^{datamsg}TEST_RMIF_RECV_BUFFER___xxxxxxxx__yyyyyyyyyyyy__zzzzzzzzzzz__ABC____ "
    enddo

    pipe on
      ! startup client (sender) RMIF
      ! increase max send queue to maximum number of messages we are trying to send out
      ! to prevent Send Queue is full exception from Rmif library
      rmif/id=SENDER/msgid=MAIN/PPT={WINDOW=^window,MaxQueue=^msgCnt}/STATEM=^statemachine/VERBOSE=^verbose PORT=-1

      message FUNC=sendw ID=SENDER NAME="OPEN" DATA={ID="REMOTE",HP="^host:^port"}

    pipe off

    if droppedPackets gt 0 then
      info "^{droppedPackets} dropped packets during test."
    endif
  endif
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
    info "Client sending ^msgCnt SET messages to remote server (receiver), packet size=^{datamsg.length}"
    do i 1 msgCnt
      message FUNC=send ID=REMOTE NAME="SET" INFO=0 DATA={i=^i,msg=^datamsg}
    enddo
    message FUNC=send ID=cntrl NAME=CLIENT_DONE INFO=0 DATA={TEST=PASS}

  elseif msg.name eqs "OPENED" then
    message FUNC=send ID=cntrl NAME=CLIENT_STARTED DATA={TEST=PASS}

  elseif msg.name eqs "SET" then
    ! do nothing
  elseif msg.name eqss "CLOSE" then
    ! ignore CLOSE and CLOSED messages
  elseif msg.name eqs "NULL"
    ! ignore
  elseif msg.name eqs "LINKERR" then
    if msg.data.text EQSS "PKTDROP" then
      calc L:droppedPackets droppedPackets 1 +
      ! warn "Received a LINKERR: ^{msg.data.text} droppedPackets=^{droppedPackets}"
    else
      ! notify control macro of all other link errors as test failure
      message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 &
              DATA={TEXT="got unexpected LINKERR: ^{msg.data.text}.",TEST=FAIL}
    endif
  else
    ! notify control macro of unknown message as a failed test
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 &
            DATA={TEXT="got unhandled message name=^msg.name data=^msg.data",TEST=FAIL}
  endif

return