// requirePeer finds or creates peer via its UUID string func (n *node) requirePeer(identity string, endpoint string) (peer *peer, err error) { peer, ok := n.peers[identity] if !ok { // Purge any previous peer on same endpoint for _, p := range n.peers { if p.endpoint == endpoint { p.disconnect() } } peer = newPeer(identity) err = peer.connect(n.uuid, endpoint) if err != nil { return nil, err } // Handshake discovery by sending HELLO as first message m := msg.NewHello() m.Endpoint = n.endpoint m.Status = n.status m.Name = n.name for key := range n.ownGroups { m.Groups = append(m.Groups, key) } for key, header := range n.headers { m.Headers[key] = header } peer.send(m) n.peers[identity] = peer // TODO(armen): Send new peer event to logger, if any } return peer, nil }
func TestGroup(t *testing.T) { mailbox, err := zmq.NewSocket(zmq.DEALER) if err != nil { t.Fatal(err) } mailbox.Bind("tcp://127.0.0.1:5552") group := newGroup("tlests") me := make([]byte, 16) io.ReadFull(crand.Reader, me) you := make([]byte, 16) io.ReadFull(crand.Reader, you) peer := newPeer(string(you)) if peer.connected { t.Fatal("Peer shouldn't be connected yet") } err = peer.connect(me, "tcp://127.0.0.1:5552") if err != nil { t.Fatal(err) } if !peer.connected { t.Fatal("Peer should be connected") } group.join(peer) m := msg.NewHello() m.Endpoint = "tcp://127.0.0.1:5551" group.send(m) exp, err := m.Marshal() if err != nil { t.Fatal(err) } exp[5] = 1 // Sequence now is 1 got, err := mailbox.Recv(0) if err != nil { t.Fatal(err) } gotb := []byte(got) if !bytes.Equal(gotb, exp) { t.Errorf("Hello message is corrupted") } peer.destroy() }