startmacro L:port[9333]

  switch remote remote DEF=localhost:9222

  info "^reg.main.name: default local port ^port, remote ^remote"
  set sfreq -1
  set cfreq -1
  set fwidth -1
  set myres "Not initialized"

  pipe/setup on
    panel/controls=gc/setup
    rmif/id=rmif/msgid=MAIN/http/prange=5 port
    plot/msgid=main _waveb cnt=motion
    plot {LT="NMS",URL="http://nextmidas.com:8080/nms/",&
             BACKGROUND="BLUEMARBLE"}|world.shp{LAYER={LABEL="NAME",OPAQ=0}}|cities &
             view=latlon
    gcontrol menu main "Main" "Open,Close,Stop"
      set gc.main.itemColors {Open=Green,Close=Yellow,Stop=Red}

    gcontrol label sets open
      gcontrol dval   button_sfreq "SineFreq"   sfreq 1 1e9 1e3
      gcontrol dval   button_cfreq "CenterFreq" cfreq 1 1e9 1e3
      gcontrol prompt button_myres "String Result" myres

    gcontrol label more open
      gcontrol menu more "More" "Ping,Keys,Set,Get"
      gcontrol pipemon pmon _waveb
      gcontrol/mon=reg.rmif.linkBW      DVAL linkbw  "LinkBW"      0 0 -1 /noedit /msg=f
      gcontrol/mon=reg.rmif.linkLatency DVAL linkLat "LinkLatency" 0 0 -1 /noedit /msg=f

  pipe off

endmacro

procedure processMessage
  !!info "^reg.main.name: msg ^msg.name, from ^msg.fid"
  if msg.fid EQS "PLOT" then
    if msg.name eqs "MARK" then
      set gc.BUTTON_SFREQ.value msg.data.x
      message FUNC=send ID=XM NAME="SET" DATA={SFREQ=^msg.data.x}
    endif
  
  elseif msg.name EQS "MAIN" then

    if msg.data EQS "OPEN" then
      message func=sendw ID=RMIF name="OPEN" DATA={ID=XM,HP=^remote} 

    elseif msg.data EQS "CLOSE" then
      say TBD

    elseif msg.data EQS "STOP" then
      pipe stop

    else
      warning "Do not recognize selection ^msg.name, data ^msg.data"
    endif

  elseif msg.name EQS "MORE" then
    if msg.data EQS "GET" then
      message FUNC=send ID=XM NAME="GET" DATA={SFREQ=?,CFREQ=?}
    elseif msg.data EQS "SET" then
      info "^reg.main.name: sending SET to X-Midas"
      message FUNC=send ID=XM NAME="SET" DATA={CFREQ=1234.5,SFREQ=4567.8,MYRES="From NeXtMidas"}
    else
      info "^reg.main.name: sending msg name ^msg.data to XM"
      message FUNC=send ID=XM NAME="^msg.data"
    endif

  elseif msg.name EQSS "BUTTON" then
    if msg.name EQS button_cfreq then
      message FUNC=send ID=XM NAME="SET" DATA={CFREQ=^msg.data}
    elseif msg.name EQS button_fwidth then
      message FUNC=send ID=XM NAME="SET" DATA={FWIDTH=^msg.data}
    elseif msg.name EQS button_sfreq then
      message FUNC=send ID=XM NAME="SET" DATA={SFREQ=^msg.data}
    elseif msg.name EQS button_myres then
      message FUNC=send ID=XM NAME="SET" DATA={MYRES="^msg.data"}
    endif

  elseif msg.fid EQS "RMIF" then
    if msg.name EQS "OPENED" then
      info "^reg.main.name: opening channel for acks"
      message send XM ,, "ADDC" 1 "Q:ACK"
      info "^reg.main.name: opening channel for pipe"
      message send XM ,, "ADDC" 2 {_waveb=_waveb}
    elseif msg.name EQS "CLOSED" then 
      info "^reg.main.name: RMIF closed ^msg.data"
  
  
    else
      warning "^reg.main.name: unhandled RMIF msg ^msg.name"
    endif

  elseif msg.fid EQS "XM" then
    if msg.name EQS "PROPERTY" then
      info "^reg.main.name: Got ^msg.fid REMOTE PROPERTY ^msg.data"
    endif
  else
    warning "Do not recognize msg ^msg.name from ^msg.fid"
  endif

return
