Пример #1
0
func register(org *types.Organizer_t, port *int, fingers uint,
	maxs uint, alive *bool) (*types.Chord_t, *net.TCPListener) {
	var self, joinhost *types.Host_t
	var listener *net.TCPListener
	var tries = 10

	for joinhost == nil {
		if tries <= 0 {
			*alive = false
		}
		tries--

		h, l := host.SetupHost(port)
		if h == nil || l == nil {
			panic(nil)
		}

		joinhost = org.Register(h)
		if joinhost == nil {
			l.Close()
			time.Sleep(1 * time.Second)
		} else {
			self = h
			listener = l
		}
		if !*alive {
			return nil, nil
		}
	}
	c := chord.New(self, *org, fingers, maxs)
	if c == nil {
		panic(nil)
	}

	c.Successor = joinhost

	ok := netutils.RPCRegisterName("Chord", c)
	if ok {
		go netutils.RPCServe(listener)
	}
	for !c.Node.Alive() {
		time.Sleep(1 * time.Second)
	}
	println("Serving RPC on: " + c.Node.Address())

	return c, listener
}
Пример #2
0
func main() {
	var ALIVE = true
	exitfunc := func() {
		ALIVE = false
	}
	flag.Parse()

	sighandler := sighandler.New()

	n = node.New()
	netutils.RPCRegisterName("Node", n)
	host, listener := host.SetupHost(port)
	if host == nil || listener == nil {
		panic(nil)
	}
	fmt.Println("Serving from: " + host.Address())

	sighandler.RegisterExitHandler(exitfunc)

	http.HandleFunc("/Nodes", handleNodeReq)

	go netutils.RPCServe(listener)
	go graphServe()
	go startconsole(exitfunc)
	go func() {
		for {
			n.ControlPresenceOfRandomNode()
			time.Sleep(1 * time.Second)
		}
	}()
	for ALIVE {
		time.Sleep(100 * time.Millisecond)
	}
	if *killnodesonexit {
		err := killnode([]string{"ALL"})
		if err != nil {
			fmt.Println(err.Error())
		}
	}
}