Beispiel #1
0
func (h *ServiceHost) setURI() error {
	addr, err := h.TCPAddr()
	if err != nil {
		return err
	}

	var host string
	if addr.IP.Equal(net.IPv4zero) {
		// Use localhost if address is INADDR_ANY
		host = "127.0.0.1"
	} else {
		host = addr.IP.String()
	}

	h.selfURI = uri.New(host, strconv.Itoa(addr.Port))
	return nil
}
Beispiel #2
0
// RemoteURI returns a URI to the TestServiceHost with a host address
// that is reachable from the given address.
func (th *ServiceHost) RemoteURI(address string) uri.URI {
	conn, err := net.Dial("udp", address)
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	_, curPort, err := net.SplitHostPort(th.URI().Host)
	if err != nil {
		panic(err)
	}

	newHost, _, err := net.SplitHostPort(conn.LocalAddr().String())
	if err != nil {
		panic(err)
	}

	return uri.New(newHost, curPort)
}