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

  pipe on
    rmif/id=RMIF/msgid=main/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 "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
    ! do nothing
  elseif msg.name eqs "OPEN" then
    calc openCount openCount 1 +
    if openCount eq 1 then
      set okInfo TEST(msg.info,eqs,0)
      set okData TEST(msg.data,typeof,nxm.sys.net.Rmif$Remote)
      set ok     TEST(okInfo,and,okData)
      message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="got expected OPEN message",TEST=^ok}
      
      ! We should be done now, but wait a few seconds to see if extra OPENs come in
      set this.timer(0) wait
    else
      message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=1 DATA={TEXT="got unwanted OPEN message",TEST=FAIL}
    endif
    
  elseif msg.name eqs "TIMER" then
    ! done waiting for any extra messages
    message FUNC=send ID=cntrl NAME=SERVER_DONE DATA={TEST=PASS}

  elseif msg.name eqss "CLOSE" then
    ! ignore CLOSE and CLOSED messages
  elseif msg.name eqs "NULL" then
    ! ignore
  else
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 &
            DATA={TEXT="got unhandled message name=^msg.name",TEST=FALSE}
  endif
return
