示例#1
0
文件: host.go 项目: kelsebo/go-chord
func SetupHttpListener(host *types.Host_t, port *int) *net.TCPListener {
	if types.Port_t(*port) == host.Port {
		host.HttpPort = types.Port_t(*port)
		return nil
	}

	listener := netutils.GetInetTCPListener(false, port)
	if listener == nil {
		return nil
	}
	addr, err := net.ResolveTCPAddr("tcp4", listener.Addr().String())
	if err != nil {
		return nil
	}
	host.HttpPort = types.Port_t(addr.Port)
	return listener
}
示例#2
0
文件: node.go 项目: kelsebo/go-chord
func (n *Node_t) Register(h *types.Host_t, reply *types.Host_t) error {
	nodemap := *n.Nodemap
	fmt.Print("Register request: '" + h.String() + "'\t... ")
	if nodemap == nil {
		panic(nil)
	}

	_, ok := nodemap[h.ID]
	if ok {
		fmt.Println("Got register request from someone I already know")
		return errors.New("P O")
	}

	n.getJoinHost(h, reply)
	n.addNode(h)
	fmt.Print("Telling new host to join on\t'" + reply.String() + "' ... ")
	fmt.Println("OK")

	return nil
}
示例#3
0
文件: host.go 项目: kelsebo/go-chord
func New(addr string, port string) (*types.Host_t, error) {
	h := new(types.Host_t)
	tcpaddr, err := net.ResolveTCPAddr("tcp4", addr+":"+port)
	if err != nil {
		return nil, err
	}

	h.IP = types.IP_t(tcpaddr.IP.String())
	h.Port = types.Port_t(tcpaddr.Port)
	h.ID = h.Hash()

	return h, nil
}