;+
; NAME:
;       egso_specprov_node
; PURPOSE:
;       Find active node of EGSO Special Providers
;
; CALLING SEQUENCE:
;       result = egso_specprov_node([/sec] [/sfc] [/dso] [uoc])
; OUTPUTS:
;       result       node to use for query to EGSO Special Provider
;                    if none working, or not connected to internet, returns -1
; KEYWORDS: 
;       sec          find node for SEC
;       sfc          find node for SFC
;       dso          find node for DSO
;       uoc          find node for UOC
;       backup       select instance specified by value of backup keyword
;
; METHOD:
;       Try primary and secondary nodes of requested EGSO Special Provider 
;       until one works. Try each twice; default is SEC
;       If /backup keyword set, use instance specified by its value
;       
; RESTRICTIONS:
;       Must be connected to the Internet to return node
; HISTORY:
;       30-May-2005  Written by Bob Bentley (MSSL/UCL)
;
;-

FUNCTION egso_specprov_node, node, sec=sec, sfc=sfc, dso=dso, uoc=uoc, $
		backup=backup

default=0
if not keyword_set(sec) and not keyword_set(sfc) and $
   not keyword_set(dso) and not keyword_set(uoc) then default=1

if keyword_set(sec) or default then $
  nodes = ['http://sec.ts.astro.it/sec_server2.php', $
           'http://msslxz.mssl.ucl.ac.uk/sec/sec_server.php', $
           'http://sec.na.astro.it/sec_server.php']

if keyword_set(dso) then $
  nodes = ['http://dso.na.astro.it:8080/axis/services/dsows', $
           'http://egso.to.astro.it:8080/axis/services/dsows']

if keyword_set(sfc) then $
  nodes = ['http://solar.inf.brad.ac.uk/sfc_server.php', $
           'http://linux2.inf.brad.ac.uk:8080/sfc_server.php']

if keyword_set(uoc) then $
  nodes = ['http://solarnet.to.astro.it:8080/axis/services/wsUOC', $
           'http://sec.ts.astro.it:8080/axis/services/wsUOC']

knodes = n_elements(nodes)

;  allow node selection using the /backup keyword
if keyword_set(backup) then begin
  qnode = min([knodes-1,backup])
  node = nodes(qnode)
  if not have_network(node,/reset) then begin
    message, 'Problem with node: '+node, /cont
    message, 'Verify that you are connected to the network and retry',/cont
    return, -1
  endif else begin
    message, 'Network connection check OK: '+node, /cont
    return, node
  endelse
endif


;  find a node that works
for k = 0,knodes-1 do begin
  node = nodes(k)

;  try each node twice in attempt to find one
  for j = 0,1 do begin
    if not have_network(node,/reset) then begin
      message, 'Problem with node: '+node, /cont
    endif else begin
      message, 'Network connection check OK: '+node, /cont
      return, node
    endelse
  endfor

endfor  

message, 'Verify that you are connected to the network and retry',/cont
return, -1

END
