!
! Server part of RMIF test
!
! @author  Neon Ngo
! @bug 1548: RMIF cross talk
!
! <cntrl> - The parent macro object
! <port>  - The port of the first server. Second server will listen on port+1
!
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 L:count           0
  set L:openCount       0
  set L:expLinkerrCount 0
  set L:linkerrCount    0
  set messageInterval   0.25
  invoke ACK_FUNC nxm.sys.net.Rmif.ACK

  pipe on
    rmif/id=rmifserver1/msgid=main/STATEM=^statemachine/VERBOSE=^verbose ^port
    rmif/id=rmifserver2/msgid=main/STATEM=^statemachine/VERBOSE=^verbose ^{port}+1
    message FUNC=send ID=cntrl NAME=SERVER_STARTED INFO=0 DATA={TEST=PASS}

    ! following line is only for manual standalone testing
    if cntrl isNull then message FUNC=send ID=main NAME=START_TEST
    !  set this.timer(0) messageInterval
  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
    set testResult TEST(linkerrCount,LE,0)
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="unexpected LINKERRs=^linkerrCount expected LINKERRs=^expLinkerrCount",TEST=^testResult}

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

  elseif msg.name eqs "START_TEST" then
    ! start timer to send ACK messages periodically
    set this.timer(0) messageInterval

  elseif msg.name eqs "TIMER" then
    calc L:count count 1 +
    timex now curTime
    if /debugs then say "got TIMER: ^count at time=^curTime  sending ACKs to RMIF servers"
    message send rmifserver1 ,, "ACK",, "{SRC=Server1,X=^count,UPDATE=s:^curTime}"
    message send rmifserver2 ,, "ACK",, "{SRC=Server2,X=^count,UPDATE=s:^curTime}"
    message send rmifserver1 ,, "ACK",, "{SRC=Server1,Y=^count,UPDATE=s:^curTime}"
    message send rmifserver2 ,, "ACK",, "{SRC=Server2,Y=^count,UPDATE=s:^curTime}"

  elseif msg.name eqs "OPEN" then
    calc L:openCount openCount 1 +

    if openCount eq 1 then
      message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="got open from first RMIF client.",TEST=TRUE}
    elseif openCount eq 2 then
      message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="got open from second RMIF client.",TEST=TRUE}
      ! server side is done when second RMIF client connects
      message FUNC=send ID=cntrl NAME=SERVER_DONE DATA={TEST=TRUE}
    endif

  elseif msg.name eqss "CLOSE" then
    if /debugs then say "^reg.main.name: Got ^msg.name from ^msg data=^msg.data"
    ! ignore CLOSE and CLOSED messages

  elseif msg.name eqs "LINKERR" then
    if /debugs then say "===> DEBUG: Received a LINKERR. ^msg.data"
    ! ignore packet drop (PKTDROP link errors for ACK packets)
    res s:text ^{msg.data.text}
    if ^msg.data.packet isNull OR text.startsWith("PKTDROP") isFalse OR ^msg.data.packet.func neq ^ACK_FUNC then
      calc L:linkerrCount linkerrCount 1 +
      message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="got unexpected LINK ERROR: ^msg ^{msg.data}",TEST=FALSE}
    else
      calc L:expLinkerrCount expLinkerrCount 1 +
    endif

  elseif msg.name eqs "NULL"
    ! ignore

  else
    say "^reg.main.name: Got unknown msg from ^msg data=^msg.data"
    message FUNC=send ID=cntrl NAME=SERVER_TEST INFO=0 DATA={TEXT="got unhandled message name=^msg.name data=^msg.data",TEST=FALSE}
  endif
return
