peer.go 1.05 KB
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
go lint  
Juan Batiz-Benet committed
9
// PeerId is a byte slice representing the identity of a peer.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11
type PeerId mh.Multihash

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
12
// PeerBook maps Key (string) : *Peer (slices are not comparable).
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
type PeerBook map[u.Key]*Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
15 16
// Peer represents the identity information of an IPFS Node, including
// a PeerId, and relevant Addresses.
17
type Peer struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18
	Id        PeerId
19 20 21
	Addresses []*ma.Multiaddr
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
22
// Key returns the PeerId as a Key (string) for maps.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24 25 26
func (p *Peer) Key() u.Key {
	return u.Key(p.Id)
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
27
// AddAddress adds the given Multiaddr address to Peer's addresses.
28 29 30 31
func (p *Peer) AddAddress(a *ma.Multiaddr) {
	p.Addresses = append(p.Addresses, a)
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
32
// NetAddress returns the first Multiaddr found for a given network.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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
}