peer.go 1.71 KB
Newer Older
1 2 3
package peer

import (
Jeromy's avatar
Jeromy committed
4
	"sync"
Jeromy's avatar
Jeromy committed
5
	"time"
6

Jeromy's avatar
Jeromy committed
7
	b58 "github.com/jbenet/go-base58"
8
	ic "github.com/jbenet/go-ipfs/crypto"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	u "github.com/jbenet/go-ipfs/util"
10 11
	ma "github.com/jbenet/go-multiaddr"
	mh "github.com/jbenet/go-multihash"
12 13

	"bytes"
14 15
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16 17
// ID is a byte slice representing the identity of a peer.
type ID mh.Multihash
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18

19
// Utililty function for comparing two peer ID's
20 21
func (id ID) Equal(other ID) bool {
	return bytes.Equal(id, other)
22 23
}

24
func (id ID) Pretty() string {
25
	return b58.Encode(id)
26 27
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28 29
// Map maps Key (string) : *Peer (slices are not comparable).
type Map map[u.Key]*Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
31
// Peer represents the identity information of an IPFS Node, including
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32
// ID, and relevant Addresses.
33
type Peer struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34
	ID        ID
35
	Addresses []*ma.Multiaddr
Jeromy's avatar
Jeromy committed
36

37 38
	PrivKey ic.PrivKey
	PubKey  ic.PubKey
39

Jeromy's avatar
Jeromy committed
40
	latency   time.Duration
41
	latenLock sync.RWMutex
42 43
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
// Key returns the ID as a Key (string) for maps.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
func (p *Peer) Key() u.Key {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
	return u.Key(p.ID)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
49
// AddAddress adds the given Multiaddr address to Peer's addresses.
50 51 52 53
func (p *Peer) AddAddress(a *ma.Multiaddr) {
	p.Addresses = append(p.Addresses, a)
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
54
// NetAddress returns the first Multiaddr found for a given network.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
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
}
Jeromy's avatar
Jeromy committed
70

71 72 73 74 75
func (p *Peer) GetLatency() (out time.Duration) {
	p.latenLock.RLock()
	out = p.latency
	p.latenLock.RUnlock()
	return
Jeromy's avatar
Jeromy committed
76 77
}

78 79
// TODO: Instead of just keeping a single number,
//		 keep a running average over the last hour or so
80 81 82 83
func (p *Peer) SetLatency(laten time.Duration) {
	p.latenLock.Lock()
	p.latency = laten
	p.latenLock.Unlock()
Jeromy's avatar
Jeromy committed
84
}