Esempio n. 1
0
// NewFromHello creates a new Peer from a HELLO message.
func NewFromHello(m *messages.Hello, incoming *connection.Incoming, cfg *config.Config) (p *Peer, err error) {
	p, err = New(config.UUID(m.UUID), m.Address, m.Port, incoming, cfg)
	if err != nil {
		return
	}
	p.logger.Println("Received HELLO")

	p.Send(&messages.HelloOk{})

	return
}
Esempio n. 2
0
func (d *Discoverer) onHello(m messages.Message) {
	d.m.Lock()
	defer d.m.Unlock()
	h := m.(*messages.Hello)
	d.logger.Println("Got HELLO from", h.UUID)
	if p, s := d.nodeSeen(config.UUID(h.UUID)); s {
		d.logger.Println("Node is already known")
		p.Send(&messages.HelloOk{})
		return
	}
	p, err := peer.NewFromHello(h, d.incoming, d.cfg)
	if err != nil {
		d.logger.Println("ERROR: ", err)
	}
	if !d.cfg.Exporting() {
		p.Check()
	}
	p.Removed().Then(d.removeFromSeen)
	p.Connected().Then(d.removeFromSeen)

	d.connectedPeers.JoinFuture(p.Connected())
	d.seenNodes[p.UUID()] = p
}
Esempio n. 3
0
func (i *Incoming) listen() {
	poller := zmq4.NewPoller()
	poller.Add(i.skt, zmq4.POLLIN)

	for {
		if i.close.Completed() {
			err := i.skt.Close()
			i.in.Close()
			i.closed.Complete(err)
			return
		}
		sockets, err := poller.Poll(100 * time.Millisecond)
		if err != nil {
			continue
		}
		for range sockets {
			msg, err := i.skt.RecvMessage(0)
			if err == nil && !i.in.Closed().Completed() {
				i.in.Add(Message{i.addr, config.UUID(msg[0]), msg[1:]})
			}
		}
	}
}
Esempio n. 4
0
func (n Node) UUID() config.UUID {
	return config.UUID(strings.Split(n.Name, ":")[0])
}