! This will test the functionality of RMIF serving out files via the MFTP (UDP)
! protocol and retrieving it via NOOP client (which uses MftpResource).
!
! @bug 2250 RMIF: mftp:// server protocol broken since 2.7.3
! @author  Neon Ngo
! @since NeXtMidas 2.8.3
!
! <port>  The server's port number (defaults to -1 for any available port)
!
startmacro L:port[-1]
  switch INTERACTIVE interAct     GET DEF=TRUE  SDEF=TRUE
  switch DEBUG       debug        GET DEF=false SDEF=true
  switch VERBOSE     verbose      GET DEF=false SDEF=true
  switch STATEM      statemachine GET DEF=false SDEF=true

  if statemachine then
    warn "^reg.main.name: This test fails when running with /STATEM because unsolicited packets &
                          are getting rejected when trying to fetch a file. This test case needs &
                          to be looked further into. However, because our core user does not use &
                          mftp, this will be a relatively low priority."
  else
    call setDatDir

    set isRunningTest "FALSE"
    pipe on
      rmif/id=rmifid/STATEM=^statemachine/VERBOSE=^verbose ^port
      set mftpurl "mftp://localhost:^{reg.rmifid.port}"

      ! set timer to start test case (since we need RMIF to be in process state before doing using mftp protocol)
      set this.timer(0) 1.0
    pipe off
  endif
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 "TIMER" then
    noop mftp://localhost:^{reg.rmifid.port}/^{datDir}.apenny.prm ram:test_rmif_mftp_testfile.tmp
    assert/text="^reg.main.name: Served file via mftp://server:port/file.ext protocol" ram:test_rmif_mftp_testfile.tmp feq ^{datDir}.apenny.prm
    erase/warn=n ram:test_rmif_mftp_testfile.tmp
    pipe stop
  endif
return

!****************************************************************************!
procedure processException m:emsg
  ! FAIL on unknown exception
  assert/text="^reg.main.name: Got unexpected Exception: ^emsg.data" "FALSE"
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return
