peer.go 716 Bytes
Newer Older
1 2 3
package peer

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4
	u "github.com/jbenet/go-ipfs/util"
5 6 7 8
	ma "github.com/jbenet/go-multiaddr"
	mh "github.com/jbenet/go-multihash"
)

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12
// have to map Key (string) : *Peer because slices are not comparable.
type PeerBook map[u.Key]*Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13

14
type Peer struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
	Id        PeerId
16 17 18
	Addresses []*ma.Multiaddr
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19 20 21 22
func (p *Peer) Key() u.Key {
	return u.Key(p.Id)
}

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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
}