startmacro/nmq=8k/ps=128k/rmifid=2 N:nsamples[1e9]
!******************************************************************************!
!
! Training - Remoting - X-Midas server application
!
! @author Jay Hallam
! @version $Id: remoteserver.txt,v 1.2 2006/11/02 14:36:34 jph Exp $
!******************************************************************************!

local item itemStr MainMenu
local sfreq,cfreq,fwidth,SRate,amp,StatusString
local FrameSize,form
!##############################################################################!
local port 

local /M/

!Intentionally Global Variables
!  cities site world sv1 sv2

  ! No tables in the X-Midas macro language, set the individual results
  res SRate 1e5
  res sfreq SRate/10
  res form "SF"
  res cfreq 0.0
  res fwidth 0.1
  res amp 1e4
  res A:StatusString "Up and running"

!##############################################################################!
  res port 9222 

  ! We will parse this menu later so we do not have to use item numbers
  res MainMenu "Get,Set,Reset,Test,Stop"

  ! Form the full path to the cities file in the NeXtMidas DAT area
  fname cities dat cities prm nxm
  fname site dat site prm nxm
  fname world dat world prm nxm
  fname sv1 dat sv1 prm nxm
  fname sv2 dat sv2 prm nxm
  
  xpipe/setup/controls on
    WAVEFORM/tl=1k/ctag=3:SFREQ _wave ^form nsamples SIN 1e4 1e4,,, 1/SRate
    MFFT/fftp=3 _wave _wavep 1k hann 0.0
    STATUS/quiet _wavep ,,,,, FrameSize

    XRTPLOT/xs=3/mtag=X:SFREQ/xcnt=2/mark=3 _wavep(cl=8*FrameSize) 1e-5 1e4 l2 ,, 70
!###############################################################################! 
    THIN _wavep _thin 1 -1 50
    MAGNITUDE/tl=FrameSize _thin _waveb(ft=b) 20 log
    XRTRASTER/xs=2/mtag=X:CFREQ/lpb=2/stretch/xn="Raster - Byte" _waveb
!###      XRTRASTER/xs=2/mtag=X:CFREQ/lpb=2/stretch/xn="Raster - Float"/ _wavep 1e-5 1e4 l2 FrameSize 20
    XRTDISPLAY/xs=4 SFREQ/"Sine Frequency" FWIDTH/"Filter Width" CFREQ/"Center Freq"

    if site FEXISTS then
      xgeoplot/xs=5 world|cities ^site
    else
      res alert "Could not find the SITE file for XGEOPLOT!"
      warning "^alert"
      xcall message "^alert" 3
      beep 2
    endif
!################################## START OF MODS ##############################! 
    rmif/id=2/msgid=1/PKTMSG port sfreq|cfreq|fwidth|_waveb
 
!##################################  END OF MODS  ##############################! 

  xpipe off

endmacro

!
! Start of the controls section
!
startcontrols

!******************************************************************************!
label initialentry

  ! The main MENU control widget
  xcontrol/lab=doMainMenu menu item ,, "MACRO;^MainMenu" 

  ! Widgets for controlling the Sine and Center frequencies
  xcontrol/name="SineFreq:   "/fmt="(F12.2)"/nocont/nobut dval &
                  sfreq sfreq -SRate SRate SRate/20 
  xcontrol/name="CenterFreq: "/fmt="(F12.2)"/nocont/nobut dval &
                  cfreq cfreq -SRate SRate SRate/20 

  xcontrol/disp/cols=32/len=32/name="Status: " AMON StatusString
  ! Monitor the global message queue for any msg addressed to this macro (ID=1)
  xcontrol/lab=getmsg msmon 1 10
return

!******************************************************************************!
label doMainMenu
  ! Parse the item string from the menu
  sedit MainMenu itemStr "ELEM" item "," UPCASE
  if itemStr eqs "GET" then
    ! Display the values
    say "Sine FREQ=^sfreq, Center FREQ=^cfreq"
  elseif itemStr eqs "SET" then
    ! Set the values
    res SFREQ 20E3
    res CFREQ 10E3
  elseif itemStr eqs "RESET"
    res SFREQ 10E3
    res CFREQ=10E3
  elseif itemStr eqs "TEST" then
    ! other test
  elseif itemStr eqs "STOP" then
    remove site world cities sv1 sv2 ! Remove all global vars
    stop
  endif
return

!******************************************************************************!
label getmsg

  ! Get any messages addressed to this macro (ID=1)
  message get /M/
  say "Got a ^M.name message from ^M.id"

!################################## START OF MODS ##############################! 
!
!  These are the type of messages expected when /PKTMSG is used on RMIF
!
  if M.name eqs "RPKT"
    info "Got msg ^M.name from ^M.id, data ^M.RPKT_data"
    if M.RPKT_func eqs "SET"
      rmsg "T2R" ,,,, M.RPKT_data d_
      loop d_nk nn   ! loop through table entries
        res P_^{d_k^nn} ^{d_v^nn}
      endloop
      rmsg "SEND" M.RPKT_addr "ACK" M.info ,, d_
    else if M.RPKT_func eqs "GET"
      rmsg "T2R" ,,,, M.RPKT_data d_
      loop d_nk nn   ! loop through table entries
        res  d_v^nn P_^{d_k^nn}
      endloop
      rmsg "SEND" M.RPKT_addr "RET" M.info ,, d_
    else if M.RPKT_func eqs "ACK"
      rmsg "T2R" ,,,, M.RPKT_data d_
      say "Got ACK = ^M.RPKT_data"
      res d_*
    else if M.RPKT_func eqs "RET"
      rmsg "T2R" ,,,, M.RPKT_data d_
      say "Got RET = ^M.RPKT_data"
      res d_*
    endif
    rem/quiet d_*

!
!  These are the type of messages expected when /KEYMSG is used on RMIF
!
  elseif M.name eqs "GETKEY"
    say "GetKey name = ^M.GETKEY_name, val = ^M.GETKEY_value
    
  elseif M.name eqs "RETKEY"
    say "RetKey name = ^M.RETKEY_name, val =  ^M.RETKEY_value

  elseif M.name eqs "SETKEY"
    say "SetKey name = ^M.SETKEY_name, val =  ^M.SETKEY_value

  elseif M.name eqs "ACKKEY"
    say "AckKey name = ^M.ACKKEY_name, val =  ^M.ACKKEY_value

  elseif M.name eqs "RERR"
    say "Link Err: ^M.RERR_text for ^M.RERR_func ^M.RERR_data from ^M.RERR_addr"

  else
    warning "Unprocessed msg ^M.name message from ^M.id"
  endif
!################################## END OF MODS ##############################! 
return

!
! End of the controls section
!
endcontrols
