!
! This will test number of OPEN messages received following an RMIF open
!
! @author  J. Czechowski IV
!
startmacro O:cntrl L:port S:host["LOCALHOST"]
  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 wait        5  ! Number of extra seconds to wait for extra messages
  set openedCount 0  ! Number of OPENED messages

  pipe on
    ! Client port should be -1, any available
    rmif/id=RMIF/msgid=main/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort
    message FUNC=sendw ID=RMIF NAME="OPEN" DATA={ID=RECEIVER,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
    ! We should be done now, but wait a few seconds to see if extra OPENEDs come in
    set this.timer(0) wait

  elseif msg.name eqs "SET" then
    ! do nothing

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

    calc openedCount openedCount 1 +
    if openedCount eq 1 then
      message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 DATA={TEXT="got expected OPENED message",TEST=TRUE}
    else
      message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 DATA={TEXT="got unwanted OPENED message",TEST=FAIL}
    endif

  elseif msg.name eqs "TIMER" then
    ! done waiting for any extra messages
    message FUNC=send ID=cntrl NAME=CLIENT_DONE DATA={TEST=PASS}
    
  elseif msg.name eqss "CLOSE" then
    ! ignore CLOSE and CLOSED messages

  elseif msg.name eqs "NULL"
    ! ignore

  else
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 DATA={TEXT="Got unhandled message name=^msg.name",TEST=FALSE}
  endif
return
