!
! Client (sender) side of RMIF Remote reuse test. The RMIF instance used to
! send messages here uses a large window size and only one max RDP retry
! to increase the likelihood of a packet drop to capture problem for test case.
!
! <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
!
! @bug     1051
! @author  Neon Ngo
! @since   NeXtMidas 2.5.1

startmacro O:cntrl L:port S:host["LOCALHOST"] L:msgCnt[150] L:windowSize[80]
  switch SIZEMUL multipler    GET DEF=1000
  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:anyAvailablePort -1
    set L:packetDropped 0 ! number of packets dropped

    ! setup big test message under 32K limit (to increase likelihood of a packet drop)
    new java.lang.StringBuffer tempmsg1
    set templateData1 "BUG_1051_TEST_REMOTE_REUSE_29" ! 29 chars
    do i 1 ^multipler
      invoke ,, tempmsg1.append("^{templateData1}")
    enddo

    pipe on
      ! startup client (sender) RMIF
      rmif/id=SENDER/msgid=MAIN/PPT={WINDOW=^windowSize,RETRY=1,MAXQUEUE=^msgCnt}/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort

      ! tell client RMIF to open connection to remote RMIF server
      ! to simulate bug, we do a second OPEN immediately (which should go out before
      ! the OPENED message from the first OPEN on REMOTE1 arrives.
      message FUNC=sendw ID=SENDER NAME="OPEN" DATA={ID="REMOTE1",HP="^host:^port"}
      message FUNC=sendw ID=SENDER NAME="OPEN" DATA={ID="REMOTE2",HP="^host:^port"}

    pipe off
  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
    if packetDropped LE 0 then
      info "Unable to simulate PKTDROP LINKERR for second part of test case."
    else
      message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 DATA={TEXT="sender able to simulate ^packetDropped dropped packets (PKTDROP)",TEST=PASS}
    endif
    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~=^tempmsg1.length"
    do i 1 msgCnt
      message FUNC=send ID=REMOTE2 NAME="SET" INFO=0 DATA={"^i ^{tempmsg1}"}
    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
      set L:packetDropped (packetDropped + 1)
      ! forward packet drop link error to control macro
      message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=1 DATA={TEXT=^msg.data.text,TEST=FAIL}
    else
      WARN "Received a LINKERR. ^msg.data"
      ! notify control macro of link error
      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
