!
! RMIF ADDC warning message test case
! CLIENT
!
! <cntrl>   - The parent control macro object
! <port>    - The KNOWN port of the server.
! <scenario>- The RMIF configuration scenario:
!   (WORKS)    1 : The rmif thread, the remote object, and the Q:ACK channel are defined by the
!                  "rmif" command in the pipe section.
!                  Opens rmif session, defines the remote object, and configures a Q:ACK channel
!                  on the default IC=1.
!
!   (WORKS)    2 : The rmif thread and the remote object are defined in the "rmif" command in the
!                  pipe section. The Q:ACK channel is established via an ADDC "message" to the
!                  remote object in the pipe section.
!                  Opens rmif session and defines the remote object. Then sends an ADDC "message"
!                  to the remote object to add the Q:ACK channel on the default IC=1.
!
!   (FAILS)    3 : The rmif thread is defined in the "rmif" command. The remote object is defined
!                  with an OPEN "message". The Q:ACK channel is established via an ADDC "message"
!                  to the remote object. All of this is done in the pipe section.
!                  Opens rmif session. Then sends an OPEN "message to establish the remote object.
!                  Then sends an ADDC "message" to the remote object to add the Q:ACK channel on
!                  the channel defined in the INFO field of the "message" command.
!                  * Generates a WARN message in console.
!
!   (WORKS)    4 : The rmif thread is defined in the "rmif" command. The remote object is defined
!                  with an OPEN "message". This is done in the pipe section.
!                  The Q:ACK channel is established via an ADDC "message" to the remote object.
!                  The ADDC "message" is sent from the processMessage() section.
!                  Opens rmif session. Then sends an OPEN "message to establish the remote object.
!                  After receipt of a "START_TEST" "message", an ADDC "message" is sent to
!                  the remote object to add the Q:ACK channel on the channel defined in the INFO
!                  field of the "message" command.
!
!   (WORKS)    5 : The rmif thread is defined in the "rmif" command in the pipe section. The remote
!                  object is defined with an OPEN "message" and the Q:ACK channel is established
!                  via an ADDC "message" to the remote object. Both the OPEN "message" and the
!                  ADDC "message" are sent from the processMessage() section.
!                  Opens rmif session. After receipt of a "START_TEST" "message", an OPEN "message"
!                  and an ADDC "message" are sent to the remote object to add the Q:ACK channel
!                  on the channel defined in the INFO field of the "message" command.
!
! @bug     1057
! @author  Jim Roush
! @since   NeXtMidas 2.5.1
!
startmacro O:cntrl L:port L:scenario[1] S:host["localhost"]
  switch DEBUG   debug        GET DEF=false SDEF=true
  switch VERBOSE verbose      GET DEF=false SDEF=true
  switch TRACE   trace        GET DEF=false SDEF=true
  switch STATEM  statemachine GET DEF=false SDEF=true

  set reg.cntrl  cntrl  ! save reference to parent control macro
  set L:anyAvailablePort -1
  set BAR 0
  set COUNT 0

  pipe on

    if ^scenario eq 2 then
       INFO "Running SCENARIO 2 -----------------------------------------*"
     ! Startup client (sender) RMIF and REMOTE object
       rmif/id=SENDER/msgid=MAIN/PPT={VERBOSE=^trace}/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort ,, {ID=REMOTE,HP="^host:^port"}
     ! Add a Q:ACK channel to the new RMIF REMOTE object
       message FUNC=SEND ID=REMOTE NAME="ADDC" INFO=10 DATA="Q:ACK"

    elseif ^scenario eq 3 then
       INFO "Running SCENARIO 3 -----------------------------------------*"
     ! Startup client (sender) RMIF
       rmif/id=SENDER/msgid=MAIN/PPT={VERBOSE=^trace}/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort
     ! Open RMIF REMOTE object
       message FUNC=SENDW ID=SENDER NAME="OPEN" DATA={ID=REMOTE,HP="^host:^port"}
     ! Add a Q:ACK channel to the new RMIF REMOTE object
       INFO "You will receive a 'No channel open...' warning from the ADDC message."
       INFO "Test will fail because no Q:ACK channel has been configured."
       message FUNC=SEND ID=SENDER NAME="ADDC" INFO=10 DATA="Q:ACK"

    elseif ^scenario eq 4 then
       INFO "Running SCENARIO 4 -----------------------------------------*"
     ! Startup client (sender) RMIF
       rmif/id=SENDER/msgid=MAIN/PPT={VERBOSE=^trace}/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort
     ! Open RMIF REMOTE object
       message FUNC=sendw ID=SENDER NAME="OPEN" DATA={ID=REMOTE,HP="^host:^port"}

    elseif ^scenario eq 5 then
       INFO "Running SCENARIO 5 -----------------------------------------*"
     ! Startup client (sender) RMIF
       rmif/id=SENDER/msgid=MAIN/PPT={VERBOSE=^trace}/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort
     ! Send client started message to controller
       message FUNC=send ID=cntrl NAME="CLIENT_STARTED" INFO=0 DATA={TEST=PASS}

    else
       INFO "Running SCENARIO 1 -----------------------------------------*"
     ! Startup client (sender) RMIF and REMOTE object with Q:ACK channel
       rmif/id=SENDER/msgid=MAIN/PPT={VERBOSE=^trace}/STATEM=^statemachine/VERBOSE=^verbose anyAvailablePort ,, {ID=REMOTE,HP="^host:^port"} "Q:ACK"

    endif

  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 "START_TEST" then
    if ^scenario eq 4 then
      ! Add the Q:ACK channel to the new RMIF REMOTE object
      message FUNC=send ID=REMOTE NAME="ADDC" INFO=11 DATA="Q:ACK"

    elseif ^scenario eq 5 then
     ! Open RMIF REMOTE object
       message FUNC=sendw ID=SENDER NAME="OPEN" DATA={ID=REMOTE,HP="^host:^port"}
     ! Add the Q:ACK channel to the new RMIF REMOTE object
       message FUNC=send ID=REMOTE NAME=ADDC INFO=11 DATA="Q:ACK"
    else
     ! do nothing
    endif

    message FUNC=send ID=REMOTE NAME=SET ,, DATA={BAR="G:GLOP"}

  elseif msg.name eqs "OPEN" then
    ! ignore
  elseif msg.name eqs "OPENED" then
    if ^scenario eq 5 then
      !do nothing
    else
     ! Send client started message to controller
      message FUNC=send ID=cntrl NAME=CLIENT_STARTED DATA={TEST=PASS}
    endif

  elseif msg.name eqs "SET" then
    ! do nothing
  elseif msg.name eqs "ACK" then
    foreach key inTable msg.data
      res ^key msg.data.^key
    endfor
    message FUNC=send ID=cntrl NAME="CLIENT_DONE" INFO=0 DATA={TEST=PASS}

  elseif msg.name eqs "CLOSE" then
    message FUNC=send ID=REMOTE NAME=CLOSE
  elseif msg.name eqs "CLOSED" then
    ! ignore
  elseif msg.name eqs "NULL"
    ! ignore
  elseif msg.name eqs "STOP_TEST" then
    rem reg.cntrl   ! we put this in manually, now remove it
    pipe stop       ! sent by cntrl
  elseif msg.name eqs "LINKERR" then
    WARN "Received a LINKERR. ^msg.data"
     ! notify control macro of link error and corrupt packet
      message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 &
              DATA={TEXT="got LINKERR Message=^{msg.data.text}",TEST=FAIL}
  else
    ! notify control macro of unknown message as a failed test
    message FUNC=send ID=cntrl NAME=CLIENT_TEST INFO=0 &
            DATA={TEXT="got unhandled message name=^msg.name data=^msg.data",TEST=FAIL}
  endif

return
