Beispiel #1
0
// ForHostname creates a new default messenger (HTTP), using UPIDBindingAddress to
// determine the binding-address used for both the UPID.Host and Transport binding address.
func ForHostname(proc *process.Process, hostname string, bindingAddress net.IP, port uint16, publishedAddress net.IP) (Messenger, error) {
	upid := upid.UPID{
		ID:   proc.Label(),
		Port: strconv.Itoa(int(port)),
	}
	host, err := UPIDBindingAddress(hostname, bindingAddress)
	if err != nil {
		return nil, err
	}

	var publishedHost string
	if publishedAddress != nil {
		publishedHost, err = UPIDBindingAddress(hostname, publishedAddress)
		if err != nil {
			return nil, err
		}
	}

	if publishedHost != "" {
		upid.Host = publishedHost
	} else {
		upid.Host = host
	}

	return NewHttpWithBindingAddress(upid, bindingAddress), nil
}
// create a new default messenger (HTTP). If a non-nil, non-wildcard bindingAddress is
// specified then it will be used for both the UPID and Transport binding address. Otherwise
// hostname is resolved to an IP address and the UPID.Host is set to that address and the
// bindingAddress is passed through to the Transport.
func ForHostname(proc *process.Process, hostname string, bindingAddress net.IP, port uint16) (Messenger, error) {
	upid := &upid.UPID{
		ID:   proc.Label(),
		Port: strconv.Itoa(int(port)),
	}
	if bindingAddress != nil && "0.0.0.0" != bindingAddress.String() {
		upid.Host = bindingAddress.String()
	} else {
		ips, err := net.LookupIP(hostname)
		if err != nil {
			return nil, err
		}
		// try to find an ipv4 and use that
		ip := net.IP(nil)
		for _, addr := range ips {
			if ip = addr.To4(); ip != nil {
				break
			}
		}
		if ip == nil {
			// no ipv4? best guess, just take the first addr
			if len(ips) > 0 {
				ip = ips[0]
				log.Warningf("failed to find an IPv4 address for '%v', best guess is '%v'", hostname, ip)
			} else {
				return nil, fmt.Errorf("failed to determine IP address for host '%v'", hostname)
			}
		}
		upid.Host = ip.String()
	}
	return NewHttpWithBindingAddress(upid, bindingAddress), nil
}
// ForHostname creates a new default messenger (HTTP), using UPIDBindingAddress to
// determine the binding-address used for both the UPID.Host and Transport binding address.
func ForHostname(proc *process.Process, hostname string, bindingAddress net.IP, port uint16) (Messenger, error) {
	upid := &upid.UPID{
		ID:   proc.Label(),
		Port: strconv.Itoa(int(port)),
	}
	host, err := UPIDBindingAddress(hostname, bindingAddress)
	if err != nil {
		return nil, err
	}
	upid.Host = host
	return NewHttpWithBindingAddress(upid, bindingAddress), nil
}