!******************************************************************************!
! Test the SINKDG and SOURCEDG primitive.
! Arguments: <port> The port number (defaults to -1 for semi-random port between 18800-18899)
!
!   Author: J. Czechowski IV / Neon Ngo
!  Version: $Id: test_sinkdg.mm,v 1.6 2015/12/22 16:48:20 ntn Exp $
!******************************************************************************!
startmacro L:port[-1]
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=^interAct SDEF=TRUE

  if baseline then
    set host   "127.0.0.1"
    if port == -1 then
      calc L:port random 100 * 18800 +   ! semi-random port in range of [18800-18899]
    endif

    info "!!!!! It is ok is see 'sinkdg Invalid Input File' below !!!!!"
    pipe on
      sinkdg/ttl=4/hostport="^host:^port"
      sourcedg/on/nortp ram:temp1{size=64} ram:temp2{size=64,FORM=SB} HOSTPORT="^host:^port"
      sendto "MAIN" "SEND" "TEST"
    pipe off
  endif
endmacro

!******************************************************************************!
procedure processMessage
  if msg.name EQS "MAIN" OR msg.name EQS "TIMER" then
    !set reg.sourcedg.state "OFF"  ! Turn off the flow of packets
    message send "SINKDG"   ,, "EXITNET"
    message send "SOURCEDG" ,, "EXITNET"

    assert/text="^reg.main.name: Check got data(0) = 'T'" FILE(ram:temp2).data(0) eq 0x54
    assert/text="^reg.main.name: Check got data(1) = 'E'" FILE(ram:temp2).data(1) eq 0x45
    assert/text="^reg.main.name: Check got data(2) = 'S'" FILE(ram:temp2).data(2) eq 0x53
    assert/text="^reg.main.name: Check got data(3) = 'T'" FILE(ram:temp2).data(3) eq 0x54
    pipe stop

  elseif msg.name EQS "SEND" then
    info "Sending '^{msg.data}' packet to ^host:^port..."
    invoke ,, reg.sinkdg.send(host,"^port",^{msg.data})

    set this.timer(0) 1.5  ! set timer to end test case

  else
    say "^reg.main.name: Unhandled message name: ^msg.name data: ^msg.data"
  endif
return
