# Copyright (C) 2016 Free Software Foundation, Inc.

# This file is part of DejaGnu.
#
# DejaGnu is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DejaGnu is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DejaGnu; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.

# This file requires a slightly modified dmucs, which has been
# modified to support multiple architectures for toolchain
# testing. DMUCS sends the CPU load to a server, which we can query
# using the 'gethost' command to get the next available slave for a
# remote test based on which one is the least busy. The code for this
# enhanced DMUCS is at: http://git.linaro.org/git/toolchain/dmucs.git

proc dmucs_gethost { server arch } {
    # gethost in debug mode returns this output:
    # --node and --arch are unique to the Linaro branch of DMUCS
    #
    # Writing -->host 127.0.1.1 <-- to the server
    # Calling Sgets
    # Got -->10.6.1.24<-- from the server
    # tmp is -->DISTCC_HOSTS=10.6.1.24/100<--
    set slave ""
    spawn gethost -s $server --node --arch $arch
    expect {
	# This is the IP number we want.
	"^\[0-9\]*\.\[0-9\]*\.\[0-9\]*\.\[0-9\]*" {
	    set pos [expr {[string first "/" $expect_out(buffer)] - 1}]
	    set slave [string range $expect_out(buffer) 0 $pos]
	}
	timeout {
	    perror "DMUCS gethost command timed out"
	}
	eof {
	    perror "Failed to connect to DMUCS server"
	}
    }
    return $slave
}
