! Server part of sending pipe data over RMIF test
!
! @bug     2146 RMIF: incorrect internal packet buffer size
! @author  Neon Ngo
!
! <cntrl>     - The parent macro object
! <port>      - The KNOWN port of the server.  Client uses any available port.
! <frameSize> - Size of frame for served type 2000 pipe (in bytes)
startmacro O:cntrl L:port L:frameSize[32K] D:waitTime[5.0]
  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 sampleRate 1e5
  set sr sampleRate
  set sfreq sampleRate/10
  set amp 100
  set verbose TEST(this.verbose OR this.m.parent.macro.verbose REXISTS AND this.m.parent.macro.verbose)

  pipe on
    ! create pipe with LRS (noise) and throttle it so that we don't saturate the network
    waveform out=_pipe form=SB elem=inf shape=LRS freq=(sfreq*.5) amp=amp delta=1/sr /throttle=real
    ! change pipe to type 2000 file for test case with supported packet size = RMIF's packet's max data size
    headermod _pipe FrameSize=^frameSize
    ! startup RMIF and serve pipe as a property
    rmif/id=RMIF/msgid=main/STATEM=^statemachine/VERBOSE=^verbose ^port _pipe
    sendto cntrl "SERVER_STARTED" {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
    ! test case for BUG 2756
    set stillRunning TEST(REG.RMIF,REXISTS)
    sendto cntrl "SERVER_TEST" {TEXT="RMIF still running 2 (BUG 2756)",TEST=^stillRunning}

    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
    ! wait a few seconds to send some pipe data out to remote client
    set this.timer(0) waitTime
  elseif msg.name eqs "TIMER" then
    ! test case for BUG 2756
    set stillRunning TEST(REG.RMIF,REXISTS)
    sendto cntrl "SERVER_TEST" {TEXT="RMIF still running 1 (BUG 2756)",TEST=^stillRunning}
    ! done waiting for any extra messages / sending out pipe data
    sendto cntrl "SERVER_DONE" {TEST=PASS}
  elseif msg.name eqss "CLOSE" then
    ! ignore CLOSE and CLOSED messages
  elseif msg.name eqs "LINKERR" then
    sendto cntrl "SERVER_TEST" {TEXT="got unexpected error",TEST=FALSE}
  elseif msg.name eqs "ERROR_SERVING_PROPERTY" then
    ! for BUG 2756 - it is ok to get this message when frameSize greater 32K (RMIF data size limit per packet)
    if frameSize > 32K then
      if msg.data.comment rexists then
        set comment msg.data.comment
      else
        set comment ""
      endif
      if verbose OR debug then info "got expected ^msg.name for size > RMIF max data length. ^comment"
    else
      sendto cntrl "SERVER_TEST" {TEXT="got name=^{msg.name} data=^{msg.data}",TEST=FALSE}

    endif
  elseif msg.name eqs "NULL" then
    ! ignore
  else
    warn "GOT unknown message: ^msg data=^msg.data"
    sendto cntrl "SERVER_TEST" {TEXT="got unhandled message name=^msg.name",TEST=FALSE}
  endif
return
