peer.go 623 Bytes
Newer Older
1 2 3 4 5 6 7
package peer

import (
	ma "github.com/jbenet/go-multiaddr"
	mh "github.com/jbenet/go-multihash"
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8 9 10 11 12
type PeerId mh.Multihash

// have to map string : *Peer because slices are not comparable.
type PeerBook map[string]*Peer

13
type Peer struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
	Id        PeerId
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	Addresses []*ma.Multiaddr
}

func (p *Peer) AddAddress(a *ma.Multiaddr) {
	p.Addresses = append(p.Addresses, a)
}

func (p *Peer) NetAddress(n string) *ma.Multiaddr {
	for _, a := range p.Addresses {
		ps, err := a.Protocols()
		if err != nil {
			continue // invalid addr
		}

		for _, p := range ps {
			if p.Name == n {
				return a
			}
		}
	}
	return nil
}