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

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

7 8 9
	b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10
	ic "github.com/jbenet/go-ipfs/crypto"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11
	u "github.com/jbenet/go-ipfs/util"
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

Siraj Ravel's avatar
Siraj Ravel committed
19
// Equal is utililty function for comparing two peer ID's
20 21
func (id ID) Equal(other ID) bool {
	return bytes.Equal(id, other)
22 23
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24
// Pretty returns a b58-encoded string of the ID
25
func (id ID) Pretty() string {
26
	return b58.Encode(id)
27 28
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29 30 31 32 33
// DecodePrettyID returns a b58-encoded string of the ID
func DecodePrettyID(s string) ID {
	return b58.Decode(s)
}

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
37
// Peer represents the identity information of an IPFS Node, including
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
// ID, and relevant Addresses.
39
type Peer struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40
	ID        ID
41
	Addresses []ma.Multiaddr
Jeromy's avatar
Jeromy committed
42

43 44
	PrivKey ic.PrivKey
	PubKey  ic.PubKey
45

46 47 48
	latency time.Duration

	sync.RWMutex
49 50
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51
// Key returns the ID as a Key (string) for maps.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52
func (p *Peer) Key() u.Key {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53
	return u.Key(p.ID)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54 55
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
56
// AddAddress adds the given Multiaddr address to Peer's addresses.
57
func (p *Peer) AddAddress(a ma.Multiaddr) {
58 59 60
	p.Lock()
	defer p.Unlock()

61 62 63 64 65
	for _, addr := range p.Addresses {
		if addr.Equal(a) {
			return
		}
	}
66 67 68
	p.Addresses = append(p.Addresses, a)
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
69
// NetAddress returns the first Multiaddr found for a given network.
70
func (p *Peer) NetAddress(n string) ma.Multiaddr {
71 72 73
	p.RLock()
	defer p.RUnlock()

74
	for _, a := range p.Addresses {
75
		for _, p := range a.Protocols() {
76 77 78 79 80 81 82
			if p.Name == n {
				return a
			}
		}
	}
	return nil
}
Jeromy's avatar
Jeromy committed
83

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84
// GetLatency retrieves the current latency measurement.
85
func (p *Peer) GetLatency() (out time.Duration) {
86
	p.RLock()
87
	out = p.latency
88
	p.RUnlock()
89
	return
Jeromy's avatar
Jeromy committed
90 91
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92
// SetLatency sets the latency measurement.
93 94
// TODO: Instead of just keeping a single number,
//		 keep a running average over the last hour or so
95
// Yep, should be EWMA or something. (-jbenet)
96
func (p *Peer) SetLatency(laten time.Duration) {
97
	p.Lock()
98
	p.latency = laten
99
	p.Unlock()
Jeromy's avatar
Jeromy committed
100
}