!
! Client (sender) side of RMIF RDP send buffer overflow test
!
! <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     1112
! @author  Neon Ngo
! @since   NeXtMidas 2.5.1
!
startmacro O:cntrl L:port L:msgCnt[120] S:host["localhost"]
  switch TRACE   trace        GET DEF=false SDEF=true
  switch DEBUG   debug        GET DEF=false SDEF=true
  switch VERBOSE verbose      GET DEF=false SDEF=true
  switch STATEM  statemachine GET DEF=false SDEF=true

  ! workaround since CONFIDENCE sets /DEBUG= to an integer (1 or 0) and in 3.1.0 it is propagated to
  ! sub-macros, so we need to convert it back to a boolean (true or false)
  set z:debug TEST(debug)

  set reg.cntrl  cntrl  ! save reference to parent control macro
  set l:maxRetry 5      ! maximum number of RDP send packet retries

  ! setup big test message under 32K limit
  ! using three messages under 32K to try to fill 64K buffer
  ! note the packet size were choose so they are somewhat prime
  ! which will help show the problem faster
  new java.lang.StringBuffer tempmsg1
  new java.lang.StringBuffer tempmsg2
  new java.lang.StringBuffer tempmsg3
  set templateData1 "BUG_1112_TEST_BIG_MESSAGE__29" ! 29 chars
  set templateData2 "ANOTHER_BIG_MESSAGE__23"       ! 23 chars
  set templateData3 "BIG_MESSAGE______19"           ! 19 chars
  !set templateData3 "BIG_MESSAGE____17"             ! 17 chars
  !set templateData3 "ANOTHER_BIG_1"                 ! 13 chars
  do i 1 1000
    invoke ,, tempmsg1.append("^{templateData1}")
    invoke ,, tempmsg2.append("^{templateData2}")
    invoke ,, tempmsg3.append("^{templateData3}")
  enddo
  if debug then say "Big message 1 length = ^tempmsg1.length"
  if debug then say "Big message 2 length = ^tempmsg2.length"
  if debug then say "Big message 3 length = ^tempmsg3.length"

  pipe on
    ! startup client (sender) RMIF
    rmif/id=SENDER/msgid=MAIN/PPT={RETRY=^maxRetry,DEBUG=^trace,VERBOSE=^trace}/STATEM=^statemachine/VERBOSE=^verbose PORT=-1

    ! tell client RMIF to open connection to remote RMIF server
    message FUNC=sendw ID=SENDER NAME="OPEN" DATA={ID=REMOTE,HP="^host:^port"}

  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
    rem reg.cntrl   ! we put this in manually, now remove it
    pipe stop       ! sent by ctrl

  elseif msg.name eqs "START_TEST" then
    if debug then INFO "Client (sender) sending ^msgCnt SET messages to remote server (receiver)..."
    do i 1 msgCnt
      calculator l:x i 3 MOD 1 +
      message FUNC=send ID=REMOTE NAME="SET" INFO=0 DATA={"^i ^{tempmsg^x}"}
    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
    WARN "Received a LINKERR. ^msg.data"
    set try msg.data.packet.try
    set rep msg.data.packet.rep
    results isInvalidTry TEST(try GT maxRetry)
    results isInvalidRep TEST(rep NEQ 69 AND rep NEQ 73)
    if debug then
      if isInvalidTry then WARN "Invalid try [^try] in packet, should be <= ^maxRetry"
      if isInvalidRep then WARN "invalid byte rep [^.rep] in packet, should be 69 (E) or 73 (I)"
    endif
    if isInvalidTry OR isInvalidRep then
     ! notify control macro of link error and corrupt packet
      message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 &
              DATA={TEXT="got LINKERR and packet is corrupted. ^{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
