#
# When this file is sourced, a server process is automatically
# started that can be used for for testing purposes.  If the
# server is already running (i.e., some other test sources this file)
# then no new server is started.
#
# The following global variables are defined as a side effect of
# sourcing this file:
#
#	hostname -- the name of the host on which the server is running
#	S_PORT -- the port of the server
#

#
# Determine a port number 
#
set l [dp_MakeRPCServer 0 dp_CheckHost none 1]
catch {close [lindex $l 0]}
set S_PORT [expr 10+[lindex $l 1]]

#
# Setup: Start a server as another process, and use it to connect.
#
exec $argv0 -f server $S_PORT &
puts "exec $argv0 -f server $S_PORT &"
set hostname localhost

#
# This loop keeps trying to connect until it succeeds.
# Important since the exec may take a while on some machines...
#
dp_after 1000
set itry 0
while { [catch "dp_MakeRPCClient $hostname $S_PORT" rhost] } {
    puts stdout "Trying to connect to server..."
    incr itry
    if { $itry > 5 } {
	puts stdout "Unable to connect to server"
	break
    }
    dp_after 2000
}
catch "close $rhost"
