!
! Server (receiver) 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 to
!             verify against the number of SET messages received.
!
! @bug      1112
! @author   Neon Ngo
! @since    NeXtMidas 2.5.1
!
startmacro O:cntrl L:port L:msgCnt[120]
  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   ! control macro

  set wait     5        ! Number of extra seconds to wait for extra messages
  set L:count  0        ! Total SET messages received
  set passFail "PASS"   ! Overall status
  set normalStop false  ! If this macro was stopped normally (internally)

  pipe on
    rmif/id=RECEIVER/msgid=MAIN/ppt={DEBUG=^debug}/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 "START_TEST" then
    ! do nothing, waiting for client to send packets

  elseif msg.name eqs "STOP_TEST" then
    if normalStop ISFALSE then
      ! got STOP_TEST message from control macro, check if we received correct # of messages
      set passFail TEST(count,EQ,msgCnt)
      message FUNC=send ID=cntrl NAME=SERVER_TEST &
              DATA={TEXT="received ^count messages, expected ^{msgCnt}.",TEST=^passFail}
    endif

    rem reg.cntrl  ! we put this in manually, now remove it
    pipe stop      ! sent by cntrl

  elseif msg.name eqs "SET" then
    calc L:count count 1 +
    if count GT msgCnt then
      set passFail FAIL
      message FUNC=send ID=cntrl NAME=SERVER_TEST &
              DATA={TEXT="received ^count messages, expected ^msgCnt.",TEST=FAIL}
    elseif count EQ msgCnt then
      ! We should be done now, but wait a few seconds to see if extra messages are sent...
      set this.timer(0) wait
    endif

  elseif msg.name eqs "OPEN" then
    ! ignore
  elseif msg.name eqss "CLOSE" then
    ! ignore CLOSE and CLOSED messages
  elseif msg.name eqs "NULL" then
    ! ignore
  elseif msg.name eqs "TIMER" then
    ! done waiting for any extra messages
    set normalStop true
    set passFail TEST(count,eq,msgCnt)
    message FUNC=send ID=cntrl NAME=SERVER_TEST &
            DATA={TEXT="received ^count messages, expected ^msgCnt.",TEST=^passFail}
    message FUNC=send ID=cntrl NAME=SERVER_DONE DATA={TEST=^passFail}

  elseif msg.name eqs "LINKERR" then
    ! notify control macro of link error as a failed test
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 &
            DATA={TEXT="got LINKERR data=^msg.data.text",TEST=FAIL}
  else
    ! notify control macro of unknown message as a failed test
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 &
            DATA={TEXT="got unhandled message name=^msg.name data=^msg.data",TEST=FAIL}
  endif
return
