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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
// Map maps Key (string) : *Peer (slices are not comparable).
type Map 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
// Peer represents the identity information of an IPFS Node, including
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
// ID, and relevant Addresses.
17
type Peer struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18
	ID        ID
19 20 21
	Addresses []*ma.Multiaddr
}

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

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
}